starknet_crypto/
error.rs

1mod sign_error {
2    /// Errors when performing ECDSA [`sign`](fn.sign) operations.
3    #[derive(Debug)]
4    pub enum SignError {
5        /// The message hash is not in the range of `[0, 2^251)`.
6        InvalidMessageHash,
7        /// The random `k` value results in an invalid signature. A different `k` value should be
8        /// used instead, typically by using a new seed per RFC-6979.
9        InvalidK,
10    }
11
12    #[cfg(feature = "std")]
13    impl std::error::Error for SignError {}
14
15    impl core::fmt::Display for SignError {
16        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17            match self {
18                Self::InvalidMessageHash => write!(f, "Invalid message hash"),
19                Self::InvalidK => write!(f, "Invalid k"),
20            }
21        }
22    }
23}
24pub use sign_error::SignError;
25
26mod verify_error {
27    /// Errors when performing ECDSA [`verify`](fn.verify) operations.
28    #[derive(Debug)]
29    pub enum VerifyError {
30        /// The public key is not a valid point on the STARK curve.
31        InvalidPublicKey,
32        /// The message hash is not in the range of `[0, 2^251)`.
33        InvalidMessageHash,
34        /// The `r` value is not in the range of `[0, 2^251)`.
35        InvalidR,
36        /// The `s` value is not in the range of `[0, 2^251)`.
37        InvalidS,
38    }
39
40    #[cfg(feature = "std")]
41    impl std::error::Error for VerifyError {}
42
43    impl core::fmt::Display for VerifyError {
44        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45            match self {
46                Self::InvalidMessageHash => write!(f, "Invalid message hash"),
47                Self::InvalidPublicKey => write!(f, "Invalid public key"),
48                Self::InvalidR => write!(f, "Invalid r"),
49                Self::InvalidS => write!(f, "Invalid s"),
50            }
51        }
52    }
53}
54pub use verify_error::VerifyError;
55
56mod recover_error {
57    /// Errors when performing ECDSA [`recover`](fn.recover) operations.
58    #[derive(Debug)]
59    pub enum RecoverError {
60        /// The message hash is not in the range of `[0, 2^251)`.
61        InvalidMessageHash,
62        /// The `r` value is not in the range of `[0, 2^251)`.
63        InvalidR,
64        /// The `s` value is not in the range of `[0,
65        /// 0x0800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f)`.
66        InvalidS,
67        /// The `v` value is neither `0` nor `1`.
68        InvalidV,
69    }
70
71    #[cfg(feature = "std")]
72    impl std::error::Error for RecoverError {}
73
74    impl core::fmt::Display for RecoverError {
75        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76            match self {
77                Self::InvalidMessageHash => write!(f, "Invalid message hash"),
78                Self::InvalidR => write!(f, "Invalid r"),
79                Self::InvalidS => write!(f, "Invalid s"),
80                Self::InvalidV => write!(f, "Invalid v"),
81            }
82        }
83    }
84}
85pub use recover_error::RecoverError;