1#[cfg(feature = "backtraces")]
2use std::backtrace::Backtrace;
3use std::fmt::Debug;
4use thiserror::Error;
5
6pub type CryptoResult<T> = core::result::Result<T, CryptoError>;
7
8#[derive(Error, Debug, PartialEq)]
9pub enum CryptoError {
10 #[error("Crypto error: {msg}")]
11 GenericErr {
12 msg: String,
13 #[cfg(feature = "backtraces")]
14 backtrace: Backtrace,
15 },
16 #[error("Invalid point on curve")]
17 InvalidPointOnCurve {
18 #[cfg(feature = "backtraces")]
19 backtrace: Backtrace,
20 },
21 #[error("Invalid hash format")]
22 InvalidHashFormat {
23 #[cfg(feature = "backtraces")]
24 backtrace: Backtrace,
25 },
26 #[error("Invalid public key format")]
27 InvalidPubkeyFormat {
28 #[cfg(feature = "backtraces")]
29 backtrace: Backtrace,
30 },
31 #[error("Invalid proof format")]
32 InvalidProofFormat {
33 #[cfg(feature = "backtraces")]
34 backtrace: Backtrace,
35 },
36}
37
38impl CryptoError {
39 pub fn generic_err(msg: impl Into<String>) -> Self {
40 CryptoError::GenericErr {
41 msg: msg.into(),
42 #[cfg(feature = "backtraces")]
43 backtrace: Backtrace::capture(),
44 }
45 }
46
47 pub fn invalid_point_on_curve() -> Self {
48 CryptoError::InvalidPointOnCurve {
49 #[cfg(feature = "backtraces")]
50 backtrace: Backtrace::capture(),
51 }
52 }
53
54 pub fn invalid_hash_format() -> Self {
55 CryptoError::InvalidHashFormat {
56 #[cfg(feature = "backtraces")]
57 backtrace: Backtrace::capture(),
58 }
59 }
60
61 pub fn invalid_pubkey_format() -> Self {
62 CryptoError::InvalidPubkeyFormat {
63 #[cfg(feature = "backtraces")]
64 backtrace: Backtrace::capture(),
65 }
66 }
67
68 pub fn invalid_proof_format() -> Self {
69 CryptoError::InvalidProofFormat {
70 #[cfg(feature = "backtraces")]
71 backtrace: Backtrace::capture(),
72 }
73 }
74
75 pub fn code(&self) -> u32 {
78 match self {
79 CryptoError::InvalidPointOnCurve { .. } => 2,
80 CryptoError::InvalidHashFormat { .. } => 3,
81 CryptoError::InvalidProofFormat { .. } => 4,
82 CryptoError::InvalidPubkeyFormat { .. } => 5,
83 CryptoError::GenericErr { .. } => 10,
84 }
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn generic_err_works() {
94 let error = CryptoError::generic_err("something went wrong in a general way");
95 match error {
96 CryptoError::GenericErr { msg, .. } => {
97 assert_eq!(msg, "something went wrong in a general way")
98 }
99 _ => panic!("wrong error type!"),
100 }
101 }
102
103 #[test]
104 fn invalid_point_on_curve_works() {
105 let error = CryptoError::invalid_point_on_curve();
106 match error {
107 CryptoError::InvalidPointOnCurve { .. } => {}
108 _ => panic!("wrong error type!"),
109 }
110 }
111
112 #[test]
113 fn invalid_hash_format_works() {
114 let error = CryptoError::invalid_hash_format();
115 match error {
116 CryptoError::InvalidHashFormat { .. } => {}
117 _ => panic!("wrong error type!"),
118 }
119 }
120
121 #[test]
122 fn invalid_proof_format_works() {
123 let error = CryptoError::invalid_proof_format();
124 match error {
125 CryptoError::InvalidProofFormat { .. } => {}
126 _ => panic!("wrong error type!"),
127 }
128 }
129
130 #[test]
131 fn invalid_pubkey_format_works() {
132 let error = CryptoError::invalid_pubkey_format();
133 match error {
134 CryptoError::InvalidPubkeyFormat { .. } => {}
135 _ => panic!("wrong error type!"),
136 }
137 }
138
139 #[test]
140 fn code_works() {
141 assert_eq!(CryptoError::invalid_point_on_curve().code(), 2);
142 assert_eq!(CryptoError::invalid_hash_format().code(), 3);
143 assert_eq!(CryptoError::invalid_proof_format().code(), 4);
144 assert_eq!(CryptoError::invalid_pubkey_format().code(), 5);
145 assert_eq!(CryptoError::generic_err("test").code(), 10);
146 }
147}