Skip to main content

sigma_proofs/
errors.rs

1//! # Error: Error Types for Zero-Knowledge Proofs.
2//!
3//! This module defines the [`Error`] enum, which enumerates the possible failure modes
4//! encountered during the execution of interactive or non-interactive Sigma protocols.
5//!
6//! These errors include:
7//! - Failed proof verification,
8//! - Mismatched parameter lengths (e.g., during batch verification),
9//! - Access to unassigned group variables in constraint systems.
10
11use alloc::string::String;
12#[cfg(not(feature = "std"))]
13use core::fmt;
14
15/// Represents an invalid instance error.
16#[derive(Debug)]
17#[cfg_attr(feature = "std", derive(thiserror::Error))]
18#[cfg_attr(feature = "std", error("Invalid instance: {message}"))]
19pub struct InvalidInstance {
20    /// The error message describing what's invalid about the instance.
21    pub message: String,
22}
23
24impl InvalidInstance {
25    /// Create a new InvalidInstance error with the given message.
26    pub fn new(message: impl Into<String>) -> Self {
27        Self {
28            message: message.into(),
29        }
30    }
31}
32
33impl From<InvalidInstance> for Error {
34    fn from(_err: InvalidInstance) -> Self {
35        Error::InvalidInstanceWitnessPair
36    }
37}
38
39/// Represents an error encountered during the execution of a Sigma protocol.
40///
41/// This may occur during proof generation, response computation, or verification.
42#[non_exhaustive]
43#[derive(Debug)]
44#[cfg_attr(feature = "std", derive(thiserror::Error))]
45pub enum Error {
46    /// The proof is invalid: verification failed.
47    #[cfg_attr(feature = "std", error("Verification failed."))]
48    VerificationFailure,
49    /// Indicates an invalid statement/witness pair
50    #[cfg_attr(feature = "std", error("Invalid instance/witness pair."))]
51    InvalidInstanceWitnessPair,
52    /// Uninitialized group element variable.
53    #[cfg_attr(
54        feature = "std",
55        error("Uninitialized group element variable: {var_debug}")
56    )]
57    UnassignedGroupVar {
58        /// Debug representation of the unassigned variable.
59        var_debug: String,
60    },
61}
62
63impl From<spongefish::VerificationError> for Error {
64    fn from(_value: spongefish::VerificationError) -> Self {
65        Error::VerificationFailure
66    }
67}
68
69// Manual Display implementation for no_std compatibility
70#[cfg(not(feature = "std"))]
71impl fmt::Display for InvalidInstance {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        write!(f, "Invalid instance: {}", self.message)
74    }
75}
76
77#[cfg(not(feature = "std"))]
78impl fmt::Display for Error {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        match self {
81            Error::VerificationFailure => write!(f, "Verification failed."),
82            Error::InvalidInstanceWitnessPair => write!(f, "Invalid instance/witness pair."),
83            Error::UnassignedGroupVar { var_debug } => {
84                write!(f, "Uninitialized group element variable: {}", var_debug)
85            }
86        }
87    }
88}
89
90pub type Result<T> = core::result::Result<T, Error>;
91
92/// Construct an `Ok` value of type `Result<T, sigma_proofs::errors::Error>`.
93pub const fn Ok<T>(value: T) -> Result<T> {
94    Result::Ok(value)
95}