Skip to main content

machine_cat/
error.rs

1//! Project-wide error type.
2
3/// All errors that can arise in machine-cat.
4#[derive(Debug)]
5pub enum Error {
6    /// An error propagated from proof-cat.
7    ProofCat(proof_cat::Error),
8    /// An error propagated from plonkish-cat.
9    Plonkish(plonkish_cat::Error),
10    /// Column index out of bounds.
11    ColumnOutOfBounds {
12        /// The column index that was accessed.
13        index: usize,
14        /// The total number of columns.
15        column_count: usize,
16    },
17    /// Trace has zero rows.
18    EmptyTrace,
19    /// Trace column count does not match the AIR's column count.
20    ColumnCountMismatch {
21        /// The expected column count (from the AIR).
22        expected: usize,
23        /// The actual column count (from the trace).
24        actual: usize,
25    },
26    /// Trace row count is not a power of two.
27    TraceNotPowerOfTwo {
28        /// The row count.
29        row_count: usize,
30    },
31    /// An AIR constraint was not satisfied at a row pair.
32    UnsatisfiedAirConstraint {
33        /// The row index of the failing pair.
34        row: usize,
35    },
36    /// AIR has no constraints.
37    NoConstraints,
38    /// Trace has fewer than 2 rows.
39    InsufficientRows {
40        /// The row count.
41        row_count: usize,
42    },
43    /// Row length does not match column count.
44    RowLengthMismatch {
45        /// The row index.
46        row: usize,
47        /// The expected length.
48        expected: usize,
49        /// The actual length.
50        actual: usize,
51    },
52}
53
54impl core::fmt::Display for Error {
55    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
56        match self {
57            Self::ProofCat(e) => write!(f, "proof-cat error: {e}"),
58            Self::Plonkish(e) => write!(f, "plonkish-cat error: {e}"),
59            Self::ColumnOutOfBounds {
60                index,
61                column_count,
62            } => write!(
63                f,
64                "column index {index} out of bounds (column count: {column_count})"
65            ),
66            Self::EmptyTrace => write!(f, "trace has zero rows"),
67            Self::ColumnCountMismatch { expected, actual } => {
68                write!(
69                    f,
70                    "column count mismatch: expected {expected}, got {actual}"
71                )
72            }
73            Self::TraceNotPowerOfTwo { row_count } => {
74                write!(f, "trace row count {row_count} is not a power of two")
75            }
76            Self::UnsatisfiedAirConstraint { row } => {
77                write!(f, "AIR constraint not satisfied at row pair ({row}, {})", row + 1)
78            }
79            Self::NoConstraints => write!(f, "AIR has no constraints"),
80            Self::InsufficientRows { row_count } => {
81                write!(f, "trace has {row_count} rows, need at least 2")
82            }
83            Self::RowLengthMismatch {
84                row,
85                expected,
86                actual,
87            } => write!(
88                f,
89                "row {row} has {actual} elements, expected {expected}"
90            ),
91        }
92    }
93}
94
95impl std::error::Error for Error {
96    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
97        match self {
98            Self::ProofCat(e) => Some(e),
99            Self::Plonkish(e) => Some(e),
100            Self::ColumnOutOfBounds { .. }
101            | Self::EmptyTrace
102            | Self::ColumnCountMismatch { .. }
103            | Self::TraceNotPowerOfTwo { .. }
104            | Self::UnsatisfiedAirConstraint { .. }
105            | Self::NoConstraints
106            | Self::InsufficientRows { .. }
107            | Self::RowLengthMismatch { .. } => None,
108        }
109    }
110}
111
112impl From<proof_cat::Error> for Error {
113    fn from(e: proof_cat::Error) -> Self {
114        Self::ProofCat(e)
115    }
116}
117
118impl From<plonkish_cat::Error> for Error {
119    fn from(e: plonkish_cat::Error) -> Self {
120        Self::Plonkish(e)
121    }
122}