cosmwasm_std/errors/
verification_error.rs

1use core::fmt::Debug;
2
3use super::BT;
4
5#[cfg(not(target_arch = "wasm32"))]
6use cosmwasm_crypto::CryptoError;
7
8#[derive(Debug, PartialEq, thiserror::Error)]
9pub enum AggregationError {
10    #[error("List of points is empty")]
11    Empty,
12    #[error("List is not an expected multiple")]
13    NotMultiple,
14}
15
16#[derive(Debug, PartialEq, thiserror::Error)]
17pub enum PairingEqualityError {
18    #[error("List is not a multiple of 48")]
19    NotMultipleG1,
20    #[error("List is not a multiple of 96")]
21    NotMultipleG2,
22    #[error("Not the same amount of points passed")]
23    UnequalPointAmount,
24}
25
26#[derive(Debug, thiserror::Error)]
27pub enum VerificationError {
28    #[error("Aggregation error: {source}")]
29    Aggregation { source: AggregationError },
30    #[error("Batch error")]
31    BatchErr,
32    #[error("Generic error")]
33    GenericErr,
34    #[error("Invalid hash format")]
35    InvalidHashFormat,
36    #[error("Invalid signature format")]
37    InvalidSignatureFormat,
38    #[error("Invalid public key format")]
39    InvalidPubkeyFormat,
40    #[error("Invalid recovery parameter. Supported values: 0 and 1.")]
41    InvalidRecoveryParam,
42    #[error("Invalid point")]
43    InvalidPoint,
44    #[error("Unknown hash function")]
45    UnknownHashFunction,
46    #[error("Aggregation pairing equality error: {source}")]
47    PairingEquality { source: PairingEqualityError },
48    #[error("Unknown error: {error_code}")]
49    UnknownErr { error_code: u32, backtrace: BT },
50}
51
52impl VerificationError {
53    pub fn unknown_err(error_code: u32) -> Self {
54        VerificationError::UnknownErr {
55            error_code,
56
57            backtrace: BT::capture(),
58        }
59    }
60}
61
62impl PartialEq<VerificationError> for VerificationError {
63    fn eq(&self, rhs: &VerificationError) -> bool {
64        match self {
65            VerificationError::Aggregation { source: lhs_source } => {
66                matches!(rhs, VerificationError::Aggregation { source: rhs_source } if rhs_source == lhs_source)
67            }
68            VerificationError::PairingEquality { source: lhs_source } => {
69                matches!(rhs, VerificationError::PairingEquality { source: rhs_source } if rhs_source == lhs_source)
70            }
71            VerificationError::BatchErr => matches!(rhs, VerificationError::BatchErr),
72            VerificationError::GenericErr => matches!(rhs, VerificationError::GenericErr),
73            VerificationError::InvalidHashFormat => {
74                matches!(rhs, VerificationError::InvalidHashFormat)
75            }
76            VerificationError::InvalidPubkeyFormat => {
77                matches!(rhs, VerificationError::InvalidPubkeyFormat)
78            }
79            VerificationError::InvalidSignatureFormat => {
80                matches!(rhs, VerificationError::InvalidSignatureFormat)
81            }
82            VerificationError::InvalidRecoveryParam => {
83                matches!(rhs, VerificationError::InvalidRecoveryParam)
84            }
85            VerificationError::InvalidPoint => matches!(rhs, VerificationError::InvalidPoint),
86            VerificationError::UnknownHashFunction => {
87                matches!(rhs, VerificationError::UnknownHashFunction)
88            }
89            VerificationError::UnknownErr { error_code, .. } => {
90                if let VerificationError::UnknownErr {
91                    error_code: rhs_error_code,
92                    ..
93                } = rhs
94                {
95                    error_code == rhs_error_code
96                } else {
97                    false
98                }
99            }
100        }
101    }
102}
103
104#[cfg(not(target_arch = "wasm32"))]
105impl From<CryptoError> for VerificationError {
106    fn from(original: CryptoError) -> Self {
107        match original {
108            CryptoError::Aggregation {
109                source: cosmwasm_crypto::AggregationError::Empty,
110                ..
111            } => VerificationError::Aggregation {
112                source: AggregationError::Empty,
113            },
114            CryptoError::Aggregation {
115                source: cosmwasm_crypto::AggregationError::NotMultiple { .. },
116                ..
117            } => VerificationError::Aggregation {
118                source: AggregationError::NotMultiple,
119            },
120            CryptoError::PairingEquality {
121                source: cosmwasm_crypto::PairingEqualityError::NotMultipleG1 { .. },
122                ..
123            } => VerificationError::PairingEquality {
124                source: PairingEqualityError::NotMultipleG1,
125            },
126            CryptoError::PairingEquality {
127                source: cosmwasm_crypto::PairingEqualityError::NotMultipleG2 { .. },
128                ..
129            } => VerificationError::PairingEquality {
130                source: PairingEqualityError::NotMultipleG2,
131            },
132            CryptoError::PairingEquality {
133                source: cosmwasm_crypto::PairingEqualityError::UnequalPointAmount { .. },
134                ..
135            } => VerificationError::PairingEquality {
136                source: PairingEqualityError::UnequalPointAmount,
137            },
138            CryptoError::InvalidHashFormat { .. } => VerificationError::InvalidHashFormat,
139            CryptoError::InvalidPubkeyFormat { .. } => VerificationError::InvalidPubkeyFormat,
140            CryptoError::InvalidSignatureFormat { .. } => VerificationError::InvalidSignatureFormat,
141            CryptoError::GenericErr { .. } => VerificationError::GenericErr,
142            CryptoError::InvalidRecoveryParam { .. } => VerificationError::InvalidRecoveryParam,
143            CryptoError::InvalidPoint { .. } => VerificationError::InvalidPoint,
144            CryptoError::BatchErr { .. } => VerificationError::BatchErr,
145            CryptoError::UnknownHashFunction { .. } => VerificationError::UnknownHashFunction,
146        }
147    }
148}
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153
154    // constructors
155    #[test]
156    fn unknown_err_works() {
157        let error = VerificationError::unknown_err(123);
158        match error {
159            VerificationError::UnknownErr { error_code, .. } => assert_eq!(error_code, 123),
160            _ => panic!("wrong error type!"),
161        }
162    }
163}