From 8374e07c4ae66a48e65cc13351024bbff4d5b158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Awirski?= Date: Sat, 3 May 2025 20:33:30 +0200 Subject: [PATCH] combine at_depth and at_depth_nonempty methods --- miden-crypto/src/merkle/sparse_path.rs | 29 ++++++-------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/miden-crypto/src/merkle/sparse_path.rs b/miden-crypto/src/merkle/sparse_path.rs index 4ab24f3..59dd828 100644 --- a/miden-crypto/src/merkle/sparse_path.rs +++ b/miden-crypto/src/merkle/sparse_path.rs @@ -71,33 +71,16 @@ impl SparseMerklePath { return Err(MerkleError::DepthTooBig(node_depth.get().into())); } - let node = self - .at_depth_nonempty(node_depth) - .unwrap_or_else(|| *EmptySubtreeRoots::entry(self.depth(), node_depth.get())); + let node = if self.is_depth_empty(node_depth) { + *EmptySubtreeRoots::entry(self.depth(), node_depth.get()) + } else { + let nonempty_index = self.get_nonempty_index(node_depth); + self.nodes[nonempty_index] + }; Ok(node) } - /// Get a specific non-empty node in this path at a given depth, or `None` if the specified - /// node is an empty node. - fn at_depth_nonempty(&self, node_depth: NonZero) -> Option { - assert!( - node_depth.get() <= self.depth(), - "node depth {} cannot be greater than tree depth {}", - node_depth.get(), - self.depth(), - ); - - if self.is_depth_empty(node_depth) { - return None; - } - - // Our index needs to account for all the empty nodes that aren't in `self.nodes`. - let nonempty_index = self.get_nonempty_index(node_depth); - - Some(self.nodes[nonempty_index]) - } - /// 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.