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