-
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
8 changed files
with
675 additions
and
11 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,89 @@ | ||
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) { | ||
// parameters | ||
|
||
const DEPTH: u32 = 16; | ||
const LEAVES: u64 = ((1 << DEPTH) - 1) as u64; | ||
const KEY: u64 = (LEAVES) >> 2; | ||
|
||
let mut seed = [0u8; 32]; | ||
|
||
// setup trees | ||
|
||
let mut trees: Vec<_> = [1, LEAVES / 2, LEAVES] | ||
.into_iter() | ||
.scan([0u8; 32], |seed, count| { | ||
let tree = create_simple_smt::<DEPTH>(count, seed); | ||
Some(tree) | ||
}) | ||
.collect(); | ||
|
||
let leaf = generate_word(&mut seed); | ||
|
||
// benchmarks | ||
|
||
let mut insert = c.benchmark_group(format!("smt update_leaf(depth{DEPTH})")); | ||
|
||
for tree in trees.iter_mut() { | ||
let count = tree.leaves_count() as u64; | ||
insert.bench_with_input( | ||
format!("simple smt({count})"), | ||
&(KEY % count.max(1), 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(depth{DEPTH})")); | ||
|
||
for tree in trees.iter_mut() { | ||
let count = tree.leaves_count() as u64; | ||
path.bench_with_input( | ||
format!("simple smt({count})"), | ||
&(KEY % count.max(1)), | ||
|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]), | ||
] | ||
} | ||
|
||
fn create_simple_smt<const DEPTH: u32>(count: u64, seed: &mut [u8; 32]) -> SimpleSmt { | ||
let entries: Vec<_> = (0..count) | ||
.map(|i| { | ||
let word = generate_word(seed); | ||
(i, word) | ||
}) | ||
.collect(); | ||
SimpleSmt::new(entries, DEPTH).unwrap() | ||
} |
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
use super::{Felt, FieldElement, StarkField, ONE, ZERO}; | ||
use winter_crypto::{Digest, ElementHasher, Hasher}; | ||
use super::{Digest, ElementHasher, Felt, FieldElement, Hasher, StarkField, ONE, ZERO}; | ||
|
||
pub mod blake; | ||
pub mod rpo; |
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.