use std::{backtrace::Backtrace, borrow::Cow};
use thiserror::Error;
pub type SnarkyResult<T> = std::result::Result<T, Box<RealSnarkyError>>;
pub type SnarkyRuntimeResult<T> = std::result::Result<T, Box<SnarkyRuntimeError>>;
pub type SnarkyCompileResult<T> = std::result::Result<T, SnarkyCompilationError>;
#[derive(Debug, Error)]
#[error("an error ocurred in snarky")]
pub struct RealSnarkyError {
pub source: SnarkyError,
pub loc: Option<String>,
pub label_stack: Option<Vec<Cow<'static, str>>>,
backtrace: Option<Backtrace>,
}
impl RealSnarkyError {
pub fn new(source: SnarkyError) -> Self {
let backtrace = std::env::var("SNARKY_BACKTRACE")
.ok()
.map(|_| Backtrace::capture());
Self {
source,
loc: None,
label_stack: None,
backtrace,
}
}
pub fn new_with_ctx(
source: SnarkyError,
loc: Cow<'static, str>,
label_stack: Vec<Cow<'static, str>>,
) -> Self {
let backtrace = std::env::var("SNARKY_BACKTRACE")
.ok()
.map(|_| Backtrace::capture());
Self {
source,
loc: Some(loc.to_string()),
label_stack: Some(label_stack),
backtrace,
}
}
}
#[derive(Debug, Clone, Error)]
pub enum SnarkyError {
#[error("a compilation error occurred")]
CompilationError(SnarkyCompilationError),
#[error("a runtime error occurred")]
RuntimeError(SnarkyRuntimeError),
}
#[derive(Debug, Clone, Error)]
pub enum SnarkyCompilationError {
#[error("the two values were not equal: {0} != {1}")]
ConstantAssertEquals(String, String),
}
#[derive(Debug, Clone, Error)]
pub enum SnarkyRuntimeError {
#[error(
"unsatisfied constraint #{8}: `{0} * {1} + {2} * {3} + {4} * {5} + {6} * {1} * {3} + {7} != 0`"
)]
UnsatisfiedGenericConstraint(
String,
String,
String,
String,
String,
String,
String,
String,
usize,
),
#[error("unsatisfied constraint #{0}: {1} is not a boolean (0 or 1)")]
UnsatisfiedBooleanConstraint(usize, String),
#[error("unsatisfied constraint #{0}: {1} is not equal to {2}")]
UnsatisfiedEqualConstraint(usize, String, String),
#[error("unsatisfied constraint #{0}: {1}^2 is not equal to {2}")]
UnsatisfiedSquareConstraint(usize, String, String),
#[error("unsatisfied constraint #{0}: {1} * {2} is not equal to {3}")]
UnsatisfiedR1CSConstraint(usize, String, String, String),
#[error("the number of public inputs passed ({0}) does not match the number of public inputs expected ({1})")]
PubInputMismatch(usize, usize),
#[error("the value returned by the circuit has an incorrect number of field variables. It hardcoded {1} field variables, but returned {0}")]
CircuitReturnVar(usize, usize),
}