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, RootPath, ValuePath};
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 NodeNotInStore(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 RootNotInStore(Word),
51}
52
53impl fmt::Display for MerkleError {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 use MerkleError::*;
56 match self {
57 ConflictingRoots(roots) => write!(f, "the merkle paths roots do not match {roots:?}"),
58 DepthTooSmall(depth) => write!(f, "the provided depth {depth} is too small"),
59 DepthTooBig(depth) => write!(f, "the provided depth {depth} is too big"),
60 NumLeavesNotPowerOfTwo(leaves) => {
61 write!(f, "the leaves count {leaves} is not a power of 2")
62 }
63 InvalidIndex(index) => write!(
64 f,
65 "the index value {} is not valid for the depth {}", index.value(), index.depth()
66 ),
67 InvalidDepth { expected, provided } => write!(
68 f,
69 "the provided depth {provided} is not valid for {expected}"
70 ),
71 InvalidPath(_path) => write!(f, "the provided path is not valid"),
72 InvalidEntriesCount(max, provided) => write!(f, "the provided number of entries is {provided}, but the maximum for the given depth is {max}"),
73 NodeNotInSet(index) => write!(f, "the node indexed by {index} is not in the set"),
74 NodeNotInStore(hash, index) => write!(f, "the node {:?} indexed by {} and depth {} is not in the store", hash, index.value(), index.depth(),),
75 RootNotInStore(root) => write!(f, "the root {:?} is not in the store", root),
76 }
77 }
78}
79
80#[cfg(feature = "std")]
81impl std::error::Error for MerkleError {}
82
83// HELPER FUNCTIONS
84// ================================================================================================
85
86#[cfg(test)]
87const fn int_to_node(value: u64) -> Word {
88 [Felt::new(value), ZERO, ZERO, ZERO]
89}