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