fhe_math/
errors.rs

1use thiserror::Error;
2
3use crate::rq::Representation;
4
5/// The Result type for this library.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Enum encapsulation all the possible errors from this library.
9#[derive(Debug, Error, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Indicates an invalid modulus
13    #[error("Invalid modulus: modulus {0} should be between 2 and (1 << 62) - 1.")]
14    InvalidModulus(u64),
15
16    /// Indicates an error in the serialization / deserialization.
17    #[error("{0}")]
18    Serialization(String),
19
20    /// Indicates that there is no more contexts to switch to.
21    #[error("This is the last context.")]
22    NoMoreContext,
23
24    /// Indicates that the provided context is invalid.
25    #[error("Invalid context provided.")]
26    InvalidContext,
27
28    /// Indicates an incorrect representation.
29    #[error("Incorrect representation: got {0:?}, expected {1:?}.")]
30    IncorrectRepresentation(Representation, Representation),
31
32    /// Indicates that the seed size is incorrect.
33    #[error("Invalid seed: got {0} bytes, expected {1} bytes.")]
34    InvalidSeedSize(usize, usize),
35
36    /// Indicates a default error
37    /// TODO: To delete when transition is over
38    #[error("{0}")]
39    Default(String),
40}
41
42#[cfg(test)]
43mod tests {
44    use crate::{rq::Representation, Error};
45
46    #[test]
47    fn error_strings() {
48        assert_eq!(
49            Error::InvalidModulus(0).to_string(),
50            "Invalid modulus: modulus 0 should be between 2 and (1 << 62) - 1."
51        );
52        assert_eq!(Error::Serialization("test".to_string()).to_string(), "test");
53        assert_eq!(
54            Error::NoMoreContext.to_string(),
55            "This is the last context."
56        );
57        assert_eq!(
58            Error::InvalidContext.to_string(),
59            "Invalid context provided."
60        );
61        assert_eq!(
62            Error::IncorrectRepresentation(Representation::Ntt, Representation::NttShoup)
63                .to_string(),
64            "Incorrect representation: got Ntt, expected NttShoup."
65        );
66        assert_eq!(
67            Error::InvalidSeedSize(0, 1).to_string(),
68            "Invalid seed: got 0 bytes, expected 1 bytes."
69        );
70    }
71}