Skip to main content

minroot_core/
error.rs

1//! Project-wide error type for `minroot-core`.
2
3use core::fmt;
4
5/// Errors arising from field arithmetic and `MinRoot` computation.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Error {
8    /// A value exceeds the field modulus.
9    OutOfRange {
10        /// The limb index or context where overflow was detected.
11        context: &'static str,
12    },
13    /// Polynomial coefficient count does not match the expected width.
14    CoefficientCountMismatch {
15        /// Number of coefficients provided.
16        got: usize,
17        /// Number of coefficients expected.
18        expected: usize,
19    },
20    /// Attempted to invert zero in the field.
21    DivisionByZero,
22    /// Iteration count must be positive.
23    ZeroIterations,
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::OutOfRange { context } => {
30                write!(f, "value out of field range in {context}")
31            }
32            Self::CoefficientCountMismatch { got, expected } => {
33                write!(
34                    f,
35                    "coefficient count mismatch: got {got}, expected {expected}"
36                )
37            }
38            Self::DivisionByZero => write!(f, "division by zero in field"),
39            Self::ZeroIterations => {
40                write!(f, "iteration count must be positive")
41            }
42        }
43    }
44}
45
46impl core::error::Error for Error {}