fancy_garbling/
errors.rs

1//! Errors that may be output by this library.
2
3use std::fmt::{self, Display, Formatter};
4
5/// General wire deserialization error
6#[cfg(feature = "serde")]
7#[derive(Debug)]
8pub enum WireDeserializationError {
9    /// Deserialization of `WireMod3` failed
10    InvalidWireMod3,
11    /// Deserialization of `WireModQ` failed
12    InvalidWireModQ(ModQDeserializationError),
13}
14
15/// `WireModQ` wire deserialization error
16#[cfg(feature = "serde")]
17#[derive(Debug)]
18pub enum ModQDeserializationError {
19    /// Modulus must be greater than 1
20    BadModulus(u16),
21
22    /// One of the digits is larger than the modulus
23    DigitTooLarge {
24        /// The invalid digit
25        digit: u16,
26        /// Modulus of wire
27        modulus: u16,
28    },
29
30    /// Unexpected number of digits
31    InvalidDigitsLength {
32        /// Number of digits given
33        got: usize,
34        /// Number of digits expected (based on modulus)
35        needed: usize,
36    },
37}
38
39////////////////////////////////////////////////////////////////////////////////
40// Serialization error
41//
42
43#[cfg(feature = "serde")]
44impl Display for WireDeserializationError {
45    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
46        match self {
47            WireDeserializationError::InvalidWireMod3 => {
48                "deserialization of WireMod3 failed: both lsb and msb cannot be set".fmt(f)
49            }
50            WireDeserializationError::InvalidWireModQ(e) => {
51                write!(f, "deserialization of WireModQ failed: {}", e)
52            }
53        }
54    }
55}
56
57#[cfg(feature = "serde")]
58impl Display for ModQDeserializationError {
59    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
60        match self {
61            ModQDeserializationError::BadModulus(modulus) => {
62                write!(f, "modulus must be at least 2. Got {}", modulus)
63            }
64            ModQDeserializationError::DigitTooLarge { digit, modulus } => {
65                write!(
66                    f,
67                    "a digit {} is greater than the modulus ({}) ",
68                    digit, modulus
69                )
70            }
71            ModQDeserializationError::InvalidDigitsLength { got, needed } => {
72                write!(
73                    f,
74                    "invalid number of digits. Expected {}, got {}",
75                    needed, got
76                )
77            }
78        }
79    }
80}
81
82/// Errors emitted by the circuit parser.
83#[derive(Debug)]
84pub enum CircuitParserError {
85    /// An I/O error occurred.
86    IoError(std::io::Error),
87    /// A regular expression parsing error occurred.
88    RegexError(regex::Error),
89    /// An error occurred parsing an integer.
90    ParseIntError,
91    /// An error occurred parsing a line.
92    ParseLineError(String),
93    /// An error occurred parsing a gate type.
94    ParseGateError(String),
95}
96
97impl Display for CircuitParserError {
98    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
99        match self {
100            CircuitParserError::IoError(e) => write!(f, "io error: {}", e),
101            CircuitParserError::RegexError(e) => write!(f, "regex error: {}", e),
102            CircuitParserError::ParseIntError => write!(f, "unable to parse integer"),
103            CircuitParserError::ParseLineError(s) => write!(f, "unable to parse line '{}'", s),
104            CircuitParserError::ParseGateError(s) => write!(f, "unable to parse gate '{}'", s),
105        }
106    }
107}
108
109impl From<std::io::Error> for CircuitParserError {
110    fn from(e: std::io::Error) -> CircuitParserError {
111        CircuitParserError::IoError(e)
112    }
113}
114
115impl From<regex::Error> for CircuitParserError {
116    fn from(e: regex::Error) -> CircuitParserError {
117        CircuitParserError::RegexError(e)
118    }
119}
120
121impl From<std::num::ParseIntError> for CircuitParserError {
122    fn from(_: std::num::ParseIntError) -> CircuitParserError {
123        CircuitParserError::ParseIntError
124    }
125}