1use 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 static ref GENERATOR_G: Vec<Group<CanaryV0 >> = CanaryV0::new_bases("AleoAccountEncryptionAndSignatureScheme0");
40
41 static ref VARUNA_FS_PARAMETERS: FiatShamirParameters<CanaryV0> = FiatShamir::<CanaryV0>::sample_parameters();
43
44 static ref COMMITMENT_DOMAIN: Field<CanaryV0> = Field::<CanaryV0>::new_domain_separator("AleoCommitment0");
46 static ref ENCRYPTION_DOMAIN: Field<CanaryV0> = Field::<CanaryV0>::new_domain_separator("AleoSymmetricEncryption0");
48 static ref GRAPH_KEY_DOMAIN: Field<CanaryV0> = Field::<CanaryV0>::new_domain_separator("AleoGraphKey0");
50 static ref SERIAL_NUMBER_DOMAIN: Field<CanaryV0> = Field::<CanaryV0>::new_domain_separator("AleoSerialNumber0");
52
53 pub static ref CANARY_BHP_256: BHP256<CanaryV0> = BHP256::<CanaryV0>::setup("AleoBHP256").expect("Failed to setup BHP256");
55 pub static ref CANARY_BHP_512: BHP512<CanaryV0> = BHP512::<CanaryV0>::setup("AleoBHP512").expect("Failed to setup BHP512");
57 pub static ref CANARY_BHP_768: BHP768<CanaryV0> = BHP768::<CanaryV0>::setup("AleoBHP768").expect("Failed to setup BHP768");
59 pub static ref CANARY_BHP_1024: BHP1024<CanaryV0> = BHP1024::<CanaryV0>::setup("AleoBHP1024").expect("Failed to setup BHP1024");
61
62 pub static ref CANARY_PEDERSEN_64: Pedersen64<CanaryV0> = Pedersen64::<CanaryV0>::setup("AleoPedersen64");
64 pub static ref CANARY_PEDERSEN_128: Pedersen128<CanaryV0> = Pedersen128::<CanaryV0>::setup("AleoPedersen128");
66
67 pub static ref CANARY_POSEIDON_2: Poseidon2<CanaryV0> = Poseidon2::<CanaryV0>::setup("AleoPoseidon2").expect("Failed to setup Poseidon2");
69 pub static ref CANARY_POSEIDON_4: Poseidon4<CanaryV0> = Poseidon4::<CanaryV0>::setup("AleoPoseidon4").expect("Failed to setup Poseidon4");
71 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 fn new_bases(message: &str) -> Vec<Group<Self>> {
103 let (base, _, _) = Blake2Xs::hash_to_curve::<<Self as Environment>::Affine>(message);
105
106 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 const EDWARDS_A: Self::Field = Console::EDWARDS_A;
127 const EDWARDS_D: Self::Field = Console::EDWARDS_D;
129 const MONTGOMERY_A: Self::Field = Console::MONTGOMERY_A;
131 const MONTGOMERY_B: Self::Field = Console::MONTGOMERY_B;
133}
134
135impl Network for CanaryV0 {
136 type BlockHash = AleoID<Field<Self>, { hrp2!("ab") }>;
138 type RatificationID = AleoID<Field<Self>, { hrp2!("ar") }>;
140 type StateRoot = AleoID<Field<Self>, { hrp2!("sr") }>;
142 type TransactionID = AleoID<Field<Self>, { hrp2!(TRANSACTION_PREFIX) }>;
144 type TransitionID = AleoID<Field<Self>, { hrp2!("au") }>;
146 type TransmissionChecksum = u128;
148
149 #[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 #[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 const GENESIS_TIMESTAMP: i64 = 1715776496 ;
161 const ID: u16 = 2;
163 const INCLUSION_FUNCTION_NAME: &'static str = snarkvm_parameters::canary::NETWORK_INCLUSION_FUNCTION_NAME;
165 #[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 #[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 const NAME: &'static str = "Aleo Canary (v0)";
185 const SHORT_NAME: &'static str = "canary";
187 const _CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] =
191 CANARY_V0_CONSENSUS_VERSION_HEIGHTS;
192
193 #[allow(non_snake_case)]
195 fn INCLUSION_UPGRADE_HEIGHT() -> Result<u32> {
196 Self::CONSENSUS_HEIGHT(ConsensusVersion::V8)
197 }
198
199 fn genesis_bytes() -> &'static [u8] {
201 snarkvm_parameters::canary::GenesisBytes::load_bytes()
202 }
203
204 fn restrictions_list_as_str() -> &'static str {
206 snarkvm_parameters::canary::RESTRICTIONS_LIST
207 }
208
209 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 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 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 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 fn inclusion_v0_proving_key() -> &'static Arc<VarunaProvingKey<Self>> {
239 static INSTANCE: OnceLock<Arc<VarunaProvingKey<Console>>> = OnceLock::new();
240 INSTANCE.get_or_init(|| {
241 Arc::new(
243 CircuitProvingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_V0_PROVING_KEY[1..])
244 .expect("Failed to load inclusion_v0 proving key."),
245 )
246 })
247 }
248
249 fn inclusion_v0_verifying_key() -> &'static Arc<VarunaVerifyingKey<Self>> {
251 static INSTANCE: OnceLock<Arc<VarunaVerifyingKey<Console>>> = OnceLock::new();
252 INSTANCE.get_or_init(|| {
253 Arc::new(
255 CircuitVerifyingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_V0_VERIFYING_KEY[1..])
256 .expect("Failed to load inclusion_v0 verifying key."),
257 )
258 })
259 }
260
261 fn inclusion_proving_key() -> &'static Arc<VarunaProvingKey<Self>> {
263 static INSTANCE: OnceLock<Arc<VarunaProvingKey<Console>>> = OnceLock::new();
264 INSTANCE.get_or_init(|| {
265 Arc::new(
267 CircuitProvingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_PROVING_KEY[1..])
268 .expect("Failed to load inclusion proving key."),
269 )
270 })
271 }
272
273 fn inclusion_verifying_key() -> &'static Arc<VarunaVerifyingKey<Self>> {
275 static INSTANCE: OnceLock<Arc<VarunaVerifyingKey<Console>>> = OnceLock::new();
276 INSTANCE.get_or_init(|| {
277 Arc::new(
279 CircuitVerifyingKey::from_bytes_le(&snarkvm_parameters::canary::INCLUSION_VERIFYING_KEY[1..])
280 .expect("Failed to load inclusion verifying key."),
281 )
282 })
283 }
284
285 fn g_powers() -> &'static Vec<Group<Self>> {
287 &GENERATOR_G
288 }
289
290 fn g_scalar_multiply(scalar: &Scalar<Self>) -> Group<Self> {
292 GENERATOR_G
293 .iter()
294 .zip_eq(&scalar.to_bits_le())
295 .filter_map(|(base, bit)| match bit {
296 true => Some(base),
297 false => None,
298 })
299 .sum()
300 }
301
302 fn varuna_universal_prover() -> &'static UniversalProver<Self::PairingCurve> {
304 MainnetV0::varuna_universal_prover()
305 }
306
307 fn varuna_universal_verifier() -> &'static UniversalVerifier<Self::PairingCurve> {
309 MainnetV0::varuna_universal_verifier()
310 }
311
312 fn varuna_fs_parameters() -> &'static FiatShamirParameters<Self> {
314 &VARUNA_FS_PARAMETERS
315 }
316
317 fn commitment_domain() -> Field<Self> {
319 *COMMITMENT_DOMAIN
320 }
321
322 fn encryption_domain() -> Field<Self> {
324 *ENCRYPTION_DOMAIN
325 }
326
327 fn graph_key_domain() -> Field<Self> {
329 *GRAPH_KEY_DOMAIN
330 }
331
332 fn serial_number_domain() -> Field<Self> {
334 *SERIAL_NUMBER_DOMAIN
335 }
336
337 fn commit_bhp256(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
339 CANARY_BHP_256.commit(input, randomizer)
340 }
341
342 fn commit_bhp512(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
344 CANARY_BHP_512.commit(input, randomizer)
345 }
346
347 fn commit_bhp768(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
349 CANARY_BHP_768.commit(input, randomizer)
350 }
351
352 fn commit_bhp1024(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
354 CANARY_BHP_1024.commit(input, randomizer)
355 }
356
357 fn commit_ped64(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
359 CANARY_PEDERSEN_64.commit(input, randomizer)
360 }
361
362 fn commit_ped128(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
364 CANARY_PEDERSEN_128.commit(input, randomizer)
365 }
366
367 fn commit_to_group_bhp256(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
369 CANARY_BHP_256.commit_uncompressed(input, randomizer)
370 }
371
372 fn commit_to_group_bhp512(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
374 CANARY_BHP_512.commit_uncompressed(input, randomizer)
375 }
376
377 fn commit_to_group_bhp768(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
379 CANARY_BHP_768.commit_uncompressed(input, randomizer)
380 }
381
382 fn commit_to_group_bhp1024(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
384 CANARY_BHP_1024.commit_uncompressed(input, randomizer)
385 }
386
387 fn commit_to_group_ped64(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
389 CANARY_PEDERSEN_64.commit_uncompressed(input, randomizer)
390 }
391
392 fn commit_to_group_ped128(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
394 CANARY_PEDERSEN_128.commit_uncompressed(input, randomizer)
395 }
396
397 fn hash_bhp256(input: &[bool]) -> Result<Field<Self>> {
399 CANARY_BHP_256.hash(input)
400 }
401
402 fn hash_bhp512(input: &[bool]) -> Result<Field<Self>> {
404 CANARY_BHP_512.hash(input)
405 }
406
407 fn hash_bhp768(input: &[bool]) -> Result<Field<Self>> {
409 CANARY_BHP_768.hash(input)
410 }
411
412 fn hash_bhp1024(input: &[bool]) -> Result<Field<Self>> {
414 CANARY_BHP_1024.hash(input)
415 }
416
417 fn hash_keccak256(input: &[bool]) -> Result<Vec<bool>> {
419 Keccak256::default().hash(input)
420 }
421
422 fn hash_keccak384(input: &[bool]) -> Result<Vec<bool>> {
424 Keccak384::default().hash(input)
425 }
426
427 fn hash_keccak512(input: &[bool]) -> Result<Vec<bool>> {
429 Keccak512::default().hash(input)
430 }
431
432 fn hash_ped64(input: &[bool]) -> Result<Field<Self>> {
434 CANARY_PEDERSEN_64.hash(input)
435 }
436
437 fn hash_ped128(input: &[bool]) -> Result<Field<Self>> {
439 CANARY_PEDERSEN_128.hash(input)
440 }
441
442 fn hash_psd2(input: &[Field<Self>]) -> Result<Field<Self>> {
444 CANARY_POSEIDON_2.hash(input)
445 }
446
447 fn hash_psd4(input: &[Field<Self>]) -> Result<Field<Self>> {
449 CANARY_POSEIDON_4.hash(input)
450 }
451
452 fn hash_psd8(input: &[Field<Self>]) -> Result<Field<Self>> {
454 CANARY_POSEIDON_8.hash(input)
455 }
456
457 fn hash_sha3_256(input: &[bool]) -> Result<Vec<bool>> {
459 Sha3_256::default().hash(input)
460 }
461
462 fn hash_sha3_384(input: &[bool]) -> Result<Vec<bool>> {
464 Sha3_384::default().hash(input)
465 }
466
467 fn hash_sha3_512(input: &[bool]) -> Result<Vec<bool>> {
469 Sha3_512::default().hash(input)
470 }
471
472 fn hash_many_psd2(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
474 CANARY_POSEIDON_2.hash_many(input, num_outputs)
475 }
476
477 fn hash_many_psd4(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
479 CANARY_POSEIDON_4.hash_many(input, num_outputs)
480 }
481
482 fn hash_many_psd8(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
484 CANARY_POSEIDON_8.hash_many(input, num_outputs)
485 }
486
487 fn hash_to_group_bhp256(input: &[bool]) -> Result<Group<Self>> {
489 CANARY_BHP_256.hash_uncompressed(input)
490 }
491
492 fn hash_to_group_bhp512(input: &[bool]) -> Result<Group<Self>> {
494 CANARY_BHP_512.hash_uncompressed(input)
495 }
496
497 fn hash_to_group_bhp768(input: &[bool]) -> Result<Group<Self>> {
499 CANARY_BHP_768.hash_uncompressed(input)
500 }
501
502 fn hash_to_group_bhp1024(input: &[bool]) -> Result<Group<Self>> {
504 CANARY_BHP_1024.hash_uncompressed(input)
505 }
506
507 fn hash_to_group_ped64(input: &[bool]) -> Result<Group<Self>> {
509 CANARY_PEDERSEN_64.hash_uncompressed(input)
510 }
511
512 fn hash_to_group_ped128(input: &[bool]) -> Result<Group<Self>> {
514 CANARY_PEDERSEN_128.hash_uncompressed(input)
515 }
516
517 fn hash_to_group_psd2(input: &[Field<Self>]) -> Result<Group<Self>> {
519 CANARY_POSEIDON_2.hash_to_group(input)
520 }
521
522 fn hash_to_group_psd4(input: &[Field<Self>]) -> Result<Group<Self>> {
524 CANARY_POSEIDON_4.hash_to_group(input)
525 }
526
527 fn hash_to_group_psd8(input: &[Field<Self>]) -> Result<Group<Self>> {
529 CANARY_POSEIDON_8.hash_to_group(input)
530 }
531
532 fn hash_to_scalar_psd2(input: &[Field<Self>]) -> Result<Scalar<Self>> {
534 CANARY_POSEIDON_2.hash_to_scalar(input)
535 }
536
537 fn hash_to_scalar_psd4(input: &[Field<Self>]) -> Result<Scalar<Self>> {
539 CANARY_POSEIDON_4.hash_to_scalar(input)
540 }
541
542 fn hash_to_scalar_psd8(input: &[Field<Self>]) -> Result<Scalar<Self>> {
544 CANARY_POSEIDON_8.hash_to_scalar(input)
545 }
546
547 fn merkle_tree_bhp<const DEPTH: u8>(leaves: &[Vec<bool>]) -> Result<BHPMerkleTree<Self, DEPTH>> {
549 MerkleTree::new(&*CANARY_BHP_1024, &*CANARY_BHP_512, leaves)
550 }
551
552 fn merkle_tree_psd<const DEPTH: u8>(leaves: &[Vec<Field<Self>>]) -> Result<PoseidonMerkleTree<Self, DEPTH>> {
554 MerkleTree::new(&*CANARY_POSEIDON_4, &*CANARY_POSEIDON_2, leaves)
555 }
556
557 fn verify_merkle_path_bhp<const DEPTH: u8>(
559 path: &MerklePath<Self, DEPTH>,
560 root: &Field<Self>,
561 leaf: &Vec<bool>,
562 ) -> bool {
563 path.verify(&*CANARY_BHP_1024, &*CANARY_BHP_512, root, leaf)
564 }
565
566 fn verify_merkle_path_psd<const DEPTH: u8>(
568 path: &MerklePath<Self, DEPTH>,
569 root: &Field<Self>,
570 leaf: &Vec<Field<Self>>,
571 ) -> bool {
572 path.verify(&*CANARY_POSEIDON_4, &*CANARY_POSEIDON_2, root, leaf)
573 }
574}
575
576#[cfg(test)]
577mod tests {
578 use super::*;
579
580 type CurrentNetwork = CanaryV0;
581
582 #[test]
583 fn test_g_scalar_multiply() {
584 let scalar = Scalar::rand(&mut TestRng::default());
586 let group = CurrentNetwork::g_scalar_multiply(&scalar);
587 assert_eq!(group, CurrentNetwork::g_powers()[0] * scalar);
588 }
589}