halo2_axiom/plonk/
error.rs

1use std::error;
2use std::fmt;
3use std::io;
4
5use super::TableColumn;
6use super::{Any, Column};
7
8/// This is an error that could occur during proving or circuit synthesis.
9// TODO: these errors need to be cleaned up
10#[derive(Debug)]
11pub enum Error {
12    /// This is an error that can occur during synthesis of the circuit, for
13    /// example, when the witness is not present.
14    Synthesis,
15    /// The provided instances do not match the circuit parameters.
16    InvalidInstances,
17    /// The constraint system is not satisfied.
18    ConstraintSystemFailure,
19    /// Out of bounds index passed to a backend
20    BoundsFailure,
21    /// Opening error
22    Opening,
23    /// Transcript error
24    Transcript(io::Error),
25    /// `k` is too small for the given circuit.
26    NotEnoughRowsAvailable {
27        /// The current value of `k` being used.
28        current_k: u32,
29    },
30    /// Instance provided exceeds number of available rows
31    InstanceTooLarge,
32    /// Circuit synthesis requires global constants, but circuit configuration did not
33    /// call [`ConstraintSystem::enable_constant`] on fixed columns with sufficient space.
34    ///
35    /// [`ConstraintSystem::enable_constant`]: crate::plonk::ConstraintSystem::enable_constant
36    NotEnoughColumnsForConstants,
37    /// The instance sets up a copy constraint involving a column that has not been
38    /// included in the permutation.
39    ColumnNotInPermutation(Column<Any>),
40    /// An error relating to a lookup table.
41    TableError(TableError),
42}
43
44impl From<io::Error> for Error {
45    fn from(error: io::Error) -> Self {
46        // The only place we can get io::Error from is the transcript.
47        Error::Transcript(error)
48    }
49}
50
51impl Error {
52    /// Constructs an `Error::NotEnoughRowsAvailable`.
53    pub(crate) fn not_enough_rows_available(current_k: u32) -> Self {
54        Error::NotEnoughRowsAvailable { current_k }
55    }
56}
57
58impl fmt::Display for Error {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        match self {
61            Error::Synthesis => write!(f, "General synthesis error"),
62            Error::InvalidInstances => write!(f, "Provided instances do not match the circuit"),
63            Error::ConstraintSystemFailure => write!(f, "The constraint system is not satisfied"),
64            Error::BoundsFailure => write!(f, "An out-of-bounds index was passed to the backend"),
65            Error::Opening => write!(f, "Multi-opening proof was invalid"),
66            Error::Transcript(e) => write!(f, "Transcript error: {}", e),
67            Error::NotEnoughRowsAvailable { current_k } => write!(
68                f,
69                "k = {} is too small for the given circuit. Try using a larger value of k",
70                current_k,
71            ),
72            Error::InstanceTooLarge => write!(f, "Instance vectors are larger than the circuit"),
73            Error::NotEnoughColumnsForConstants => {
74                write!(
75                    f,
76                    "Too few fixed columns are enabled for global constants usage"
77                )
78            }
79            Error::ColumnNotInPermutation(column) => write!(
80                f,
81                "Column {:?} must be included in the permutation. Help: try applying `meta.enable_equalty` on the column",
82                column
83            ),
84            Error::TableError(error) => write!(f, "{}", error)
85        }
86    }
87}
88
89impl error::Error for Error {
90    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
91        match self {
92            Error::Transcript(e) => Some(e),
93            _ => None,
94        }
95    }
96}
97
98/// This is an error that could occur during table synthesis.
99#[derive(Debug)]
100pub enum TableError {
101    /// A `TableColumn` has not been assigned.
102    ColumnNotAssigned(TableColumn),
103    /// A Table has columns of uneven lengths.
104    UnevenColumnLengths((TableColumn, usize), (TableColumn, usize)),
105    /// Attempt to assign a used `TableColumn`
106    UsedColumn(TableColumn),
107    /// Attempt to overwrite a default value
108    OverwriteDefault(TableColumn, String, String),
109}
110
111impl fmt::Display for TableError {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        match self {
114            TableError::ColumnNotAssigned(col) => {
115                write!(
116                    f,
117                    "{:?} not fully assigned. Help: assign a value at offset 0.",
118                    col
119                )
120            }
121            TableError::UnevenColumnLengths((col, col_len), (table, table_len)) => write!(
122                f,
123                "{:?} has length {} while {:?} has length {}",
124                col, col_len, table, table_len
125            ),
126            TableError::UsedColumn(col) => {
127                write!(f, "{:?} has already been used", col)
128            }
129            TableError::OverwriteDefault(col, default, val) => {
130                write!(
131                    f,
132                    "Attempted to overwrite default value {} with {} in {:?}",
133                    default, val, col
134                )
135            }
136        }
137    }
138}