Skip to main content

frost_core/
error.rs

1//! FROST Error types
2
3use crate::{Ciphersuite, Identifier};
4use alloc::vec::Vec;
5use thiserror::Error;
6
7/// An error related to FROST.
8#[non_exhaustive]
9#[derive(Error, Debug, Clone, Eq, PartialEq)]
10pub enum Error<C: Ciphersuite> {
11    /// min_signers is invalid
12    #[error("min_signers must be at least 2 and not larger than max_signers")]
13    InvalidMinSigners,
14    /// max_signers is invalid
15    #[error("max_signers must be at least 2")]
16    InvalidMaxSigners,
17    /// max_signers is invalid
18    #[error("coefficients must have min_signers-1 elements")]
19    InvalidCoefficients,
20    /// This identifier is unserializable.
21    #[error("Malformed identifier is unserializable.")]
22    MalformedIdentifier,
23    /// This identifier is duplicated.
24    #[error("Duplicated identifier.")]
25    DuplicatedIdentifier,
26    /// This identifier does not belong to a participant in the signing process.
27    #[error("Unknown identifier.")]
28    UnknownIdentifier,
29    /// Incorrect number of identifiers.
30    #[error("Incorrect number of identifiers.")]
31    IncorrectNumberOfIdentifiers,
32    /// The encoding of a signing key was malformed.
33    #[error("Malformed signing key encoding.")]
34    MalformedSigningKey,
35    /// The encoding of a verifying key was malformed.
36    #[error("Malformed verifying key encoding.")]
37    MalformedVerifyingKey,
38    /// The encoding of a signature was malformed.
39    #[error("Malformed signature encoding.")]
40    MalformedSignature,
41    /// Signature verification failed.
42    #[error("Invalid signature.")]
43    InvalidSignature,
44    /// Duplicated shares provided
45    #[error("Duplicated shares provided.")]
46    DuplicatedShares,
47    /// Incorrect number of shares.
48    #[error("Incorrect number of shares.")]
49    IncorrectNumberOfShares,
50    /// Commitment equals the identity
51    #[error("Commitment equals the identity.")]
52    IdentityCommitment,
53    /// The participant's commitment is missing from the Signing Package
54    #[error("The Signing Package must contain the participant's Commitment.")]
55    MissingCommitment,
56    /// The participant's commitment is incorrect
57    #[error("The participant's commitment is incorrect.")]
58    IncorrectCommitment,
59    /// Incorrect number of commitments.
60    #[error("Incorrect number of commitments.")]
61    IncorrectNumberOfCommitments,
62    /// Signature share verification failed.
63    #[error("Invalid signature share.")]
64    InvalidSignatureShare {
65        /// The identifier of the signer whose share validation failed.
66        culprits: Vec<Identifier<C>>,
67    },
68    /// Secret share verification failed.
69    #[error("Invalid secret share.")]
70    InvalidSecretShare {
71        /// The identifier of the signer whose secret share validation failed,
72        /// if possible to identify.
73        culprit: Option<Identifier<C>>,
74    },
75    /// Round 1 package not found for Round 2 participant.
76    #[error("Round 1 package not found for Round 2 participant.")]
77    PackageNotFound,
78    /// Incorrect number of packages.
79    #[error("Incorrect number of packages.")]
80    IncorrectNumberOfPackages,
81    /// The incorrect package was specified.
82    #[error("The incorrect package was specified.")]
83    IncorrectPackage,
84    /// The ciphersuite does not support DKG.
85    #[error("The ciphersuite does not support DKG.")]
86    DKGNotSupported,
87    /// The proof of knowledge is not valid.
88    #[error("The proof of knowledge is not valid.")]
89    InvalidProofOfKnowledge {
90        /// The identifier of the signer whose share validation failed.
91        culprit: Identifier<C>,
92    },
93    /// Error in scalar Field.
94    #[error("Error in scalar Field.")]
95    FieldError(#[from] FieldError),
96    /// Error in elliptic curve Group.
97    #[error("Error in elliptic curve Group.")]
98    GroupError(#[from] GroupError),
99    /// Error in coefficient commitment deserialization.
100    #[error("Invalid coefficient")]
101    InvalidCoefficient,
102    /// The ciphersuite does not support deriving identifiers from strings.
103    #[error("The ciphersuite does not support deriving identifiers from strings.")]
104    IdentifierDerivationNotSupported,
105    /// Error serializing value.
106    #[error("Error serializing value.")]
107    SerializationError,
108    /// Error deserializing value.
109    #[error("Error deserializing value.")]
110    DeserializationError,
111}
112
113impl<C> Error<C>
114where
115    C: Ciphersuite,
116{
117    /// Return the identifiers of the participants that caused the error.
118    /// Returns an empty vector if not applicable for the error.
119    ///
120    /// This can be used to penalize participants that do not follow the
121    /// protocol correctly, e.g. removing them from further signings.
122    pub fn culprits(&self) -> Vec<Identifier<C>> {
123        // Use an exhaustive match to make sure that if we add new enum items
124        // then we will explicitly check if they should be added here.
125        match self {
126            Error::InvalidSignatureShare { culprits } => culprits.clone(),
127            Error::InvalidProofOfKnowledge { culprit } => vec![*culprit],
128            Error::InvalidSecretShare { culprit } => culprit.map(|i| vec![i]).unwrap_or_default(),
129            Error::InvalidMinSigners
130            | Error::InvalidMaxSigners
131            | Error::InvalidCoefficients
132            | Error::MalformedIdentifier
133            | Error::MalformedSigningKey
134            | Error::MalformedVerifyingKey
135            | Error::MalformedSignature
136            | Error::InvalidSignature
137            | Error::DuplicatedShares
138            | Error::IncorrectNumberOfShares
139            | Error::IdentityCommitment
140            | Error::MissingCommitment
141            | Error::IncorrectCommitment
142            | Error::PackageNotFound
143            | Error::IncorrectNumberOfPackages
144            | Error::IncorrectPackage
145            | Error::DKGNotSupported
146            | Error::FieldError(_)
147            | Error::GroupError(_)
148            | Error::DuplicatedIdentifier
149            | Error::InvalidCoefficient
150            | Error::UnknownIdentifier
151            | Error::IncorrectNumberOfIdentifiers
152            | Error::IncorrectNumberOfCommitments
153            | Error::SerializationError
154            | Error::DeserializationError
155            | Error::IdentifierDerivationNotSupported => vec![],
156        }
157    }
158}
159
160/// An error related to a scalar Field.
161#[non_exhaustive]
162#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
163pub enum FieldError {
164    /// The encoding of a group scalar was malformed.
165    #[error("Malformed scalar encoding.")]
166    MalformedScalar,
167    /// This scalar MUST NOT be zero.
168    #[error("Invalid for this scalar to be zero.")]
169    InvalidZeroScalar,
170}
171
172/// An error related to a Group (usually an elliptic curve or constructed from one) or one of its Elements.
173#[non_exhaustive]
174#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
175pub enum GroupError {
176    /// The encoding of a group element was malformed.
177    #[error("Malformed group element encoding.")]
178    MalformedElement,
179    /// This element MUST NOT be the identity.
180    #[error("Invalid for this element to be the identity.")]
181    InvalidIdentityElement,
182    /// This element MUST have (large) prime order.
183    #[error("Invalid for this element to not have large prime order.")]
184    InvalidNonPrimeOrderElement,
185}