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 pub static ref ENCRYPTION_DOMAIN: Field<MainnetV0> = Field::<MainnetV0>::new_domain_separator("AleoSymmetricEncryption0");
45 pub static ref GRAPH_KEY_DOMAIN: Field<MainnetV0> = Field::<MainnetV0>::new_domain_separator("AleoGraphKey0");
47 pub static ref SERIAL_NUMBER_DOMAIN: Field<MainnetV0> = Field::<MainnetV0>::new_domain_separator("AleoSerialNumber0");
49
50 pub static ref BHP_256: BHP256<MainnetV0> = BHP256::<MainnetV0>::setup("AleoBHP256").expect("Failed to setup BHP256");
52 pub static ref BHP_512: BHP512<MainnetV0> = BHP512::<MainnetV0>::setup("AleoBHP512").expect("Failed to setup BHP512");
54 pub static ref BHP_768: BHP768<MainnetV0> = BHP768::<MainnetV0>::setup("AleoBHP768").expect("Failed to setup BHP768");
56 pub static ref BHP_1024: BHP1024<MainnetV0> = BHP1024::<MainnetV0>::setup("AleoBHP1024").expect("Failed to setup BHP1024");
58
59 pub static ref PEDERSEN_64: Pedersen64<MainnetV0> = Pedersen64::<MainnetV0>::setup("AleoPedersen64");
61 pub static ref PEDERSEN_128: Pedersen128<MainnetV0> = Pedersen128::<MainnetV0>::setup("AleoPedersen128");
63
64 pub static ref POSEIDON_2: Poseidon2<MainnetV0> = Poseidon2::<MainnetV0>::setup("AleoPoseidon2").expect("Failed to setup Poseidon2");
66 pub static ref POSEIDON_4: Poseidon4<MainnetV0> = Poseidon4::<MainnetV0>::setup("AleoPoseidon4").expect("Failed to setup Poseidon4");
68 pub static ref POSEIDON_8: Poseidon8<MainnetV0> = Poseidon8::<MainnetV0>::setup("AleoPoseidon8").expect("Failed to setup Poseidon8");
70
71 pub static ref CREDITS_PROVING_KEYS: IndexMap<String, Arc<VarunaProvingKey<Console>>> = {
72 let mut map = IndexMap::new();
73 snarkvm_parameters::insert_credit_keys!(map, VarunaProvingKey<Console>, Prover);
74 map
75 };
76 pub static ref CREDITS_VERIFYING_KEYS: IndexMap<String, Arc<VarunaVerifyingKey<Console>>> = {
77 let mut map = IndexMap::new();
78 snarkvm_parameters::insert_credit_keys!(map, VarunaVerifyingKey<Console>, Verifier);
79 map
80 };
81}
82
83pub const TRANSACTION_PREFIX: &str = "at";
84
85#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
86pub struct MainnetV0;
87
88impl MainnetV0 {
89 fn new_bases(message: &str) -> Vec<Group<Self>> {
91 let (base, _, _) = Blake2Xs::hash_to_curve::<<Self as Environment>::Affine>(message);
93
94 let mut g = Group::<Self>::new(base);
96 let mut g_bases = Vec::with_capacity(Scalar::<Self>::size_in_bits());
97 for _ in 0..Scalar::<Self>::size_in_bits() {
98 g_bases.push(g);
99 g = g.double();
100 }
101 g_bases
102 }
103}
104
105impl Environment for MainnetV0 {
106 type Affine = <Console as Environment>::Affine;
107 type BigInteger = <Console as Environment>::BigInteger;
108 type Field = <Console as Environment>::Field;
109 type PairingCurve = <Console as Environment>::PairingCurve;
110 type Projective = <Console as Environment>::Projective;
111 type Scalar = <Console as Environment>::Scalar;
112
113 const EDWARDS_A: Self::Field = Console::EDWARDS_A;
115 const EDWARDS_D: Self::Field = Console::EDWARDS_D;
117 const MONTGOMERY_A: Self::Field = Console::MONTGOMERY_A;
119 const MONTGOMERY_B: Self::Field = Console::MONTGOMERY_B;
121}
122
123impl Network for MainnetV0 {
124 type BlockHash = AleoID<Field<Self>, { hrp2!("ab") }>;
126 type RatificationID = AleoID<Field<Self>, { hrp2!("ar") }>;
128 type StateRoot = AleoID<Field<Self>, { hrp2!("sr") }>;
130 type TransactionID = AleoID<Field<Self>, { hrp2!(TRANSACTION_PREFIX) }>;
132 type TransitionID = AleoID<Field<Self>, { hrp2!("au") }>;
134 type TransmissionChecksum = u128;
136
137 #[cfg(not(any(test, feature = "test")))]
140 const CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); 7] = [
141 (ConsensusVersion::V1, 0),
142 (ConsensusVersion::V2, 2_800_000),
143 (ConsensusVersion::V3, 4_900_000),
144 (ConsensusVersion::V4, 6_135_000),
145 (ConsensusVersion::V5, 7_060_000),
146 (ConsensusVersion::V6, 7_560_000),
147 (ConsensusVersion::V7, 7_570_000),
148 ];
149 #[cfg(any(test, feature = "test"))]
152 const CONSENSUS_VERSION_HEIGHTS: [(ConsensusVersion, u32); 7] = [
153 (ConsensusVersion::V1, 0),
154 (ConsensusVersion::V2, 10),
155 (ConsensusVersion::V3, 11),
156 (ConsensusVersion::V4, 12),
157 (ConsensusVersion::V5, 13),
158 (ConsensusVersion::V6, 14),
159 (ConsensusVersion::V7, 15),
160 ];
161 const EDITION: u16 = 0;
163 #[cfg(not(feature = "test"))]
165 const GENESIS_COINBASE_TARGET: u64 = (1u64 << 29).saturating_sub(1);
166 #[cfg(feature = "test")]
169 const GENESIS_COINBASE_TARGET: u64 = (1u64 << 5).saturating_sub(1);
170 #[cfg(not(feature = "test"))]
172 const GENESIS_PROOF_TARGET: u64 = 1u64 << 27;
173 #[cfg(feature = "test")]
176 const GENESIS_PROOF_TARGET: u64 = 1u64 << 3;
177 const GENESIS_TIMESTAMP: i64 = 1725462000 ;
179 const ID: u16 = 0;
181 const INCLUSION_FUNCTION_NAME: &'static str = snarkvm_parameters::mainnet::NETWORK_INCLUSION_FUNCTION_NAME;
183 #[cfg(not(any(test, feature = "test")))]
185 const MAX_CERTIFICATES: [(ConsensusVersion, u16); 4] = [
186 (ConsensusVersion::V1, 16),
187 (ConsensusVersion::V3, 25),
188 (ConsensusVersion::V5, 30),
189 (ConsensusVersion::V6, 35),
190 ];
191 #[cfg(any(test, feature = "test"))]
193 const MAX_CERTIFICATES: [(ConsensusVersion, u16); 4] = [
194 (ConsensusVersion::V1, 100),
195 (ConsensusVersion::V3, 100),
196 (ConsensusVersion::V5, 100),
197 (ConsensusVersion::V6, 100),
198 ];
199 const NAME: &'static str = "Aleo Mainnet (v0)";
201
202 fn genesis_bytes() -> &'static [u8] {
204 snarkvm_parameters::mainnet::GenesisBytes::load_bytes()
205 }
206
207 fn restrictions_list_as_str() -> &'static str {
209 snarkvm_parameters::mainnet::RESTRICTIONS_LIST
210 }
211
212 fn get_credits_proving_key(function_name: String) -> Result<&'static Arc<VarunaProvingKey<Self>>> {
214 CREDITS_PROVING_KEYS
215 .get(&function_name)
216 .ok_or_else(|| anyhow!("Proving key for credits.aleo/{function_name}' not found"))
217 }
218
219 fn get_credits_verifying_key(function_name: String) -> Result<&'static Arc<VarunaVerifyingKey<Self>>> {
221 CREDITS_VERIFYING_KEYS
222 .get(&function_name)
223 .ok_or_else(|| anyhow!("Verifying key for credits.aleo/{function_name}' not found"))
224 }
225
226 fn inclusion_proving_key() -> &'static Arc<VarunaProvingKey<Self>> {
228 static INSTANCE: OnceCell<Arc<VarunaProvingKey<Console>>> = OnceCell::new();
229 INSTANCE.get_or_init(|| {
230 Arc::new(
232 CircuitProvingKey::from_bytes_le(&snarkvm_parameters::mainnet::INCLUSION_PROVING_KEY[1..])
233 .expect("Failed to load inclusion proving key."),
234 )
235 })
236 }
237
238 fn inclusion_verifying_key() -> &'static Arc<VarunaVerifyingKey<Self>> {
240 static INSTANCE: OnceCell<Arc<VarunaVerifyingKey<Console>>> = OnceCell::new();
241 INSTANCE.get_or_init(|| {
242 Arc::new(
244 CircuitVerifyingKey::from_bytes_le(&snarkvm_parameters::mainnet::INCLUSION_VERIFYING_KEY[1..])
245 .expect("Failed to load inclusion verifying key."),
246 )
247 })
248 }
249
250 fn g_powers() -> &'static Vec<Group<Self>> {
252 &GENERATOR_G
253 }
254
255 fn g_scalar_multiply(scalar: &Scalar<Self>) -> Group<Self> {
257 GENERATOR_G
258 .iter()
259 .zip_eq(&scalar.to_bits_le())
260 .filter_map(|(base, bit)| match bit {
261 true => Some(base),
262 false => None,
263 })
264 .sum()
265 }
266
267 fn varuna_universal_prover() -> &'static UniversalProver<Self::PairingCurve> {
269 static INSTANCE: OnceCell<UniversalProver<<Console as Environment>::PairingCurve>> = OnceCell::new();
270 INSTANCE.get_or_init(|| {
271 snarkvm_algorithms::polycommit::kzg10::UniversalParams::load()
272 .expect("Failed to load universal SRS (KZG10).")
273 .to_universal_prover()
274 .expect("Failed to convert universal SRS (KZG10) to the prover.")
275 })
276 }
277
278 fn varuna_universal_verifier() -> &'static UniversalVerifier<Self::PairingCurve> {
280 static INSTANCE: OnceCell<UniversalVerifier<<Console as Environment>::PairingCurve>> = OnceCell::new();
281 INSTANCE.get_or_init(|| {
282 snarkvm_algorithms::polycommit::kzg10::UniversalParams::load()
283 .expect("Failed to load universal SRS (KZG10).")
284 .to_universal_verifier()
285 .expect("Failed to convert universal SRS (KZG10) to the verifier.")
286 })
287 }
288
289 fn varuna_fs_parameters() -> &'static FiatShamirParameters<Self> {
291 &VARUNA_FS_PARAMETERS
292 }
293
294 fn encryption_domain() -> Field<Self> {
296 *ENCRYPTION_DOMAIN
297 }
298
299 fn graph_key_domain() -> Field<Self> {
301 *GRAPH_KEY_DOMAIN
302 }
303
304 fn serial_number_domain() -> Field<Self> {
306 *SERIAL_NUMBER_DOMAIN
307 }
308
309 fn commit_bhp256(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
311 BHP_256.commit(input, randomizer)
312 }
313
314 fn commit_bhp512(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
316 BHP_512.commit(input, randomizer)
317 }
318
319 fn commit_bhp768(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
321 BHP_768.commit(input, randomizer)
322 }
323
324 fn commit_bhp1024(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
326 BHP_1024.commit(input, randomizer)
327 }
328
329 fn commit_ped64(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
331 PEDERSEN_64.commit(input, randomizer)
332 }
333
334 fn commit_ped128(input: &[bool], randomizer: &Scalar<Self>) -> Result<Field<Self>> {
336 PEDERSEN_128.commit(input, randomizer)
337 }
338
339 fn commit_to_group_bhp256(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
341 BHP_256.commit_uncompressed(input, randomizer)
342 }
343
344 fn commit_to_group_bhp512(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
346 BHP_512.commit_uncompressed(input, randomizer)
347 }
348
349 fn commit_to_group_bhp768(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
351 BHP_768.commit_uncompressed(input, randomizer)
352 }
353
354 fn commit_to_group_bhp1024(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
356 BHP_1024.commit_uncompressed(input, randomizer)
357 }
358
359 fn commit_to_group_ped64(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
361 PEDERSEN_64.commit_uncompressed(input, randomizer)
362 }
363
364 fn commit_to_group_ped128(input: &[bool], randomizer: &Scalar<Self>) -> Result<Group<Self>> {
366 PEDERSEN_128.commit_uncompressed(input, randomizer)
367 }
368
369 fn hash_bhp256(input: &[bool]) -> Result<Field<Self>> {
371 BHP_256.hash(input)
372 }
373
374 fn hash_bhp512(input: &[bool]) -> Result<Field<Self>> {
376 BHP_512.hash(input)
377 }
378
379 fn hash_bhp768(input: &[bool]) -> Result<Field<Self>> {
381 BHP_768.hash(input)
382 }
383
384 fn hash_bhp1024(input: &[bool]) -> Result<Field<Self>> {
386 BHP_1024.hash(input)
387 }
388
389 fn hash_keccak256(input: &[bool]) -> Result<Vec<bool>> {
391 Keccak256::default().hash(input)
392 }
393
394 fn hash_keccak384(input: &[bool]) -> Result<Vec<bool>> {
396 Keccak384::default().hash(input)
397 }
398
399 fn hash_keccak512(input: &[bool]) -> Result<Vec<bool>> {
401 Keccak512::default().hash(input)
402 }
403
404 fn hash_ped64(input: &[bool]) -> Result<Field<Self>> {
406 PEDERSEN_64.hash(input)
407 }
408
409 fn hash_ped128(input: &[bool]) -> Result<Field<Self>> {
411 PEDERSEN_128.hash(input)
412 }
413
414 fn hash_psd2(input: &[Field<Self>]) -> Result<Field<Self>> {
416 POSEIDON_2.hash(input)
417 }
418
419 fn hash_psd4(input: &[Field<Self>]) -> Result<Field<Self>> {
421 POSEIDON_4.hash(input)
422 }
423
424 fn hash_psd8(input: &[Field<Self>]) -> Result<Field<Self>> {
426 POSEIDON_8.hash(input)
427 }
428
429 fn hash_sha3_256(input: &[bool]) -> Result<Vec<bool>> {
431 Sha3_256::default().hash(input)
432 }
433
434 fn hash_sha3_384(input: &[bool]) -> Result<Vec<bool>> {
436 Sha3_384::default().hash(input)
437 }
438
439 fn hash_sha3_512(input: &[bool]) -> Result<Vec<bool>> {
441 Sha3_512::default().hash(input)
442 }
443
444 fn hash_many_psd2(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
446 POSEIDON_2.hash_many(input, num_outputs)
447 }
448
449 fn hash_many_psd4(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
451 POSEIDON_4.hash_many(input, num_outputs)
452 }
453
454 fn hash_many_psd8(input: &[Field<Self>], num_outputs: u16) -> Vec<Field<Self>> {
456 POSEIDON_8.hash_many(input, num_outputs)
457 }
458
459 fn hash_to_group_bhp256(input: &[bool]) -> Result<Group<Self>> {
461 BHP_256.hash_uncompressed(input)
462 }
463
464 fn hash_to_group_bhp512(input: &[bool]) -> Result<Group<Self>> {
466 BHP_512.hash_uncompressed(input)
467 }
468
469 fn hash_to_group_bhp768(input: &[bool]) -> Result<Group<Self>> {
471 BHP_768.hash_uncompressed(input)
472 }
473
474 fn hash_to_group_bhp1024(input: &[bool]) -> Result<Group<Self>> {
476 BHP_1024.hash_uncompressed(input)
477 }
478
479 fn hash_to_group_ped64(input: &[bool]) -> Result<Group<Self>> {
481 PEDERSEN_64.hash_uncompressed(input)
482 }
483
484 fn hash_to_group_ped128(input: &[bool]) -> Result<Group<Self>> {
486 PEDERSEN_128.hash_uncompressed(input)
487 }
488
489 fn hash_to_group_psd2(input: &[Field<Self>]) -> Result<Group<Self>> {
491 POSEIDON_2.hash_to_group(input)
492 }
493
494 fn hash_to_group_psd4(input: &[Field<Self>]) -> Result<Group<Self>> {
496 POSEIDON_4.hash_to_group(input)
497 }
498
499 fn hash_to_group_psd8(input: &[Field<Self>]) -> Result<Group<Self>> {
501 POSEIDON_8.hash_to_group(input)
502 }
503
504 fn hash_to_scalar_psd2(input: &[Field<Self>]) -> Result<Scalar<Self>> {
506 POSEIDON_2.hash_to_scalar(input)
507 }
508
509 fn hash_to_scalar_psd4(input: &[Field<Self>]) -> Result<Scalar<Self>> {
511 POSEIDON_4.hash_to_scalar(input)
512 }
513
514 fn hash_to_scalar_psd8(input: &[Field<Self>]) -> Result<Scalar<Self>> {
516 POSEIDON_8.hash_to_scalar(input)
517 }
518
519 fn merkle_tree_bhp<const DEPTH: u8>(leaves: &[Vec<bool>]) -> Result<BHPMerkleTree<Self, DEPTH>> {
521 MerkleTree::new(&*BHP_1024, &*BHP_512, leaves)
522 }
523
524 fn merkle_tree_psd<const DEPTH: u8>(leaves: &[Vec<Field<Self>>]) -> Result<PoseidonMerkleTree<Self, DEPTH>> {
526 MerkleTree::new(&*POSEIDON_4, &*POSEIDON_2, leaves)
527 }
528
529 fn verify_merkle_path_bhp<const DEPTH: u8>(
531 path: &MerklePath<Self, DEPTH>,
532 root: &Field<Self>,
533 leaf: &Vec<bool>,
534 ) -> bool {
535 path.verify(&*BHP_1024, &*BHP_512, root, leaf)
536 }
537
538 fn verify_merkle_path_psd<const DEPTH: u8>(
540 path: &MerklePath<Self, DEPTH>,
541 root: &Field<Self>,
542 leaf: &Vec<Field<Self>>,
543 ) -> bool {
544 path.verify(&*POSEIDON_4, &*POSEIDON_2, root, leaf)
545 }
546}
547
548#[cfg(test)]
549mod tests {
550 use super::*;
551
552 type CurrentNetwork = MainnetV0;
553
554 #[test]
555 fn test_g_scalar_multiply() {
556 let scalar = Scalar::rand(&mut TestRng::default());
558 let group = CurrentNetwork::g_scalar_multiply(&scalar);
559 assert_eq!(group, CurrentNetwork::g_powers()[0] * scalar);
560 }
561}