mithril_stm/protocol/
error.rs

1//! Crate specific errors
2use anyhow::anyhow;
3use blst::BLST_ERROR;
4
5use crate::{
6    AggregateSignatureType, StmResult,
7    signature_scheme::{BlsSignature, BlsVerificationKey, BlsVerificationKeyProofOfPossession},
8};
9
10/// Error types for multi signatures.
11#[derive(Debug, thiserror::Error, Eq, PartialEq)]
12pub enum MultiSignatureError {
13    /// Invalid Single signature
14    #[error("Invalid single signature")]
15    SignatureInvalid(BlsSignature),
16
17    /// Invalid aggregate signature
18    #[error("Invalid aggregated signature")]
19    AggregateSignatureInvalid,
20
21    /// This error occurs when the the serialization of the raw bytes failed
22    #[error("Invalid bytes")]
23    SerializationError,
24
25    /// Incorrect proof of possession
26    #[error("Key with invalid PoP")]
27    KeyInvalid(Box<BlsVerificationKeyProofOfPossession>),
28
29    /// At least one signature in the batch is invalid
30    #[error("One signature in the batch is invalid")]
31    BatchInvalid,
32
33    /// Single signature is the infinity
34    #[error("Single signature is the infinity")]
35    SignatureInfinity(BlsSignature),
36
37    /// Verification key is the infinity
38    #[error("Verification key is the infinity")]
39    VerificationKeyInfinity(Box<BlsVerificationKey>),
40}
41
42/// Error types related to merkle trees.
43#[derive(Debug, Clone, thiserror::Error)]
44pub enum MerkleTreeError {
45    /// Serialization error
46    #[error("Serialization of a merkle tree failed")]
47    SerializationError,
48
49    /// Invalid merkle path
50    #[error("Path does not verify against root")]
51    PathInvalid(Vec<u8>),
52
53    /// Invalid merkle batch path
54    #[error("Batch path does not verify against root")]
55    BatchPathInvalid(Vec<u8>),
56}
57
58/// Errors which can be output by Mithril single signature verification.
59#[derive(Debug, Clone, thiserror::Error)]
60pub enum SignatureError {
61    /// There is an index out of bounds
62    #[error("Received index, {0}, is higher than what the security parameter allows, {1}.")]
63    IndexBoundFailed(u64, u64),
64
65    /// The lottery was actually lost for the signature
66    #[error("Lottery for this epoch was lost.")]
67    LotteryLost,
68
69    /// This error occurs when the the serialization of the raw bytes failed
70    #[error("Invalid bytes")]
71    SerializationError,
72}
73
74/// Error types for aggregation.
75#[derive(Debug, Clone, thiserror::Error)]
76pub enum AggregationError {
77    /// Not enough signatures were collected, got this many instead.
78    #[error("Not enough signatures. Got only {0} out of {1}.")]
79    NotEnoughSignatures(u64, u64),
80
81    #[error("Unsupported proof system: {0}")]
82    UnsupportedProofSystem(AggregateSignatureType),
83
84    /// There is a duplicate index
85    #[error("Indices are not unique.")]
86    IndexNotUnique,
87}
88
89/// Errors which can be output by Mithril aggregate verification.
90#[derive(Debug, Clone, thiserror::Error)]
91pub enum AggregateSignatureError {
92    /// This error occurs when the the serialization of the raw bytes failed
93    #[error("Invalid bytes")]
94    SerializationError,
95
96    /// Batch verification of STM aggregate signatures failed
97    #[error("Batch verification of STM aggregate signatures failed")]
98    BatchInvalid,
99
100    /// The proof system used in the aggregate signature is not supported
101    #[error("Unsupported proof system: {0}")]
102    UnsupportedProofSystem(AggregateSignatureType),
103}
104
105/// Errors which can be outputted by key registration.
106#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
107pub enum RegisterError {
108    /// This key has already been registered by a participant
109    #[error("This key has already been registered.")]
110    KeyRegistered(Box<BlsVerificationKey>),
111
112    /// The supplied key is not valid
113    #[error("The verification of correctness of the supplied key is invalid.")]
114    KeyInvalid(Box<BlsVerificationKeyProofOfPossession>),
115
116    /// Serialization error
117    #[error("Serialization error")]
118    SerializationError,
119
120    /// UnregisteredInitializer error
121    #[error("Initializer not registered. Cannot participate as a signer.")]
122    UnregisteredInitializer,
123}
124
125pub fn blst_error_to_stm_error(
126    e: BLST_ERROR,
127    sig: Option<BlsSignature>,
128    key: Option<BlsVerificationKey>,
129) -> StmResult<()> {
130    match e {
131        BLST_ERROR::BLST_SUCCESS => Ok(()),
132        BLST_ERROR::BLST_PK_IS_INFINITY => {
133            if let Some(s) = sig {
134                return Err(anyhow!(MultiSignatureError::SignatureInfinity(s)));
135            }
136            if let Some(vk) = key {
137                return Err(anyhow!(MultiSignatureError::VerificationKeyInfinity(
138                    Box::new(vk)
139                )));
140            }
141            Err(anyhow!(MultiSignatureError::SerializationError))
142        }
143        BLST_ERROR::BLST_VERIFY_FAIL => {
144            if let Some(s) = sig {
145                Err(anyhow!(MultiSignatureError::SignatureInvalid(s)))
146            } else {
147                Err(anyhow!(MultiSignatureError::AggregateSignatureInvalid))
148            }
149        }
150        _ => Err(anyhow!(MultiSignatureError::SerializationError)),
151    }
152}