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