mithril_stm/protocol/
error.rs1use anyhow::anyhow;
3use blst::BLST_ERROR;
4
5use crate::{
6 AggregateSignatureType, StmResult,
7 signature_scheme::{BlsSignature, BlsVerificationKey, BlsVerificationKeyProofOfPossession},
8};
9
10#[derive(Debug, thiserror::Error, Eq, PartialEq)]
12pub enum MultiSignatureError {
13 #[error("Invalid single signature")]
15 SignatureInvalid(BlsSignature),
16
17 #[error("Invalid aggregated signature")]
19 AggregateSignatureInvalid,
20
21 #[error("Invalid bytes")]
23 SerializationError,
24
25 #[error("Key with invalid PoP")]
27 KeyInvalid(Box<BlsVerificationKeyProofOfPossession>),
28
29 #[error("One signature in the batch is invalid")]
31 BatchInvalid,
32
33 #[error("Single signature is the infinity")]
35 SignatureInfinity(BlsSignature),
36
37 #[error("Verification key is the infinity")]
39 VerificationKeyInfinity(Box<BlsVerificationKey>),
40}
41
42#[derive(Debug, Clone, thiserror::Error)]
44pub enum MerkleTreeError {
45 #[error("Serialization of a merkle tree failed")]
47 SerializationError,
48
49 #[error("Path does not verify against root")]
51 PathInvalid(Vec<u8>),
52
53 #[error("Batch path does not verify against root")]
55 BatchPathInvalid(Vec<u8>),
56}
57
58#[derive(Debug, Clone, thiserror::Error)]
60pub enum SignatureError {
61 #[error("Received index, {0}, is higher than what the security parameter allows, {1}.")]
63 IndexBoundFailed(u64, u64),
64
65 #[error("Lottery for this epoch was lost.")]
67 LotteryLost,
68
69 #[error("Invalid bytes")]
71 SerializationError,
72}
73
74#[derive(Debug, Clone, thiserror::Error)]
76pub enum AggregationError {
77 #[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 #[error("Indices are not unique.")]
86 IndexNotUnique,
87}
88
89#[derive(Debug, Clone, thiserror::Error)]
91pub enum AggregateSignatureError {
92 #[error("Invalid bytes")]
94 SerializationError,
95
96 #[error("Batch verification of STM aggregate signatures failed")]
98 BatchInvalid,
99
100 #[error("Unsupported proof system: {0}")]
102 UnsupportedProofSystem(AggregateSignatureType),
103}
104
105#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
107pub enum RegisterError {
108 #[error("This key has already been registered.")]
110 KeyRegistered(Box<BlsVerificationKey>),
111
112 #[error("The verification of correctness of the supplied key is invalid.")]
114 KeyInvalid(Box<BlsVerificationKeyProofOfPossession>),
115
116 #[error("Serialization error")]
118 SerializationError,
119
120 #[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}