diff --git a/miden-crypto/src/merkle/mmr/tests.rs b/miden-crypto/src/merkle/mmr/tests.rs index e47a7c3..af892f4 100644 --- a/miden-crypto/src/merkle/mmr/tests.rs +++ b/miden-crypto/src/merkle/mmr/tests.rs @@ -497,14 +497,12 @@ fn test_bit_position_iterator() { assert_eq!(TrueBitPositionIterator::new(3).collect::>(), vec![0, 1],); assert_eq!(TrueBitPositionIterator::new(3).rev().collect::>(), vec![1, 0],); - assert_eq!( - TrueBitPositionIterator::new(0b11010101).collect::>(), - vec![0, 2, 4, 6, 7], - ); - assert_eq!( - TrueBitPositionIterator::new(0b11010101).rev().collect::>(), - vec![7, 6, 4, 2, 0], - ); + assert_eq!(TrueBitPositionIterator::new(0b11010101).collect::>(), vec![ + 0, 2, 4, 6, 7 + ],); + assert_eq!(TrueBitPositionIterator::new(0b11010101).rev().collect::>(), vec![ + 7, 6, 4, 2, 0 + ],); } #[test] diff --git a/miden-crypto/src/merkle/partial_mt/mod.rs b/miden-crypto/src/merkle/partial_mt/mod.rs index be7e94c..6698ba9 100644 --- a/miden-crypto/src/merkle/partial_mt/mod.rs +++ b/miden-crypto/src/merkle/partial_mt/mod.rs @@ -200,13 +200,10 @@ impl PartialMerkleTree { pub fn to_paths(&self) -> Vec<(NodeIndex, ValuePath)> { let mut paths = Vec::new(); self.leaves.iter().for_each(|&leaf| { - paths.push(( - leaf, - ValuePath { - value: self.get_node(leaf).expect("Failed to get leaf node"), - path: self.get_path(leaf).expect("Failed to get path"), - }, - )); + paths.push((leaf, ValuePath { + value: self.get_node(leaf).expect("Failed to get leaf node"), + path: self.get_path(leaf).expect("Failed to get path"), + })); }); paths } diff --git a/miden-crypto/src/merkle/partial_mt/tests.rs b/miden-crypto/src/merkle/partial_mt/tests.rs index 6723ba1..0f57ab5 100644 --- a/miden-crypto/src/merkle/partial_mt/tests.rs +++ b/miden-crypto/src/merkle/partial_mt/tests.rs @@ -215,13 +215,10 @@ fn get_paths() { let expected_paths: Vec<(NodeIndex, ValuePath)> = leaves .iter() .map(|&leaf| { - ( - leaf, - ValuePath { - value: mt.get_node(leaf).unwrap(), - path: mt.get_path(leaf).unwrap(), - }, - ) + (leaf, ValuePath { + value: mt.get_node(leaf).unwrap(), + path: mt.get_path(leaf).unwrap(), + }) }) .collect(); diff --git a/miden-crypto/src/merkle/smt/full/mod.rs b/miden-crypto/src/merkle/smt/full/mod.rs index a32a525..913d56a 100644 --- a/miden-crypto/src/merkle/smt/full/mod.rs +++ b/miden-crypto/src/merkle/smt/full/mod.rs @@ -159,10 +159,24 @@ impl Smt { } /// Returns the number of non-empty leaves in this tree. + /// + /// Note that this may return a different value from [Self::num_entries()] as a single leaf may + /// contain more than one key-value pair. pub fn num_leaves(&self) -> usize { self.leaves.len() } + /// Returns the number of key-value pairs with non-default values in this tree. + /// + /// Note that this may return a different value from [Self::num_leaves()] as a single leaf may + /// contain more than one key-value pair. + /// + /// Also note that this is currently an expensive operation is counting the number of entries + /// requires iterating over all leaves of the tree. + pub fn num_entries(&self) -> usize { + self.entries().count() + } + /// Returns the leaf to which `key` maps pub fn get_leaf(&self, key: &RpoDigest) -> SmtLeaf { >::get_leaf(self, key) diff --git a/miden-crypto/src/merkle/smt/full/tests.rs b/miden-crypto/src/merkle/smt/full/tests.rs index d793a27..d050c40 100644 --- a/miden-crypto/src/merkle/smt/full/tests.rs +++ b/miden-crypto/src/merkle/smt/full/tests.rs @@ -661,10 +661,12 @@ fn test_empty_smt_leaf_serialization() { #[test] fn test_single_smt_leaf_serialization() { - let single_leaf = SmtLeaf::new_single( - RpoDigest::from([10_u32, 11_u32, 12_u32, 13_u32]), - [1_u32.into(), 2_u32.into(), 3_u32.into(), 4_u32.into()], - ); + let single_leaf = SmtLeaf::new_single(RpoDigest::from([10_u32, 11_u32, 12_u32, 13_u32]), [ + 1_u32.into(), + 2_u32.into(), + 3_u32.into(), + 4_u32.into(), + ]); let mut serialized = single_leaf.to_bytes(); // extend buffer with random bytes @@ -677,14 +679,18 @@ fn test_single_smt_leaf_serialization() { #[test] fn test_multiple_smt_leaf_serialization_success() { let multiple_leaf = SmtLeaf::new_multiple(vec![ - ( - RpoDigest::from([10_u32, 11_u32, 12_u32, 13_u32]), - [1_u32.into(), 2_u32.into(), 3_u32.into(), 4_u32.into()], - ), - ( - RpoDigest::from([100_u32, 101_u32, 102_u32, 13_u32]), - [11_u32.into(), 12_u32.into(), 13_u32.into(), 14_u32.into()], - ), + (RpoDigest::from([10_u32, 11_u32, 12_u32, 13_u32]), [ + 1_u32.into(), + 2_u32.into(), + 3_u32.into(), + 4_u32.into(), + ]), + (RpoDigest::from([100_u32, 101_u32, 102_u32, 13_u32]), [ + 11_u32.into(), + 12_u32.into(), + 13_u32.into(), + 14_u32.into(), + ]), ]) .unwrap();