Skip to main content

miden_crypto/merkle/
sparse_path.rs

1use alloc::{borrow::Cow, vec::Vec};
2use core::{
3    iter::{self, FusedIterator},
4    num::NonZero,
5};
6
7use super::{
8    EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, NodeIndex, Word, smt::SMT_MAX_DEPTH,
9};
10use crate::{
11    hash::poseidon2::Poseidon2,
12    utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
13};
14
15/// A different representation of [`MerklePath`] designed for memory efficiency for Merkle paths
16/// with empty nodes.
17///
18/// Empty nodes in the path are stored only as their position, represented with a bitmask. A
19/// maximum of 64 nodes (`SMT_MAX_DEPTH`) can be stored (empty and non-empty). The more nodes in a
20/// path are empty, the less memory this struct will use. This type calculates empty nodes on-demand
21/// when iterated through, converted to a [MerklePath], or an empty node is retrieved with
22/// [`SparseMerklePath::at_depth()`], which will incur overhead.
23///
24/// NOTE: This type assumes that Merkle paths always span from the root of the tree to a leaf.
25/// Partial paths are not supported.
26#[derive(Clone, Debug, Default, PartialEq, Eq)]
27#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
28pub struct SparseMerklePath {
29    /// A bitmask representing empty nodes. The set bit corresponds to the depth of an empty node.
30    /// The least significant bit (bit 0) describes depth 1 node (root's children).
31    /// The `bit index + 1` is equal to node's depth.
32    empty_nodes_mask: u64,
33    /// The non-empty nodes, stored in depth-order, but not contiguous across depth.
34    nodes: Vec<Word>,
35}
36
37impl SparseMerklePath {
38    /// Constructs a new sparse Merkle path from a bitmask of empty nodes and a vector of non-empty
39    /// nodes.
40    ///
41    /// The `empty_nodes_mask` is a bitmask where each set bit indicates that the node at that
42    /// depth is empty. The least significant bit (bit 0) describes depth 1 node (root's children).
43    /// The `bit index + 1` is equal to node's depth.
44    /// The `nodes` vector must contain the non-empty nodes in depth order.
45    ///
46    /// # Errors
47    /// - [MerkleError::InvalidPathLength] if the provided `nodes` vector is shorter than the
48    ///   minimum length required by the `empty_nodes_mask`.
49    /// - [MerkleError::DepthTooBig] if the total depth of the path (calculated from the
50    ///   `empty_nodes_mask` and `nodes`) is greater than [SMT_MAX_DEPTH].
51    pub fn from_parts(empty_nodes_mask: u64, nodes: Vec<Word>) -> Result<Self, MerkleError> {
52        // The most significant set bit in the mask marks the minimum length of the path.
53        // For every zero bit before the first set bit, there must be a corresponding node in
54        // `nodes`.
55        // For example, if the mask is `0b1100`, this means that the first two nodes
56        // (depths 1 and 2) are non-empty, and the next two nodes (depths 3 and 4) are empty.
57        // The minimum length of the path is 4, and the `nodes` vector must contain at least 2
58        // nodes to account for the first two zeroes in the mask (depths 1 and 2).
59        let min_path_len = u64::BITS - empty_nodes_mask.leading_zeros();
60        let empty_nodes_count = empty_nodes_mask.count_ones();
61        let min_non_empty_nodes = (min_path_len - empty_nodes_count) as usize;
62
63        if nodes.len() < min_non_empty_nodes {
64            return Err(MerkleError::InvalidPathLength(min_non_empty_nodes));
65        }
66
67        let depth = Self::depth_from_parts(empty_nodes_mask, &nodes) as u8;
68        if depth > SMT_MAX_DEPTH {
69            return Err(MerkleError::DepthTooBig(depth as u64));
70        }
71
72        Ok(Self { empty_nodes_mask, nodes })
73    }
74
75    /// Constructs a sparse Merkle path from an iterator over Merkle nodes that also knows its
76    /// exact size (such as iterators created with [Vec::into_iter]). The iterator must be in order
77    /// of deepest to shallowest.
78    ///
79    /// Knowing the size is necessary to calculate the depth of the tree, which is needed to detect
80    /// which nodes are empty nodes.
81    ///
82    /// # Errors
83    /// Returns [MerkleError::DepthTooBig] if `tree_depth` is greater than [SMT_MAX_DEPTH].
84    pub fn from_sized_iter<I>(iterator: I) -> Result<Self, MerkleError>
85    where
86        I: IntoIterator<IntoIter: ExactSizeIterator, Item = Word>,
87    {
88        let iterator = iterator.into_iter();
89        let tree_depth = iterator.len() as u8;
90
91        if tree_depth > SMT_MAX_DEPTH {
92            return Err(MerkleError::DepthTooBig(tree_depth as u64));
93        }
94
95        let mut empty_nodes_mask: u64 = 0;
96        let mut nodes: Vec<Word> = Default::default();
97
98        for (depth, node) in iter::zip(path_depth_iter(tree_depth), iterator) {
99            let &equivalent_empty_node = EmptySubtreeRoots::entry(tree_depth, depth.get());
100            let is_empty = node == equivalent_empty_node;
101            let node = if is_empty { None } else { Some(node) };
102
103            match node {
104                Some(node) => nodes.push(node),
105                None => empty_nodes_mask |= Self::bitmask_for_depth(depth),
106            }
107        }
108
109        Ok(SparseMerklePath { nodes, empty_nodes_mask })
110    }
111
112    /// Returns the total depth of this path, i.e., the number of nodes this path represents.
113    pub fn depth(&self) -> u8 {
114        Self::depth_from_parts(self.empty_nodes_mask, &self.nodes) as u8
115    }
116
117    /// Get a specific node in this path at a given depth.
118    ///
119    /// The `depth` parameter is defined in terms of `self.depth()`. Merkle paths conventionally do
120    /// not include the root, so the shallowest depth is `1`, and the deepest depth is
121    /// `self.depth()`.
122    ///
123    /// # Errors
124    /// Returns [MerkleError::DepthTooBig] if `node_depth` is greater than the total depth of this
125    /// path.
126    pub fn at_depth(&self, node_depth: NonZero<u8>) -> Result<Word, MerkleError> {
127        if node_depth.get() > self.depth() {
128            return Err(MerkleError::DepthTooBig(node_depth.get().into()));
129        }
130
131        let node = if let Some(nonempty_index) = self.get_nonempty_index(node_depth) {
132            self.nodes[nonempty_index]
133        } else {
134            *EmptySubtreeRoots::entry(self.depth(), node_depth.get())
135        };
136
137        Ok(node)
138    }
139
140    /// Deconstructs this path into its component parts.
141    ///
142    /// Returns a tuple containing:
143    /// - a bitmask where each set bit indicates that the node at that depth is empty. The least
144    ///   significant bit (bit 0) describes depth 1 node (root's children).
145    /// - a vector of non-empty nodes in depth order.
146    pub fn into_parts(self) -> (u64, Vec<Word>) {
147        (self.empty_nodes_mask, self.nodes)
148    }
149
150    // PROVIDERS
151    // ============================================================================================
152
153    /// Constructs a borrowing iterator over the nodes in this path.
154    /// Starts from the leaf and iterates toward the root (excluding the root).
155    pub fn iter(&self) -> impl ExactSizeIterator<Item = Word> {
156        self.into_iter()
157    }
158
159    /// Computes the Merkle root for this opening.
160    pub fn compute_root(&self, index: u64, node_to_prove: Word) -> Result<Word, MerkleError> {
161        let mut index = NodeIndex::new(self.depth(), index)?;
162        let root = self.iter().fold(node_to_prove, |node, sibling| {
163            // Compute the node and move to the next iteration.
164            let children = index.build_node(node, sibling);
165            index.move_up();
166            Poseidon2::merge(&children)
167        });
168
169        Ok(root)
170    }
171
172    /// Verifies the Merkle opening proof towards the provided root.
173    ///
174    /// # Errors
175    /// Returns an error if:
176    /// - provided node index is invalid.
177    /// - root calculated during the verification differs from the provided one.
178    pub fn verify(&self, index: u64, node: Word, &expected_root: &Word) -> Result<(), MerkleError> {
179        let computed_root = self.compute_root(index, node)?;
180        if computed_root != expected_root {
181            return Err(MerkleError::ConflictingRoots {
182                expected_root,
183                actual_root: computed_root,
184            });
185        }
186
187        Ok(())
188    }
189
190    /// Given the node this path opens to, return an iterator of all the nodes that are known via
191    /// this path.
192    ///
193    /// Each item in the iterator is an [InnerNodeInfo], containing the hash of a node as `.value`,
194    /// and its two children as `.left` and `.right`. The very first item in that iterator will be
195    /// the parent of `node_to_prove` as stored in this [SparseMerklePath].
196    ///
197    /// From there, the iterator will continue to yield every further parent and both of its
198    /// children, up to and including the root node.
199    ///
200    /// If `node_to_prove` is not the node this path is an opening to, or `index` is not the
201    /// correct index for that node, the returned nodes will be meaningless.
202    ///
203    /// # Errors
204    /// Returns an error if the specified index is not valid for this path.
205    pub fn authenticated_nodes(
206        &self,
207        index: u64,
208        node_to_prove: Word,
209    ) -> Result<InnerNodeIterator<'_>, MerkleError> {
210        let index = NodeIndex::new(self.depth(), index)?;
211        Ok(InnerNodeIterator { path: self, index, value: node_to_prove })
212    }
213
214    // PRIVATE HELPERS
215    // ============================================================================================
216
217    const fn bitmask_for_depth(node_depth: NonZero<u8>) -> u64 {
218        // - 1 because paths do not include the root.
219        1 << (node_depth.get() - 1)
220    }
221
222    const fn is_depth_empty(&self, node_depth: NonZero<u8>) -> bool {
223        (self.empty_nodes_mask & Self::bitmask_for_depth(node_depth)) != 0
224    }
225
226    /// Index of the non-empty node in the `self.nodes` vector. If the specified depth is
227    /// empty, None is returned.
228    fn get_nonempty_index(&self, node_depth: NonZero<u8>) -> Option<usize> {
229        if self.is_depth_empty(node_depth) {
230            return None;
231        }
232
233        let bit_index = node_depth.get() - 1;
234        let without_shallower = self.empty_nodes_mask >> bit_index;
235        let empty_deeper = without_shallower.count_ones() as usize;
236        // The vec index we would use if we didn't have any empty nodes to account for...
237        let normal_index = (self.depth() - node_depth.get()) as usize;
238        // subtracted by the number of empty nodes that are deeper than us.
239        Some(normal_index - empty_deeper)
240    }
241
242    /// Returns the total depth of this path from its parts.
243    fn depth_from_parts(empty_nodes_mask: u64, nodes: &[Word]) -> usize {
244        nodes.len() + empty_nodes_mask.count_ones() as usize
245    }
246}
247
248// SERIALIZATION
249// ================================================================================================
250
251impl Serializable for SparseMerklePath {
252    fn write_into<W: ByteWriter>(&self, target: &mut W) {
253        target.write_u8(self.depth());
254        target.write_u64(self.empty_nodes_mask);
255        target.write_many(&self.nodes);
256    }
257}
258
259impl Deserializable for SparseMerklePath {
260    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
261        let depth = source.read_u8()?;
262        if depth > SMT_MAX_DEPTH {
263            return Err(DeserializationError::InvalidValue(format!(
264                "SparseMerklePath max depth exceeded ({depth} > {SMT_MAX_DEPTH})",
265            )));
266        }
267        let empty_nodes_mask = source.read_u64()?;
268        let empty_nodes_count = empty_nodes_mask.count_ones();
269        if empty_nodes_count > depth as u32 {
270            return Err(DeserializationError::InvalidValue(format!(
271                "SparseMerklePath has more empty nodes ({empty_nodes_count}) than its full length ({depth})",
272            )));
273        }
274        let count = depth as u32 - empty_nodes_count;
275        let nodes: Vec<Word> = source.read_many_iter(count as usize)?.collect::<Result<_, _>>()?;
276        Ok(Self { empty_nodes_mask, nodes })
277    }
278}
279
280// CONVERSIONS
281// ================================================================================================
282
283impl From<SparseMerklePath> for MerklePath {
284    fn from(sparse_path: SparseMerklePath) -> Self {
285        MerklePath::from_iter(sparse_path)
286    }
287}
288
289impl TryFrom<MerklePath> for SparseMerklePath {
290    type Error = MerkleError;
291
292    /// # Errors
293    ///
294    /// This conversion returns [MerkleError::DepthTooBig] if the path length is greater than
295    /// [`SMT_MAX_DEPTH`].
296    fn try_from(path: MerklePath) -> Result<Self, MerkleError> {
297        SparseMerklePath::from_sized_iter(path)
298    }
299}
300
301impl From<SparseMerklePath> for Vec<Word> {
302    fn from(path: SparseMerklePath) -> Self {
303        Vec::from_iter(path)
304    }
305}
306
307// ITERATORS
308// ================================================================================================
309
310/// Iterator for [`SparseMerklePath`]. Starts from the leaf and iterates toward the root (excluding
311/// the root).
312pub struct SparseMerklePathIter<'p> {
313    /// The "inner" value we're iterating over.
314    path: Cow<'p, SparseMerklePath>,
315
316    /// The depth a `next()` call will get. `next_depth == 0` indicates that the iterator has been
317    /// exhausted.
318    next_depth: u8,
319}
320
321impl Iterator for SparseMerklePathIter<'_> {
322    type Item = Word;
323
324    fn next(&mut self) -> Option<Word> {
325        let this_depth = self.next_depth;
326        // Paths don't include the root, so if `this_depth` is 0 then we keep returning `None`.
327        let this_depth = NonZero::new(this_depth)?;
328        self.next_depth = this_depth.get() - 1;
329
330        // `this_depth` is only ever decreasing, so it can't ever exceed `self.path.depth()`.
331        let node = self
332            .path
333            .at_depth(this_depth)
334            .expect("current depth should never exceed the path depth");
335        Some(node)
336    }
337
338    // SparseMerkleIter always knows its exact size.
339    fn size_hint(&self) -> (usize, Option<usize>) {
340        let remaining = ExactSizeIterator::len(self);
341        (remaining, Some(remaining))
342    }
343}
344
345impl ExactSizeIterator for SparseMerklePathIter<'_> {
346    fn len(&self) -> usize {
347        self.next_depth as usize
348    }
349}
350
351impl FusedIterator for SparseMerklePathIter<'_> {}
352
353// TODO: impl DoubleEndedIterator.
354
355impl IntoIterator for SparseMerklePath {
356    type IntoIter = SparseMerklePathIter<'static>;
357    type Item = <Self::IntoIter as Iterator>::Item;
358
359    fn into_iter(self) -> SparseMerklePathIter<'static> {
360        let tree_depth = self.depth();
361        SparseMerklePathIter {
362            path: Cow::Owned(self),
363            next_depth: tree_depth,
364        }
365    }
366}
367
368impl<'p> IntoIterator for &'p SparseMerklePath {
369    type Item = <SparseMerklePathIter<'p> as Iterator>::Item;
370    type IntoIter = SparseMerklePathIter<'p>;
371
372    fn into_iter(self) -> SparseMerklePathIter<'p> {
373        let tree_depth = self.depth();
374        SparseMerklePathIter {
375            path: Cow::Borrowed(self),
376            next_depth: tree_depth,
377        }
378    }
379}
380
381/// An iterator over nodes known by a [SparseMerklePath]. See
382/// [`SparseMerklePath::authenticated_nodes()`].
383pub struct InnerNodeIterator<'p> {
384    path: &'p SparseMerklePath,
385    index: NodeIndex,
386    value: Word,
387}
388
389impl Iterator for InnerNodeIterator<'_> {
390    type Item = InnerNodeInfo;
391
392    fn next(&mut self) -> Option<Self::Item> {
393        if self.index.is_root() {
394            return None;
395        }
396
397        let index_depth = NonZero::new(self.index.depth()).expect("non-root depth cannot be 0");
398        let path_node = self.path.at_depth(index_depth).unwrap();
399
400        let children = self.index.build_node(self.value, path_node);
401        self.value = Poseidon2::merge(&children);
402        self.index.move_up();
403
404        Some(InnerNodeInfo {
405            value: self.value,
406            left: children[0],
407            right: children[1],
408        })
409    }
410}
411
412// COMPARISONS
413// ================================================================================================
414impl PartialEq<MerklePath> for SparseMerklePath {
415    fn eq(&self, rhs: &MerklePath) -> bool {
416        if self.depth() != rhs.depth() {
417            return false;
418        }
419
420        for (node, &rhs_node) in iter::zip(self, rhs.iter()) {
421            if node != rhs_node {
422                return false;
423            }
424        }
425
426        true
427    }
428}
429
430impl PartialEq<SparseMerklePath> for MerklePath {
431    fn eq(&self, rhs: &SparseMerklePath) -> bool {
432        rhs == self
433    }
434}
435
436// HELPERS
437// ================================================================================================
438
439/// Iterator for path depths, which start at the deepest part of the tree and go the shallowest
440/// depth before the root (depth 1).
441fn path_depth_iter(tree_depth: u8) -> impl ExactSizeIterator<Item = NonZero<u8>> {
442    let top_down_iter = (1..=tree_depth).map(|depth| {
443        // RangeInclusive<1, _> guarantees depth >= 1
444        NonZero::new(depth).expect("range is bounded by 1")
445    });
446
447    // Reverse the top-down iterator to get a bottom-up iterator.
448    top_down_iter.rev()
449}
450
451// TESTS
452// ================================================================================================
453#[cfg(test)]
454mod tests {
455    use alloc::vec::Vec;
456    use core::num::NonZero;
457
458    use assert_matches::assert_matches;
459
460    use super::SparseMerklePath;
461    use crate::{
462        Felt, ONE, Word,
463        merkle::{
464            EmptySubtreeRoots, MerkleError, MerklePath, MerkleTree, NodeIndex,
465            smt::{LeafIndex, SMT_MAX_DEPTH, SimpleSmt, Smt, SparseMerkleTree},
466            sparse_path::path_depth_iter,
467        },
468    };
469
470    fn make_smt(pair_count: u64) -> Smt {
471        let entries: Vec<(Word, Word)> = (0..pair_count)
472            .map(|n| {
473                let leaf_index = ((n as f64 / pair_count as f64) * 255.0) as u64;
474                let key = Word::new([ONE, ONE, Felt::new(n), Felt::new(leaf_index)]);
475                let value = Word::new([ONE, ONE, ONE, ONE]);
476                (key, value)
477            })
478            .collect();
479
480        Smt::with_entries(entries).unwrap()
481    }
482
483    /// Manually test the exact bit patterns for a sample path of 8 nodes, including both empty and
484    /// non-empty nodes.
485    ///
486    /// This also offers an overview of what each part of the bit-math involved means and
487    /// represents.
488    #[test]
489    fn test_sparse_bits() {
490        const DEPTH: u8 = 8;
491        let raw_nodes: [Word; DEPTH as usize] = [
492            // Depth 8.
493            ([8u8, 8, 8, 8].into()),
494            // Depth 7.
495            *EmptySubtreeRoots::entry(DEPTH, 7),
496            // Depth 6.
497            *EmptySubtreeRoots::entry(DEPTH, 6),
498            // Depth 5.
499            [5u8, 5, 5, 5].into(),
500            // Depth 4.
501            [4u8, 4, 4, 4].into(),
502            // Depth 3.
503            *EmptySubtreeRoots::entry(DEPTH, 3),
504            // Depth 2.
505            *EmptySubtreeRoots::entry(DEPTH, 2),
506            // Depth 1.
507            *EmptySubtreeRoots::entry(DEPTH, 1),
508            // Root is not included.
509        ];
510
511        let sparse_nodes: [Option<Word>; DEPTH as usize] = [
512            // Depth 8.
513            Some([8u8, 8, 8, 8].into()),
514            // Depth 7.
515            None,
516            // Depth 6.
517            None,
518            // Depth 5.
519            Some([5u8, 5, 5, 5].into()),
520            // Depth 4.
521            Some([4u8, 4, 4, 4].into()),
522            // Depth 3.
523            None,
524            // Depth 2.
525            None,
526            // Depth 1.
527            None,
528            // Root is not included.
529        ];
530
531        const EMPTY_BITS: u64 = 0b0110_0111;
532
533        let sparse_path = SparseMerklePath::from_sized_iter(raw_nodes).unwrap();
534
535        assert_eq!(sparse_path.empty_nodes_mask, EMPTY_BITS);
536
537        // Keep track of how many non-empty nodes we have seen
538        let mut nonempty_idx = 0;
539
540        // Test starting from the deepest nodes (depth 8)
541        for depth in (1..=8).rev() {
542            let idx = (sparse_path.depth() - depth) as usize;
543            let bit = 1 << (depth - 1);
544
545            // Check that the depth bit is set correctly...
546            let is_set = (sparse_path.empty_nodes_mask & bit) != 0;
547            assert_eq!(is_set, sparse_nodes.get(idx).unwrap().is_none());
548
549            if is_set {
550                // Check that we don't return digests for empty nodes
551                let &test_node = sparse_nodes.get(idx).unwrap();
552                assert_eq!(test_node, None);
553            } else {
554                // Check that we can calculate non-empty indices correctly.
555                let control_node = raw_nodes.get(idx).unwrap();
556                assert_eq!(
557                    sparse_path.get_nonempty_index(NonZero::new(depth).unwrap()).unwrap(),
558                    nonempty_idx
559                );
560                let test_node = sparse_path.nodes.get(nonempty_idx).unwrap();
561                assert_eq!(test_node, control_node);
562
563                nonempty_idx += 1;
564            }
565        }
566    }
567
568    #[test]
569    fn from_parts() {
570        const DEPTH: u8 = 8;
571        let raw_nodes: [Word; DEPTH as usize] = [
572            // Depth 8.
573            ([8u8, 8, 8, 8].into()),
574            // Depth 7.
575            *EmptySubtreeRoots::entry(DEPTH, 7),
576            // Depth 6.
577            *EmptySubtreeRoots::entry(DEPTH, 6),
578            // Depth 5.
579            [5u8, 5, 5, 5].into(),
580            // Depth 4.
581            [4u8, 4, 4, 4].into(),
582            // Depth 3.
583            *EmptySubtreeRoots::entry(DEPTH, 3),
584            // Depth 2.
585            *EmptySubtreeRoots::entry(DEPTH, 2),
586            // Depth 1.
587            *EmptySubtreeRoots::entry(DEPTH, 1),
588            // Root is not included.
589        ];
590
591        let empty_nodes_mask = 0b0110_0111;
592        let nodes = vec![[8u8, 8, 8, 8].into(), [5u8, 5, 5, 5].into(), [4u8, 4, 4, 4].into()];
593        let insufficient_nodes = vec![[4u8, 4, 4, 4].into()];
594
595        let error = SparseMerklePath::from_parts(empty_nodes_mask, insufficient_nodes).unwrap_err();
596        assert_matches!(error, MerkleError::InvalidPathLength(2));
597
598        let iter_sparse_path = SparseMerklePath::from_sized_iter(raw_nodes).unwrap();
599        let sparse_path = SparseMerklePath::from_parts(empty_nodes_mask, nodes).unwrap();
600
601        assert_eq!(sparse_path, iter_sparse_path);
602    }
603
604    #[test]
605    fn from_sized_iter() {
606        let tree = make_smt(8192);
607
608        for (key, _value) in tree.entries() {
609            let index = NodeIndex::from(Smt::key_to_leaf_index(key));
610            let sparse_path = tree.get_path(key);
611            for (sparse_node, proof_idx) in
612                itertools::zip_eq(sparse_path.clone(), index.proof_indices())
613            {
614                let proof_node = tree.get_node_hash(proof_idx);
615                assert_eq!(sparse_node, proof_node);
616            }
617        }
618    }
619
620    #[test]
621    fn test_zero_sized() {
622        let nodes: Vec<Word> = Default::default();
623
624        // Sparse paths that don't actually contain any nodes should still be well behaved.
625        let sparse_path = SparseMerklePath::from_sized_iter(nodes).unwrap();
626        assert_eq!(sparse_path.depth(), 0);
627        assert_matches!(
628            sparse_path.at_depth(NonZero::new(1).unwrap()),
629            Err(MerkleError::DepthTooBig(1))
630        );
631        assert_eq!(sparse_path.iter().next(), None);
632        assert_eq!(sparse_path.into_iter().next(), None);
633    }
634
635    use proptest::prelude::*;
636
637    // Arbitrary instance for MerklePath
638    impl Arbitrary for MerklePath {
639        type Parameters = ();
640        type Strategy = BoxedStrategy<Self>;
641
642        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
643            prop::collection::vec(any::<Word>(), 0..=SMT_MAX_DEPTH as usize)
644                .prop_map(MerklePath::new)
645                .boxed()
646        }
647    }
648
649    // Arbitrary instance for SparseMerklePath
650    impl Arbitrary for SparseMerklePath {
651        type Parameters = ();
652        type Strategy = BoxedStrategy<Self>;
653
654        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
655            (0..=SMT_MAX_DEPTH as usize)
656                .prop_flat_map(|depth| {
657                    // Generate a bitmask for empty nodes - avoid overflow
658                    let max_mask = if depth > 0 && depth < 64 {
659                        (1u64 << depth) - 1
660                    } else if depth == 64 {
661                        u64::MAX
662                    } else {
663                        0
664                    };
665                    let empty_nodes_mask =
666                        prop::num::u64::ANY.prop_map(move |mask| mask & max_mask);
667
668                    // Generate non-empty nodes based on the mask
669                    empty_nodes_mask.prop_flat_map(move |mask| {
670                        let empty_count = mask.count_ones() as usize;
671                        let non_empty_count = depth.saturating_sub(empty_count);
672
673                        prop::collection::vec(any::<Word>(), non_empty_count).prop_map(
674                            move |nodes| SparseMerklePath::from_parts(mask, nodes).unwrap(),
675                        )
676                    })
677                })
678                .boxed()
679        }
680    }
681
682    proptest! {
683        #[test]
684        fn sparse_merkle_path_roundtrip_equivalence(path in any::<MerklePath>()) {
685            // Convert MerklePath to SparseMerklePath and back
686            let sparse_result = SparseMerklePath::try_from(path.clone());
687            if path.depth() <= SMT_MAX_DEPTH {
688                let sparse = sparse_result.unwrap();
689                let reconstructed = MerklePath::from(sparse);
690                prop_assert_eq!(path, reconstructed);
691            } else {
692                prop_assert!(sparse_result.is_err());
693            }
694        }
695    }
696    proptest! {
697
698        #[test]
699        fn merkle_path_roundtrip_equivalence(sparse in any::<SparseMerklePath>()) {
700            // Convert SparseMerklePath to MerklePath and back
701            let merkle = MerklePath::from(sparse.clone());
702            let reconstructed = SparseMerklePath::try_from(merkle.clone()).unwrap();
703            prop_assert_eq!(sparse, reconstructed);
704        }
705    }
706    proptest! {
707
708        #[test]
709        fn path_equivalence_tests(path in any::<MerklePath>(), path2 in any::<MerklePath>()) {
710            if path.depth() > SMT_MAX_DEPTH {
711                return Ok(());
712            }
713
714            let sparse = SparseMerklePath::try_from(path.clone()).unwrap();
715
716            // Depth consistency
717            prop_assert_eq!(path.depth(), sparse.depth());
718
719            // Node access consistency including path_depth_iter
720            if path.depth() > 0 {
721                for depth in path_depth_iter(path.depth()) {
722                    let merkle_node = path.at_depth(depth);
723                    let sparse_node = sparse.at_depth(depth);
724
725                    match (merkle_node, sparse_node) {
726                        (Some(m), Ok(s)) => prop_assert_eq!(m, s),
727                        (None, Err(_)) => {},
728                        _ => prop_assert!(false, "Inconsistent node access at depth {}", depth.get()),
729                    }
730                }
731            }
732
733            // Iterator consistency
734            if path.depth() > 0 {
735                let merkle_nodes: Vec<_> = path.iter().collect();
736                let sparse_nodes: Vec<_> = sparse.iter().collect();
737
738                prop_assert_eq!(merkle_nodes.len(), sparse_nodes.len());
739                for (m, s) in merkle_nodes.iter().zip(sparse_nodes.iter()) {
740                    prop_assert_eq!(*m, s);
741                }
742            }
743
744            // Test equality between different representations
745            if path2.depth() <= SMT_MAX_DEPTH {
746                let sparse2 = SparseMerklePath::try_from(path2.clone()).unwrap();
747                prop_assert_eq!(path == path2, sparse == sparse2);
748                prop_assert_eq!(path == sparse2, sparse == path2);
749            }
750        }
751    }
752    // rather heavy tests
753    proptest! {
754        #![proptest_config(ProptestConfig::with_cases(100))]
755
756        #[test]
757        fn compute_root_consistency(
758            tree_data in any::<RandomMerkleTree>(),
759            node in any::<Word>()
760        ) {
761            let RandomMerkleTree { tree, leaves: _,  indices } = tree_data;
762
763            for &leaf_index in indices.iter() {
764                let path = tree.get_path(NodeIndex::new(tree.depth(), leaf_index).unwrap()).unwrap();
765                let sparse = SparseMerklePath::from_sized_iter(path.clone().into_iter()).unwrap();
766
767                let merkle_root = path.compute_root(leaf_index, node);
768                let sparse_root = sparse.compute_root(leaf_index, node);
769
770                match (merkle_root, sparse_root) {
771                    (Ok(m), Ok(s)) => prop_assert_eq!(m, s),
772                    (Err(e1), Err(e2)) => {
773                        // Both should have the same error type
774                        prop_assert_eq!(format!("{:?}", e1), format!("{:?}", e2));
775                    },
776                    _ => prop_assert!(false, "Inconsistent compute_root results"),
777                }
778            }
779        }
780
781        #[test]
782        fn verify_consistency(
783            tree_data in any::<RandomMerkleTree>(),
784            node in any::<Word>()
785        ) {
786            let RandomMerkleTree { tree, leaves, indices } = tree_data;
787
788            for (i, &leaf_index) in indices.iter().enumerate() {
789                let leaf = leaves[i];
790                let path = tree.get_path(NodeIndex::new(tree.depth(), leaf_index).unwrap()).unwrap();
791                let sparse = SparseMerklePath::from_sized_iter(path.clone().into_iter()).unwrap();
792
793                let root = tree.root();
794
795                let merkle_verify = path.verify(leaf_index, leaf, &root);
796                let sparse_verify = sparse.verify(leaf_index, leaf, &root);
797
798                match (merkle_verify, sparse_verify) {
799                    (Ok(()), Ok(())) => {},
800                    (Err(e1), Err(e2)) => {
801                        // Both should have the same error type
802                        prop_assert_eq!(format!("{:?}", e1), format!("{:?}", e2));
803                    },
804                    _ => prop_assert!(false, "Inconsistent verify results"),
805                }
806
807                // Test with wrong node - both should fail
808                let wrong_verify = path.verify(leaf_index, node, &root);
809                let wrong_sparse_verify = sparse.verify(leaf_index, node, &root);
810
811                match (wrong_verify, wrong_sparse_verify) {
812                    (Ok(()), Ok(())) => prop_assert!(false, "Verification should have failed with wrong node"),
813                    (Err(_), Err(_)) => {},
814                    _ => prop_assert!(false, "Inconsistent verification results with wrong node"),
815                }
816            }
817        }
818
819        #[test]
820        fn authenticated_nodes_consistency(
821            tree_data in any::<RandomMerkleTree>()
822        ) {
823            let RandomMerkleTree { tree, leaves, indices } = tree_data;
824
825            for (i, &leaf_index) in indices.iter().enumerate() {
826                let leaf = leaves[i];
827                let path = tree.get_path(NodeIndex::new(tree.depth(), leaf_index).unwrap()).unwrap();
828                let sparse = SparseMerklePath::from_sized_iter(path.clone().into_iter()).unwrap();
829
830                let merkle_result = path.authenticated_nodes(leaf_index, leaf);
831                let sparse_result = sparse.authenticated_nodes(leaf_index, leaf);
832
833                match (merkle_result, sparse_result) {
834                    (Ok(m_iter), Ok(s_iter)) => {
835                        let merkle_nodes: Vec<_> = m_iter.collect();
836                        let sparse_nodes: Vec<_> = s_iter.collect();
837                        prop_assert_eq!(merkle_nodes.len(), sparse_nodes.len());
838                        for (m, s) in merkle_nodes.iter().zip(sparse_nodes.iter()) {
839                            prop_assert_eq!(m, s);
840                        }
841                    },
842                    (Err(e1), Err(e2)) => {
843                        prop_assert_eq!(format!("{:?}", e1), format!("{:?}", e2));
844                    },
845                    _ => prop_assert!(false, "Inconsistent authenticated_nodes results"),
846                }
847            }
848        }
849    }
850
851    #[test]
852    fn test_api_differences() {
853        // This test documents API differences between MerklePath and SparseMerklePath
854
855        // 1. MerklePath has Deref/DerefMut to Vec<Word> - SparseMerklePath does not
856        let merkle = MerklePath::new(vec![Word::default(); 3]);
857        let _vec_ref: &Vec<Word> = &merkle; // This works due to Deref
858        let _vec_mut: &mut Vec<Word> = &mut merkle.clone(); // This works due to DerefMut
859
860        // 2. SparseMerklePath has from_parts() - MerklePath uses new() or from_iter()
861        let sparse = SparseMerklePath::from_parts(0b101, vec![Word::default(); 2]).unwrap();
862        assert_eq!(sparse.depth(), 4); // depth is 4 because mask has bits set up to depth 4
863
864        // 3. SparseMerklePath has from_sized_iter() - MerklePath uses from_iter()
865        let nodes = vec![Word::default(); 3];
866        let sparse_from_iter = SparseMerklePath::from_sized_iter(nodes.clone()).unwrap();
867        let merkle_from_iter = MerklePath::from_iter(nodes);
868        assert_eq!(sparse_from_iter.depth(), merkle_from_iter.depth());
869    }
870
871    // Arbitrary instance for MerkleTree with random leaves
872    #[derive(Debug, Clone)]
873    struct RandomMerkleTree {
874        tree: MerkleTree,
875        leaves: Vec<Word>,
876        indices: Vec<u64>,
877    }
878
879    impl Arbitrary for RandomMerkleTree {
880        type Parameters = ();
881        type Strategy = BoxedStrategy<Self>;
882
883        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
884            // Generate trees with power-of-2 leaves up to 1024 (2^10)
885            prop::sample::select(&[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024])
886                .prop_flat_map(|num_leaves| {
887                    prop::collection::vec(any::<Word>(), num_leaves).prop_map(|leaves| {
888                        let tree = MerkleTree::new(leaves.clone()).unwrap();
889                        let indices: Vec<u64> = (0..leaves.len() as u64).collect();
890                        RandomMerkleTree { tree, leaves, indices }
891                    })
892                })
893                .boxed()
894        }
895    }
896
897    // Arbitrary instance for SimpleSmt with random entries
898    #[derive(Debug, Clone)]
899    struct RandomSimpleSmt {
900        tree: SimpleSmt<10>, // Depth 10 = 1024 leaves
901        entries: Vec<(u64, Word)>,
902    }
903
904    impl Arbitrary for RandomSimpleSmt {
905        type Parameters = ();
906        type Strategy = BoxedStrategy<Self>;
907
908        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
909            (1..=100usize) // 1-100 entries in an 1024-leaf tree
910                .prop_flat_map(|num_entries| {
911                    prop::collection::vec(
912                        (
913                            0..1024u64, // Valid indices for 1024-leaf tree
914                            any::<Word>(),
915                        ),
916                        num_entries,
917                    )
918                    .prop_map(|mut entries| {
919                        // Ensure unique indices to avoid duplicates
920                        let mut seen = alloc::collections::BTreeSet::new();
921                        entries.retain(|(idx, _)| seen.insert(*idx));
922
923                        let mut tree = SimpleSmt::new().unwrap();
924                        for (idx, value) in &entries {
925                            let leaf_idx = LeafIndex::new(*idx).unwrap();
926                            tree.insert(leaf_idx, *value);
927                        }
928                        RandomSimpleSmt { tree, entries }
929                    })
930                })
931                .boxed()
932        }
933    }
934
935    // Arbitrary instance for Smt with random entries
936    #[derive(Debug, Clone)]
937    struct RandomSmt {
938        tree: Smt,
939        entries: Vec<(Word, Word)>,
940    }
941
942    impl Arbitrary for RandomSmt {
943        type Parameters = ();
944        type Strategy = BoxedStrategy<Self>;
945
946        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
947            (1..=100usize) // 1-100 entries in a sparse tree
948                .prop_flat_map(|num_entries| {
949                    prop::collection::vec((any::<u64>(), any::<Word>()), num_entries).prop_map(
950                        |indices_n_values| {
951                            let entries: Vec<(Word, Word)> = indices_n_values
952                                .into_iter()
953                                .enumerate()
954                                .map(|(n, (leaf_index, value))| {
955                                    // SMT uses the most significant element (index 3) as leaf index
956                                    // Ensure we use valid leaf indices for the SMT depth
957                                    let valid_leaf_index = leaf_index % (1u64 << 60); // Use large but valid range
958                                    let key = Word::new([
959                                        Felt::new(n as u64),         // element 0
960                                        Felt::new(n as u64 + 1),     // element 1
961                                        Felt::new(n as u64 + 2),     // element 2
962                                        Felt::new(valid_leaf_index), // element 3 (leaf index)
963                                    ]);
964                                    (key, value)
965                                })
966                                .collect();
967
968                            // Ensure unique keys to avoid duplicates
969                            let mut seen = alloc::collections::BTreeSet::new();
970                            let unique_entries: Vec<_> =
971                                entries.into_iter().filter(|(key, _)| seen.insert(*key)).collect();
972
973                            let tree = Smt::with_entries(unique_entries.clone()).unwrap();
974                            RandomSmt { tree, entries: unique_entries }
975                        },
976                    )
977                })
978                .boxed()
979        }
980    }
981
982    proptest! {
983        #![proptest_config(ProptestConfig::with_cases(20))]
984
985        #[test]
986        fn simple_smt_path_consistency(tree_data in any::<RandomSimpleSmt>()) {
987            let RandomSimpleSmt { tree, entries } = tree_data;
988
989            for (leaf_index, value) in &entries {
990                let merkle_path = tree.get_path(&LeafIndex::new(*leaf_index).unwrap());
991                let sparse_path = SparseMerklePath::from_sized_iter(merkle_path.clone().into_iter()).unwrap();
992
993                // Verify both paths have same depth
994                prop_assert_eq!(merkle_path.depth(), sparse_path.depth());
995
996                // Verify both paths produce same root for the same value
997                let merkle_root = merkle_path.compute_root(*leaf_index, *value).unwrap();
998                let sparse_root = sparse_path.compute_root(*leaf_index, *value).unwrap();
999                prop_assert_eq!(merkle_root, sparse_root);
1000
1001                // Verify both paths verify correctly
1002                let tree_root = tree.root();
1003                prop_assert!(merkle_path.verify(*leaf_index, *value, &tree_root).is_ok());
1004                prop_assert!(sparse_path.verify(*leaf_index, *value, &tree_root).is_ok());
1005
1006                // Test with random additional leaf
1007                let random_leaf = Word::new([Felt::ONE; 4]);
1008                let random_index = *leaf_index ^ 1; // Ensure it's a sibling
1009
1010                // Both should fail verification with wrong leaf
1011                let merkle_wrong = merkle_path.verify(random_index, random_leaf, &tree_root);
1012                let sparse_wrong = sparse_path.verify(random_index, random_leaf, &tree_root);
1013                prop_assert_eq!(merkle_wrong.is_err(), sparse_wrong.is_err());
1014            }
1015        }
1016
1017        #[test]
1018        fn smt_path_consistency(tree_data in any::<RandomSmt>()) {
1019            let RandomSmt { tree, entries } = tree_data;
1020
1021            for (key, _value) in &entries {
1022                let (merkle_path, leaf) = tree.open(key).into_parts();
1023                let sparse_path = SparseMerklePath::from_sized_iter(merkle_path.clone().into_iter()).unwrap();
1024
1025                let leaf_index = Smt::key_to_leaf_index(key).position();
1026                let actual_value = leaf.hash(); // Use the actual leaf hash
1027
1028                // Verify both paths have same depth
1029                prop_assert_eq!(merkle_path.depth(), sparse_path.depth());
1030
1031                // Verify both paths produce same root for the same value
1032                let merkle_root = merkle_path.compute_root(leaf_index, actual_value).unwrap();
1033                let sparse_root = sparse_path.compute_root(leaf_index, actual_value).unwrap();
1034                prop_assert_eq!(merkle_root, sparse_root);
1035
1036                // Verify both paths verify correctly
1037                let tree_root = tree.root();
1038                prop_assert!(merkle_path.verify(leaf_index, actual_value, &tree_root).is_ok());
1039                prop_assert!(sparse_path.verify(leaf_index, actual_value, &tree_root).is_ok());
1040
1041                // Test authenticated nodes consistency
1042                let merkle_auth = merkle_path.authenticated_nodes(leaf_index, actual_value).unwrap().collect::<Vec<_>>();
1043                let sparse_auth = sparse_path.authenticated_nodes(leaf_index, actual_value).unwrap().collect::<Vec<_>>();
1044                prop_assert_eq!(merkle_auth, sparse_auth);
1045            }
1046        }
1047
1048        #[test]
1049        fn reverse_conversion_from_sparse(tree_data in any::<RandomMerkleTree>()) {
1050            let RandomMerkleTree { tree, leaves, indices } = tree_data;
1051
1052            for (i, &leaf_index) in indices.iter().enumerate() {
1053                let leaf = leaves[i];
1054                let merkle_path = tree.get_path(NodeIndex::new(tree.depth(), leaf_index).unwrap()).unwrap();
1055
1056                // Create SparseMerklePath first, then convert to MerklePath
1057                let sparse_path = SparseMerklePath::from_sized_iter(merkle_path.clone().into_iter()).unwrap();
1058                let converted_merkle = MerklePath::from(sparse_path.clone());
1059
1060                // Verify conversion back and forth works
1061                let back_to_sparse = SparseMerklePath::try_from(converted_merkle.clone()).unwrap();
1062                prop_assert_eq!(sparse_path, back_to_sparse);
1063
1064                // Verify all APIs work identically
1065                prop_assert_eq!(merkle_path.depth(), converted_merkle.depth());
1066
1067                let merkle_root = merkle_path.compute_root(leaf_index, leaf).unwrap();
1068                let converted_root = converted_merkle.compute_root(leaf_index, leaf).unwrap();
1069                prop_assert_eq!(merkle_root, converted_root);
1070            }
1071        }
1072    }
1073}