Skip to main content

miden_crypto/merkle/smt/full/
error.rs

1use alloc::string::String;
2
3use thiserror::Error;
4
5use crate::{
6    Word,
7    merkle::smt::{LeafIndex, MAX_LEAF_ENTRIES, SMT_DEPTH},
8};
9
10// SMT LEAF ERROR
11// =================================================================================================
12
13/// Errors that can occur when constructing or validating SMT leaves.
14#[derive(Debug, Error)]
15pub enum SmtLeafError {
16    /// Keys map to different leaf indices in a multiple-leaf structure.
17    #[error(
18        "multiple leaf requires all keys to map to the same leaf index but key1 {key_1} and key2 {key_2} map to different indices"
19    )]
20    /// A single leaf key maps to a different index than expected.
21    InconsistentMultipleLeafKeys { key_1: Word, key_2: Word },
22    #[error(
23        "single leaf key {key} maps to leaf {actual_leaf_index} but was expected to map to leaf {expected_leaf_index}"
24    )]
25    InconsistentSingleLeafIndices {
26        key: Word,
27        expected_leaf_index: LeafIndex<SMT_DEPTH>,
28        actual_leaf_index: LeafIndex<SMT_DEPTH>,
29    },
30
31    /// Supplied leaf index does not match the expected index for the provided keys.
32    #[error(
33        "supplied leaf index {leaf_index_supplied:?} does not match {leaf_index_from_keys:?} for multiple leaf"
34    )]
35    InconsistentMultipleLeafIndices {
36        leaf_index_from_keys: LeafIndex<SMT_DEPTH>,
37        leaf_index_supplied: LeafIndex<SMT_DEPTH>,
38    },
39
40    /// Multiple leaf requires at least two entries, but fewer were provided.
41    #[error("multiple leaf requires at least two entries but only {0} were given")]
42    MultipleLeafRequiresTwoEntries(usize),
43
44    /// Multiple leaf contains more entries than the maximum allowed.
45    #[error(
46        "multiple leaf contains {actual} entries but the maximum allowed is {MAX_LEAF_ENTRIES}"
47    )]
48    TooManyLeafEntries { actual: usize },
49
50    /// Elements could not be decoded into a leaf.
51    #[error("decoding error: {0}")]
52    DecodingError(String),
53}
54
55// SMT PROOF ERROR
56// =================================================================================================
57
58/// Errors that can occur when validating SMT proofs.
59#[derive(Debug, Error, PartialEq, Eq)]
60pub enum SmtProofError {
61    /// The length of the provided Merkle path is not [`SMT_DEPTH`].
62    #[error("merkle path length {0} does not match SMT depth {SMT_DEPTH}")]
63    InvalidMerklePathLength(usize),
64
65    /// The key maps to a different leaf index than the proof's leaf.
66    #[error("key maps to a different leaf index than the proof")]
67    InvalidKeyForProof,
68
69    /// The value does not match the value in the leaf for the given key.
70    #[error("value mismatch: expected {expected}, got {actual}")]
71    ValueMismatch { expected: Word, actual: Word },
72
73    /// The computed root does not match the expected root.
74    #[error("expected merkle root {expected_root} found {actual_root}")]
75    ConflictingRoots { expected_root: Word, actual_root: Word },
76
77    /// The key-value pair exists in the tree.
78    #[error("key-value pair exists in the tree: key {key}, value {value}")]
79    ValuePresent { key: Word, value: Word },
80}