snarkos_errors/gadgets/
synthesis.rs

1// Copyright (C) 2019-2020 Aleo Systems Inc.
2// This file is part of the snarkOS library.
3
4// The snarkOS library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The snarkOS library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.
16
17use std::{error::Error, fmt, io};
18
19/// This is an error that could occur during circuit synthesis contexts,
20/// such as CRS generation, proving or verification.
21#[derive(Debug)]
22pub enum SynthesisError {
23    /// During synthesis, we lacked knowledge of a variable assignment.
24    AssignmentMissing,
25    /// During synthesis, we divided by zero.
26    DivisionByZero,
27    /// During synthesis, we constructed an unsatisfiable constraint system.
28    Unsatisfiable,
29    /// During synthesis, our polynomials ended up being too high of degree
30    PolynomialDegreeTooLarge,
31    /// During proof generation, we encountered an identity in the CRS
32    UnexpectedIdentity,
33    /// During proof generation, we encountered an I/O error with the CRS
34    IoError(io::Error),
35    /// During verification, our verifying key was malformed.
36    MalformedVerifyingKey,
37    /// During CRS generation, we observed an unconstrained auxiliary variable
38    UnconstrainedVariable,
39}
40
41impl From<io::Error> for SynthesisError {
42    fn from(e: io::Error) -> SynthesisError {
43        SynthesisError::IoError(e)
44    }
45}
46
47impl Error for SynthesisError {
48    fn description(&self) -> &str {
49        match *self {
50            SynthesisError::AssignmentMissing => "an assignment for a variable could not be computed",
51            SynthesisError::DivisionByZero => "division by zero",
52            SynthesisError::Unsatisfiable => "unsatisfiable constraint system",
53            SynthesisError::PolynomialDegreeTooLarge => "polynomial degree is too large",
54            SynthesisError::UnexpectedIdentity => "encountered an identity element in the CRS",
55            SynthesisError::IoError(_) => "encountered an I/O error",
56            SynthesisError::MalformedVerifyingKey => "malformed verifying key",
57            SynthesisError::UnconstrainedVariable => "auxiliary variable was unconstrained",
58        }
59    }
60}
61
62impl fmt::Display for SynthesisError {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
64        if let &SynthesisError::IoError(ref e) = self {
65            write!(f, "I/O error: ")?;
66            e.fmt(f)
67        } else {
68            write!(f, "{}", self.to_string())
69        }
70    }
71}