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-core.
7    ProofCatCore(proof_cat_core::Error),
8    /// An error propagated from plonkish-cat.
9    Plonkish(plonkish_cat::Error),
10    /// An error propagated from field-cat (field arithmetic or byte encoding).
11    FieldCat(field_cat::Error),
12    /// Column index out of bounds.
13    ColumnOutOfBounds {
14        /// The column index that was accessed.
15        index: usize,
16        /// The total number of columns.
17        column_count: usize,
18    },
19    /// Trace has zero rows.
20    EmptyTrace,
21    /// Trace column count does not match the AIR's column count.
22    ColumnCountMismatch {
23        /// The expected column count (from the AIR).
24        expected: usize,
25        /// The actual column count (from the trace).
26        actual: usize,
27    },
28    /// Trace row count is not a power of two.
29    TraceNotPowerOfTwo {
30        /// The row count.
31        row_count: usize,
32    },
33    /// An AIR constraint was not satisfied at a row pair.
34    UnsatisfiedAirConstraint {
35        /// The row index of the failing pair.
36        row: usize,
37    },
38    /// AIR has no constraints.
39    NoConstraints,
40    /// Trace has fewer than 2 rows.
41    InsufficientRows {
42        /// The row count.
43        row_count: usize,
44    },
45    /// Row length does not match column count.
46    RowLengthMismatch {
47        /// The row index.
48        row: usize,
49        /// The expected length.
50        expected: usize,
51        /// The actual length.
52        actual: 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::ProofCatCore(e) => write!(f, "proof-cat-core error: {e}"),
60            Self::Plonkish(e) => write!(f, "plonkish-cat error: {e}"),
61            Self::FieldCat(e) => write!(f, "field-cat error: {e}"),
62            Self::ColumnOutOfBounds {
63                index,
64                column_count,
65            } => write!(
66                f,
67                "column index {index} out of bounds (column count: {column_count})"
68            ),
69            Self::EmptyTrace => write!(f, "trace has zero rows"),
70            Self::ColumnCountMismatch { expected, actual } => {
71                write!(
72                    f,
73                    "column count mismatch: expected {expected}, got {actual}"
74                )
75            }
76            Self::TraceNotPowerOfTwo { row_count } => {
77                write!(f, "trace row count {row_count} is not a power of two")
78            }
79            Self::UnsatisfiedAirConstraint { row } => {
80                write!(
81                    f,
82                    "AIR constraint not satisfied at row pair ({row}, {})",
83                    row + 1
84                )
85            }
86            Self::NoConstraints => write!(f, "AIR has no constraints"),
87            Self::InsufficientRows { row_count } => {
88                write!(f, "trace has {row_count} rows, need at least 2")
89            }
90            Self::RowLengthMismatch {
91                row,
92                expected,
93                actual,
94            } => write!(f, "row {row} has {actual} elements, expected {expected}"),
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::ProofCatCore(e) => Some(e),
103            Self::Plonkish(e) => Some(e),
104            Self::FieldCat(e) => Some(e),
105            Self::ColumnOutOfBounds { .. }
106            | Self::EmptyTrace
107            | Self::ColumnCountMismatch { .. }
108            | Self::TraceNotPowerOfTwo { .. }
109            | Self::UnsatisfiedAirConstraint { .. }
110            | Self::NoConstraints
111            | Self::InsufficientRows { .. }
112            | Self::RowLengthMismatch { .. } => None,
113        }
114    }
115}
116
117impl From<proof_cat_core::Error> for Error {
118    fn from(e: proof_cat_core::Error) -> Self {
119        Self::ProofCatCore(e)
120    }
121}
122
123impl From<plonkish_cat::Error> for Error {
124    fn from(e: plonkish_cat::Error) -> Self {
125        Self::Plonkish(e)
126    }
127}
128
129impl From<field_cat::Error> for Error {
130    fn from(e: field_cat::Error) -> Self {
131        Self::FieldCat(e)
132    }
133}