challenge_bypass_ristretto/
errors.rs

1//! Errors which may occur when parsing keys and/or tokens to or from wire formats,
2//! or verifying proofs.
3
4use core::fmt;
5use core::fmt::Display;
6
7#[cfg(feature = "std")]
8use std::error::Error;
9
10/// Internal errors.  Most application-level developers will likely not
11/// need to pay any attention to these.
12#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
13pub enum InternalError {
14    /// An error occured when converting from a `CompressedRistretto` to a `RistrettoPoint`
15    PointDecompressionError,
16    /// An error occured when interpretting bytes as a scalar
17    ScalarFormatError,
18    /// An error in the length of bytes handed to a constructor.
19    BytesLengthError {
20        /// The name of the type
21        name: &'static str,
22        /// The expected number of bytes
23        length: usize,
24    },
25    /// Verification failed
26    VerifyError,
27    /// Inputs differed in length
28    LengthMismatchError,
29    /// Decoding failed
30    DecodingError,
31}
32
33impl Display for InternalError {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        match *self {
36            InternalError::PointDecompressionError => write!(f, "Cannot decompress Edwards point"),
37            InternalError::ScalarFormatError => {
38                write!(f, "Input bytes were not in canonical representation")
39            }
40            InternalError::BytesLengthError { name: n, length: l } => {
41                write!(f, "{} must be {} bytes in length", n, l)
42            }
43            InternalError::VerifyError => write!(f, "Verification failed"),
44            InternalError::LengthMismatchError => write!(f, "Inputs differed in length"),
45            InternalError::DecodingError => write!(f, "Decoding failed"),
46        }
47    }
48}
49
50#[cfg(feature = "std")]
51impl Error for InternalError {}
52
53/// Errors when keys and/or tokens to or from wire formats, or verifying proofs.
54#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
55pub struct TokenError(pub InternalError);
56
57impl Display for TokenError {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        write!(f, "{}", self.0)
60    }
61}
62
63#[cfg(feature = "std")]
64impl Error for TokenError {
65    fn source(&self) -> Option<&(dyn Error + 'static)> {
66        Some(&self.0)
67    }
68}