remove at_idx method

This commit is contained in:
Grzegorz Świrski 2025-05-03 20:47:10 +02:00
parent 5557a89538
commit d3e70cf58b
2 changed files with 1 additions and 43 deletions

View file

@ -48,16 +48,6 @@ impl MerklePath {
self.nodes.get(index as usize).copied()
}
/// Returns a reference to the path node at the specified index, or [None] if the index is out
/// of bounds.
///
/// The node at index 0 is the deepest part of the path.
///
/// This is a checked version of using the indexing operator `[]`.
pub fn at_idx(&self, index: usize) -> Option<RpoDigest> {
self.nodes.get(index).copied()
}
/// Returns the depth in which this Merkle path proof is valid.
pub fn depth(&self) -> u8 {
self.nodes.len() as u8

View file

@ -17,7 +17,7 @@ use super::{
/// maximum of 64 nodes (`SMT_MAX_DEPTH`) can be stored (empty and non-empty). The more nodes in a
/// path are empty, the less memory this struct will use. This type calculates empty nodes on-demand
/// when iterated through, converted to a [MerklePath], or an empty node is retrieved with
/// [`SparseMerklePath::at_idx()`] or [`SparseMerklePath::at_depth()`], which will incur overhead.
/// [`SparseMerklePath::at_depth()`], which will incur overhead.
///
/// NOTE: This type assumes that Merkle paths always span from the root of the tree to a leaf.
/// Partial paths are not supported.
@ -99,28 +99,6 @@ impl SparseMerklePath {
Ok(node)
}
/// Returns the path node at the specified index, or [None] if the index is out of bounds.
///
/// The node at index 0 is the deepest part of the path.
///
/// ```
/// # use core::num::NonZero;
/// # use miden_crypto::{ZERO, ONE, hash::rpo::RpoDigest, merkle::SparseMerklePath};
/// # let zero = RpoDigest::new([ZERO; 4]);
/// # let one = RpoDigest::new([ONE; 4]);
/// # let sparse_path = SparseMerklePath::from_sized_iter(vec![zero, one, one, zero]).unwrap();
/// let depth = NonZero::new(sparse_path.depth()).unwrap();
/// assert_eq!(
/// sparse_path.at_idx(0).unwrap(),
/// sparse_path.at_depth(depth).unwrap(),
/// );
/// ```
pub fn at_idx(&self, index: usize) -> Option<RpoDigest> {
// If this overflows *or* if the depth is zero then the index was out of bounds.
let depth = NonZero::new(u8::checked_sub(self.depth(), index as u8)?)?;
self.at_depth(depth).ok()
}
// PROVIDERS
// ============================================================================================
@ -570,15 +548,6 @@ mod tests {
let sparse_node = sparse_path.at_depth(depth).unwrap();
assert_eq!(control_node, sparse_node, "at depth {depth} for entry {i}");
}
// Test random access by index.
// Letting index get to `control_path.len()` will test that both sides correctly return
// `None` for out of bounds access.
for index in 0..=(control_path.len()) {
let control_node = control_path.at_idx(index);
let sparse_node = sparse_path.at_idx(index);
assert_eq!(control_node, sparse_node);
}
}
}
@ -636,7 +605,6 @@ mod tests {
sparse_path.at_depth(NonZero::new(1).unwrap()),
Err(MerkleError::DepthTooBig(1))
);
assert_eq!(sparse_path.at_idx(0), None);
assert_eq!(sparse_path.iter().next(), None);
assert_eq!(sparse_path.into_iter().next(), None);
}