arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
miden-crypto/src/merkle/mod.rs
src/merkle/mod.rsRaw
  1use super::{
  2    hash::rpo::{Rpo256, RpoDigest},
  3    utils::collections::{vec, BTreeMap, BTreeSet, Vec},
  4    Felt, StarkField, Word, WORD_SIZE, ZERO,
  5};
  6use core::fmt;
  7
  8// REEXPORTS
  9// ================================================================================================
 10
 11mod empty_roots;
 12pub use empty_roots::EmptySubtreeRoots;
 13
 14mod index;
 15pub use index::NodeIndex;
 16
 17mod merkle_tree;
 18pub use merkle_tree::{path_to_text, tree_to_text, MerkleTree};
 19
 20mod path;
 21pub use path::{MerklePath, RootPath, ValuePath};
 22
 23mod path_set;
 24pub use path_set::MerklePathSet;
 25
 26mod simple_smt;
 27pub use simple_smt::SimpleSmt;
 28
 29mod tiered_smt;
 30pub use tiered_smt::TieredSmt;
 31
 32mod mmr;
 33pub use mmr::{Mmr, MmrPeaks, MmrProof};
 34
 35mod store;
 36pub use store::MerkleStore;
 37
 38mod node;
 39pub use node::InnerNodeInfo;
 40
 41// ERRORS
 42// ================================================================================================
 43
 44#[derive(Clone, Debug, PartialEq, Eq)]
 45pub enum MerkleError {
 46    ConflictingRoots(Vec<RpoDigest>),
 47    DepthTooSmall(u8),
 48    DepthTooBig(u64),
 49    DuplicateValuesForIndex(u64),
 50    DuplicateValuesForKey(RpoDigest),
 51    InvalidIndex { depth: u8, value: u64 },
 52    InvalidDepth { expected: u8, provided: u8 },
 53    InvalidPath(MerklePath),
 54    InvalidNumEntries(usize, usize),
 55    NodeNotInSet(NodeIndex),
 56    NodeNotInStore(RpoDigest, NodeIndex),
 57    NumLeavesNotPowerOfTwo(usize),
 58    RootNotInStore(RpoDigest),
 59}
 60
 61impl fmt::Display for MerkleError {
 62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 63        use MerkleError::*;
 64        match self {
 65            ConflictingRoots(roots) => write!(f, "the merkle paths roots do not match {roots:?}"),
 66            DepthTooSmall(depth) => write!(f, "the provided depth {depth} is too small"),
 67            DepthTooBig(depth) => write!(f, "the provided depth {depth} is too big"),
 68            DuplicateValuesForIndex(key) => write!(f, "multiple values provided for key {key}"),
 69            DuplicateValuesForKey(key) => write!(f, "multiple values provided for key {key}"),
 70            InvalidIndex{ depth, value} => write!(
 71                f,
 72                "the index value {value} is not valid for the depth {depth}"
 73            ),
 74            InvalidDepth { expected, provided } => write!(
 75                f,
 76                "the provided depth {provided} is not valid for {expected}"
 77            ),
 78            InvalidPath(_path) => write!(f, "the provided path is not valid"),
 79            InvalidNumEntries(max, provided) => write!(f, "the provided number of entries is {provided}, but the maximum for the given depth is {max}"),
 80            NodeNotInSet(index) => write!(f, "the node with index ({index}) is not in the set"),
 81            NodeNotInStore(hash, index) => write!(f, "the node {hash:?} with index ({index}) is not in the store"),
 82            NumLeavesNotPowerOfTwo(leaves) => {
 83                write!(f, "the leaves count {leaves} is not a power of 2")
 84            }
 85            RootNotInStore(root) => write!(f, "the root {:?} is not in the store", root),
 86        }
 87    }
 88}
 89
 90#[cfg(feature = "std")]
 91impl std::error::Error for MerkleError {}
 92
 93// HELPER FUNCTIONS
 94// ================================================================================================
 95
 96#[cfg(test)]
 97const fn int_to_node(value: u64) -> RpoDigest {
 98    RpoDigest::new([Felt::new(value), ZERO, ZERO, ZERO])
 99}
100
101#[cfg(test)]
102const fn int_to_leaf(value: u64) -> Word {
103    [Felt::new(value), ZERO, ZERO, ZERO]
104}