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
 42// ERRORS
 43// ================================================================================================
 44
 45#[derive(Clone, Debug, PartialEq, Eq)]
 46pub enum MerkleError {
 47    ConflictingRoots(Vec<Word>),
 48    DepthTooSmall(u8),
 49    DepthTooBig(u64),
 50    DuplicateValuesForIndex(u64),
 51    DuplicateValuesForKey(RpoDigest),
 52    InvalidIndex { depth: u8, value: u64 },
 53    InvalidDepth { expected: u8, provided: u8 },
 54    InvalidPath(MerklePath),
 55    InvalidNumEntries(usize, usize),
 56    NodeNotInSet(NodeIndex),
 57    NodeNotInStore(Word, NodeIndex),
 58    NumLeavesNotPowerOfTwo(usize),
 59    RootNotInStore(Word),
 60}
 61
 62impl fmt::Display for MerkleError {
 63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 64        use MerkleError::*;
 65        match self {
 66            ConflictingRoots(roots) => write!(f, "the merkle paths roots do not match {roots:?}"),
 67            DepthTooSmall(depth) => write!(f, "the provided depth {depth} is too small"),
 68            DepthTooBig(depth) => write!(f, "the provided depth {depth} is too big"),
 69            DuplicateValuesForIndex(key) => write!(f, "multiple values provided for key {key}"),
 70            DuplicateValuesForKey(key) => write!(f, "multiple values provided for key {key}"),
 71            InvalidIndex{ depth, value} => write!(
 72                f,
 73                "the index value {value} is not valid for the depth {depth}"
 74            ),
 75            InvalidDepth { expected, provided } => write!(
 76                f,
 77                "the provided depth {provided} is not valid for {expected}"
 78            ),
 79            InvalidPath(_path) => write!(f, "the provided path is not valid"),
 80            InvalidNumEntries(max, provided) => write!(f, "the provided number of entries is {provided}, but the maximum for the given depth is {max}"),
 81            NodeNotInSet(index) => write!(f, "the node with index ({index}) is not in the set"),
 82            NodeNotInStore(hash, index) => write!(f, "the node {hash:?} with index ({index}) is not in the store"),
 83            NumLeavesNotPowerOfTwo(leaves) => {
 84                write!(f, "the leaves count {leaves} is not a power of 2")
 85            }
 86            RootNotInStore(root) => write!(f, "the root {:?} is not in the store", root),
 87        }
 88    }
 89}
 90
 91#[cfg(feature = "std")]
 92impl std::error::Error for MerkleError {}
 93
 94// HELPER FUNCTIONS
 95// ================================================================================================
 96
 97#[cfg(test)]
 98const fn int_to_node(value: u64) -> Word {
 99    [Felt::new(value), ZERO, ZERO, ZERO]
100}