snarkvm_console_network/
canary_v0.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
16use super::*;
17use crate::TRANSACTION_PREFIX;
18use snarkvm_console_algorithms::{
19    BHP256,
20    BHP512,
21    BHP768,
22    BHP1024,
23    Blake2Xs,
24    Keccak256,
25    Keccak384,
26    Keccak512,
27    Pedersen64,
28    Pedersen128,
29    Poseidon2,
30    Poseidon4,
31    Poseidon8,
32    Sha3_256,
33    Sha3_384,
34    Sha3_512,
35};
36
37lazy_static! {
38    /// The group bases for the Aleo signature and encryption schemes.
39    static ref GENERATOR_G: Vec<Group<CanaryV0 >> = CanaryV0::new_bases("AleoAccountEncryptionAndSignatureScheme0");
40
41    /// The Varuna sponge parameters.
42    static ref VARUNA_FS_PARAMETERS: FiatShamirParameters<CanaryV0> = FiatShamir::<CanaryV0>::sample_parameters();
43
44    /// The commitment domain as a constant field element.
45    static ref COMMITMENT_DOMAIN: Field<CanaryV0> = Field::<CanaryV0>::new_domain_separator("AleoCommitment0");
46    /// The encryption domain as a constant field element.
47    static ref ENCRYPTION_DOMAIN: Field<CanaryV0> = Field::<CanaryV0>::new_domain_separator("AleoSymmetricEncryption0");
48    /// The graph key domain as a constant field element.
49    static ref GRAPH_KEY_DOMAIN: Field<CanaryV0> = Field::<CanaryV0>::new_domain_separator("AleoGraphKey0");
50    /// The serial number domain as a constant field element.
51    static ref SERIAL_NUMBER_DOMAIN: Field<CanaryV0> = Field::<CanaryV0>::new_domain_separator("AleoSerialNumber0");
52
53    /// The BHP hash function, which can take an input of up to 256 bits.
54    pub static ref CANARY_BHP_256: BHP256<CanaryV0> = BHP256::<CanaryV0>::setup("AleoBHP256").expect("Failed to setup BHP256");
55    /// The BHP hash function, which can take an input of up to 512 bits.
56    pub static ref CANARY_BHP_512: BHP512<CanaryV0> = BHP512::<CanaryV0>::setup("AleoBHP512").expect("Failed to setup BHP512");
57    /// The BHP hash function, which can take an input of up to 768 bits.
58    pub static ref CANARY_BHP_768: BHP768<CanaryV0> = BHP768::<CanaryV0>::setup("AleoBHP768").expect("Failed to setup BHP768");
59    /// The BHP hash function, which can take an input of up to 1024 bits.
60    pub static ref CANARY_BHP_1024: BHP1024<CanaryV0> = BHP1024::<CanaryV0>::setup("AleoBHP1024").expect("Failed to setup BHP1024");
61
62    /// The Pedersen hash function, which can take an input of up to 64 bits.
63    pub static ref CANARY_PEDERSEN_64: Pedersen64<CanaryV0> = Pedersen64::<CanaryV0>::setup("AleoPedersen64");
64    /// The Pedersen hash function, which can take an input of up to 128 bits.
65    pub static ref CANARY_PEDERSEN_128: Pedersen128<CanaryV0> = Pedersen128::<CanaryV0>::setup("AleoPedersen128");
66
67    /// The Poseidon hash function, using a rate of 2.
68    pub static ref CANARY_POSEIDON_2: Poseidon2<CanaryV0> = Poseidon2::<CanaryV0>::setup("AleoPoseidon2").expect("Failed to setup Poseidon2");
69    /// The Poseidon hash function, using a rate of 4.
70    pub static ref CANARY_POSEIDON_4: Poseidon4<CanaryV0> = Poseidon4::<CanaryV0>::setup("AleoPoseidon4").expect("Failed to setup Poseidon4");
71    /// The Poseidon hash function, using a rate of 8.
72    pub static ref CANARY_POSEIDON_8: Poseidon8<CanaryV0> = Poseidon8::<CanaryV0>::setup("AleoPoseidon8").expect("Failed to setup Poseidon8");
73
74    pub static ref CANARY_CREDITS_V0_PROVING_KEYS: IndexMap<String, Arc<VarunaProvingKey<Console>>> = {
75        let mut map = IndexMap::new();
76        snarkvm_parameters::insert_canary_credit_v0_keys!(map, VarunaProvingKey<Console>, Prover);
77        map
78    };
79    pub static ref CANARY_CREDITS_V0_VERIFYING_KEYS: IndexMap<String, Arc<VarunaVerifyingKey<Console>>> = {
80        let mut map = IndexMap::new();
81        snarkvm_parameters::insert_canary_credit_v0_keys!(map, VarunaVerifyingKey<Console>, Verifier);
82        map
83    };
84
85    pub static ref CANARY_CREDITS_PROVING_KEYS: IndexMap<String, Arc<VarunaProvingKey<Console>>> = {
86        let mut map = IndexMap::new();
87        snarkvm_parameters::insert_canary_credit_keys!(map, VarunaProvingKey<Console>, Prover);
88        map
89    };
90    pub static ref CANARY_CREDITS_VERIFYING_KEYS: IndexMap<String, Arc<VarunaVerifyingKey<Console>>> = {
91        let mut map = IndexMap::new();
92        snarkvm_parameters::insert_canary_credit_keys!(map, VarunaVerifyingKey<Console>, Verifier);
93        map
94    };
95}
96
97#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
98pub struct CanaryV0;
99
100impl CanaryV0 {
101    /// Initializes a new instance of group bases from a given input domain message.
102    fn new_bases(message: &str) -> Vec<Group<Self>> {
103        // Hash the given message to a point on the curve, to initialize the starting base.
104        let (base, _, _) = Blake2Xs::hash_to_curve::<<Self as Environment>::Affine>(message);
105
106        // Compute the bases up to the size of the scalar field (in bits).
107        let mut g = Group::<Self>::new(base);
108        let mut g_bases = Vec::with_capacity(Scalar::<Self>::size_in_bits());
109        for _ in 0..Scalar::<Self>::size_in_bits() {
110            g_bases.push(g);
111            g = g.double();
112        }
113        g_bases
114    }
115}
116
117impl Environment for CanaryV0 {
118    type Affine = <Console as Environment>::Affine;
119    type BigInteger = <Console as Environment>::BigInteger;
120    type Field = <Console as Environment>::Field;
121    type PairingCurve = <Console as Environment>::PairingCurve;
122    type Projective = <Console as Environment>::Projective;
123    type Scalar = <Console as Environment>::Scalar;
124
125    /// The coefficient `A` of the twisted Edwards curve.
126    const EDWARDS_A: Self::Field = Console::EDWARDS_A;
127    /// The coefficient `D` of the twisted Edwards curve.
128    const EDWARDS_D: Self::Field = Console::EDWARDS_D;
129    /// The coefficient `A` of the Montgomery curve.
130    const MONTGOMERY_A: Self::Field = Console::MONTGOMERY_A;
131    /// The coefficient `B` of the Montgomery curve.
132    const MONTGOMERY_B: Self::Field = Console::MONTGOMERY_B;
133}
134
135impl Network for CanaryV0 {
136    /// The block hash type.
137    type BlockHash = AleoID<Field<Self>, { hrp2!("ab") }>;
138    /// The ratification ID type.
139    type RatificationID = AleoID<Field<Self>, { hrp2!("ar") }>;
140    /// The state root type.
141    type StateRoot = AleoID<Field<Self>, { hrp2!("sr") }>;
142    /// The transaction ID type.
143    type TransactionID = AleoID<Field<Self>, { hrp2!(TRANSACTION_PREFIX) }>;
144    /// The transition ID type.
145    type TransitionID = AleoID<Field<Self>, { hrp2!("au") }>;
146    /// The transmission checksum type.
147    type TransmissionChecksum = u128;
148
149    /// The genesis block coinbase target.
150    #[cfg(not(feature = "test_targets"))]
151    const GENESIS_COINBASE_TARGET: u64 = (1u64 << 29).saturating_sub(1);
152    #[cfg(feature = "test_targets")]
153    const GENESIS_COINBASE_TARGET: u64 = (1u64 << 5).saturating_sub(1);
154    /// The genesis block proof target.
155    #[cfg(not(feature = "test_targets"))]
156    const GENESIS_PROOF_TARGET: u64 = 1u64 << 27;
157    #[cfg(feature = "test_targets")]
158    const GENESIS_PROOF_TARGET: u64 = 1u64 << 3;
159    /// The fixed timestamp of the genesis block.
160    const GENESIS_TIMESTAMP: i64 = 1715776496 /* 2024-05-15 12:34:56 UTC */;
161    /// The network ID.
162    const ID: u16 = 2;
163    /// The function name for the inclusion circuit.
164    const INCLUSION_FUNCTION_NAME: &'static str = snarkvm_parameters::canary::NETWORK_INCLUSION_FUNCTION_NAME;
165    /// A list of (consensus_version, size) pairs indicating the maximum number of certificates in a batch.
166    #[cfg(not(any(test, feature = "test")))]
167    const MAX_CERTIFICATES: [(ConsensusVersion, u16); 5] = [
168        (ConsensusVersion::V1, 100),
169        (ConsensusVersion::V3, 100),
170        (ConsensusVersion::V5, 100),
171        (ConsensusVersion::V6, 100),
172        (ConsensusVersion::V9, 100),
173    ];
174    /// A list of (consensus_version, size) pairs indicating the maximum number of certificates in a batch.
175    #[cfg(any(test, feature = "test"))]
176    const MAX_CERTIFICATES: [(ConsensusVersion, u16); 5] = [
177        (ConsensusVersion::V1, 25),
178        (ConsensusVersion::V3, 25),
179        (ConsensusVersion::V5, 25),
180        (ConsensusVersion::V6, 25),
181        (ConsensusVersion::V9, 25),
182    ];
183    /// The (long) network name.
184    const NAME: &'static str = "Aleo Canary (v0)";
185    /// The short network name.
186    const SHORT_NAME: &'static str = "canary";
187    /// A list of (consensus_version, block_height) pairs indicating when each consensus version takes effect.
188    /// Documentation for what is changed at each version can be found in `ConsensusVersion`.
189    /// Do not read this directly outside of tests, use `N::CONSENSUS_VERSION_HEIGHTS()` instead.
190    const _CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] =
191        CANARY_V0_CONSENSUS_VERSION_HEIGHTS;
192
193    /// Returns the block height where the the inclusion proof will be updated.
194    #[allow(non_snake_case)]
195    fn INCLUSION_UPGRADE_HEIGHT() -> Result<u32> {
196        Self::CONSENSUS_HEIGHT(ConsensusVersion::V8)
197    }
198
199    /// Returns the genesis block bytes.
200    fn genesis_bytes() -> &'static [u8] {
201        snarkvm_parameters::canary::GenesisBytes::load_bytes()
202    }
203
204    /// Returns the restrictions list as a JSON-compatible string.
205    fn restrictions_list_as_str() -> &'static str {
206        snarkvm_parameters::canary::RESTRICTIONS_LIST
207    }
208
209    /// Returns the proving key for the given function name in the v0 version of `credits.aleo`.
210    fn get_credits_v0_proving_key(function_name: String) -> Result<&'static Arc<VarunaProvingKey<Self>>> {
211        CANARY_CREDITS_V0_PROVING_KEYS
212            .get(&function_name)
213            .ok_or_else(|| anyhow!("Proving key (v0) for credits.aleo/{function_name}' not found"))
214    }
215
216    /// Returns the verifying key for the given function name in the v0 version of `credits.aleo`.
217    fn get_credits_v0_verifying_key(function_name: String) -> Result<&'static Arc<VarunaVerifyingKey<Self>>> {
218        CANARY_CREDITS_V0_VERIFYING_KEYS
219            .get(&function_name)
220            .ok_or_else(|| anyhow!("Verifying key (v0) for credits_v0.aleo/{function_name}' not found"))
221    }
222
223    /// Returns the proving key for the given function name in `credits.aleo`.
224    fn get_credits_proving_key(function_name: String) -> Result<&'static Arc<VarunaProvingKey<Self>>> {
225        CANARY_CREDITS_PROVING_KEYS
226            .get(&function_name)
227            .ok_or_else(|| anyhow!("Proving key for credits.aleo/{function_name}' not found"))
228    }
229
230    /// Returns the verifying key for the given function name in `credits.aleo`.
231    fn get_credits_verifying_key(function_name: String) -> Result<&'static Arc<VarunaVerifyingKey<Self>>> {
232        CANARY_CREDITS_VERIFYING_KEYS
233            .get(&function_name)
234            .ok_or_else(|| anyhow!("Verifying key for credits.aleo/{function_name}' not found"))
235    }
236
237    #[cfg(not(feature = "wasm"))]
238    /// Returns the `proving key` for the inclusion_v0 circuit.
239    fn inclusion_v0_proving_key() -> &'static Arc<VarunaProvingKey<Self>> {
240        static INSTANCE: OnceLock<Arc<VarunaProvingKey<Console>>> = OnceLock::new();
241        INSTANCE.get_or_init(|| {
242            // Skipping the first byte, which is the encoded version.
243            Arc::new(
244                CircuitProvingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_V0_PROVING_KEY[1..])
245                    .expect("Failed to load inclusion_v0 proving key."),
246            )
247        })
248    }
249
250    #[cfg(feature = "wasm")]
251    /// Returns the `proving key` for the inclusion_v0 circuit.
252    fn inclusion_v0_proving_key(inclusion_key_bytes: Option<Vec<u8>>) -> &'static Arc<VarunaProvingKey<Self>> {
253        static INSTANCE: OnceLock<Arc<VarunaProvingKey<Console>>> = OnceLock::new();
254        INSTANCE.get_or_init(|| {
255            inclusion_key_bytes
256                .map(|bytes| {
257                    snarkvm_parameters::canary::InclusionV0Prover::verify_bytes(&bytes)
258                        .expect("Bytes provided did not match expected inclusion checksum.");
259                    Arc::new(
260                        CircuitProvingKey::from_bytes_le(&bytes[1..]).expect("Failed to load inclusion proving key."),
261                    )
262                })
263                .unwrap_or_else(|| {
264                    Arc::new(
265                        CircuitProvingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_V0_PROVING_KEY[1..])
266                            .expect("Failed to load inclusion proving key."),
267                    )
268                })
269        })
270    }
271
272    /// Returns the `verifying key` for the inclusion_v0 circuit.
273    fn inclusion_v0_verifying_key() -> &'static Arc<VarunaVerifyingKey<Self>> {
274        static INSTANCE: OnceLock<Arc<VarunaVerifyingKey<Console>>> = OnceLock::new();
275        INSTANCE.get_or_init(|| {
276            // Skipping the first byte, which is the encoded version.
277            Arc::new(
278                CircuitVerifyingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_V0_VERIFYING_KEY[1..])
279                    .expect("Failed to load inclusion_v0 verifying key."),
280            )
281        })
282    }
283
284    #[cfg(not(feature = "wasm"))]
285    /// Returns the `proving key` for the inclusion circuit.
286    fn inclusion_proving_key() -> &'static Arc<VarunaProvingKey<Self>> {
287        static INSTANCE: OnceLock<Arc<VarunaProvingKey<Console>>> = OnceLock::new();
288        INSTANCE.get_or_init(|| {
289            // Skipping the first byte, which is the encoded version.
290            Arc::new(
291                CircuitProvingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_PROVING_KEY[1..])
292                    .expect("Failed to load inclusion proving key."),
293            )
294        })
295    }
296
297    #[cfg(feature = "wasm")]
298    /// Returns the `proving key` for the inclusion circuit.
299    fn inclusion_proving_key(inclusion_key_bytes: Option<Vec<u8>>) -> &'static Arc<VarunaProvingKey<Self>> {
300        static INSTANCE: OnceLock<Arc<VarunaProvingKey<Console>>> = OnceLock::new();
301        INSTANCE.get_or_init(|| {
302            inclusion_key_bytes
303                .map(|bytes| {
304                    snarkvm_parameters::canary::InclusionProver::verify_bytes(&bytes)
305                        .expect("Bytes provided did not match expected inclusion checksum.");
306                    Arc::new(
307                        CircuitProvingKey::from_bytes_le(&bytes[1..]).expect("Failed to load inclusion proving key."),
308                    )
309                })
310                .unwrap_or_else(|| {
311                    Arc::new(
312                        CircuitProvingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_PROVING_KEY[1..])
313                            .expect("Failed to load inclusion proving key."),
314                    )
315                })
316        })
317    }
318
319    /// Returns the `verifying key` for the inclusion circuit.
320    fn inclusion_verifying_key() -> &'static Arc<VarunaVerifyingKey<Self>> {
321        static INSTANCE: OnceLock<Arc<VarunaVerifyingKey<Console>>> = OnceLock::new();
322        INSTANCE.get_or_init(|| {
323            // Skipping the first byte, which is the encoded version.
324            Arc::new(
325                CircuitVerifyingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_VERIFYING_KEY[1..])
326                    .expect("Failed to load inclusion verifying key."),
327            )
328        })
329    }
330
331    /// Returns the powers of `G`.
332    fn g_powers() -> &'static Vec<Group<Self>> {
333        &GENERATOR_G
334    }
335
336    /// Returns the scalar multiplication on the generator `G`.
337    fn g_scalar_multiply(scalar: &Scalar<Self>) -> Group<Self> {
338        GENERATOR_G
339            .iter()
340            .zip_eq(&scalar.to_bits_le())
341            .filter_map(|(base, bit)| match bit {
342                true => Some(base),
343                false => None,
344            })
345            .sum()
346    }
347
348    /// Returns the Varuna universal prover.
349    fn varuna_universal_prover() -> &'static UniversalProver<Self::PairingCurve> {
350        MainnetV0::varuna_universal_prover()
351    }
352
353    /// Returns the Varuna universal verifier.
354    fn varuna_universal_verifier() -> &'static UniversalVerifier<Self::PairingCurve> {
355        MainnetV0::varuna_universal_verifier()
356    }
357
358    /// Returns the sponge parameters used for the sponge in the Varuna SNARK.
359    fn varuna_fs_parameters() -> &'static FiatShamirParameters<Self> {
360        &VARUNA_FS_PARAMETERS
361    }
362
363    /// Returns the commitment domain as a constant field element.
364    fn commitment_domain() -> Field<Self> {
365        *COMMITMENT_DOMAIN
366    }
367
368    /// Returns the encryption domain as a constant field element.
369    fn encryption_domain() -> Field<Self> {
370        *ENCRYPTION_DOMAIN
371    }
372
373    /// Returns the graph key domain as a constant field element.
374    fn graph_key_domain() -> Field<Self> {
375        *GRAPH_KEY_DOMAIN
376    }
377
378    /// Returns the serial number domain as a constant field element.
379    fn serial_number_domain() -> Field<Self> {
380        *SERIAL_NUMBER_DOMAIN
381    }
382
383    /// Returns a BHP commitment with an input hasher of 256-bits and randomizer.
384    fn commit_bhp256(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
385        CANARY_BHP_256.commit(input, randomizer)
386    }
387
388    /// Returns a BHP commitment with an input hasher of 512-bits and randomizer.
389    fn commit_bhp512(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
390        CANARY_BHP_512.commit(input, randomizer)
391    }
392
393    /// Returns a BHP commitment with an input hasher of 768-bits and randomizer.
394    fn commit_bhp768(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
395        CANARY_BHP_768.commit(input, randomizer)
396    }
397
398    /// Returns a BHP commitment with an input hasher of 1024-bits and randomizer.
399    fn commit_bhp1024(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
400        CANARY_BHP_1024.commit(input, randomizer)
401    }
402
403    /// Returns a Pedersen commitment for the given (up to) 64-bit input and randomizer.
404    fn commit_ped64(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
405        CANARY_PEDERSEN_64.commit(input, randomizer)
406    }
407
408    /// Returns a Pedersen commitment for the given (up to) 128-bit input and randomizer.
409    fn commit_ped128(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
410        CANARY_PEDERSEN_128.commit(input, randomizer)
411    }
412
413    /// Returns a BHP commitment with an input hasher of 256-bits and randomizer.
414    fn commit_to_group_bhp256(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
415        CANARY_BHP_256.commit_uncompressed(input, randomizer)
416    }
417
418    /// Returns a BHP commitment with an input hasher of 512-bits and randomizer.
419    fn commit_to_group_bhp512(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
420        CANARY_BHP_512.commit_uncompressed(input, randomizer)
421    }
422
423    /// Returns a BHP commitment with an input hasher of 768-bits and randomizer.
424    fn commit_to_group_bhp768(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
425        CANARY_BHP_768.commit_uncompressed(input, randomizer)
426    }
427
428    /// Returns a BHP commitment with an input hasher of 1024-bits and randomizer.
429    fn commit_to_group_bhp1024(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
430        CANARY_BHP_1024.commit_uncompressed(input, randomizer)
431    }
432
433    /// Returns a Pedersen commitment for the given (up to) 64-bit input and randomizer.
434    fn commit_to_group_ped64(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
435        CANARY_PEDERSEN_64.commit_uncompressed(input, randomizer)
436    }
437
438    /// Returns a Pedersen commitment for the given (up to) 128-bit input and randomizer.
439    fn commit_to_group_ped128(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
440        CANARY_PEDERSEN_128.commit_uncompressed(input, randomizer)
441    }
442
443    /// Returns the BHP hash with an input hasher of 256-bits.
444    fn hash_bhp256(input: &[bool]) -> Result<Field<Self>> {
445        CANARY_BHP_256.hash(input)
446    }
447
448    /// Returns the BHP hash with an input hasher of 512-bits.
449    fn hash_bhp512(input: &[bool]) -> Result<Field<Self>> {
450        CANARY_BHP_512.hash(input)
451    }
452
453    /// Returns the BHP hash with an input hasher of 768-bits.
454    fn hash_bhp768(input: &[bool]) -> Result<Field<Self>> {
455        CANARY_BHP_768.hash(input)
456    }
457
458    /// Returns the BHP hash with an input hasher of 1024-bits.
459    fn hash_bhp1024(input: &[bool]) -> Result<Field<Self>> {
460        CANARY_BHP_1024.hash(input)
461    }
462
463    /// Returns the Keccak hash with a 256-bit output.
464    fn hash_keccak256(input: &[bool]) -> Result<Vec<bool>> {
465        Keccak256::default().hash(input)
466    }
467
468    /// Returns the Keccak hash with a 384-bit output.
469    fn hash_keccak384(input: &[bool]) -> Result<Vec<bool>> {
470        Keccak384::default().hash(input)
471    }
472
473    /// Returns the Keccak hash with a 512-bit output.
474    fn hash_keccak512(input: &[bool]) -> Result<Vec<bool>> {
475        Keccak512::default().hash(input)
476    }
477
478    /// Returns the Pedersen hash for a given (up to) 64-bit input.
479    fn hash_ped64(input: &[bool]) -> Result<Field<Self>> {
480        CANARY_PEDERSEN_64.hash(input)
481    }
482
483    /// Returns the Pedersen hash for a given (up to) 128-bit input.
484    fn hash_ped128(input: &[bool]) -> Result<Field<Self>> {
485        CANARY_PEDERSEN_128.hash(input)
486    }
487
488    /// Returns the Poseidon hash with an input rate of 2.
489    fn hash_psd2(input: &[Field<Self>]) -> Result<Field<Self>> {
490        CANARY_POSEIDON_2.hash(input)
491    }
492
493    /// Returns the Poseidon hash with an input rate of 4.
494    fn hash_psd4(input: &[Field<Self>]) -> Result<Field<Self>> {
495        CANARY_POSEIDON_4.hash(input)
496    }
497
498    /// Returns the Poseidon hash with an input rate of 8.
499    fn hash_psd8(input: &[Field<Self>]) -> Result<Field<Self>> {
500        CANARY_POSEIDON_8.hash(input)
501    }
502
503    /// Returns the SHA-3 hash with a 256-bit output.
504    fn hash_sha3_256(input: &[bool]) -> Result<Vec<bool>> {
505        Sha3_256::default().hash(input)
506    }
507
508    /// Returns the SHA-3 hash with a 384-bit output.
509    fn hash_sha3_384(input: &[bool]) -> Result<Vec<bool>> {
510        Sha3_384::default().hash(input)
511    }
512
513    /// Returns the SHA-3 hash with a 512-bit output.
514    fn hash_sha3_512(input: &[bool]) -> Result<Vec<bool>> {
515        Sha3_512::default().hash(input)
516    }
517
518    /// Returns the extended Poseidon hash with an input rate of 2.
519    fn hash_many_psd2(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
520        CANARY_POSEIDON_2.hash_many(input, num_outputs)
521    }
522
523    /// Returns the extended Poseidon hash with an input rate of 4.
524    fn hash_many_psd4(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
525        CANARY_POSEIDON_4.hash_many(input, num_outputs)
526    }
527
528    /// Returns the extended Poseidon hash with an input rate of 8.
529    fn hash_many_psd8(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
530        CANARY_POSEIDON_8.hash_many(input, num_outputs)
531    }
532
533    /// Returns the BHP hash with an input hasher of 256-bits.
534    fn hash_to_group_bhp256(input: &[bool]) -> Result<Group<Self>> {
535        CANARY_BHP_256.hash_uncompressed(input)
536    }
537
538    /// Returns the BHP hash with an input hasher of 512-bits.
539    fn hash_to_group_bhp512(input: &[bool]) -> Result<Group<Self>> {
540        CANARY_BHP_512.hash_uncompressed(input)
541    }
542
543    /// Returns the BHP hash with an input hasher of 768-bits.
544    fn hash_to_group_bhp768(input: &[bool]) -> Result<Group<Self>> {
545        CANARY_BHP_768.hash_uncompressed(input)
546    }
547
548    /// Returns the BHP hash with an input hasher of 1024-bits.
549    fn hash_to_group_bhp1024(input: &[bool]) -> Result<Group<Self>> {
550        CANARY_BHP_1024.hash_uncompressed(input)
551    }
552
553    /// Returns the Pedersen hash for a given (up to) 64-bit input.
554    fn hash_to_group_ped64(input: &[bool]) -> Result<Group<Self>> {
555        CANARY_PEDERSEN_64.hash_uncompressed(input)
556    }
557
558    /// Returns the Pedersen hash for a given (up to) 128-bit input.
559    fn hash_to_group_ped128(input: &[bool]) -> Result<Group<Self>> {
560        CANARY_PEDERSEN_128.hash_uncompressed(input)
561    }
562
563    /// Returns the Poseidon hash with an input rate of 2 on the affine curve.
564    fn hash_to_group_psd2(input: &[Field<Self>]) -> Result<Group<Self>> {
565        CANARY_POSEIDON_2.hash_to_group(input)
566    }
567
568    /// Returns the Poseidon hash with an input rate of 4 on the affine curve.
569    fn hash_to_group_psd4(input: &[Field<Self>]) -> Result<Group<Self>> {
570        CANARY_POSEIDON_4.hash_to_group(input)
571    }
572
573    /// Returns the Poseidon hash with an input rate of 8 on the affine curve.
574    fn hash_to_group_psd8(input: &[Field<Self>]) -> Result<Group<Self>> {
575        CANARY_POSEIDON_8.hash_to_group(input)
576    }
577
578    /// Returns the Poseidon hash with an input rate of 2 on the scalar field.
579    fn hash_to_scalar_psd2(input: &[Field<Self>]) -> Result<Scalar<Self>> {
580        CANARY_POSEIDON_2.hash_to_scalar(input)
581    }
582
583    /// Returns the Poseidon hash with an input rate of 4 on the scalar field.
584    fn hash_to_scalar_psd4(input: &[Field<Self>]) -> Result<Scalar<Self>> {
585        CANARY_POSEIDON_4.hash_to_scalar(input)
586    }
587
588    /// Returns the Poseidon hash with an input rate of 8 on the scalar field.
589    fn hash_to_scalar_psd8(input: &[Field<Self>]) -> Result<Scalar<Self>> {
590        CANARY_POSEIDON_8.hash_to_scalar(input)
591    }
592
593    /// Returns a Merkle tree with a BHP leaf hasher of 1024-bits and a BHP path hasher of 512-bits.
594    fn merkle_tree_bhp<const DEPTH: u8>(leaves: &[Vec<bool>]) -> Result<BHPMerkleTree<Self, DEPTH>> {
595        MerkleTree::new(&*CANARY_BHP_1024, &*CANARY_BHP_512, leaves)
596    }
597
598    /// Returns a Merkle tree with a Poseidon leaf hasher with input rate of 4 and a Poseidon path hasher with input rate of 2.
599    fn merkle_tree_psd<const DEPTH: u8>(leaves: &[Vec<Field<Self>>]) -> Result<PoseidonMerkleTree<Self, DEPTH>> {
600        MerkleTree::new(&*CANARY_POSEIDON_4, &*CANARY_POSEIDON_2, leaves)
601    }
602
603    /// Returns `true` if the given Merkle path is valid for the given root and leaf.
604    fn verify_merkle_path_bhp<const DEPTH: u8>(
605        path: &MerklePath<Self, DEPTH>,
606        root: &Field<Self>,
607        leaf: &Vec<bool>,
608    ) -> bool {
609        path.verify(&*CANARY_BHP_1024, &*CANARY_BHP_512, root, leaf)
610    }
611
612    /// Returns `true` if the given Merkle path is valid for the given root and leaf.
613    fn verify_merkle_path_psd<const DEPTH: u8>(
614        path: &MerklePath<Self, DEPTH>,
615        root: &Field<Self>,
616        leaf: &Vec<Field<Self>>,
617    ) -> bool {
618        path.verify(&*CANARY_POSEIDON_4, &*CANARY_POSEIDON_2, root, leaf)
619    }
620}
621
622#[cfg(test)]
623mod tests {
624    use super::*;
625
626    type CurrentNetwork = CanaryV0;
627
628    #[test]
629    fn test_g_scalar_multiply() {
630        // Compute G^r.
631        let scalar = Scalar::rand(&mut TestRng::default());
632        let group = CurrentNetwork::g_scalar_multiply(&scalar);
633        assert_eq!(group, CurrentNetwork::g_powers()[0] * scalar);
634    }
635}