feat: add merkle path wrapper

A Merkle path is a vector of nodes, regardless of the Merkle tree
implementation.

This commit introduces an encapsulation for such vector, also to provide
functionality that is common between different algorithms such as
opening verification.

related issue: #36
This commit is contained in:
Victor Lopez
2023-02-11 12:23:10 +01:00
parent 66da469ec4
commit 21a8cbcb45
6 changed files with 227 additions and 132 deletions

View File

@@ -1,4 +1,4 @@
use super::{BTreeMap, MerkleError, Rpo256, RpoDigest, Vec, Word};
use super::{BTreeMap, MerkleError, MerklePath, Rpo256, RpoDigest, Vec, Word};
#[cfg(test)]
mod tests;
@@ -102,7 +102,7 @@ impl SimpleSmt {
/// Returns an error if:
/// * The specified key does not exist as a branch or leaf node
/// * The specified depth is greater than the depth of the tree.
pub fn get_path(&self, depth: u32, key: u64) -> Result<Vec<Word>, MerkleError> {
pub fn get_path(&self, depth: u32, key: u64) -> Result<MerklePath, MerkleError> {
if depth == 0 {
return Err(MerkleError::DepthTooSmall(depth));
} else if depth > self.depth() {
@@ -124,7 +124,7 @@ impl SimpleSmt {
path.push(sibling_node.into());
curr_key >>= 1;
}
Ok(path)
Ok(path.into())
}
/// Return a Merkle path from the leaf at the specified key to the root. The leaf itself is not
@@ -133,7 +133,7 @@ impl SimpleSmt {
/// # Errors
/// Returns an error if:
/// * The specified key does not exist as a leaf node.
pub fn get_leaf_path(&self, key: u64) -> Result<Vec<Word>, MerkleError> {
pub fn get_leaf_path(&self, key: u64) -> Result<MerklePath, MerkleError> {
self.get_path(self.depth(), key)
}