arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
miden-crypto/src/dsa/rpo_falcon512/error.rs
src/dsa/rpo_falcon512/error.rsRaw
 1use super::{LOG_N, MODULUS, PK_LEN};
 2use core::fmt;
 3
 4#[derive(Clone, Debug, PartialEq, Eq)]
 5pub enum FalconError {
 6    KeyGenerationFailed,
 7    PubKeyDecodingExtraData,
 8    PubKeyDecodingInvalidCoefficient(u32),
 9    PubKeyDecodingInvalidLength(usize),
10    PubKeyDecodingInvalidTag(u8),
11    SigDecodingTooBigHighBits(u32),
12    SigDecodingInvalidRemainder,
13    SigDecodingNonZeroUnusedBitsLastByte,
14    SigDecodingMinusZero,
15    SigDecodingIncorrectEncodingAlgorithm,
16    SigDecodingNotSupportedDegree(u8),
17    SigGenerationFailed,
18}
19
20impl fmt::Display for FalconError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        use FalconError::*;
23        match self {
24            KeyGenerationFailed => write!(f, "Failed to generate a private-public key pair"),
25            PubKeyDecodingExtraData => {
26                write!(f, "Failed to decode public key: input not fully consumed")
27            }
28            PubKeyDecodingInvalidCoefficient(val) => {
29                write!(f, "Failed to decode public key: coefficient {val} is greater than or equal to the field modulus {MODULUS}")
30            }
31            PubKeyDecodingInvalidLength(len) => {
32                write!(f, "Failed to decode public key: expected {PK_LEN} bytes but received {len}")
33            }
34            PubKeyDecodingInvalidTag(byte) => {
35                write!(f, "Failed to decode public key: expected the first byte to be {LOG_N} but was {byte}")
36            }
37            SigDecodingTooBigHighBits(m) => {
38                write!(f, "Failed to decode signature: high bits {m} exceed 2048")
39            }
40            SigDecodingInvalidRemainder => {
41                write!(f, "Failed to decode signature: incorrect remaining data")
42            }
43            SigDecodingNonZeroUnusedBitsLastByte => {
44                write!(f, "Failed to decode signature: Non-zero unused bits in the last byte")
45            }
46            SigDecodingMinusZero => write!(f, "Failed to decode signature: -0 is forbidden"),
47            SigDecodingIncorrectEncodingAlgorithm => write!(f, "Failed to decode signature: not supported encoding algorithm"),
48            SigDecodingNotSupportedDegree(log_n) => write!(f, "Failed to decode signature: only supported irreducible polynomial degree is 512, 2^{log_n} was provided"),
49            SigGenerationFailed => write!(f, "Failed to generate a signature"),
50        }
51    }
52}
53
54#[cfg(feature = "std")]
55impl std::error::Error for FalconError {}