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, Vec},
 4    Felt, StarkField, Word, ZERO,
 5};
 6use core::fmt;
 7
 8// REEXPORTS
 9// ================================================================================================
10mod index;
11pub use index::NodeIndex;
12
13mod merkle_tree;
14pub use merkle_tree::MerkleTree;
15
16mod path;
17pub use path::MerklePath;
18
19mod path_set;
20pub use path_set::MerklePathSet;
21
22mod simple_smt;
23pub use simple_smt::SimpleSmt;
24
25mod mmr;
26pub use mmr::{Mmr, MmrPeaks};
27
28// ERRORS
29// ================================================================================================
30
31#[derive(Clone, Debug)]
32pub enum MerkleError {
33    DepthTooSmall(u8),
34    DepthTooBig(u64),
35    NumLeavesNotPowerOfTwo(usize),
36    InvalidIndex(NodeIndex),
37    InvalidDepth { expected: u8, provided: u8 },
38    InvalidPath(MerklePath),
39    InvalidEntriesCount(usize, usize),
40    NodeNotInSet(u64),
41}
42
43impl fmt::Display for MerkleError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        use MerkleError::*;
46        match self {
47            DepthTooSmall(depth) => write!(f, "the provided depth {depth} is too small"),
48            DepthTooBig(depth) => write!(f, "the provided depth {depth} is too big"),
49            NumLeavesNotPowerOfTwo(leaves) => {
50                write!(f, "the leaves count {leaves} is not a power of 2")
51            }
52            InvalidIndex(index) => write!(
53                f,
54                "the index value {} is not valid for the depth {}", index.value(), index.depth()
55            ),
56            InvalidDepth { expected, provided } => write!(
57                f,
58                "the provided depth {provided} is not valid for {expected}"
59            ),
60            InvalidPath(_path) => write!(f, "the provided path is not valid"),
61            InvalidEntriesCount(max, provided) => write!(f, "the provided number of entries is {provided}, but the maximum for the given depth is {max}"),
62            NodeNotInSet(index) => write!(f, "the node indexed by {index} is not in the set"),
63        }
64    }
65}
66
67#[cfg(feature = "std")]
68impl std::error::Error for MerkleError {}
69
70// HELPER FUNCTIONS
71// ================================================================================================
72
73#[cfg(test)]
74const fn int_to_node(value: u64) -> Word {
75    [Felt::new(value), ZERO, ZERO, ZERO]
76}