safe_zk_token_sdk/
errors.rs

1//! Errors related to proving and verifying proofs.
2use {
3    crate::{range_proof::errors::RangeProofError, sigma_proofs::errors::*},
4    thiserror::Error,
5};
6
7#[derive(Error, Clone, Debug, Eq, PartialEq)]
8pub enum ProofError {
9    #[error("invalid transfer amount range")]
10    TransferAmount,
11    #[error("proof generation failed")]
12    Generation,
13    #[error("proof verification failed")]
14    VerificationError(ProofType, ProofVerificationError),
15    #[error("failed to decrypt ciphertext")]
16    Decryption,
17    #[error("invalid ciphertext data")]
18    CiphertextDeserialization,
19    #[error("invalid pubkey data")]
20    PubkeyDeserialization,
21    #[error("ciphertext does not exist in instruction data")]
22    MissingCiphertext,
23}
24
25#[derive(Clone, Debug, Eq, PartialEq)]
26pub enum ProofType {
27    EqualityProof,
28    ValidityProof,
29    ZeroBalanceProof,
30    FeeSigmaProof,
31    PubkeyValidityProof,
32    RangeProof,
33}
34
35#[derive(Error, Clone, Debug, Eq, PartialEq)]
36pub enum ProofVerificationError {
37    #[error("required algebraic relation does not hold")]
38    AlgebraicRelation,
39    #[error("malformed proof")]
40    Deserialization,
41    #[error("multiscalar multiplication failed")]
42    MultiscalarMul,
43    #[error("transcript failed to produce a challenge")]
44    Transcript(#[from] TranscriptError),
45    #[error(
46        "attempted to verify range proof with a non-power-of-two bit size or bit size is too big"
47    )]
48    InvalidBitSize,
49    #[error("insufficient generators for the proof")]
50    InvalidGeneratorsLength,
51    #[error("number of blinding factors do not match the number of values")]
52    WrongNumBlindingFactors,
53}
54
55#[derive(Error, Clone, Debug, Eq, PartialEq)]
56pub enum TranscriptError {
57    #[error("point is the identity")]
58    ValidationError,
59}
60
61impl From<RangeProofError> for ProofError {
62    fn from(err: RangeProofError) -> Self {
63        Self::VerificationError(ProofType::RangeProof, err.0)
64    }
65}
66
67impl From<EqualityProofError> for ProofError {
68    fn from(err: EqualityProofError) -> Self {
69        Self::VerificationError(ProofType::EqualityProof, err.0)
70    }
71}
72
73impl From<FeeSigmaProofError> for ProofError {
74    fn from(err: FeeSigmaProofError) -> Self {
75        Self::VerificationError(ProofType::FeeSigmaProof, err.0)
76    }
77}
78
79impl From<ZeroBalanceProofError> for ProofError {
80    fn from(err: ZeroBalanceProofError) -> Self {
81        Self::VerificationError(ProofType::ZeroBalanceProof, err.0)
82    }
83}
84impl From<ValidityProofError> for ProofError {
85    fn from(err: ValidityProofError) -> Self {
86        Self::VerificationError(ProofType::ValidityProof, err.0)
87    }
88}
89
90impl From<PubkeyValidityProofError> for ProofError {
91    fn from(err: PubkeyValidityProofError) -> Self {
92        Self::VerificationError(ProofType::PubkeyValidityProof, err.0)
93    }
94}