arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
miden-crypto/src/merkle/error.rs
src/merkle/error.rsRaw
 1use alloc::vec::Vec;
 2use core::fmt;
 3
 4use super::{smt::SmtLeafError, MerklePath, NodeIndex, RpoDigest};
 5
 6#[derive(Clone, Debug, PartialEq, Eq)]
 7pub enum MerkleError {
 8    ConflictingRoots(Vec<RpoDigest>),
 9    DepthTooSmall(u8),
10    DepthTooBig(u64),
11    DuplicateValuesForIndex(u64),
12    DuplicateValuesForKey(RpoDigest),
13    InvalidIndex { depth: u8, value: u64 },
14    InvalidDepth { expected: u8, provided: u8 },
15    InvalidSubtreeDepth { subtree_depth: u8, tree_depth: u8 },
16    InvalidPath(MerklePath),
17    InvalidNumEntries(usize),
18    NodeNotInSet(NodeIndex),
19    NodeNotInStore(RpoDigest, NodeIndex),
20    NumLeavesNotPowerOfTwo(usize),
21    RootNotInStore(RpoDigest),
22    SmtLeaf(SmtLeafError),
23}
24
25impl fmt::Display for MerkleError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        use MerkleError::*;
28        match self {
29            ConflictingRoots(roots) => write!(f, "the merkle paths roots do not match {roots:?}"),
30            DepthTooSmall(depth) => write!(f, "the provided depth {depth} is too small"),
31            DepthTooBig(depth) => write!(f, "the provided depth {depth} is too big"),
32            DuplicateValuesForIndex(key) => write!(f, "multiple values provided for key {key}"),
33            DuplicateValuesForKey(key) => write!(f, "multiple values provided for key {key}"),
34            InvalidIndex { depth, value } => {
35                write!(f, "the index value {value} is not valid for the depth {depth}")
36            },
37            InvalidDepth { expected, provided } => {
38                write!(f, "the provided depth {provided} is not valid for {expected}")
39            },
40            InvalidSubtreeDepth { subtree_depth, tree_depth } => {
41                write!(f, "tried inserting a subtree of depth {subtree_depth} into a tree of depth {tree_depth}")
42            },
43            InvalidPath(_path) => write!(f, "the provided path is not valid"),
44            InvalidNumEntries(max) => write!(f, "number of entries exceeded the maximum: {max}"),
45            NodeNotInSet(index) => write!(f, "the node with index ({index}) is not in the set"),
46            NodeNotInStore(hash, index) => {
47                write!(f, "the node {hash:?} with index ({index}) is not in the store")
48            },
49            NumLeavesNotPowerOfTwo(leaves) => {
50                write!(f, "the leaves count {leaves} is not a power of 2")
51            },
52            RootNotInStore(root) => write!(f, "the root {:?} is not in the store", root),
53            SmtLeaf(smt_leaf_error) => write!(f, "smt leaf error: {smt_leaf_error}"),
54        }
55    }
56}
57
58#[cfg(feature = "std")]
59impl std::error::Error for MerkleError {}
60
61impl From<SmtLeafError> for MerkleError {
62    fn from(value: SmtLeafError) -> Self {
63        Self::SmtLeaf(value)
64    }
65}