snarkvm_circuit_network/lib.rs
1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#![forbid(unsafe_code)]
17#![allow(clippy::too_many_arguments)]
18
19pub mod canary_v0;
20pub use canary_v0::*;
21
22pub mod testnet_v0;
23pub use testnet_v0::*;
24
25pub mod v0;
26pub use v0::*;
27
28use snarkvm_circuit_collections::merkle_tree::MerklePath;
29use snarkvm_circuit_types::{Boolean, Field, Group, Scalar, environment::Environment};
30
31/// Attention: Do not use `Send + Sync` on this trait, as it is not thread-safe.
32pub trait Aleo: Environment {
33 /// The maximum number of field elements in data (must not exceed u16::MAX).
34 const MAX_DATA_SIZE_IN_FIELDS: u32 = <Self::Network as console::Network>::MAX_DATA_SIZE_IN_FIELDS;
35
36 /// Initializes the global constants for the Aleo environment.
37 fn initialize_global_constants();
38
39 /// Returns the encryption domain as a constant field element.
40 fn encryption_domain() -> Field<Self>;
41
42 /// Returns the graph key domain as a constant field element.
43 fn graph_key_domain() -> Field<Self>;
44
45 /// Returns the serial number domain as a constant field element.
46 fn serial_number_domain() -> Field<Self>;
47
48 /// Returns the scalar multiplication on the generator `G`.
49 fn g_scalar_multiply(scalar: &Scalar<Self>) -> Group<Self>;
50
51 /// Returns a BHP commitment with an input hasher of 256-bits.
52 fn commit_bhp256(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
53
54 /// Returns a BHP commitment with an input hasher of 512-bits.
55 fn commit_bhp512(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
56
57 /// Returns a BHP commitment with an input hasher of 768-bits.
58 fn commit_bhp768(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
59
60 /// Returns a BHP commitment with an input hasher of 1024-bits.
61 fn commit_bhp1024(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
62
63 /// Returns a Pedersen commitment for the given (up to) 64-bit input and randomizer.
64 fn commit_ped64(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
65
66 /// Returns a Pedersen commitment for the given (up to) 128-bit input and randomizer.
67 fn commit_ped128(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
68
69 /// Returns a BHP commitment with an input hasher of 256-bits.
70 fn commit_to_group_bhp256(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
71
72 /// Returns a BHP commitment with an input hasher of 512-bits.
73 fn commit_to_group_bhp512(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
74
75 /// Returns a BHP commitment with an input hasher of 768-bits.
76 fn commit_to_group_bhp768(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
77
78 /// Returns a BHP commitment with an input hasher of 1024-bits.
79 fn commit_to_group_bhp1024(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
80
81 /// Returns a Pedersen commitment for the given (up to) 64-bit input and randomizer.
82 fn commit_to_group_ped64(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
83
84 /// Returns a Pedersen commitment for the given (up to) 128-bit input and randomizer.
85 fn commit_to_group_ped128(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
86
87 /// Returns the BHP hash with an input hasher of 256-bits.
88 fn hash_bhp256(input: &[Boolean<Self>]) -> Field<Self>;
89
90 /// Returns the BHP hash with an input hasher of 512-bits.
91 fn hash_bhp512(input: &[Boolean<Self>]) -> Field<Self>;
92
93 /// Returns the BHP hash with an input hasher of 768-bits.
94 fn hash_bhp768(input: &[Boolean<Self>]) -> Field<Self>;
95
96 /// Returns the BHP hash with an input hasher of 1024-bits.
97 fn hash_bhp1024(input: &[Boolean<Self>]) -> Field<Self>;
98
99 /// Returns the Keccak hash with a 256-bit output.
100 fn hash_keccak256(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
101
102 /// Returns the Keccak hash with a 384-bit output.
103 fn hash_keccak384(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
104
105 /// Returns the Keccak hash with a 512-bit output.
106 fn hash_keccak512(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
107
108 /// Returns the Pedersen hash for a given (up to) 64-bit input.
109 fn hash_ped64(input: &[Boolean<Self>]) -> Field<Self>;
110
111 /// Returns the Pedersen hash for a given (up to) 128-bit input.
112 fn hash_ped128(input: &[Boolean<Self>]) -> Field<Self>;
113
114 /// Returns the Poseidon hash with an input rate of 2.
115 fn hash_psd2(input: &[Field<Self>]) -> Field<Self>;
116
117 /// Returns the Poseidon hash with an input rate of 4.
118 fn hash_psd4(input: &[Field<Self>]) -> Field<Self>;
119
120 /// Returns the Poseidon hash with an input rate of 8.
121 fn hash_psd8(input: &[Field<Self>]) -> Field<Self>;
122
123 /// Returns the SHA-3 hash with a 256-bit output.
124 fn hash_sha3_256(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
125
126 /// Returns the SHA-3 hash with a 384-bit output.
127 fn hash_sha3_384(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
128
129 /// Returns the SHA-3 hash with a 512-bit output.
130 fn hash_sha3_512(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
131
132 /// Returns the extended Poseidon hash with an input rate of 2.
133 fn hash_many_psd2(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>>;
134
135 /// Returns the extended Poseidon hash with an input rate of 4.
136 fn hash_many_psd4(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>>;
137
138 /// Returns the extended Poseidon hash with an input rate of 8.
139 fn hash_many_psd8(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>>;
140
141 /// Returns the BHP hash with an input hasher of 256-bits.
142 fn hash_to_group_bhp256(input: &[Boolean<Self>]) -> Group<Self>;
143
144 /// Returns the BHP hash with an input hasher of 512-bits.
145 fn hash_to_group_bhp512(input: &[Boolean<Self>]) -> Group<Self>;
146
147 /// Returns the BHP hash with an input hasher of 768-bits.
148 fn hash_to_group_bhp768(input: &[Boolean<Self>]) -> Group<Self>;
149
150 /// Returns the BHP hash with an input hasher of 1024-bits.
151 fn hash_to_group_bhp1024(input: &[Boolean<Self>]) -> Group<Self>;
152
153 /// Returns the Pedersen hash for a given (up to) 64-bit input.
154 fn hash_to_group_ped64(input: &[Boolean<Self>]) -> Group<Self>;
155
156 /// Returns the Pedersen hash for a given (up to) 128-bit input.
157 fn hash_to_group_ped128(input: &[Boolean<Self>]) -> Group<Self>;
158
159 /// Returns the Poseidon hash with an input rate of 2 on the affine curve.
160 fn hash_to_group_psd2(input: &[Field<Self>]) -> Group<Self>;
161
162 /// Returns the Poseidon hash with an input rate of 4 on the affine curve.
163 fn hash_to_group_psd4(input: &[Field<Self>]) -> Group<Self>;
164
165 /// Returns the Poseidon hash with an input rate of 8 on the affine curve.
166 fn hash_to_group_psd8(input: &[Field<Self>]) -> Group<Self>;
167
168 /// Returns the Poseidon hash with an input rate of 2 on the scalar field.
169 fn hash_to_scalar_psd2(input: &[Field<Self>]) -> Scalar<Self>;
170
171 /// Returns the Poseidon hash with an input rate of 4 on the scalar field.
172 fn hash_to_scalar_psd4(input: &[Field<Self>]) -> Scalar<Self>;
173
174 /// Returns the Poseidon hash with an input rate of 8 on the scalar field.
175 fn hash_to_scalar_psd8(input: &[Field<Self>]) -> Scalar<Self>;
176
177 /// Returns `true` if the given Merkle path is valid for the given root and leaf.
178 #[allow(clippy::ptr_arg)]
179 fn verify_merkle_path_bhp<const DEPTH: u8>(
180 path: &MerklePath<Self, DEPTH>,
181 root: &Field<Self>,
182 leaf: &Vec<Boolean<Self>>,
183 ) -> Boolean<Self>;
184
185 /// Returns `true` if the given Merkle path is valid for the given root and leaf.
186 #[allow(clippy::ptr_arg)]
187 fn verify_merkle_path_psd<const DEPTH: u8>(
188 path: &MerklePath<Self, DEPTH>,
189 root: &Field<Self>,
190 leaf: &Vec<Field<Self>>,
191 ) -> Boolean<Self>;
192}