From 9cdf160bb07d18d2c6e3566166eb2ebe7291be1b Mon Sep 17 00:00:00 2001 From: Qyriad Date: Wed, 13 Nov 2024 15:32:48 -0700 Subject: [PATCH 1/3] smt: add sorted_pairs_to_leaves() and test for it --- src/merkle/smt/mod.rs | 245 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) diff --git a/src/merkle/smt/mod.rs b/src/merkle/smt/mod.rs index 03d9d45..6e4bd2e 100644 --- a/src/merkle/smt/mod.rs +++ b/src/merkle/smt/mod.rs @@ -1,4 +1,7 @@ use alloc::{collections::BTreeMap, vec::Vec}; +use core::mem; + +use num::Integer; use super::{EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, NodeIndex}; use crate::{ @@ -346,6 +349,67 @@ pub(crate) trait SparseMerkleTree { /// /// The length `path` is guaranteed to be equal to `DEPTH` fn path_and_leaf_to_opening(path: MerklePath, leaf: Self::Leaf) -> Self::Opening; + + /// Performs the initial transforms for constructing a [`SparseMerkleTree`] by composing + /// subtrees. In other words, this function takes the key-value inputs to the tree, and produces + /// the inputs to feed into [`SparseMerkleTree::build_subtree()`]. + /// + /// `pairs` *must* already be sorted **by leaf index column**, not simply sorted by key. If + /// `pairs` is not correctly sorted, the returned computations will be incorrect. + /// + /// # Panics + /// With debug assertions on, this function panics if it detects that `pairs` is not correctly + /// sorted. Without debug assertions, the returned computations will be incorrect. + fn sorted_pairs_to_leaves( + pairs: Vec<(Self::Key, Self::Value)>, + ) -> PairComputations { + debug_assert!(pairs.is_sorted_by_key(|(key, _)| Self::key_to_leaf_index(key).value())); + + let mut accumulator: PairComputations = Default::default(); + let mut accumulated_leaves: Vec = Default::default(); + + // As we iterate, we'll keep track of the kv-pairs we've seen so far that correspond to a + // single leaf. When we see a pair that's in a different leaf, we'll swap these pairs + // out and store them in our accumulated leaves. + let mut current_leaf_buffer: Vec<(Self::Key, Self::Value)> = Default::default(); + + let mut iter = pairs.into_iter().peekable(); + while let Some((key, value)) = iter.next() { + let col = Self::key_to_leaf_index(&key).index.value(); + let peeked_col = iter.peek().map(|(key, _v)| { + let index = Self::key_to_leaf_index(key); + let next_col = index.index.value(); + // We panic if `pairs` is not sorted by column. + debug_assert!(next_col >= col); + next_col + }); + current_leaf_buffer.push((key, value)); + + // If the next pair is the same column as this one, then we're done after adding this + // pair to the buffer. + if peeked_col == Some(col) { + continue; + } + + // Otherwise, the next pair is a different column, or there is no next pair. Either way + // it's time to swap out our buffer. + let leaf_pairs = mem::take(&mut current_leaf_buffer); + let leaf = Self::pairs_to_leaf(leaf_pairs); + let hash = Self::hash_leaf(&leaf); + + accumulator.nodes.insert(col, leaf); + accumulated_leaves.push(SubtreeLeaf { col, hash }); + + debug_assert!(current_leaf_buffer.is_empty()); + } + + // TODO: determine is there is any notable performance difference between computing + // subtree boundaries after the fact as an iterator adapter (like this), versus computing + // subtree boundaries as we go. Either way this function is only used at the beginning of a + // parallel construction, so it should not be a critical path. + accumulator.leaves = SubtreeLeavesIter::from_leaves(&mut accumulated_leaves).collect(); + accumulator + } } // INNER NODE @@ -463,3 +527,184 @@ impl MutationSet { self.new_root } } + +// SUBTREES +// ================================================================================================ +/// A depth-8 subtree contains 256 "columns" that can possibly be occupied. +const COLS_PER_SUBTREE: u64 = u64::pow(2, 8); + +/// Helper struct for organizing the data we care about when computing Merkle subtrees. +/// +/// Note that these represet "conceptual" leaves of some subtree, not necessarily +/// [`SparseMerkleTree::Leaf`]. +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] +pub struct SubtreeLeaf { + /// The 'value' field of [`NodeIndex`]. When computing a subtree, the depth is already known. + pub col: u64, + /// The hash of the node this `SubtreeLeaf` represents. + pub hash: RpoDigest, +} + +/// Helper struct to organize the return value of [`SparseMerkleTree::sorted_pairs_to_leaves()`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct PairComputations { + /// Literal leaves to be added to the sparse Merkle tree's internal mapping. + pub nodes: BTreeMap, + /// "Conceptual" leaves that will be used for computations. + pub leaves: Vec>, +} + +// Derive requires `L` to impl Default, even though we don't actually need that. +impl Default for PairComputations { + fn default() -> Self { + Self { + nodes: Default::default(), + leaves: Default::default(), + } + } +} + +#[derive(Debug)] +struct SubtreeLeavesIter<'s> { + leaves: core::iter::Peekable>, +} +impl<'s> SubtreeLeavesIter<'s> { + fn from_leaves(leaves: &'s mut Vec) -> Self { + Self { leaves: leaves.drain(..).peekable() } + } +} +impl<'s> core::iter::Iterator for SubtreeLeavesIter<'s> { + type Item = Vec; + + /// Each `next()` collects an entire subtree. + fn next(&mut self) -> Option> { + let mut subtree: Vec = Default::default(); + + let mut last_subtree_col = 0; + + while let Some(leaf) = self.leaves.peek() { + last_subtree_col = u64::max(1, last_subtree_col); + let is_exact_multiple = Integer::is_multiple_of(&last_subtree_col, &COLS_PER_SUBTREE); + let next_subtree_col = if is_exact_multiple { + u64::next_multiple_of(last_subtree_col + 1, COLS_PER_SUBTREE) + } else { + last_subtree_col.next_multiple_of(COLS_PER_SUBTREE) + }; + + last_subtree_col = leaf.col; + if leaf.col < next_subtree_col { + subtree.push(self.leaves.next().unwrap()); + } else if subtree.is_empty() { + continue; + } else { + break; + } + } + + if subtree.is_empty() { + debug_assert!(self.leaves.peek().is_none()); + return None; + } + + Some(subtree) + } +} + +// TESTS +// ================================================================================================ +#[cfg(test)] +mod test { + use alloc::{collections::BTreeMap, vec::Vec}; + + use super::{PairComputations, SmtLeaf, SparseMerkleTree, SubtreeLeaf, SubtreeLeavesIter}; + + use crate::{hash::rpo::RpoDigest, merkle::Smt, Felt, Word, ONE}; + + fn smtleaf_to_subtree_leaf(leaf: &SmtLeaf) -> SubtreeLeaf { + SubtreeLeaf { + col: leaf.index().index.value(), + hash: leaf.hash(), + } + } + + #[test] + fn test_sorted_pairs_to_leaves() { + let entries: Vec<(RpoDigest, Word)> = vec![ + // Subtree 0. + (RpoDigest::new([ONE, ONE, ONE, Felt::new(16)]), [ONE; 4]), + (RpoDigest::new([ONE, ONE, ONE, Felt::new(17)]), [ONE; 4]), + // Leaf index collision. + (RpoDigest::new([ONE, ONE, Felt::new(10), Felt::new(20)]), [ONE; 4]), + (RpoDigest::new([ONE, ONE, Felt::new(20), Felt::new(20)]), [ONE; 4]), + // Subtree 1. Normal single leaf again. + (RpoDigest::new([ONE, ONE, ONE, Felt::new(400)]), [ONE; 4]), // Subtree boundary. + (RpoDigest::new([ONE, ONE, ONE, Felt::new(401)]), [ONE; 4]), + // Subtree 2. Another normal leaf. + (RpoDigest::new([ONE, ONE, ONE, Felt::new(1024)]), [ONE; 4]), + ]; + + let control = Smt::with_entries(entries.clone()).unwrap(); + + let control_leaves: Vec = { + let mut entries_iter = entries.iter().cloned(); + let mut next_entry = || entries_iter.next().unwrap(); + let control_leaves = vec![ + // Subtree 0. + SmtLeaf::Single(next_entry()), + SmtLeaf::Single(next_entry()), + SmtLeaf::new_multiple(vec![next_entry(), next_entry()]).unwrap(), + // Subtree 1. + SmtLeaf::Single(next_entry()), + SmtLeaf::Single(next_entry()), + // Subtree 2. + SmtLeaf::Single(next_entry()), + ]; + assert_eq!(entries_iter.next(), None); + control_leaves + }; + + let control_subtree_leaves: Vec> = { + let mut control_leaves_iter = control_leaves.iter(); + let mut next_leaf = || control_leaves_iter.next().unwrap(); + + let control_subtree_leaves: Vec> = [ + // Subtree 0. + vec![next_leaf(), next_leaf(), next_leaf()], + // Subtree 1. + vec![next_leaf(), next_leaf()], + // Subtree 2. + vec![next_leaf()], + ] + .map(|subtree| subtree.into_iter().map(smtleaf_to_subtree_leaf).collect()) + .to_vec(); + assert_eq!(control_leaves_iter.next(), None); + control_subtree_leaves + }; + + let subtrees: PairComputations = Smt::sorted_pairs_to_leaves(entries); + // This will check that the hashes, columns, and subtree assignments all match. + assert_eq!(subtrees.leaves, control_subtree_leaves); + + // Flattening and re-separating out the leaves into subtrees should have the same result. + let mut all_leaves: Vec = + subtrees.leaves.clone().into_iter().flatten().collect(); + let re_grouped: Vec> = SubtreeLeavesIter::from_leaves(&mut all_leaves).collect(); + assert_eq!(subtrees.leaves, re_grouped); + + // Then finally we might as well check the computed leaf nodes too. + let control_leaves: BTreeMap = control + .leaves() + .map(|(index, value)| (index.index.value(), value.clone())) + .collect(); + + for (column, test_leaf) in subtrees.nodes { + if test_leaf.is_empty() { + continue; + } + let control_leaf = control_leaves + .get(&column) + .unwrap_or_else(|| panic!("no leaf node found for column {column}")); + assert_eq!(control_leaf, &test_leaf); + } + } +} From df37bc7fa1e080b45537f3fa720a2771be0797d2 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Thu, 14 Nov 2024 14:04:15 -0700 Subject: [PATCH 2/3] smt: implement single subtree-8 hashing, w/ benchmarks & tests This will be composed into depth-8-subtree-based computation of entire sparse Merkle trees. --- Cargo.toml | 4 + benches/smt-subtree.rs | 136 +++++++++++++++++++++++++++++ src/merkle/mod.rs | 2 +- src/merkle/smt/full/mod.rs | 22 ++++- src/merkle/smt/mod.rs | 174 ++++++++++++++++++++++++++++++++++++- 5 files changed, 332 insertions(+), 6 deletions(-) create mode 100644 benches/smt-subtree.rs diff --git a/Cargo.toml b/Cargo.toml index 5d124c6..ec59d42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,10 @@ harness = false name = "smt" harness = false +[[bench]] +name = "smt-subtree" +harness = false + [[bench]] name = "store" harness = false diff --git a/benches/smt-subtree.rs b/benches/smt-subtree.rs new file mode 100644 index 0000000..cd7454a --- /dev/null +++ b/benches/smt-subtree.rs @@ -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(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])] +} diff --git a/src/merkle/mod.rs b/src/merkle/mod.rs index a562aa5..dc897dc 100644 --- a/src/merkle/mod.rs +++ b/src/merkle/mod.rs @@ -23,7 +23,7 @@ pub use path::{MerklePath, RootPath, ValuePath}; mod smt; pub use smt::{ LeafIndex, MutationSet, SimpleSmt, Smt, SmtLeaf, SmtLeafError, SmtProof, SmtProofError, - SMT_DEPTH, SMT_MAX_DEPTH, SMT_MIN_DEPTH, + SubtreeLeaf, SMT_DEPTH, SMT_MAX_DEPTH, SMT_MIN_DEPTH, }; mod mmr; diff --git a/src/merkle/smt/full/mod.rs b/src/merkle/smt/full/mod.rs index 1e2c574..fd9a4f5 100644 --- a/src/merkle/smt/full/mod.rs +++ b/src/merkle/smt/full/mod.rs @@ -6,7 +6,7 @@ use alloc::{ use super::{ EmptySubtreeRoots, Felt, InnerNode, InnerNodeInfo, LeafIndex, MerkleError, MerklePath, - MutationSet, NodeIndex, Rpo256, RpoDigest, SparseMerkleTree, Word, EMPTY_WORD, + MutationSet, NodeIndex, Rpo256, RpoDigest, SparseMerkleTree, SubtreeLeaf, Word, EMPTY_WORD, }; mod error; @@ -249,6 +249,26 @@ impl Smt { None } } + + /// Builds Merkle nodes from a bottom layer of "leaves" -- represented by a horizontal index and + /// the hash of the leaf at that index. `leaves` *must* be sorted by horizontal index, and + /// `leaves` must not contain more than one depth-8 subtree's worth of leaves. + /// + /// This function will then calculate the inner nodes above each leaf for 8 layers, as well as + /// the "leaves" for the next 8-deep subtree, so this function can effectively be chained into + /// itself. + /// + /// # Panics + /// With debug assertions on, this function panics under invalid inputs: if `leaves` contains + /// more entries than can fit in a depth-8 subtree, if `leaves` contains leaves belonging to + /// different depth-8 subtrees, if `bottom_depth` is lower in the tree than the specified + /// maximum depth (`DEPTH`), or if `leaves` is not sorted. + pub fn build_subtree( + leaves: Vec, + bottom_depth: u8, + ) -> (BTreeMap, Vec) { + >::build_subtree(leaves, bottom_depth) + } } impl SparseMerkleTree for Smt { diff --git a/src/merkle/smt/mod.rs b/src/merkle/smt/mod.rs index 6e4bd2e..2cafc90 100644 --- a/src/merkle/smt/mod.rs +++ b/src/merkle/smt/mod.rs @@ -410,14 +410,120 @@ pub(crate) trait SparseMerkleTree { accumulator.leaves = SubtreeLeavesIter::from_leaves(&mut accumulated_leaves).collect(); accumulator } + + /// Builds Merkle nodes from a bottom layer of "leaves" -- represented by a horizontal index and + /// the hash of the leaf at that index. `leaves` *must* be sorted by horizontal index, and + /// `leaves` must not contain more than one depth-8 subtree's worth of leaves. + /// + /// This function will then calculate the inner nodes above each leaf for 8 layers, as well as + /// the "leaves" for the next 8-deep subtree, so this function can effectively be chained into + /// itself. + /// + /// # Panics + /// With debug assertions on, this function panics under invalid inputs: if `leaves` contains + /// more entries than can fit in a depth-8 subtree, if `leaves` contains leaves belonging to + /// different depth-8 subtrees, if `bottom_depth` is lower in the tree than the specified + /// maximum depth (`DEPTH`), or if `leaves` is not sorted. + fn build_subtree( + mut leaves: Vec, + bottom_depth: u8, + ) -> (BTreeMap, Vec) { + debug_assert!(bottom_depth <= DEPTH); + debug_assert!(Integer::is_multiple_of(&bottom_depth, &SUBTREE_DEPTH)); + debug_assert!(leaves.len() <= usize::pow(2, SUBTREE_DEPTH as u32)); + + let subtree_root = bottom_depth - SUBTREE_DEPTH; + + let mut inner_nodes: BTreeMap = Default::default(); + + let mut next_leaves: Vec = Vec::with_capacity(leaves.len() / 2); + + for next_depth in (subtree_root..bottom_depth).rev() { + debug_assert!(next_depth <= bottom_depth); + + // `next_depth` is the stuff we're making. + // `current_depth` is the stuff we have. + let current_depth = next_depth + 1; + + let mut iter = leaves.drain(..).peekable(); + while let Some(first) = iter.next() { + // On non-continuous iterations, including the first iteration, `first_column` may + // be a left or right node. On subsequent continuous iterations, we will always call + // `iter.next()` twice. + + // On non-continuous iterations (including the very first iteration), this column + // could be either on the left or the right. If the next iteration is not + // discontinuous with our right node, then the next iteration's + + let is_right = first.col.is_odd(); + let (left, right) = if is_right { + // Discontinuous iteration: we have no left node, so it must be empty. + + let left = SubtreeLeaf { + col: first.col - 1, + hash: *EmptySubtreeRoots::entry(DEPTH, current_depth), + }; + let right = first; + + (left, right) + } else { + let left = first; + + let right_col = first.col + 1; + let right = match iter.peek().copied() { + Some(SubtreeLeaf { col, .. }) if col == right_col => { + // Our inputs must be sorted. + debug_assert!(left.col <= col); + // The next leaf in the iterator is our sibling. Use it and consume it! + iter.next().unwrap() + }, + // Otherwise, the leaves don't contain our sibling, so our sibling must be + // empty. + _ => SubtreeLeaf { + col: right_col, + hash: *EmptySubtreeRoots::entry(DEPTH, current_depth), + }, + }; + + (left, right) + }; + + let index = NodeIndex::new_unchecked(current_depth, left.col).parent(); + let node = InnerNode { left: left.hash, right: right.hash }; + let hash = node.hash(); + + let &equivalent_empty_hash = EmptySubtreeRoots::entry(DEPTH, next_depth); + // If this hash is empty, then it doesn't become a new inner node, nor does it count + // as a leaf for the next depth. + if hash != equivalent_empty_hash { + inner_nodes.insert(index, node); + // FIXME: is it possible for this to end up not being sorted? I don't think so. + next_leaves.push(SubtreeLeaf { col: index.value(), hash }); + } + } + + // Stop borrowing `leaves`, so we can swap it. + // The iterator is empty at this point anyway. + drop(iter); + + // After each depth, consider the stuff we just made the new "leaves", and empty the + // other collection. + mem::swap(&mut leaves, &mut next_leaves); + } + + (inner_nodes, leaves) + } } // INNER NODE // ================================================================================================ +/// This struct is public so functions returning it can be used in `benches/`, but is otherwise not +/// part of the public API. +#[doc(hidden)] #[derive(Debug, Default, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -pub(crate) struct InnerNode { +pub struct InnerNode { pub left: RpoDigest, pub right: RpoDigest, } @@ -530,8 +636,11 @@ impl MutationSet { // SUBTREES // ================================================================================================ +/// A subtree is of depth 8. +const SUBTREE_DEPTH: u8 = 8; + /// A depth-8 subtree contains 256 "columns" that can possibly be occupied. -const COLS_PER_SUBTREE: u64 = u64::pow(2, 8); +const COLS_PER_SUBTREE: u64 = u64::pow(2, SUBTREE_DEPTH as u32); /// Helper struct for organizing the data we care about when computing Merkle subtrees. /// @@ -616,9 +725,16 @@ impl<'s> core::iter::Iterator for SubtreeLeavesIter<'s> { mod test { use alloc::{collections::BTreeMap, vec::Vec}; - use super::{PairComputations, SmtLeaf, SparseMerkleTree, SubtreeLeaf, SubtreeLeavesIter}; + use super::{ + PairComputations, SmtLeaf, SparseMerkleTree, SubtreeLeaf, SubtreeLeavesIter, + COLS_PER_SUBTREE, SUBTREE_DEPTH, + }; - use crate::{hash::rpo::RpoDigest, merkle::Smt, Felt, Word, ONE}; + use crate::{ + hash::rpo::RpoDigest, + merkle::{NodeIndex, Smt, SMT_DEPTH}, + Felt, Word, ONE, + }; fn smtleaf_to_subtree_leaf(leaf: &SmtLeaf) -> SubtreeLeaf { SubtreeLeaf { @@ -707,4 +823,54 @@ mod test { assert_eq!(control_leaf, &test_leaf); } } + + // Helper for the below tests. + fn generate_entries(pair_count: u64) -> Vec<(RpoDigest, Word)> { + (0..pair_count) + .map(|i| { + let leaf_index = ((i as f64 / pair_count as f64) * (pair_count as f64)) as u64; + let key = RpoDigest::new([ONE, ONE, Felt::new(i), Felt::new(leaf_index)]); + let value = [ONE, ONE, ONE, Felt::new(i)]; + (key, value) + }) + .collect() + } + + #[test] + fn test_single_subtree() { + // A single subtree's worth of leaves. + const PAIR_COUNT: u64 = COLS_PER_SUBTREE; + + let entries = generate_entries(PAIR_COUNT); + + let control = Smt::with_entries(entries.clone()).unwrap(); + + // `entries` should already be sorted by nature of how we constructed it. + let leaves = Smt::sorted_pairs_to_leaves(entries).leaves; + let leaves = leaves.into_iter().next().unwrap(); + + let (first_subtree, next_leaves) = Smt::build_subtree(leaves, SMT_DEPTH); + assert!(!first_subtree.is_empty()); + + // The inner nodes computed from that subtree should match the nodes in our control tree. + for (index, node) in first_subtree.into_iter() { + let control = control.get_inner_node(index); + assert_eq!( + control, node, + "subtree-computed node at index {index:?} does not match control", + ); + } + + // The "next leaves" returned should also have matching hashes from the equivalent nodes in + // our control tree. + for SubtreeLeaf { col, hash } in next_leaves { + let index = NodeIndex::new(SMT_DEPTH - SUBTREE_DEPTH, col).unwrap(); + let control_node = control.get_inner_node(index); + let control = control_node.hash(); + assert_eq!( + control, hash, + "subtree-computed next leaf at index {index:?} does not match control", + ); + } + } } From b3260c261e9cbcb3bc7849893b6540862a052b77 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Thu, 14 Nov 2024 14:45:26 -0700 Subject: [PATCH 3/3] merkle: add a benchmark for constructing 256-balanced trees This is intended for comparison with the benchmarks from the previous commit. This benchmark represents the theoretical perfect-efficiency performance we could possibly (but impractically) get for computing depth-8 sparse Merkle subtrees. --- Cargo.toml | 4 +++ benches/merkle.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 benches/merkle.rs diff --git a/Cargo.toml b/Cargo.toml index ec59d42..74df3ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,10 @@ harness = false name = "smt-subtree" harness = false +[[bench]] +name = "merkle" +harness = false + [[bench]] name = "store" harness = false diff --git a/benches/merkle.rs b/benches/merkle.rs new file mode 100644 index 0000000..7d6bb2c --- /dev/null +++ b/benches/merkle.rs @@ -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 = + (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 = (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])] +}