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