Skip to main content

proof_cat/
error.rs

1//! Project-wide error type.
2
3/// All errors that can arise in proof-cat.
4#[derive(Debug)]
5pub enum Error {
6    /// An error propagated from plonkish-cat.
7    Plonkish(plonkish_cat::Error),
8    /// Witness length does not match the wire count.
9    WitnessSizeMismatch {
10        /// The number of wires expected.
11        expected: usize,
12        /// The number of wire values provided.
13        actual: usize,
14    },
15    /// A constraint was not satisfied by the witness.
16    UnsatisfiedConstraint {
17        /// The zero-based index of the failing constraint.
18        index: usize,
19    },
20    /// Sumcheck round count does not match the number of variables.
21    RoundCountMismatch {
22        /// The expected number of rounds.
23        expected: usize,
24        /// The actual number of rounds in the proof.
25        actual: usize,
26    },
27    /// Sumcheck final evaluation check failed.
28    SumcheckFinalMismatch,
29    /// Merkle opening proof does not match the committed root.
30    MerkleVerificationFailed,
31    /// Polynomial evaluation point has wrong dimension.
32    DimensionMismatch {
33        /// The expected number of variables.
34        expected: usize,
35        /// The actual dimension of the point.
36        actual: usize,
37    },
38    /// Constraint set is empty; nothing to prove.
39    EmptyConstraintSet,
40    /// A value is not a power of two where one was required.
41    NotPowerOfTwo {
42        /// The non-power-of-two value.
43        value: usize,
44    },
45    /// Field element could not be decoded from bytes.
46    InvalidFieldEncoding,
47    /// Leaf index out of bounds for the Merkle tree.
48    LeafIndexOutOfBounds {
49        /// The requested index.
50        index: usize,
51        /// The number of leaves.
52        leaf_count: usize,
53    },
54}
55
56impl core::fmt::Display for Error {
57    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58        match self {
59            Self::Plonkish(e) => write!(f, "plonkish-cat error: {e}"),
60            Self::WitnessSizeMismatch { expected, actual } => {
61                write!(f, "witness size mismatch: expected {expected}, got {actual}")
62            }
63            Self::UnsatisfiedConstraint { index } => {
64                write!(f, "constraint {index} not satisfied by witness")
65            }
66            Self::RoundCountMismatch { expected, actual } => {
67                write!(
68                    f,
69                    "sumcheck round count mismatch: expected {expected}, got {actual}"
70                )
71            }
72            Self::SumcheckFinalMismatch => {
73                write!(f, "sumcheck final evaluation does not match claim")
74            }
75            Self::MerkleVerificationFailed => {
76                write!(f, "Merkle opening verification failed")
77            }
78            Self::DimensionMismatch { expected, actual } => {
79                write!(
80                    f,
81                    "dimension mismatch: expected {expected} variables, got {actual}"
82                )
83            }
84            Self::EmptyConstraintSet => write!(f, "constraint set is empty"),
85            Self::NotPowerOfTwo { value } => {
86                write!(f, "{value} is not a power of two")
87            }
88            Self::InvalidFieldEncoding => write!(f, "invalid field element encoding"),
89            Self::LeafIndexOutOfBounds { index, leaf_count } => {
90                write!(
91                    f,
92                    "leaf index {index} out of bounds (tree has {leaf_count} leaves)"
93                )
94            }
95        }
96    }
97}
98
99impl std::error::Error for Error {
100    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
101        match self {
102            Self::Plonkish(e) => Some(e),
103            Self::WitnessSizeMismatch { .. }
104            | Self::UnsatisfiedConstraint { .. }
105            | Self::RoundCountMismatch { .. }
106            | Self::SumcheckFinalMismatch
107            | Self::MerkleVerificationFailed
108            | Self::DimensionMismatch { .. }
109            | Self::EmptyConstraintSet
110            | Self::NotPowerOfTwo { .. }
111            | Self::InvalidFieldEncoding
112            | Self::LeafIndexOutOfBounds { .. } => None,
113        }
114    }
115}
116
117impl From<plonkish_cat::Error> for Error {
118    fn from(e: plonkish_cat::Error) -> Self {
119        Self::Plonkish(e)
120    }
121}