Skip to main content

miden_crypto/merkle/
error.rs

1use alloc::string::String;
2
3use thiserror::Error;
4
5use super::{NodeIndex, Word, smt::MAX_LEAF_ENTRIES};
6
7#[derive(Debug, Error)]
8pub enum MerkleError {
9    #[error("expected merkle root {expected_root} found {actual_root}")]
10    ConflictingRoots { expected_root: Word, actual_root: Word },
11    #[error("provided merkle tree depth {0} is too small")]
12    DepthTooSmall(u8),
13    #[error("provided merkle tree depth {0} is too big")]
14    DepthTooBig(u64),
15    #[error("multiple values provided for merkle tree index {0}")]
16    DuplicateValuesForIndex(u64),
17    #[error("entry {node} is not a leaf")]
18    EntryIsNotLeaf { node: NodeIndex },
19    #[error("node index value {value} is not valid for depth {depth}")]
20    InvalidNodeIndex { depth: u8, value: u64 },
21    #[error("provided node index depth {provided} does not match expected depth {expected}")]
22    InvalidNodeIndexDepth { expected: u8, provided: u8 },
23    #[error("provided node list should have a minimum length of {0}")]
24    InvalidPathLength(usize),
25    #[error("merkle subtree depth {subtree_depth} exceeds merkle tree depth {tree_depth}")]
26    SubtreeDepthExceedsDepth { subtree_depth: u8, tree_depth: u8 },
27    #[error("number of entries in the merkle tree exceeds the maximum of 2^{0}")]
28    TooManyEntries(u8),
29    #[error("number of entries in a leaf ({actual}) exceeds the maximum of ({MAX_LEAF_ENTRIES})")]
30    TooManyLeafEntries { actual: usize },
31    #[error("node index `{0}` not found in the tree")]
32    NodeIndexNotFoundInTree(NodeIndex),
33    #[error("node {0:?} with index `{1}` not found in the store")]
34    NodeIndexNotFoundInStore(Word, NodeIndex),
35    #[error("number of provided merkle tree leaves {0} is not a power of two")]
36    NumLeavesNotPowerOfTwo(usize),
37    #[error("root {0:?} is not in the store")]
38    RootNotInStore(Word),
39    #[error("partial smt does not track the merkle path for key {0}")]
40    UntrackedKey(Word),
41    #[error("internal error: {0}")]
42    InternalError(String),
43}
44
45#[cfg(feature = "concurrent")]
46impl From<crate::merkle::smt::LargeSmtError> for MerkleError {
47    fn from(err: crate::merkle::smt::LargeSmtError) -> Self {
48        use alloc::string::ToString;
49
50        match err {
51            crate::merkle::smt::LargeSmtError::Merkle(me) => me,
52            crate::merkle::smt::LargeSmtError::Storage(se) => {
53                MerkleError::InternalError(se.to_string())
54            },
55            crate::merkle::smt::LargeSmtError::RootMismatch { expected, actual } => {
56                MerkleError::ConflictingRoots {
57                    expected_root: expected,
58                    actual_root: actual,
59                }
60            },
61            crate::merkle::smt::LargeSmtError::StorageNotEmpty => {
62                MerkleError::InternalError("storage is not empty".into())
63            },
64        }
65    }
66}