use scuttlebutt::Block;
use std::fmt::{self, Display, Formatter};
#[derive(Debug)]
pub enum FancyError {
UnequalModuli,
InvalidArg(String),
InvalidArgNum {
got: usize,
needed: usize,
},
InvalidArgMod {
got: u16,
needed: u16,
},
ArgNotBinary,
NoTruthTable,
InvalidTruthTable,
UninitializedValue,
}
#[derive(Debug)]
pub enum DummyError {
NotEnoughGarblerInputs,
NotEnoughEvaluatorInputs,
EncodingError,
FancyError(FancyError),
}
#[derive(Debug)]
pub enum EvaluatorError {
NotEnoughGarblerInputs,
NotEnoughEvaluatorInputs,
DecodingFailed,
CommunicationError(String),
FancyError(FancyError),
}
#[derive(Debug)]
pub enum GarblerError {
CommunicationError(String),
AsymmetricHalfGateModuliMax8(u16),
TruthTableRequired,
DeltaRequired,
EncodingError,
FancyError(FancyError),
}
#[derive(Debug)]
pub enum CircuitBuilderError {
ReuseUndefined,
FancyError(FancyError),
}
#[derive(Debug)]
pub enum InformerError {
FancyError(FancyError),
}
impl Display for FancyError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
FancyError::UnequalModuli => "unequal moduli".fmt(f),
FancyError::InvalidArg(s) => write!(f, "invalid argument: {}", s),
FancyError::InvalidArgNum { got, needed } => write!(
f,
"invalid number of arguments: needed {} but got {}",
got, needed
),
FancyError::InvalidArgMod { got, needed } => write!(
f,
"invalid modulus: got mod {} but require mod {}",
got, needed
),
FancyError::ArgNotBinary => "argument bundle must be boolean".fmt(f),
FancyError::NoTruthTable => "truth table required".fmt(f),
FancyError::InvalidTruthTable => "invalid truth table".fmt(f),
FancyError::UninitializedValue => {
"uninitialized value in circuit. is the circuit topologically sorted?".fmt(f)
}
}
}
}
impl Display for DummyError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
DummyError::NotEnoughGarblerInputs => "not enough garbler inputs".fmt(f),
DummyError::NotEnoughEvaluatorInputs => "not enough evaluator inputs".fmt(f),
DummyError::EncodingError => "not enough inputs or moduli".fmt(f),
DummyError::FancyError(e) => write!(f, "fancy error: {}", e),
}
}
}
impl From<FancyError> for DummyError {
fn from(e: FancyError) -> DummyError {
DummyError::FancyError(e)
}
}
impl Display for EvaluatorError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
EvaluatorError::NotEnoughGarblerInputs => "not enough garbler inputs".fmt(f),
EvaluatorError::NotEnoughEvaluatorInputs => "not enough evaluator inputs".fmt(f),
EvaluatorError::DecodingFailed => write!(f, "decodiing failed"),
EvaluatorError::CommunicationError(s) => write!(f, "communication error: {}", s),
EvaluatorError::FancyError(e) => write!(f, "fancy error: {}", e),
}
}
}
impl From<FancyError> for EvaluatorError {
fn from(e: FancyError) -> Self {
EvaluatorError::FancyError(e)
}
}
impl From<std::io::Error> for EvaluatorError {
fn from(e: std::io::Error) -> Self {
EvaluatorError::CommunicationError(e.to_string())
}
}
impl From<std::sync::mpsc::RecvError> for EvaluatorError {
fn from(e: std::sync::mpsc::RecvError) -> Self {
EvaluatorError::CommunicationError(e.to_string())
}
}
impl Display for GarblerError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
GarblerError::CommunicationError(s) => write!(f, "{}", s),
GarblerError::AsymmetricHalfGateModuliMax8(q) => write!(
f,
"the small modulus in a half gate with asymmetric moduli is capped at 8, got {}",
q
),
GarblerError::TruthTableRequired => {
"truth table required for garbler projection gates".fmt(f)
}
GarblerError::DeltaRequired => {
"delta from previous execution of garbler must be provided with wire to reuse"
.fmt(f)
}
GarblerError::EncodingError => {
"encoding failed: unequal length input values and moduli".fmt(f)
}
GarblerError::FancyError(e) => write!(f, "{}", e),
}
}
}
impl From<FancyError> for GarblerError {
fn from(e: FancyError) -> Self {
GarblerError::FancyError(e)
}
}
impl From<std::io::Error> for GarblerError {
fn from(e: std::io::Error) -> Self {
GarblerError::CommunicationError(e.to_string())
}
}
impl From<std::sync::mpsc::SendError<Vec<Block>>> for GarblerError {
fn from(e: std::sync::mpsc::SendError<Vec<Block>>) -> Self {
GarblerError::CommunicationError(e.to_string())
}
}
impl Display for CircuitBuilderError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
CircuitBuilderError::FancyError(e) => write!(f, "fancy error: {}", e),
CircuitBuilderError::ReuseUndefined => write!(
f,
"reuse is undefined for circuits. it is unclear what it means to reuse a
CircuitRef from a previous circuit."
),
}
}
}
impl From<FancyError> for CircuitBuilderError {
fn from(e: FancyError) -> Self {
CircuitBuilderError::FancyError(e)
}
}
impl Display for InformerError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
InformerError::FancyError(e) => write!(f, "fancy error: {}", e),
}
}
}
impl From<FancyError> for InformerError {
fn from(e: FancyError) -> InformerError {
InformerError::FancyError(e)
}
}
#[derive(Debug)]
pub enum CircuitParserError {
IoError(std::io::Error),
RegexError(regex::Error),
ParseIntError,
ParseLineError(String),
ParseGateError(String),
}
impl Display for CircuitParserError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
CircuitParserError::IoError(e) => write!(f, "io error: {}", e),
CircuitParserError::RegexError(e) => write!(f, "regex error: {}", e),
CircuitParserError::ParseIntError => write!(f, "unable to parse integer"),
CircuitParserError::ParseLineError(s) => write!(f, "unable to parse line '{}'", s),
CircuitParserError::ParseGateError(s) => write!(f, "unable to parse gate '{}'", s),
}
}
}
impl From<std::io::Error> for CircuitParserError {
fn from(e: std::io::Error) -> CircuitParserError {
CircuitParserError::IoError(e)
}
}
impl From<regex::Error> for CircuitParserError {
fn from(e: regex::Error) -> CircuitParserError {
CircuitParserError::RegexError(e)
}
}
impl From<std::num::ParseIntError> for CircuitParserError {
fn from(_: std::num::ParseIntError) -> CircuitParserError {
CircuitParserError::ParseIntError
}
}