From 9307178873cd5fb950b58ce36b5fe02ea8df6e94 Mon Sep 17 00:00:00 2001 From: Victor Lopez Date: Thu, 16 Feb 2023 21:14:07 +0100 Subject: [PATCH] feat: add `from_elements` to `NodeIndex` --- src/merkle/index.rs | 14 +++++++++++++- src/merkle/merkle_tree.rs | 4 ++-- src/merkle/mod.rs | 4 ++-- src/merkle/simple_smt/mod.rs | 6 +++--- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/merkle/index.rs b/src/merkle/index.rs index 270bbbe..95d7123 100644 --- a/src/merkle/index.rs +++ b/src/merkle/index.rs @@ -1,4 +1,4 @@ -use super::RpoDigest; +use super::{Felt, MerkleError, RpoDigest, StarkField}; // NODE INDEX // ================================================================================================ @@ -19,6 +19,18 @@ impl NodeIndex { Self { depth, value } } + /// Creates a node index from a pair of field elements representing the depth and value. + /// + /// # Errors + /// + /// Will error if the `u64` representation of the depth doesn't fit a `u8`. + pub fn from_elements(depth: &Felt, value: &Felt) -> Result { + let depth = depth.as_int(); + let depth = u8::try_from(depth).map_err(|_| MerkleError::DepthTooBig(depth))?; + let value = value.as_int(); + Ok(Self::new(depth, value)) + } + /// Creates a new node index pointing to the root of the tree. pub const fn root() -> Self { Self { depth: 0, value: 0 } diff --git a/src/merkle/merkle_tree.rs b/src/merkle/merkle_tree.rs index 01c5072..71cdf5c 100644 --- a/src/merkle/merkle_tree.rs +++ b/src/merkle/merkle_tree.rs @@ -73,7 +73,7 @@ impl MerkleTree { if index.is_root() { return Err(MerkleError::DepthTooSmall(index.depth())); } else if index.depth() > self.depth() { - return Err(MerkleError::DepthTooBig(index.depth())); + return Err(MerkleError::DepthTooBig(index.depth() as u64)); } else if !index.is_valid() { return Err(MerkleError::InvalidIndex(index)); } @@ -93,7 +93,7 @@ impl MerkleTree { if index.is_root() { return Err(MerkleError::DepthTooSmall(index.depth())); } else if index.depth() > self.depth() { - return Err(MerkleError::DepthTooBig(index.depth())); + return Err(MerkleError::DepthTooBig(index.depth() as u64)); } else if !index.is_valid() { return Err(MerkleError::InvalidIndex(index)); } diff --git a/src/merkle/mod.rs b/src/merkle/mod.rs index 04550f7..0b82752 100644 --- a/src/merkle/mod.rs +++ b/src/merkle/mod.rs @@ -1,7 +1,7 @@ use super::{ hash::rpo::{Rpo256, RpoDigest}, utils::collections::{vec, BTreeMap, Vec}, - Felt, Word, ZERO, + Felt, StarkField, Word, ZERO, }; use core::fmt; @@ -26,7 +26,7 @@ pub use simple_smt::SimpleSmt; #[derive(Clone, Debug)] pub enum MerkleError { DepthTooSmall(u8), - DepthTooBig(u8), + DepthTooBig(u64), NumLeavesNotPowerOfTwo(usize), InvalidIndex(NodeIndex), InvalidDepth { expected: u8, provided: u8 }, diff --git a/src/merkle/simple_smt/mod.rs b/src/merkle/simple_smt/mod.rs index 28623c8..186ac25 100644 --- a/src/merkle/simple_smt/mod.rs +++ b/src/merkle/simple_smt/mod.rs @@ -49,7 +49,7 @@ impl SimpleSmt { if depth < Self::MIN_DEPTH { return Err(MerkleError::DepthTooSmall(depth)); } else if Self::MAX_DEPTH < depth { - return Err(MerkleError::DepthTooBig(depth)); + return Err(MerkleError::DepthTooBig(depth as u64)); } else if entries.len() > max { return Err(MerkleError::InvalidEntriesCount(max, entries.len())); } @@ -86,7 +86,7 @@ impl SimpleSmt { if index.is_root() { Err(MerkleError::DepthTooSmall(index.depth())) } else if index.depth() > self.depth() { - Err(MerkleError::DepthTooBig(index.depth())) + Err(MerkleError::DepthTooBig(index.depth() as u64)) } else if index.depth() == self.depth() { self.store.get_leaf_node(index.value()) } else { @@ -106,7 +106,7 @@ impl SimpleSmt { if index.is_root() { return Err(MerkleError::DepthTooSmall(index.depth())); } else if index.depth() > self.depth() { - return Err(MerkleError::DepthTooBig(index.depth())); + return Err(MerkleError::DepthTooBig(index.depth() as u64)); } else if index.depth() == self.depth() && !self.store.check_leaf_node_exists(index.value()) { return Err(MerkleError::InvalidIndex(index.with_depth(self.depth())));