Browse Source

Merge pull request #62 from 0xPolygonMiden/vlopes11-feat-add-node-index-from-felt

feat: add `from_elements` to `NodeIndex`
al-gkr-basic-workflow
Victor Lopes 2 years ago
committed by GitHub
parent
commit
b1a5ed6b5d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 8 deletions
  1. +13
    -1
      src/merkle/index.rs
  2. +2
    -2
      src/merkle/merkle_tree.rs
  3. +2
    -2
      src/merkle/mod.rs
  4. +3
    -3
      src/merkle/simple_smt/mod.rs

+ 13
- 1
src/merkle/index.rs

@ -1,4 +1,4 @@
use super::RpoDigest;
use super::{Felt, MerkleError, RpoDigest, StarkField};
// NODE INDEX // NODE INDEX
// ================================================================================================ // ================================================================================================
@ -19,6 +19,18 @@ impl NodeIndex {
Self { depth, value } 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. /// Creates a new node index pointing to the root of the tree.
pub const fn root() -> Self { pub const fn root() -> Self {
Self { depth: 0, value: 0 } Self { depth: 0, value: 0 }

+ 2
- 2
src/merkle/merkle_tree.rs

@ -73,7 +73,7 @@ impl MerkleTree {
if index.is_root() { if index.is_root() {
return Err(MerkleError::DepthTooSmall(index.depth())); return Err(MerkleError::DepthTooSmall(index.depth()));
} else if index.depth() > self.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() { } else if !index.is_valid() {
return Err(MerkleError::InvalidIndex(index)); return Err(MerkleError::InvalidIndex(index));
} }
@ -93,7 +93,7 @@ impl MerkleTree {
if index.is_root() { if index.is_root() {
return Err(MerkleError::DepthTooSmall(index.depth())); return Err(MerkleError::DepthTooSmall(index.depth()));
} else if index.depth() > self.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() { } else if !index.is_valid() {
return Err(MerkleError::InvalidIndex(index)); return Err(MerkleError::InvalidIndex(index));
} }

+ 2
- 2
src/merkle/mod.rs

@ -1,7 +1,7 @@
use super::{ use super::{
hash::rpo::{Rpo256, RpoDigest}, hash::rpo::{Rpo256, RpoDigest},
utils::collections::{vec, BTreeMap, Vec}, utils::collections::{vec, BTreeMap, Vec},
Felt, Word, ZERO,
Felt, StarkField, Word, ZERO,
}; };
use core::fmt; use core::fmt;
@ -26,7 +26,7 @@ pub use simple_smt::SimpleSmt;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum MerkleError { pub enum MerkleError {
DepthTooSmall(u8), DepthTooSmall(u8),
DepthTooBig(u8),
DepthTooBig(u64),
NumLeavesNotPowerOfTwo(usize), NumLeavesNotPowerOfTwo(usize),
InvalidIndex(NodeIndex), InvalidIndex(NodeIndex),
InvalidDepth { expected: u8, provided: u8 }, InvalidDepth { expected: u8, provided: u8 },

+ 3
- 3
src/merkle/simple_smt/mod.rs

@ -49,7 +49,7 @@ impl SimpleSmt {
if depth < Self::MIN_DEPTH { if depth < Self::MIN_DEPTH {
return Err(MerkleError::DepthTooSmall(depth)); return Err(MerkleError::DepthTooSmall(depth));
} else if Self::MAX_DEPTH < depth { } else if Self::MAX_DEPTH < depth {
return Err(MerkleError::DepthTooBig(depth));
return Err(MerkleError::DepthTooBig(depth as u64));
} else if entries.len() > max { } else if entries.len() > max {
return Err(MerkleError::InvalidEntriesCount(max, entries.len())); return Err(MerkleError::InvalidEntriesCount(max, entries.len()));
} }
@ -86,7 +86,7 @@ impl SimpleSmt {
if index.is_root() { if index.is_root() {
Err(MerkleError::DepthTooSmall(index.depth())) Err(MerkleError::DepthTooSmall(index.depth()))
} else if index.depth() > self.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() { } else if index.depth() == self.depth() {
self.store.get_leaf_node(index.value()) self.store.get_leaf_node(index.value())
} else { } else {
@ -106,7 +106,7 @@ impl SimpleSmt {
if index.is_root() { if index.is_root() {
return Err(MerkleError::DepthTooSmall(index.depth())); return Err(MerkleError::DepthTooSmall(index.depth()));
} else if index.depth() > self.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()) } else if index.depth() == self.depth() && !self.store.check_leaf_node_exists(index.value())
{ {
return Err(MerkleError::InvalidIndex(index.with_depth(self.depth()))); return Err(MerkleError::InvalidIndex(index.with_depth(self.depth())));

Loading…
Cancel
Save