feat: add from_elements
to NodeIndex
This commit is contained in:
parent
3af53e63cf
commit
9307178873
4 changed files with 20 additions and 8 deletions
|
@ -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<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.
|
||||
pub const fn root() -> Self {
|
||||
Self { depth: 0, value: 0 }
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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 },
|
||||
|
|
|
@ -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())));
|
||||
|
|
Loading…
Add table
Reference in a new issue