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