-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] implement subtree-based SMT computations #341
Open
Qyriad
wants to merge
16
commits into
0xPolygonMiden:next
Choose a base branch
from
reilabs:qyriad/parallel-construction
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b585f9c
merkle: add parent() helper function on NodeIndex
Qyriad ae772d2
smt: add pairs_to_leaf() to trait
Qyriad 8b10465
smt: add sorted_pairs_to_leaves() and test for it
Qyriad 16456aa
smt: implement single subtree-8 hashing, w/ benchmarks & tests
Qyriad 1863dab
merkle: add a benchmark for constructing 256-balanced trees
Qyriad cd1dc7c
smt: test that SparseMerkleTree::build_subtree() is composable
Qyriad 475c826
smt: test that subtree logic can correctly construct an entire tree
Qyriad 5b9480a
smt: implement test for basic parallelized subtree computation w/ rayon
Qyriad 38422f5
smt: add from_raw_parts() to trait interface
Qyriad cc144a6
smt: add parallel constructors to Smt and SimpleSmt
Qyriad ec2dfdf
smt: add benchmarks for parallel construction
Qyriad 3f52ef3
add news item for smt parallel subtree construction
Qyriad c9b4682
refactor: integrate parallel implementations
6d93c0d
remove concurrent `SimpleSmt::with_leaves`
krushimir 5cdd3fc
refactor: `build_subtree`
krushimir 3cbfbaf
chore: address review comments
krushimir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,66 @@ | ||
//! Benchmark for building a [`miden_crypto::merkle::MerkleTree`]. This is intended to be compared | ||
//! with the results from `benches/smt-subtree.rs`, as building a fully balanced Merkle tree with | ||
//! 256 leaves should indicate the *absolute best* performance we could *possibly* get for building | ||
//! a depth-8 sparse Merkle subtree, though practically speaking building a fully balanced Merkle | ||
//! tree will perform better than the sparse version. At the time of this writing (2024/11/24), this | ||
//! benchmark is about four times more efficient than the equivalent benchmark in | ||
//! `benches/smt-subtree.rs`. | ||
use std::{hint, mem, time::Duration}; | ||
|
||
use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; | ||
use miden_crypto::{merkle::MerkleTree, Felt, Word, ONE}; | ||
use rand_utils::prng_array; | ||
|
||
fn balanced_merkle_even(c: &mut Criterion) { | ||
c.bench_function("balanced-merkle-even", |b| { | ||
b.iter_batched( | ||
|| { | ||
let entries: Vec<Word> = | ||
(0..256).map(|i| [Felt::new(i), ONE, ONE, Felt::new(i)]).collect(); | ||
assert_eq!(entries.len(), 256); | ||
entries | ||
}, | ||
|leaves| { | ||
let tree = MerkleTree::new(hint::black_box(leaves)).unwrap(); | ||
assert_eq!(tree.depth(), 8); | ||
}, | ||
BatchSize::SmallInput, | ||
); | ||
}); | ||
} | ||
|
||
fn balanced_merkle_rand(c: &mut Criterion) { | ||
let mut seed = [0u8; 32]; | ||
c.bench_function("balanced-merkle-rand", |b| { | ||
b.iter_batched( | ||
|| { | ||
let entries: Vec<Word> = (0..256).map(|_| generate_word(&mut seed)).collect(); | ||
assert_eq!(entries.len(), 256); | ||
entries | ||
}, | ||
|leaves| { | ||
let tree = MerkleTree::new(hint::black_box(leaves)).unwrap(); | ||
assert_eq!(tree.depth(), 8); | ||
}, | ||
BatchSize::SmallInput, | ||
); | ||
}); | ||
} | ||
|
||
criterion_group! { | ||
name = smt_subtree_group; | ||
config = Criterion::default() | ||
.measurement_time(Duration::from_secs(20)) | ||
.configure_from_args(); | ||
targets = balanced_merkle_even, balanced_merkle_rand | ||
} | ||
criterion_main!(smt_subtree_group); | ||
|
||
// HELPER FUNCTIONS | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
fn generate_word(seed: &mut [u8; 32]) -> Word { | ||
mem::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::{fmt::Debug, hint, mem, time::Duration}; | ||
|
||
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; | ||
use miden_crypto::{hash::rpo::RpoDigest, merkle::Smt, Felt, Word, ONE}; | ||
use rand_utils::prng_array; | ||
use winter_utils::Randomizable; | ||
|
||
// 2^0, 2^4, 2^8, 2^12, 2^16 | ||
const PAIR_COUNTS: [u64; 6] = [1, 16, 256, 4096, 65536, 1_048_576]; | ||
|
||
fn smt_parallel_subtree(c: &mut Criterion) { | ||
let mut seed = [0u8; 32]; | ||
|
||
let mut group = c.benchmark_group("parallel-subtrees"); | ||
bobbinth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for pair_count in PAIR_COUNTS { | ||
let bench_id = BenchmarkId::from_parameter(pair_count); | ||
group.bench_with_input(bench_id, &pair_count, |b, &pair_count| { | ||
b.iter_batched( | ||
|| { | ||
// Setup. | ||
let entries: Vec<(RpoDigest, Word)> = (0..pair_count) | ||
.map(|i| { | ||
let count = pair_count as f64; | ||
let idx = ((i as f64 / count) * (count)) as u64; | ||
let key = RpoDigest::new([ | ||
generate_value(&mut seed), | ||
ONE, | ||
Felt::new(i), | ||
Felt::new(idx), | ||
]); | ||
let value = generate_word(&mut seed); | ||
(key, value) | ||
}) | ||
.collect(); | ||
bobbinth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let control = Smt::with_entries(entries.clone()).unwrap(); | ||
(entries, control) | ||
}, | ||
|(entries, control)| { | ||
// Benchmarked function. | ||
let tree = Smt::with_entries_par(hint::black_box(entries)).unwrap(); | ||
assert_eq!(tree.root(), control.root()); | ||
}, | ||
BatchSize::SmallInput, | ||
); | ||
}); | ||
} | ||
} | ||
|
||
criterion_group! { | ||
name = smt_subtree_group; | ||
config = Criterion::default() | ||
//.measurement_time(Duration::from_secs(960)) | ||
.measurement_time(Duration::from_secs(60)) | ||
.sample_size(10) | ||
.configure_from_args(); | ||
targets = smt_parallel_subtree | ||
} | ||
criterion_main!(smt_subtree_group); | ||
|
||
// HELPER FUNCTIONS | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
fn generate_value<T: Copy + Debug + Randomizable>(seed: &mut [u8; 32]) -> T { | ||
mem::swap(seed, &mut prng_array(*seed)); | ||
let value: [T; 1] = rand_utils::prng_array(*seed); | ||
value[0] | ||
} | ||
|
||
fn generate_word(seed: &mut [u8; 32]) -> Word { | ||
mem::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
use std::{fmt::Debug, hint, mem, time::Duration}; | ||
|
||
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; | ||
use miden_crypto::{ | ||
hash::rpo::RpoDigest, | ||
merkle::{NodeIndex, Smt, SmtLeaf, SubtreeLeaf, SMT_DEPTH}, | ||
Felt, Word, ONE, | ||
}; | ||
use rand_utils::prng_array; | ||
use winter_utils::Randomizable; | ||
|
||
const PAIR_COUNTS: [u64; 5] = [1, 64, 128, 192, 256]; | ||
|
||
fn smt_subtree_even(c: &mut Criterion) { | ||
let mut seed = [0u8; 32]; | ||
|
||
let mut group = c.benchmark_group("subtree8-even"); | ||
|
||
for pair_count in PAIR_COUNTS { | ||
let bench_id = BenchmarkId::from_parameter(pair_count); | ||
group.bench_with_input(bench_id, &pair_count, |b, &pair_count| { | ||
b.iter_batched( | ||
|| { | ||
// Setup. | ||
let entries: Vec<(RpoDigest, Word)> = (0..pair_count) | ||
.map(|n| { | ||
// A single depth-8 subtree can have a maximum of 255 leaves. | ||
let leaf_index = ((n as f64 / pair_count as f64) * 255.0) as u64; | ||
let key = RpoDigest::new([ | ||
generate_value(&mut seed), | ||
ONE, | ||
Felt::new(n), | ||
Felt::new(leaf_index), | ||
]); | ||
let value = generate_word(&mut seed); | ||
(key, value) | ||
}) | ||
.collect(); | ||
|
||
let mut leaves: Vec<_> = entries | ||
.iter() | ||
.map(|(key, value)| { | ||
let leaf = SmtLeaf::new_single(*key, *value); | ||
let col = NodeIndex::from(leaf.index()).value(); | ||
let hash = leaf.hash(); | ||
SubtreeLeaf { col, hash } | ||
}) | ||
.collect(); | ||
leaves.sort(); | ||
leaves.dedup_by_key(|leaf| leaf.col); | ||
leaves | ||
}, | ||
|leaves| { | ||
// Benchmarked function. | ||
let (subtree, _) = | ||
Smt::build_subtree(hint::black_box(leaves), hint::black_box(SMT_DEPTH)); | ||
assert!(!subtree.is_empty()); | ||
}, | ||
BatchSize::SmallInput, | ||
); | ||
}); | ||
} | ||
} | ||
|
||
fn smt_subtree_random(c: &mut Criterion) { | ||
let mut seed = [0u8; 32]; | ||
|
||
let mut group = c.benchmark_group("subtree8-rand"); | ||
|
||
for pair_count in PAIR_COUNTS { | ||
let bench_id = BenchmarkId::from_parameter(pair_count); | ||
group.bench_with_input(bench_id, &pair_count, |b, &pair_count| { | ||
b.iter_batched( | ||
|| { | ||
// Setup. | ||
let entries: Vec<(RpoDigest, Word)> = (0..pair_count) | ||
.map(|i| { | ||
let leaf_index: u8 = generate_value(&mut seed); | ||
let key = RpoDigest::new([ | ||
ONE, | ||
ONE, | ||
Felt::new(i), | ||
Felt::new(leaf_index as u64), | ||
]); | ||
let value = generate_word(&mut seed); | ||
(key, value) | ||
}) | ||
.collect(); | ||
|
||
let mut leaves: Vec<_> = entries | ||
.iter() | ||
.map(|(key, value)| { | ||
let leaf = SmtLeaf::new_single(*key, *value); | ||
let col = NodeIndex::from(leaf.index()).value(); | ||
let hash = leaf.hash(); | ||
SubtreeLeaf { col, hash } | ||
}) | ||
.collect(); | ||
leaves.sort(); | ||
leaves | ||
}, | ||
|leaves| { | ||
let (subtree, _) = | ||
Smt::build_subtree(hint::black_box(leaves), hint::black_box(SMT_DEPTH)); | ||
assert!(!subtree.is_empty()); | ||
}, | ||
BatchSize::SmallInput, | ||
); | ||
}); | ||
} | ||
} | ||
|
||
criterion_group! { | ||
name = smt_subtree_group; | ||
config = Criterion::default() | ||
.measurement_time(Duration::from_secs(40)) | ||
.sample_size(60) | ||
.configure_from_args(); | ||
targets = smt_subtree_even, smt_subtree_random | ||
} | ||
criterion_main!(smt_subtree_group); | ||
|
||
// HELPER FUNCTIONS | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
fn generate_value<T: Copy + Debug + Randomizable>(seed: &mut [u8; 32]) -> T { | ||
mem::swap(seed, &mut prng_array(*seed)); | ||
let value: [T; 1] = rand_utils::prng_array(*seed); | ||
value[0] | ||
} | ||
|
||
fn generate_word(seed: &mut [u8; 32]) -> Word { | ||
mem::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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually need to impose this restriction? If we don't require
concurrent
we could run the benchmark for both concurrent and sequential modes, rgiht?