From 901816f139549d566b19fd6f1c96f16a5fc41d22 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Tue, 25 Feb 2025 21:02:08 +0100 Subject: [PATCH] WIP: smt: factor out MerklePath logic --- src/merkle/smt/mod.rs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/merkle/smt/mod.rs b/src/merkle/smt/mod.rs index 5d8c933..b02aa00 100644 --- a/src/merkle/smt/mod.rs +++ b/src/merkle/smt/mod.rs @@ -79,28 +79,32 @@ pub(crate) trait SparseMerkleTree { // PROVIDED METHODS // --------------------------------------------------------------------------------------------- - /// Returns an opening of the leaf associated with `key`. Conceptually, an opening is a Merkle - /// path to the leaf, as well as the leaf itself. - fn open(&self, key: &Self::Key) -> Self::Opening { - let leaf = self.get_leaf(key); - + /// Returns a [`MerklePath`] to the specified key. + /// + /// Mostly this is an implementation detail of [`Self::open()`]. + fn path(&self, key: &Self::Key) -> MerklePath { let mut index: NodeIndex = { let leaf_index: LeafIndex = Self::key_to_leaf_index(key); leaf_index.into() }; - let merkle_path = { - let mut path = Vec::with_capacity(index.depth() as usize); - for _ in 0..index.depth() { - let is_right = index.is_value_odd(); - index.move_up(); - let InnerNode { left, right } = self.get_inner_node(index); - let value = if is_right { left } else { right }; - path.push(value); - } + let mut path = Vec::with_capacity(index.depth() as usize); + for _ in 0..index.depth() { + let is_right = index.is_value_odd(); + index.move_up(); + let InnerNode { left, right } = self.get_inner_node(index); + let value = if is_right { left } else { right }; + path.push(value); + } - MerklePath::new(path) - }; + MerklePath::new(path) + } + + /// Returns an opening of the leaf associated with `key`. Conceptually, an opening is a Merkle + /// path to the leaf, as well as the leaf itself. + fn open(&self, key: &Self::Key) -> Self::Opening { + let leaf = self.get_leaf(key); + let merkle_path = self.path(key); Self::path_and_leaf_to_opening(merkle_path, leaf) }