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    /// An error propagated from proof-cat-core (transcript, sumcheck, Merkle, multilinear poly).
9    ProofCatCore(proof_cat_core::Error),
10    /// Witness length does not match the wire count.
11    WitnessSizeMismatch {
12        /// The number of wires expected.
13        expected: usize,
14        /// The number of wire values provided.
15        actual: usize,
16    },
17    /// A constraint was not satisfied by the witness.
18    UnsatisfiedConstraint {
19        /// The zero-based index of the failing constraint.
20        index: usize,
21    },
22    /// Constraint set is empty; nothing to prove.
23    EmptyConstraintSet,
24}
25
26impl core::fmt::Display for Error {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        match self {
29            Self::Plonkish(e) => write!(f, "plonkish-cat error: {e}"),
30            Self::ProofCatCore(e) => write!(f, "proof-cat-core error: {e}"),
31            Self::WitnessSizeMismatch { expected, actual } => {
32                write!(
33                    f,
34                    "witness size mismatch: expected {expected}, got {actual}"
35                )
36            }
37            Self::UnsatisfiedConstraint { index } => {
38                write!(f, "constraint {index} not satisfied by witness")
39            }
40            Self::EmptyConstraintSet => write!(f, "constraint set is empty"),
41        }
42    }
43}
44
45impl std::error::Error for Error {
46    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
47        match self {
48            Self::Plonkish(e) => Some(e),
49            Self::ProofCatCore(e) => Some(e),
50            Self::WitnessSizeMismatch { .. }
51            | Self::UnsatisfiedConstraint { .. }
52            | Self::EmptyConstraintSet => None,
53        }
54    }
55}
56
57impl From<plonkish_cat::Error> for Error {
58    fn from(e: plonkish_cat::Error) -> Self {
59        Self::Plonkish(e)
60    }
61}
62
63impl From<proof_cat_core::Error> for Error {
64    fn from(e: proof_cat_core::Error) -> Self {
65        Self::ProofCatCore(e)
66    }
67}