-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit moves the previous implementation of `SparseMerkleTree` from miden-core to this crate. It also include a couple of new tests, a bench suite, and a couple of minor fixes. The original API was preserved to maintain compatibility with `AdviceTape`. closes #21
- Loading branch information
Showing
7 changed files
with
666 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use core::mem::swap; | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
use miden_crypto::{merkle::SimpleSmt, Felt, Word}; | ||
use rand_utils::prng_array; | ||
|
||
fn smt_rpo(c: &mut Criterion) { | ||
// setup trees | ||
|
||
let mut seed = [0u8; 32]; | ||
let mut trees = vec![]; | ||
|
||
for depth in 14..=20 { | ||
let leaves = ((1 << depth) - 1) as u64; | ||
for count in [1, leaves / 2, leaves] { | ||
let entries: Vec<_> = (0..count) | ||
.map(|i| { | ||
let word = generate_word(&mut seed); | ||
(i, word) | ||
}) | ||
.collect(); | ||
let tree = SimpleSmt::new(entries, depth).unwrap(); | ||
trees.push(tree); | ||
} | ||
} | ||
|
||
let leaf = generate_word(&mut seed); | ||
|
||
// benchmarks | ||
|
||
let mut insert = c.benchmark_group(format!("smt update_leaf")); | ||
|
||
for tree in trees.iter_mut() { | ||
let depth = tree.depth(); | ||
let count = tree.leaves_count() as u64; | ||
let key = count >> 2; | ||
insert.bench_with_input( | ||
format!("simple smt(depth:{depth},count:{count})"), | ||
&(key, leaf), | ||
|b, (key, leaf)| { | ||
b.iter(|| { | ||
tree.update_leaf(black_box(*key), black_box(*leaf)).unwrap(); | ||
}); | ||
}, | ||
); | ||
} | ||
|
||
insert.finish(); | ||
|
||
let mut path = c.benchmark_group(format!("smt get_leaf_path")); | ||
|
||
for tree in trees.iter_mut() { | ||
let depth = tree.depth(); | ||
let count = tree.leaves_count() as u64; | ||
let key = count >> 2; | ||
path.bench_with_input( | ||
format!("simple smt(depth:{depth},count:{count})"), | ||
&key, | ||
|b, key| { | ||
b.iter(|| { | ||
tree.get_leaf_path(black_box(*key)).unwrap(); | ||
}); | ||
}, | ||
); | ||
} | ||
|
||
path.finish(); | ||
} | ||
|
||
criterion_group!(smt_group, smt_rpo); | ||
criterion_main!(smt_group); | ||
|
||
// HELPER FUNCTIONS | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
fn generate_word(seed: &mut [u8; 32]) -> Word { | ||
swap(seed, &mut prng_array(*seed)); | ||
let nums: [u64; 4] = prng_array(*seed); | ||
[ | ||
Felt::new(nums[0]), | ||
Felt::new(nums[1]), | ||
Felt::new(nums[2]), | ||
Felt::new(nums[3]), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.