1use super::*;
17use snarkvm_console_algorithms::{
18 BHP256,
19 BHP512,
20 BHP768,
21 BHP1024,
22 Blake2Xs,
23 Keccak256,
24 Keccak384,
25 Keccak512,
26 Pedersen64,
27 Pedersen128,
28 Poseidon2,
29 Poseidon4,
30 Poseidon8,
31 Sha3_256,
32 Sha3_384,
33 Sha3_512,
34};
35
36lazy_static! {
37 pub static ref GENERATOR_G: Vec<Group<MainnetV0 >> = MainnetV0::new_bases("AleoAccountEncryptionAndSignatureScheme0");
39
40 pub static ref VARUNA_FS_PARAMETERS: FiatShamirParameters<MainnetV0> = FiatShamir::<MainnetV0>::sample_parameters();
42
43 static ref COMMITMENT_DOMAIN: Field<MainnetV0> = Field::<MainnetV0>::new_domain_separator("AleoCommitment0");
45 pub static ref ENCRYPTION_DOMAIN: Field<MainnetV0> = Field::<MainnetV0>::new_domain_separator("AleoSymmetricEncryption0");
47 pub static ref GRAPH_KEY_DOMAIN: Field<MainnetV0> = Field::<MainnetV0>::new_domain_separator("AleoGraphKey0");
49 pub static ref SERIAL_NUMBER_DOMAIN: Field<MainnetV0> = Field::<MainnetV0>::new_domain_separator("AleoSerialNumber0");
51
52 pub static ref BHP_256: BHP256<MainnetV0> = BHP256::<MainnetV0>::setup("AleoBHP256").expect("Failed to setup BHP256");
54 pub static ref BHP_512: BHP512<MainnetV0> = BHP512::<MainnetV0>::setup("AleoBHP512").expect("Failed to setup BHP512");
56 pub static ref BHP_768: BHP768<MainnetV0> = BHP768::<MainnetV0>::setup("AleoBHP768").expect("Failed to setup BHP768");
58 pub static ref BHP_1024: BHP1024<MainnetV0> = BHP1024::<MainnetV0>::setup("AleoBHP1024").expect("Failed to setup BHP1024");
60
61 pub static ref PEDERSEN_64: Pedersen64<MainnetV0> = Pedersen64::<MainnetV0>::setup("AleoPedersen64");
63 pub static ref PEDERSEN_128: Pedersen128<MainnetV0> = Pedersen128::<MainnetV0>::setup("AleoPedersen128");
65
66 pub static ref POSEIDON_2: Poseidon2<MainnetV0> = Poseidon2::<MainnetV0>::setup("AleoPoseidon2").expect("Failed to setup Poseidon2");
68 pub static ref POSEIDON_4: Poseidon4<MainnetV0> = Poseidon4::<MainnetV0>::setup("AleoPoseidon4").expect("Failed to setup Poseidon4");
70 pub static ref POSEIDON_8: Poseidon8<MainnetV0> = Poseidon8::<MainnetV0>::setup("AleoPoseidon8").expect("Failed to setup Poseidon8");
72
73 pub static ref CREDITS_V0_PROVING_KEYS: IndexMap<String, Arc<VarunaProvingKey<Console>>> = {
74 let mut map = IndexMap::new();
75 snarkvm_parameters::insert_credit_v0_keys!(map, VarunaProvingKey<Console>, Prover);
76 map
77 };
78 pub static ref CREDITS_V0_VERIFYING_KEYS: IndexMap<String, Arc<VarunaVerifyingKey<Console>>> = {
79 let mut map = IndexMap::new();
80 snarkvm_parameters::insert_credit_v0_keys!(map, VarunaVerifyingKey<Console>, Verifier);
81 map
82 };
83
84 pub static ref CREDITS_PROVING_KEYS: IndexMap<String, Arc<VarunaProvingKey<Console>>> = {
85 let mut map = IndexMap::new();
86 snarkvm_parameters::insert_credit_keys!(map, VarunaProvingKey<Console>, Prover);
87 map
88 };
89 pub static ref CREDITS_VERIFYING_KEYS: IndexMap<String, Arc<VarunaVerifyingKey<Console>>> = {
90 let mut map = IndexMap::new();
91 snarkvm_parameters::insert_credit_keys!(map, VarunaVerifyingKey<Console>, Verifier);
92 map
93 };
94}
95
96pub const TRANSACTION_PREFIX: &str = "at";
97
98#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
99pub struct MainnetV0;
100
101impl MainnetV0 {
102 fn new_bases(message: &str) -> Vec<Group<Self>> {
104 let (base, _, _) = Blake2Xs::hash_to_curve::<<Self as Environment>::Affine>(message);
106
107 let mut g = Group::<Self>::new(base);
109 let mut g_bases = Vec::with_capacity(Scalar::<Self>::size_in_bits());
110 for _ in 0..Scalar::<Self>::size_in_bits() {
111 g_bases.push(g);
112 g = g.double();
113 }
114 g_bases
115 }
116}
117
118impl Environment for MainnetV0 {
119 type Affine = <Console as Environment>::Affine;
120 type BigInteger = <Console as Environment>::BigInteger;
121 type Field = <Console as Environment>::Field;
122 type PairingCurve = <Console as Environment>::PairingCurve;
123 type Projective = <Console as Environment>::Projective;
124 type Scalar = <Console as Environment>::Scalar;
125
126 const EDWARDS_A: Self::Field = Console::EDWARDS_A;
128 const EDWARDS_D: Self::Field = Console::EDWARDS_D;
130 const MONTGOMERY_A: Self::Field = Console::MONTGOMERY_A;
132 const MONTGOMERY_B: Self::Field = Console::MONTGOMERY_B;
134}
135
136impl Network for MainnetV0 {
137 type BlockHash = AleoID<Field<Self>, { hrp2!("ab") }>;
139 type RatificationID = AleoID<Field<Self>, { hrp2!("ar") }>;
141 type StateRoot = AleoID<Field<Self>, { hrp2!("sr") }>;
143 type TransactionID = AleoID<Field<Self>, { hrp2!(TRANSACTION_PREFIX) }>;
145 type TransitionID = AleoID<Field<Self>, { hrp2!("au") }>;
147 type TransmissionChecksum = u128;
149
150 #[cfg(not(feature = "test"))]
152 const GENESIS_COINBASE_TARGET: u64 = (1u64 << 29).saturating_sub(1);
153 #[cfg(feature = "test")]
156 const GENESIS_COINBASE_TARGET: u64 = (1u64 << 5).saturating_sub(1);
157 #[cfg(not(feature = "test"))]
159 const GENESIS_PROOF_TARGET: u64 = 1u64 << 27;
160 #[cfg(feature = "test")]
163 const GENESIS_PROOF_TARGET: u64 = 1u64 << 3;
164 const GENESIS_TIMESTAMP: i64 = 1725462000 ;
166 const ID: u16 = 0;
168 const INCLUSION_FUNCTION_NAME: &'static str = snarkvm_parameters::mainnet::NETWORK_INCLUSION_FUNCTION_NAME;
170 #[cfg(not(any(test, feature = "test")))]
172 const MAX_CERTIFICATES: [(ConsensusVersion, u16); 5] = [
173 (ConsensusVersion::V1, 16),
174 (ConsensusVersion::V3, 25),
175 (ConsensusVersion::V5, 30),
176 (ConsensusVersion::V6, 35),
177 (ConsensusVersion::V9, 40),
178 ];
179 #[cfg(any(test, feature = "test"))]
181 const MAX_CERTIFICATES: [(ConsensusVersion, u16); 5] = [
182 (ConsensusVersion::V1, 100),
183 (ConsensusVersion::V3, 101),
184 (ConsensusVersion::V5, 102),
185 (ConsensusVersion::V6, 103),
186 (ConsensusVersion::V9, 104),
187 ];
188 const NAME: &'static str = "Aleo Mainnet (v0)";
190 const SHORT_NAME: &'static str = "mainnet";
192 const _CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); NUM_CONSENSUS_VERSIONS] =
196 MAINNET_V0_CONSENSUS_VERSION_HEIGHTS;
197
198 #[allow(non_snake_case)]
200 fn INCLUSION_UPGRADE_HEIGHT() -> Result<u32> {
201 Self::CONSENSUS_HEIGHT(ConsensusVersion::V8)
202 }
203
204 fn genesis_bytes() -> &'static [u8] {
206 snarkvm_parameters::mainnet::GenesisBytes::load_bytes()
207 }
208
209 fn restrictions_list_as_str() -> &'static str {
211 snarkvm_parameters::mainnet::RESTRICTIONS_LIST
212 }
213
214 fn get_credits_v0_proving_key(function_name: String) -> Result<&'static Arc<VarunaProvingKey<Self>>> {
216 CREDITS_V0_PROVING_KEYS
217 .get(&function_name)
218 .ok_or_else(|| anyhow!("Proving key (v0) for credits.aleo/{function_name}' not found"))
219 }
220
221 fn get_credits_v0_verifying_key(function_name: String) -> Result<&'static Arc<VarunaVerifyingKey<Self>>> {
223 CREDITS_V0_VERIFYING_KEYS
224 .get(&function_name)
225 .ok_or_else(|| anyhow!("Verifying key (v0) for credits_v0.aleo/{function_name}' not found"))
226 }
227
228 fn get_credits_proving_key(function_name: String) -> Result<&'static Arc<VarunaProvingKey<Self>>> {
230 CREDITS_PROVING_KEYS
231 .get(&function_name)
232 .ok_or_else(|| anyhow!("Proving key for credits.aleo/{function_name}' not found"))
233 }
234
235 fn get_credits_verifying_key(function_name: String) -> Result<&'static Arc<VarunaVerifyingKey<Self>>> {
237 CREDITS_VERIFYING_KEYS
238 .get(&function_name)
239 .ok_or_else(|| anyhow!("Verifying key for credits.aleo/{function_name}' not found"))
240 }
241
242 fn inclusion_v0_proving_key() -> &'static Arc<VarunaProvingKey<Self>> {
244 static INSTANCE: OnceLock<Arc<VarunaProvingKey<Console>>> = OnceLock::new();
245 INSTANCE.get_or_init(|| {
246 Arc::new(
248 CircuitProvingKey::from_bytes_le(&snarkvm_parameters::mainnet::INCLUSION_V0_PROVING_KEY[1..])
249 .expect("Failed to load inclusion_v0 proving key."),
250 )
251 })
252 }
253
254 fn inclusion_v0_verifying_key() -> &'static Arc<VarunaVerifyingKey<Self>> {
256 static INSTANCE: OnceLock<Arc<VarunaVerifyingKey<Console>>> = OnceLock::new();
257 INSTANCE.get_or_init(|| {
258 Arc::new(
260 CircuitVerifyingKey::from_bytes_le(&snarkvm_parameters::mainnet::INCLUSION_V0_VERIFYING_KEY[1..])
261 .expect("Failed to load inclusion_v0 verifying key."),
262 )
263 })
264 }
265
266 fn inclusion_proving_key() -> &'static Arc<VarunaProvingKey<Self>> {
268 static INSTANCE: OnceLock<Arc<VarunaProvingKey<Console>>> = OnceLock::new();
269 INSTANCE.get_or_init(|| {
270 Arc::new(
272 CircuitProvingKey::from_bytes_le(&snarkvm_parameters::mainnet::INCLUSION_PROVING_KEY[1..])
273 .expect("Failed to load inclusion proving key."),
274 )
275 })
276 }
277
278 fn inclusion_verifying_key() -> &'static Arc<VarunaVerifyingKey<Self>> {
280 static INSTANCE: OnceLock<Arc<VarunaVerifyingKey<Console>>> = OnceLock::new();
281 INSTANCE.get_or_init(|| {
282 Arc::new(
284 CircuitVerifyingKey::from_bytes_le(&snarkvm_parameters::mainnet::INCLUSION_VERIFYING_KEY[1..])
285 .expect("Failed to load inclusion verifying key."),
286 )
287 })
288 }
289
290 fn g_powers() -> &'static Vec<Group<Self>> {
292 &GENERATOR_G
293 }
294
295 fn g_scalar_multiply(scalar: &Scalar<Self>) -> Group<Self> {
297 GENERATOR_G
298 .iter()
299 .zip_eq(&scalar.to_bits_le())
300 .filter_map(|(base, bit)| match bit {
301 true => Some(base),
302 false => None,
303 })
304 .sum()
305 }
306
307 fn varuna_universal_prover() -> &'static UniversalProver<Self::PairingCurve> {
309 static INSTANCE: OnceLock<UniversalProver<<Console as Environment>::PairingCurve>> = OnceLock::new();
310 INSTANCE.get_or_init(|| {
311 snarkvm_algorithms::polycommit::kzg10::UniversalParams::load()
312 .expect("Failed to load universal SRS (KZG10).")
313 .to_universal_prover()
314 .expect("Failed to convert universal SRS (KZG10) to the prover.")
315 })
316 }
317
318 fn varuna_universal_verifier() -> &'static UniversalVerifier<Self::PairingCurve> {
320 static INSTANCE: OnceLock<UniversalVerifier<<Console as Environment>::PairingCurve>> = OnceLock::new();
321 INSTANCE.get_or_init(|| {
322 snarkvm_algorithms::polycommit::kzg10::UniversalParams::load()
323 .expect("Failed to load universal SRS (KZG10).")
324 .to_universal_verifier()
325 .expect("Failed to convert universal SRS (KZG10) to the verifier.")
326 })
327 }
328
329 fn varuna_fs_parameters() -> &'static FiatShamirParameters<Self> {
331 &VARUNA_FS_PARAMETERS
332 }
333
334 fn commitment_domain() -> Field<Self> {
336 *COMMITMENT_DOMAIN
337 }
338
339 fn encryption_domain() -> Field<Self> {
341 *ENCRYPTION_DOMAIN
342 }
343
344 fn graph_key_domain() -> Field<Self> {
346 *GRAPH_KEY_DOMAIN
347 }
348
349 fn serial_number_domain() -> Field<Self> {
351 *SERIAL_NUMBER_DOMAIN
352 }
353
354 fn commit_bhp256(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
356 BHP_256.commit(input, randomizer)
357 }
358
359 fn commit_bhp512(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
361 BHP_512.commit(input, randomizer)
362 }
363
364 fn commit_bhp768(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
366 BHP_768.commit(input, randomizer)
367 }
368
369 fn commit_bhp1024(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
371 BHP_1024.commit(input, randomizer)
372 }
373
374 fn commit_ped64(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
376 PEDERSEN_64.commit(input, randomizer)
377 }
378
379 fn commit_ped128(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
381 PEDERSEN_128.commit(input, randomizer)
382 }
383
384 fn commit_to_group_bhp256(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
386 BHP_256.commit_uncompressed(input, randomizer)
387 }
388
389 fn commit_to_group_bhp512(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
391 BHP_512.commit_uncompressed(input, randomizer)
392 }
393
394 fn commit_to_group_bhp768(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
396 BHP_768.commit_uncompressed(input, randomizer)
397 }
398
399 fn commit_to_group_bhp1024(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
401 BHP_1024.commit_uncompressed(input, randomizer)
402 }
403
404 fn commit_to_group_ped64(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
406 PEDERSEN_64.commit_uncompressed(input, randomizer)
407 }
408
409 fn commit_to_group_ped128(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
411 PEDERSEN_128.commit_uncompressed(input, randomizer)
412 }
413
414 fn hash_bhp256(input: &[bool]) -> Result<Field<Self>> {
416 BHP_256.hash(input)
417 }
418
419 fn hash_bhp512(input: &[bool]) -> Result<Field<Self>> {
421 BHP_512.hash(input)
422 }
423
424 fn hash_bhp768(input: &[bool]) -> Result<Field<Self>> {
426 BHP_768.hash(input)
427 }
428
429 fn hash_bhp1024(input: &[bool]) -> Result<Field<Self>> {
431 BHP_1024.hash(input)
432 }
433
434 fn hash_keccak256(input: &[bool]) -> Result<Vec<bool>> {
436 Keccak256::default().hash(input)
437 }
438
439 fn hash_keccak384(input: &[bool]) -> Result<Vec<bool>> {
441 Keccak384::default().hash(input)
442 }
443
444 fn hash_keccak512(input: &[bool]) -> Result<Vec<bool>> {
446 Keccak512::default().hash(input)
447 }
448
449 fn hash_ped64(input: &[bool]) -> Result<Field<Self>> {
451 PEDERSEN_64.hash(input)
452 }
453
454 fn hash_ped128(input: &[bool]) -> Result<Field<Self>> {
456 PEDERSEN_128.hash(input)
457 }
458
459 fn hash_psd2(input: &[Field<Self>]) -> Result<Field<Self>> {
461 POSEIDON_2.hash(input)
462 }
463
464 fn hash_psd4(input: &[Field<Self>]) -> Result<Field<Self>> {
466 POSEIDON_4.hash(input)
467 }
468
469 fn hash_psd8(input: &[Field<Self>]) -> Result<Field<Self>> {
471 POSEIDON_8.hash(input)
472 }
473
474 fn hash_sha3_256(input: &[bool]) -> Result<Vec<bool>> {
476 Sha3_256::default().hash(input)
477 }
478
479 fn hash_sha3_384(input: &[bool]) -> Result<Vec<bool>> {
481 Sha3_384::default().hash(input)
482 }
483
484 fn hash_sha3_512(input: &[bool]) -> Result<Vec<bool>> {
486 Sha3_512::default().hash(input)
487 }
488
489 fn hash_many_psd2(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
491 POSEIDON_2.hash_many(input, num_outputs)
492 }
493
494 fn hash_many_psd4(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
496 POSEIDON_4.hash_many(input, num_outputs)
497 }
498
499 fn hash_many_psd8(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
501 POSEIDON_8.hash_many(input, num_outputs)
502 }
503
504 fn hash_to_group_bhp256(input: &[bool]) -> Result<Group<Self>> {
506 BHP_256.hash_uncompressed(input)
507 }
508
509 fn hash_to_group_bhp512(input: &[bool]) -> Result<Group<Self>> {
511 BHP_512.hash_uncompressed(input)
512 }
513
514 fn hash_to_group_bhp768(input: &[bool]) -> Result<Group<Self>> {
516 BHP_768.hash_uncompressed(input)
517 }
518
519 fn hash_to_group_bhp1024(input: &[bool]) -> Result<Group<Self>> {
521 BHP_1024.hash_uncompressed(input)
522 }
523
524 fn hash_to_group_ped64(input: &[bool]) -> Result<Group<Self>> {
526 PEDERSEN_64.hash_uncompressed(input)
527 }
528
529 fn hash_to_group_ped128(input: &[bool]) -> Result<Group<Self>> {
531 PEDERSEN_128.hash_uncompressed(input)
532 }
533
534 fn hash_to_group_psd2(input: &[Field<Self>]) -> Result<Group<Self>> {
536 POSEIDON_2.hash_to_group(input)
537 }
538
539 fn hash_to_group_psd4(input: &[Field<Self>]) -> Result<Group<Self>> {
541 POSEIDON_4.hash_to_group(input)
542 }
543
544 fn hash_to_group_psd8(input: &[Field<Self>]) -> Result<Group<Self>> {
546 POSEIDON_8.hash_to_group(input)
547 }
548
549 fn hash_to_scalar_psd2(input: &[Field<Self>]) -> Result<Scalar<Self>> {
551 POSEIDON_2.hash_to_scalar(input)
552 }
553
554 fn hash_to_scalar_psd4(input: &[Field<Self>]) -> Result<Scalar<Self>> {
556 POSEIDON_4.hash_to_scalar(input)
557 }
558
559 fn hash_to_scalar_psd8(input: &[Field<Self>]) -> Result<Scalar<Self>> {
561 POSEIDON_8.hash_to_scalar(input)
562 }
563
564 fn merkle_tree_bhp<const DEPTH: u8>(leaves: &[Vec<bool>]) -> Result<BHPMerkleTree<Self, DEPTH>> {
566 MerkleTree::new(&*BHP_1024, &*BHP_512, leaves)
567 }
568
569 fn merkle_tree_psd<const DEPTH: u8>(leaves: &[Vec<Field<Self>>]) -> Result<PoseidonMerkleTree<Self, DEPTH>> {
571 MerkleTree::new(&*POSEIDON_4, &*POSEIDON_2, leaves)
572 }
573
574 fn verify_merkle_path_bhp<const DEPTH: u8>(
576 path: &MerklePath<Self, DEPTH>,
577 root: &Field<Self>,
578 leaf: &Vec<bool>,
579 ) -> bool {
580 path.verify(&*BHP_1024, &*BHP_512, root, leaf)
581 }
582
583 fn verify_merkle_path_psd<const DEPTH: u8>(
585 path: &MerklePath<Self, DEPTH>,
586 root: &Field<Self>,
587 leaf: &Vec<Field<Self>>,
588 ) -> bool {
589 path.verify(&*POSEIDON_4, &*POSEIDON_2, root, leaf)
590 }
591}
592
593#[cfg(test)]
594mod tests {
595 use super::*;
596
597 type CurrentNetwork = MainnetV0;
598
599 #[test]
600 fn test_g_scalar_multiply() {
601 let scalar = Scalar::rand(&mut TestRng::default());
603 let group = CurrentNetwork::g_scalar_multiply(&scalar);
604 assert_eq!(group, CurrentNetwork::g_powers()[0] * scalar);
605 }
606}