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 entries must be sorted by key in strictly increasing order.
45    #[error(
46        "multiple leaf requires strictly increasing keys but key {next} does not follow key {previous}"
47    )]
48    UnsortedMultipleLeafKeys { previous: Word, next: Word },
49
50    /// Multiple leaf contains more entries than the maximum allowed.
51    #[error(
52        "multiple leaf contains {actual} entries but the maximum allowed is {MAX_LEAF_ENTRIES}"
53    )]
54    TooManyLeafEntries { actual: usize },
55
56    /// Elements could not be decoded into a leaf.
57    #[error("decoding error: {0}")]
58    DecodingError(String),
59}
60
61// SMT PROOF ERROR
62// =================================================================================================
63
64/// Errors that can occur when validating SMT proofs.
65#[derive(Debug, Error, PartialEq, Eq)]
66pub enum SmtProofError {
67    /// The length of the provided Merkle path is not [`SMT_DEPTH`].
68    #[error("merkle path length {0} does not match SMT depth {SMT_DEPTH}")]
69    InvalidMerklePathLength(usize),
70
71    /// The key maps to a different leaf index than the proof's leaf.
72    #[error("key maps to a different leaf index than the proof")]
73    InvalidKeyForProof,
74
75    /// The value does not match the value in the leaf for the given key.
76    #[error("value mismatch: expected {expected}, got {actual}")]
77    ValueMismatch { expected: Word, actual: Word },
78
79    /// The computed root does not match the expected root.
80    #[error("expected merkle root {expected_root} found {actual_root}")]
81    ConflictingRoots { expected_root: Word, actual_root: Word },
82
83    /// The key-value pair exists in the tree.
84    #[error("key-value pair exists in the tree: key {key}, value {value}")]
85    ValuePresent { key: Word, value: Word },
86}