ark_bulletproofs/
errors.rs

1//! Errors related to proving and verifying proofs.
2
3use ark_serialize::SerializationError;
4use ark_std::{
5    fmt,
6    string::{String, ToString},
7    vec::Vec,
8};
9
10/// Represents an error in proof creation, verification, or parsing.
11#[derive(Clone, Eq, PartialEq)]
12pub enum ProofError {
13    /// This error occurs when a proof failed to verify.
14    VerificationError,
15    /// This error occurs when the proof encoding is malformed.
16    FormatError,
17    /// This error occurs during proving if the number of blinding
18    /// factors does not match the number of values.
19    WrongNumBlindingFactors,
20    /// This error occurs when attempting to create a proof with
21    /// bitsize other than \\(8\\), \\(16\\), \\(32\\), or \\(64\\).
22    InvalidBitsize,
23    /// This error occurs when attempting to create an aggregated
24    /// proof with non-power-of-two aggregation size.
25    InvalidAggregation,
26    /// This error occurs when there are insufficient generators for the proof.
27    InvalidGeneratorsLength,
28    /// This error results from an internal error during proving.
29    ///
30    /// The single-party prover is implemented by performing
31    /// multiparty computation with ourselves.  However, because the
32    /// MPC protocol is not exposed by the single-party API, we
33    /// consider its errors to be internal errors.
34    ProvingError(MPCError),
35    /// This error occurs if serialization fails
36    SerializationError(String),
37}
38
39impl fmt::Debug for ProofError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            ProofError::VerificationError => write!(f, "Proof verification failed."),
43            ProofError::FormatError => write!(f, "Proof data could not be parsed."),
44            ProofError::WrongNumBlindingFactors => {
45                write!(f, "Wrong number of blinding factors supplied.")
46            }
47            ProofError::InvalidBitsize => write!(f, "Invalid bitsize, must have n = 8,16,32,64."),
48            ProofError::InvalidAggregation => {
49                write!(f, "Invalid aggregation size, m must be a power of 2.")
50            }
51            ProofError::InvalidGeneratorsLength => {
52                write!(f, "Invalid generators size, too few generators for proof")
53            }
54            ProofError::ProvingError(e) => {
55                write!(f, "Internal error during proof creation: {:?}", e)
56            }
57            ProofError::SerializationError(e) => {
58                write!(f, "Serialization error: {}", e)
59            }
60        }
61    }
62}
63
64impl fmt::Display for ProofError {
65    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66        write!(f, "{:?}", self)
67    }
68}
69
70impl From<MPCError> for ProofError {
71    fn from(e: MPCError) -> ProofError {
72        match e {
73            MPCError::InvalidBitsize => ProofError::InvalidBitsize,
74            MPCError::InvalidAggregation => ProofError::InvalidAggregation,
75            MPCError::InvalidGeneratorsLength => ProofError::InvalidGeneratorsLength,
76            _ => ProofError::ProvingError(e),
77        }
78    }
79}
80
81/// Represents an error during the multiparty computation protocol for
82/// proof aggregation.
83///
84/// This is a separate type from the `ProofError` to allow a layered
85/// API: although the MPC protocol is used internally for single-party
86/// proving, its API should not expose the complexity of the MPC
87/// protocol.
88#[derive(Clone, Eq, PartialEq)]
89pub enum MPCError {
90    /// This error occurs when the dealer gives a zero challenge,
91    /// which would annihilate the blinding factors.
92    MaliciousDealer,
93    /// This error occurs when attempting to create a proof with
94    /// bitsize other than \\(8\\), \\(16\\), \\(32\\), or \\(64\\).
95    InvalidBitsize,
96    /// This error occurs when attempting to create an aggregated
97    /// proof with non-power-of-two aggregation size.
98    InvalidAggregation,
99    /// This error occurs when there are insufficient generators for the proof.
100    InvalidGeneratorsLength,
101    /// This error occurs when the dealer is given the wrong number of
102    /// value commitments.
103    WrongNumBitCommitments,
104    /// This error occurs when the dealer is given the wrong number of
105    /// polynomial commitments.
106    WrongNumPolyCommitments,
107    /// This error occurs when the dealer is given the wrong number of
108    /// proof shares.
109    WrongNumProofShares,
110    /// This error occurs when one or more parties submit malformed
111    /// proof shares.
112    MalformedProofShares {
113        /// A vector with the indexes of the parties whose shares were malformed.
114        bad_shares: Vec<usize>,
115    },
116}
117
118impl fmt::Debug for MPCError {
119    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120        match self {
121            MPCError::MaliciousDealer => write!(f, "Dealer gave a malicious challenge value."),
122            MPCError::InvalidBitsize => write!(f, "Invalid bitsize, must have n = 8,16,32,64"),
123            MPCError::InvalidAggregation => {
124                write!(f, "Invalid aggregation size, m must be a power of 2")
125            }
126            MPCError::InvalidGeneratorsLength => {
127                write!(f, "Invalid generators size, too few generators for proof")
128            }
129            MPCError::WrongNumBitCommitments => write!(f, "Wrong number of value commitments"),
130            MPCError::WrongNumPolyCommitments => write!(f, "Wrong number of value commitments"),
131            MPCError::WrongNumProofShares => write!(f, "Wrong number of proof shares"),
132            MPCError::MalformedProofShares { bad_shares } => {
133                write!(f, "Malformed proof shares from parties {:?}", bad_shares)
134            }
135        }
136    }
137}
138
139impl fmt::Display for MPCError {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        write!(f, "{:?}", self)
142    }
143}
144
145/// Represents an error during the proving or verifying of a constraint system.
146///
147/// XXX: should this be separate from a `ProofError`?
148#[cfg(feature = "yoloproofs")]
149#[derive(Clone, Eq, PartialEq)]
150pub enum R1CSError {
151    /// Occurs when there are insufficient generators for the proof.
152    InvalidGeneratorsLength,
153    /// This error occurs when the proof encoding is malformed.
154    FormatError,
155    /// Occurs when verification of an
156    /// [`R1CSProof`](::r1cs::R1CSProof) fails.
157    VerificationError,
158    /// Occurs when trying to use a missing variable assignment.
159    /// Used by gadgets that build the constraint system to signal that
160    /// a variable assignment is not provided when the prover needs it.
161    MissingAssignment,
162    /// Occurs when a gadget receives an inconsistent input.
163    GadgetError {
164        /// The description of the reasons for the error.
165        description: String,
166    },
167}
168
169impl fmt::Debug for R1CSError {
170    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171        match self {
172            R1CSError::InvalidGeneratorsLength => {
173                write!(f, "Invalid generators size, too few generators for proof")
174            }
175            R1CSError::FormatError => write!(f, "Proof data could not be parsed."),
176            R1CSError::VerificationError => write!(f, "R1CSProof did not verify correctly."),
177            R1CSError::MissingAssignment => write!(f, "Variable does not have a value assignment."),
178            R1CSError::GadgetError { description } => write!(f, "Gadget error: {}", description),
179        }
180    }
181}
182
183impl fmt::Display for R1CSError {
184    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
185        write!(f, "{:?}", self)
186    }
187}
188
189#[cfg(feature = "yoloproofs")]
190impl From<ProofError> for R1CSError {
191    fn from(e: ProofError) -> R1CSError {
192        match e {
193            ProofError::InvalidGeneratorsLength => R1CSError::InvalidGeneratorsLength,
194            ProofError::FormatError => R1CSError::FormatError,
195            ProofError::VerificationError => R1CSError::VerificationError,
196            _ => panic!("unexpected error type in conversion"),
197        }
198    }
199}
200
201impl From<ark_std::io::Error> for ProofError {
202    fn from(e: ark_std::io::Error) -> ProofError {
203        ProofError::SerializationError(e.to_string())
204    }
205}
206
207impl From<SerializationError> for ProofError {
208    fn from(_: ark_serialize::SerializationError) -> ProofError {
209        ProofError::FormatError
210    }
211}