miden-crypto/benches/smt.rs
benches/smt.rsRaw
1use core::mem::swap;
2use criterion::{black_box, criterion_group, criterion_main, Criterion};
3use miden_crypto::{merkle::SimpleSmt, Felt, Word};
4use rand_utils::prng_array;
5
6fn smt_rpo(c: &mut Criterion) {
7 // setup trees
8
9 let mut seed = [0u8; 32];
10 let mut trees = vec![];
11
12 for depth in 14..=20 {
13 let leaves = ((1 << depth) - 1) as u64;
14 for count in [1, leaves / 2, leaves] {
15 let entries: Vec<_> = (0..count)
16 .map(|i| {
17 let word = generate_word(&mut seed);
18 (i, word)
19 })
20 .collect();
21 let tree = SimpleSmt::with_leaves(depth, entries).unwrap();
22 trees.push((tree, count));
23 }
24 }
25
26 let leaf = generate_word(&mut seed);
27
28 // benchmarks
29
30 let mut insert = c.benchmark_group(format!("smt update_leaf"));
31
32 for (tree, count) in trees.iter_mut() {
33 let depth = tree.depth();
34 let key = *count >> 2;
35 insert.bench_with_input(
36 format!("simple smt(depth:{depth},count:{count})"),
37 &(key, leaf),
38 |b, (key, leaf)| {
39 b.iter(|| {
40 tree.update_leaf(black_box(*key), black_box(*leaf)).unwrap();
41 });
42 },
43 );
44 }
45
46 insert.finish();
47
48 let mut path = c.benchmark_group(format!("smt get_leaf_path"));
49
50 for (tree, count) in trees.iter_mut() {
51 let depth = tree.depth();
52 let key = *count >> 2;
53 path.bench_with_input(
54 format!("simple smt(depth:{depth},count:{count})"),
55 &key,
56 |b, key| {
57 b.iter(|| {
58 tree.get_leaf_path(black_box(*key)).unwrap();
59 });
60 },
61 );
62 }
63
64 path.finish();
65}
66
67criterion_group!(smt_group, smt_rpo);
68criterion_main!(smt_group);
69
70// HELPER FUNCTIONS
71// --------------------------------------------------------------------------------------------
72
73fn generate_word(seed: &mut [u8; 32]) -> Word {
74 swap(seed, &mut prng_array(*seed));
75 let nums: [u64; 4] = prng_array(*seed);
76 [Felt::new(nums[0]), Felt::new(nums[1]), Felt::new(nums[2]), Felt::new(nums[3])]
77}