snarkvm_circuit_network/lib.rs
1// Copyright (c) 2019-2025 Provable Inc.
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
19extern crate snarkvm_console_network as console;
20
21pub mod canary_v0;
22pub use canary_v0::*;
23
24pub mod testnet_v0;
25pub use testnet_v0::*;
26
27pub mod v0;
28pub use v0::*;
29
30use snarkvm_circuit_collections::merkle_tree::MerklePath;
31use snarkvm_circuit_types::{Boolean, Field, Group, Scalar, environment::Environment};
32
33// Note: The functions of this trait are not thread safe.
34pub trait Aleo: Environment {
35 /// The maximum number of field elements in data (must not exceed u16::MAX).
36 const MAX_DATA_SIZE_IN_FIELDS: u32 = <Self::Network as console::Network>::MAX_DATA_SIZE_IN_FIELDS;
37
38 /// Initializes the global constants for the Aleo environment.
39 fn initialize_global_constants();
40
41 /// Returns the commitment domain as a constant field element.
42 fn commitment_domain() -> Field<Self>;
43
44 /// Returns the encryption domain as a constant field element.
45 fn encryption_domain() -> Field<Self>;
46
47 /// Returns the graph key domain as a constant field element.
48 fn graph_key_domain() -> Field<Self>;
49
50 /// Returns the serial number domain as a constant field element.
51 fn serial_number_domain() -> Field<Self>;
52
53 /// Returns the scalar multiplication on the generator `G`.
54 fn g_scalar_multiply(scalar: &Scalar<Self>) -> Group<Self>;
55
56 /// Returns a BHP commitment with an input hasher of 256-bits.
57 fn commit_bhp256(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
58
59 /// Returns a BHP commitment with an input hasher of 512-bits.
60 fn commit_bhp512(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
61
62 /// Returns a BHP commitment with an input hasher of 768-bits.
63 fn commit_bhp768(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
64
65 /// Returns a BHP commitment with an input hasher of 1024-bits.
66 fn commit_bhp1024(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
67
68 /// Returns a Pedersen commitment for the given (up to) 64-bit input and randomizer.
69 fn commit_ped64(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
70
71 /// Returns a Pedersen commitment for the given (up to) 128-bit input and randomizer.
72 fn commit_ped128(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Field<Self>;
73
74 /// Returns a BHP commitment with an input hasher of 256-bits.
75 fn commit_to_group_bhp256(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
76
77 /// Returns a BHP commitment with an input hasher of 512-bits.
78 fn commit_to_group_bhp512(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
79
80 /// Returns a BHP commitment with an input hasher of 768-bits.
81 fn commit_to_group_bhp768(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
82
83 /// Returns a BHP commitment with an input hasher of 1024-bits.
84 fn commit_to_group_bhp1024(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
85
86 /// Returns a Pedersen commitment for the given (up to) 64-bit input and randomizer.
87 fn commit_to_group_ped64(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
88
89 /// Returns a Pedersen commitment for the given (up to) 128-bit input and randomizer.
90 fn commit_to_group_ped128(input: &[Boolean<Self>], randomizer: &Scalar<Self>) -> Group<Self>;
91
92 /// Returns the BHP hash with an input hasher of 256-bits.
93 fn hash_bhp256(input: &[Boolean<Self>]) -> Field<Self>;
94
95 /// Returns the BHP hash with an input hasher of 512-bits.
96 fn hash_bhp512(input: &[Boolean<Self>]) -> Field<Self>;
97
98 /// Returns the BHP hash with an input hasher of 768-bits.
99 fn hash_bhp768(input: &[Boolean<Self>]) -> Field<Self>;
100
101 /// Returns the BHP hash with an input hasher of 1024-bits.
102 fn hash_bhp1024(input: &[Boolean<Self>]) -> Field<Self>;
103
104 /// Returns the Keccak hash with a 256-bit output.
105 fn hash_keccak256(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
106
107 /// Returns the Keccak hash with a 384-bit output.
108 fn hash_keccak384(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
109
110 /// Returns the Keccak hash with a 512-bit output.
111 fn hash_keccak512(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
112
113 /// Returns the Pedersen hash for a given (up to) 64-bit input.
114 fn hash_ped64(input: &[Boolean<Self>]) -> Field<Self>;
115
116 /// Returns the Pedersen hash for a given (up to) 128-bit input.
117 fn hash_ped128(input: &[Boolean<Self>]) -> Field<Self>;
118
119 /// Returns the Poseidon hash with an input rate of 2.
120 fn hash_psd2(input: &[Field<Self>]) -> Field<Self>;
121
122 /// Returns the Poseidon hash with an input rate of 4.
123 fn hash_psd4(input: &[Field<Self>]) -> Field<Self>;
124
125 /// Returns the Poseidon hash with an input rate of 8.
126 fn hash_psd8(input: &[Field<Self>]) -> Field<Self>;
127
128 /// Returns the SHA-3 hash with a 256-bit output.
129 fn hash_sha3_256(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
130
131 /// Returns the SHA-3 hash with a 384-bit output.
132 fn hash_sha3_384(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
133
134 /// Returns the SHA-3 hash with a 512-bit output.
135 fn hash_sha3_512(input: &[Boolean<Self>]) -> Vec<Boolean<Self>>;
136
137 /// Returns the extended Poseidon hash with an input rate of 2.
138 fn hash_many_psd2(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>>;
139
140 /// Returns the extended Poseidon hash with an input rate of 4.
141 fn hash_many_psd4(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>>;
142
143 /// Returns the extended Poseidon hash with an input rate of 8.
144 fn hash_many_psd8(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>>;
145
146 /// Returns the BHP hash with an input hasher of 256-bits.
147 fn hash_to_group_bhp256(input: &[Boolean<Self>]) -> Group<Self>;
148
149 /// Returns the BHP hash with an input hasher of 512-bits.
150 fn hash_to_group_bhp512(input: &[Boolean<Self>]) -> Group<Self>;
151
152 /// Returns the BHP hash with an input hasher of 768-bits.
153 fn hash_to_group_bhp768(input: &[Boolean<Self>]) -> Group<Self>;
154
155 /// Returns the BHP hash with an input hasher of 1024-bits.
156 fn hash_to_group_bhp1024(input: &[Boolean<Self>]) -> Group<Self>;
157
158 /// Returns the Pedersen hash for a given (up to) 64-bit input.
159 fn hash_to_group_ped64(input: &[Boolean<Self>]) -> Group<Self>;
160
161 /// Returns the Pedersen hash for a given (up to) 128-bit input.
162 fn hash_to_group_ped128(input: &[Boolean<Self>]) -> Group<Self>;
163
164 /// Returns the Poseidon hash with an input rate of 2 on the affine curve.
165 fn hash_to_group_psd2(input: &[Field<Self>]) -> Group<Self>;
166
167 /// Returns the Poseidon hash with an input rate of 4 on the affine curve.
168 fn hash_to_group_psd4(input: &[Field<Self>]) -> Group<Self>;
169
170 /// Returns the Poseidon hash with an input rate of 8 on the affine curve.
171 fn hash_to_group_psd8(input: &[Field<Self>]) -> Group<Self>;
172
173 /// Returns the Poseidon hash with an input rate of 2 on the scalar field.
174 fn hash_to_scalar_psd2(input: &[Field<Self>]) -> Scalar<Self>;
175
176 /// Returns the Poseidon hash with an input rate of 4 on the scalar field.
177 fn hash_to_scalar_psd4(input: &[Field<Self>]) -> Scalar<Self>;
178
179 /// Returns the Poseidon hash with an input rate of 8 on the scalar field.
180 fn hash_to_scalar_psd8(input: &[Field<Self>]) -> Scalar<Self>;
181
182 /// Returns `true` if the given Merkle path is valid for the given root and leaf.
183 #[allow(clippy::ptr_arg)]
184 fn verify_merkle_path_bhp<const DEPTH: u8>(
185 path: &MerklePath<Self, DEPTH>,
186 root: &Field<Self>,
187 leaf: &Vec<Boolean<Self>>,
188 ) -> Boolean<Self>;
189
190 /// Returns `true` if the given Merkle path is valid for the given root and leaf.
191 #[allow(clippy::ptr_arg)]
192 fn verify_merkle_path_psd<const DEPTH: u8>(
193 path: &MerklePath<Self, DEPTH>,
194 root: &Field<Self>,
195 leaf: &Vec<Field<Self>>,
196 ) -> Boolean<Self>;
197}