keep at_depth and at_idx consistent wrt ownership

This commit is contained in:
Grzegorz Świrski 2025-04-16 10:26:46 +02:00 committed by Qyriad
parent 5f62212b3d
commit 1c5eae29c0
2 changed files with 6 additions and 6 deletions

View file

@ -43,9 +43,9 @@ impl MerklePath {
/// The `depth` parameter is defined in terms of `self.depth()`. Merkle paths conventionally do /// The `depth` parameter is defined in terms of `self.depth()`. Merkle paths conventionally do
/// not include the root, so the shallowest depth is `1`, and the deepest depth is /// not include the root, so the shallowest depth is `1`, and the deepest depth is
/// `self.depth()`. /// `self.depth()`.
pub fn at_depth(&self, depth: NonZero<u8>) -> Option<&RpoDigest> { pub fn at_depth(&self, depth: NonZero<u8>) -> Option<RpoDigest> {
let index = u8::checked_sub(self.depth(), depth.get())?; let index = u8::checked_sub(self.depth(), depth.get())?;
self.nodes.get(index as usize) 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 /// Returns a reference to the path node at the specified index, or [None] if the index is out
@ -54,8 +54,8 @@ impl MerklePath {
/// The node at index 0 is the deepest part of the path. /// The node at index 0 is the deepest part of the path.
/// ///
/// This is a checked version of using the indexing operator `[]`. /// This is a checked version of using the indexing operator `[]`.
pub fn at_idx(&self, index: usize) -> Option<&RpoDigest> { pub fn at_idx(&self, index: usize) -> Option<RpoDigest> {
self.nodes.get(index) self.nodes.get(index).copied()
} }
/// Returns the depth in which this Merkle path proof is valid. /// Returns the depth in which this Merkle path proof is valid.

View file

@ -760,7 +760,7 @@ mod tests {
// Test random access by depth. // Test random access by depth.
for depth in path_depth_iter(control_path.depth()) { for depth in path_depth_iter(control_path.depth()) {
let &control_node = control_path.at_depth(depth).unwrap(); let control_node = control_path.at_depth(depth).unwrap();
let sparse_node = sparse_path.at_depth(depth).unwrap(); let sparse_node = sparse_path.at_depth(depth).unwrap();
assert_eq!(control_node, sparse_node, "at depth {depth} for entry {i}"); assert_eq!(control_node, sparse_node, "at depth {depth} for entry {i}");
} }
@ -769,7 +769,7 @@ mod tests {
// Letting index get to `control_path.len()` will test that both sides correctly return // Letting index get to `control_path.len()` will test that both sides correctly return
// `None` for out of bounds access. // `None` for out of bounds access.
for index in 0..=(control_path.len()) { for index in 0..=(control_path.len()) {
let control_node = control_path.at_idx(index).copied(); let control_node = control_path.at_idx(index);
let sparse_node = sparse_path.at_idx(index); let sparse_node = sparse_path.at_idx(index);
assert_eq!(control_node, sparse_node); assert_eq!(control_node, sparse_node);
} }