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