miden-crypto/src/merkle/mod.rs
src/merkle/mod.rsRaw
1use super::{
2 hash::rpo::{Rpo256, RpoDigest},
3 utils::collections::{vec, BTreeMap, BTreeSet, 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
32mod store;
33pub use store::MerkleStore;
34
35// ERRORS
36// ================================================================================================
37
38#[derive(Clone, Debug, PartialEq, Eq)]
39pub enum MerkleError {
40 ConflictingRoots(Vec<Word>),
41 DepthTooSmall(u8),
42 DepthTooBig(u64),
43 NodeNotInStorage(Word, NodeIndex),
44 NumLeavesNotPowerOfTwo(usize),
45 InvalidIndex(NodeIndex),
46 InvalidDepth { expected: u8, provided: u8 },
47 InvalidPath(MerklePath),
48 InvalidEntriesCount(usize, usize),
49 NodeNotInSet(u64),
50}
51
52impl fmt::Display for MerkleError {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 use MerkleError::*;
55 match self {
56 ConflictingRoots(roots) => write!(f, "the merkle paths roots do not match {roots:?}"),
57 DepthTooSmall(depth) => write!(f, "the provided depth {depth} is too small"),
58 DepthTooBig(depth) => write!(f, "the provided depth {depth} is too big"),
59 NumLeavesNotPowerOfTwo(leaves) => {
60 write!(f, "the leaves count {leaves} is not a power of 2")
61 }
62 InvalidIndex(index) => write!(
63 f,
64 "the index value {} is not valid for the depth {}", index.value(), index.depth()
65 ),
66 InvalidDepth { expected, provided } => write!(
67 f,
68 "the provided depth {provided} is not valid for {expected}"
69 ),
70 InvalidPath(_path) => write!(f, "the provided path is not valid"),
71 InvalidEntriesCount(max, provided) => write!(f, "the provided number of entries is {provided}, but the maximum for the given depth is {max}"),
72 NodeNotInSet(index) => write!(f, "the node indexed by {index} is not in the set"),
73 NodeNotInStorage(hash, index) => write!(f, "the node {:?} indexed by {} and depth {} is not in the storage", hash, index.value(), index.depth(),),
74 }
75 }
76}
77
78#[cfg(feature = "std")]
79impl std::error::Error for MerkleError {}
80
81// HELPER FUNCTIONS
82// ================================================================================================
83
84#[cfg(test)]
85const fn int_to_node(value: u64) -> Word {
86 [Felt::new(value), ZERO, ZERO, ZERO]
87}