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