Skip to content

Commit 92a2ae3

Browse files
committed
Improve setup of tree
1 parent 62a1e09 commit 92a2ae3

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

synthesizer/benches/kary_merkle_tree.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ extern crate criterion;
1818

1919
use snarkvm_algorithms::snark::varuna::VarunaVersion;
2020
use snarkvm_circuit::{AleoV0, Eject, Environment, Inject, Mode, collections::kary_merkle_tree::*};
21-
#[allow(unused_imports)]
22-
use snarkvm_console::network::prelude::ToBits;
2321
use snarkvm_console::{
2422
algorithms::Poseidon8,
2523
collections::kary_merkle_tree::KaryMerkleTree,
@@ -41,13 +39,27 @@ type NativeLeafHasher = Poseidon8<CurrentNetwork>;
4139
type CircuitPathHasher = snarkvm_circuit::Poseidon8<AleoV0>;
4240
type CircuitLeafHasher = snarkvm_circuit::Poseidon8<AleoV0>;
4341

44-
const DEPTH: u8 = 20;
45-
const ARITY: u8 = 2;
42+
const DEPTH: u8 = 7;
43+
const ARITY: u8 = 8;
4644

4745
/// Generates the specified number of random Merkle tree leaves.
4846
macro_rules! generate_leaves {
49-
("bits", $num_leaves:expr, $rng:expr) => {{ (0..$num_leaves).map(|_| Field::<MainnetV0>::rand($rng).to_bits_le()).collect::<Vec<_>>() }};
50-
("fields", $num_leaves:expr, $rng:expr) => {{ (0..$num_leaves).map(|_| vec![Field::<MainnetV0>::rand($rng)]).collect::<Vec<_>>() }};
47+
("bits", $num_leaves:expr, $rng:expr) => {{
48+
use snarkvm_console::network::prelude::ToBits;
49+
// Generate leaf bits.
50+
(0..$num_leaves).map(|_| Field::<CurrentNetwork>::rand($rng).to_bits_le()).collect::<Vec<_>>()
51+
}};
52+
("fields", $num_leaves:expr, $rng:expr) => {{
53+
use rand::SeedableRng;
54+
use rayon::prelude::*;
55+
// Generate leaf fields in parallel.
56+
(0..$num_leaves)
57+
.map(|_| u64::rand($rng))
58+
.collect::<Vec<u64>>()
59+
.into_par_iter()
60+
.map(|seed| vec![Field::<CurrentNetwork>::rand(&mut rand::rngs::StdRng::seed_from_u64(seed))])
61+
.collect::<Vec<_>>()
62+
}};
5163
}
5264

5365
fn batch_prove(c: &mut Criterion) {
@@ -63,15 +75,15 @@ fn batch_prove(c: &mut Criterion) {
6375
let circuit_leaf_hasher = CircuitLeafHasher::new(Mode::Private, native_leaf_hasher.clone());
6476

6577
// Determine the maximum number of leaves.
66-
let max_num_leaves = (ARITY as u32).pow(DEPTH as u32);
78+
let max_num_leaves = (ARITY as u64).pow(DEPTH as u32);
6779
// Initialize the leaves.
6880
let leaves = generate_leaves!("fields", max_num_leaves, &mut rng);
6981
// Initialize the tree.
7082
let merkle_tree =
7183
KaryMerkleTree::<_, _, DEPTH, ARITY>::new(&native_leaf_hasher, &native_path_hasher, &leaves).unwrap();
7284

7385
// Log the current time elapsed.
74-
println!(" • Synthesized the Merkle tree in: {} ms", timer.elapsed().as_millis());
86+
println!(" • Synthesized the Merkle tree in: {} secs", timer.elapsed().as_secs());
7587
let timer = std::time::Instant::now();
7688

7789
// Construct the assignment closure.
@@ -100,11 +112,6 @@ fn batch_prove(c: &mut Criterion) {
100112
let candidate = path.verify(&circuit_leaf_hasher, &circuit_path_hasher, &root, &leaf);
101113
assert!(candidate.eject_value());
102114

103-
// Uncomment me to enable logging.
104-
// println!("\t• Number of public & private variables: {:?}", (CurrentAleo::num_public(), CurrentAleo::num_private()));
105-
// println!("\t• Number of constraints: {}", CurrentAleo::num_constraints());
106-
// println!("\t• Number of nonzeros: {:?}", CurrentAleo::num_nonzeros());
107-
108115
// Eject the assignment.
109116
CurrentAleo::eject_assignment_and_reset()
110117
};
@@ -123,6 +130,9 @@ fn batch_prove(c: &mut Criterion) {
123130

124131
// Log the current time elapsed.
125132
println!(" • Generated the proving key in: {} ms", timer.elapsed().as_millis());
133+
println!("\t• Number of public & private variables: {:?}", (assignment.num_public(), assignment.num_private()));
134+
println!("\t• Number of constraints: {}", assignment.num_constraints());
135+
println!("\t• Number of nonzeros: {:?}", assignment.num_nonzeros());
126136

127137
// Bench the proof construction.
128138
for num_assignments in &[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] {

0 commit comments

Comments
 (0)