Skip to main content

nym_compact_ecash/
error.rs

1// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, CompactEcashError>;
7
8#[derive(Error, Debug)]
9pub enum CompactEcashError {
10    #[error("failed to verify expiration date signatures")]
11    ExpirationDateSignatureVerification,
12
13    #[error("failed to validate expiration date signatures")]
14    ExpirationDateSignatureValidity,
15
16    #[error("empty set for aggregation")]
17    AggregationEmptySet,
18
19    #[error("duplicate indices for aggregation")]
20    AggregationDuplicateIndices,
21
22    #[error("aggregation verification error")]
23    AggregationVerification,
24
25    #[error("the provided signature is at infinity")]
26    IdentitySignature,
27
28    #[error("different element size for aggregation")]
29    AggregationSizeMismatch,
30
31    #[error("the provided commitment hash is at infinity")]
32    IdentityCommitmentHash,
33
34    #[error("withdrawal request failed to verify")]
35    WithdrawalRequestVerification,
36
37    #[error("invalid key generation parameters")]
38    KeygenParameters,
39
40    #[error("signing authority's key is too short")]
41    KeyTooShort,
42
43    #[error("empty/incomplete set of coordinates for interpolation")]
44    InterpolationSetSize,
45
46    #[error("issuance verification failed")]
47    IssuanceVerification,
48
49    #[error("trying to spend more than what's available. Spending : {spending}, available : {remaining}")]
50    SpendExceedsAllowance { spending: u64, remaining: u64 },
51
52    #[error("signature failed validity check")]
53    SpendSignaturesValidity,
54
55    #[error("signature failed verification check")]
56    SpendSignaturesVerification,
57
58    #[error("duplicate serial number in the payment")]
59    SpendDuplicateSerialNumber,
60
61    #[error("given spend date is too late")]
62    SpendDateTooLate,
63
64    #[error("given spend date is too early")]
65    SpendDateTooEarly,
66
67    #[error("ZK proof failed to verify")]
68    SpendZKProofVerification,
69
70    #[error("could not decode base 58 string - {0}")]
71    MalformedString(#[from] bs58::decode::Error),
72
73    #[error("failed to verify coin indices signatures")]
74    CoinIndicesSignatureVerification,
75
76    #[error("failed to deserialize a {object}")]
77    DeserializationFailure { object: String },
78
79    #[error("failed to deserialise {type_name}: {source}")]
80    BinaryDeserialisationFailure {
81        type_name: String,
82        source: bincode::Error,
83    },
84
85    #[error(
86        "deserialization error, expected at least {} bytes, got {}",
87        min,
88        actual
89    )]
90    DeserializationMinLength { min: usize, actual: usize },
91
92    #[error("{type_name} deserialization error, expected {expected} bytes, got {actual}")]
93    DeserializationLengthMismatch {
94        type_name: String,
95        expected: usize,
96        actual: usize,
97    },
98
99    #[error("tried to deserialize {object} with bytes of invalid length. Expected {actual} < {target} or {modulus_target} % {modulus} == 0")]
100    DeserializationInvalidLength {
101        actual: usize,
102        target: usize,
103        modulus_target: usize,
104        modulus: usize,
105        object: String,
106    },
107
108    #[error("failed to deserialize scalar from the received bytes - it might not have been canonically encoded")]
109    ScalarDeserializationFailure,
110
111    #[error("failed to deserialize G1Projective point from the received bytes - it might not have been canonically encoded")]
112    G1ProjectiveDeserializationFailure,
113
114    #[error("failed to deserialize G2Projective point from the received bytes - it might not have been canonically encoded")]
115    G2ProjectiveDeserializationFailure,
116
117    #[error("verification key is invalid for this operation")]
118    VerificationKeyTooShort,
119
120    #[error("did not provide the sufficient number of coin index signatures")]
121    InsufficientNumberOfIndexSignatures,
122
123    #[error("did not provide the sufficient number of expiration date signatures")]
124    InsufficientNumberOfExpirationSignatures,
125
126    //context : This can happen only if the wallet secret `v` was picked such that `v + coin_index + 1 == 0`.
127    //The chance of this happening is of the order 2^-381 and not failing is waay too much work.
128    //TLDR: this event can happen, but with probability 0
129    #[error("you're one of the most unluck person on your planet and your wallet cannot complete this payment")]
130    UnluckiestError,
131}