feat: add `from_elements` to `NodeIndex`
Victor Lopez victor@codx.io
Thu, 16 Feb 2023 21:14:07 +0100
4 files changed,
20 insertions(+),
8 deletions(-)
M
src/merkle/index.rs
→
src/merkle/index.rs
@@ -1,4 +1,4 @@
-use super::RpoDigest; +use super::{Felt, MerkleError, RpoDigest, StarkField}; // NODE INDEX // ================================================================================================@@ -17,6 +17,18 @@
/// Creates a new node index. pub const fn new(depth: u8, value: u64) -> Self { 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<Self, MerkleError> { + 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.
M
src/merkle/merkle_tree.rs
→
src/merkle/merkle_tree.rs
@@ -73,7 +73,7 @@ pub fn get_node(&self, index: NodeIndex) -> Result<Word, MerkleError> {
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 @@ pub fn get_path(&self, mut index: NodeIndex) -> Result<MerklePath, MerkleError> {
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)); }
M
src/merkle/mod.rs
→
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 @@
#[derive(Clone, Debug)] pub enum MerkleError { DepthTooSmall(u8), - DepthTooBig(u8), + DepthTooBig(u64), NumLeavesNotPowerOfTwo(usize), InvalidIndex(NodeIndex), InvalidDepth { expected: u8, provided: u8 },
M
src/merkle/simple_smt/mod.rs
→
src/merkle/simple_smt/mod.rs
@@ -49,7 +49,7 @@ let max = 1 << depth;
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 @@ pub fn get_node(&self, index: &NodeIndex) -> Result<Word, MerkleError> {
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 @@ pub fn get_path(&self, mut index: NodeIndex) -> Result<MerklePath, MerkleError> {
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())));