Skip to content

Commit 8fc91b8

Browse files
committed
resolved the issue of mapping RegisterError::KeyInvalid
1 parent a22a5c5 commit 8fc91b8

File tree

8 files changed

+35
-40
lines changed

8 files changed

+35
-40
lines changed

mithril-stm/src/aggregate_signature/proof/concatenation.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::bls_multi_signature::{BlsSignature, BlsVerificationKey};
88
use crate::key_registration::RegisteredParty;
99
use crate::merkle_tree::MerkleBatchPath;
1010
use crate::{
11-
AggregateVerificationKey, BasicVerifier, Parameters, SingleSignature,
12-
SingleSignatureWithRegisteredParty, StmAggregateSignatureError, StmResult,
11+
AggregateSignatureError, AggregateVerificationKey, BasicVerifier, Parameters, SingleSignature,
12+
SingleSignatureWithRegisteredParty, StmResult,
1313
};
1414

1515
/// `ConcatenationProof` uses the "concatenation" proving system (as described in Section 4.3 of the original paper.)
@@ -211,25 +211,25 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> ConcatenationProof<D> {
211211
u64_bytes.copy_from_slice(
212212
bytes
213213
.get(bytes_index..bytes_index + 8)
214-
.ok_or(StmAggregateSignatureError::SerializationError)?,
214+
.ok_or(AggregateSignatureError::SerializationError)?,
215215
);
216216
let total_sigs = usize::try_from(u64::from_be_bytes(u64_bytes))
217-
.map_err(|_| StmAggregateSignatureError::SerializationError)?;
217+
.map_err(|_| AggregateSignatureError::SerializationError)?;
218218
bytes_index += 8;
219219

220220
let mut sig_reg_list = Vec::with_capacity(total_sigs);
221221
for _ in 0..total_sigs {
222222
u64_bytes.copy_from_slice(
223223
bytes
224224
.get(bytes_index..bytes_index + 8)
225-
.ok_or(StmAggregateSignatureError::SerializationError)?,
225+
.ok_or(AggregateSignatureError::SerializationError)?,
226226
);
227227
let sig_reg_size = usize::try_from(u64::from_be_bytes(u64_bytes))
228-
.map_err(|_| StmAggregateSignatureError::SerializationError)?;
228+
.map_err(|_| AggregateSignatureError::SerializationError)?;
229229
let sig_reg = SingleSignatureWithRegisteredParty::from_bytes::<D>(
230230
bytes
231231
.get(bytes_index + 8..bytes_index + 8 + sig_reg_size)
232-
.ok_or(StmAggregateSignatureError::SerializationError)?,
232+
.ok_or(AggregateSignatureError::SerializationError)?,
233233
)?;
234234
bytes_index += 8 + sig_reg_size;
235235
sig_reg_list.push(sig_reg);
@@ -238,7 +238,7 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> ConcatenationProof<D> {
238238
let batch_proof = MerkleBatchPath::from_bytes(
239239
bytes
240240
.get(bytes_index..)
241-
.ok_or(StmAggregateSignatureError::SerializationError)?,
241+
.ok_or(AggregateSignatureError::SerializationError)?,
242242
)?;
243243

244244
Ok(ConcatenationProof {

mithril-stm/src/aggregate_signature/signature.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::hash::Hash;
66
use blake2::digest::{Digest, FixedOutput};
77
use serde::{Deserialize, Serialize};
88

9-
use crate::error::StmAggregateSignatureError;
9+
use crate::error::AggregateSignatureError;
1010
use crate::merkle_tree::MerkleBatchPath;
1111
use crate::{AggregateVerificationKey, Parameters, StmResult};
1212

@@ -103,7 +103,7 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
103103
}
104104
#[cfg(feature = "future_proof_system")]
105105
AggregateSignature::Future => Err(anyhow!(
106-
StmAggregateSignatureError::UnsupportedProofSystem(self.into())
106+
AggregateSignatureError::UnsupportedProofSystem(self.into())
107107
)),
108108
}
109109
}
@@ -131,7 +131,7 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
131131
.filter_map(|s| s.to_concatenation_proof().cloned())
132132
.collect::<Vec<_>>();
133133
if concatenation_proofs.len() != aggregate_signatures_length {
134-
return Err(anyhow!(StmAggregateSignatureError::BatchInvalid));
134+
return Err(anyhow!(AggregateSignatureError::BatchInvalid));
135135
}
136136

137137
ConcatenationProof::batch_verify(
@@ -142,14 +142,12 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
142142
)
143143
}
144144
#[cfg(feature = "future_proof_system")]
145-
AggregateSignatureType::Future => {
146-
Err(anyhow!(StmAggregateSignatureError::UnsupportedProofSystem(
147-
aggregate_signature_type
148-
)))
149-
}
145+
AggregateSignatureType::Future => Err(anyhow!(
146+
AggregateSignatureError::UnsupportedProofSystem(aggregate_signature_type)
147+
)),
150148
}
151149
})
152-
.map_err(|_| anyhow!(StmAggregateSignatureError::BatchInvalid))
150+
.map_err(|_| anyhow!(AggregateSignatureError::BatchInvalid))
153151
}
154152

155153
/// Convert an aggregate signature to bytes
@@ -173,11 +171,10 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
173171

174172
/// Extract an aggregate signature from a byte slice.
175173
pub fn from_bytes(bytes: &[u8]) -> StmResult<Self> {
176-
let proof_type_byte =
177-
bytes.first().ok_or(StmAggregateSignatureError::SerializationError)?;
174+
let proof_type_byte = bytes.first().ok_or(AggregateSignatureError::SerializationError)?;
178175
let proof_bytes = &bytes[1..];
179176
let proof_type = AggregateSignatureType::from_byte_encoding_prefix(*proof_type_byte)
180-
.ok_or(StmAggregateSignatureError::SerializationError)?;
177+
.ok_or(AggregateSignatureError::SerializationError)?;
181178
match proof_type {
182179
AggregateSignatureType::Concatenation => Ok(AggregateSignature::Concatenation(
183180
ConcatenationProof::from_bytes(proof_bytes)?,

mithril-stm/src/bls_multi_signature/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ mod tests {
9494
use rand_chacha::ChaCha20Rng;
9595
use rand_core::{RngCore, SeedableRng};
9696

97+
use crate::RegisterError;
9798
use crate::bls_multi_signature::helper::unsafe_helpers::{p1_affine_to_sig, p2_affine_to_vk};
9899
use crate::error::MultiSignatureError;
99100
use crate::key_registration::KeyRegistration;
@@ -199,8 +200,8 @@ mod tests {
199200

200201
assert!(
201202
matches!(
202-
error.downcast_ref::<MultiSignatureError>(),
203-
Some(MultiSignatureError::VerificationKeyInfinity(_))
203+
error.downcast_ref::<RegisterError>(),
204+
Some(RegisterError::KeyInvalid(_))
204205
),
205206
"Unexpected error: {error:?}");
206207
}

mithril-stm/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub enum MerkleTreeError {
5858

5959
/// Errors which can be output by Mithril single signature verification.
6060
#[derive(Debug, Clone, thiserror::Error)]
61-
pub enum StmSignatureError {
61+
pub enum SignatureError {
6262
/// There is an index out of bounds
6363
#[error("Received index, {0}, is higher than what the security parameter allows, {1}.")]
6464
IndexBoundFailed(u64, u64),
@@ -89,7 +89,7 @@ pub enum AggregationError {
8989

9090
/// Errors which can be output by Mithril aggregate verification.
9191
#[derive(Debug, Clone, thiserror::Error)]
92-
pub enum StmAggregateSignatureError {
92+
pub enum AggregateSignatureError {
9393
/// This error occurs when the the serialization of the raw bytes failed
9494
#[error("Invalid bytes")]
9595
SerializationError,

mithril-stm/src/key_registration.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ impl KeyRegistration {
3838
pk: BlsVerificationKeyProofOfPossession,
3939
) -> StmResult<()> {
4040
if let Entry::Vacant(e) = self.keys.entry(pk.vk) {
41-
pk.verify_proof_of_possession()?;
41+
pk.verify_proof_of_possession()
42+
.map_err(|_| RegisterError::KeyInvalid(Box::new(pk)))?;
4243
e.insert(stake);
4344
return Ok(());
4445
}

mithril-stm/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ pub use aggregate_signature::{
125125
AggregateSignature, AggregateSignatureType, AggregateVerificationKey, BasicVerifier, Clerk,
126126
};
127127
pub use error::{
128-
AggregationError, MultiSignatureError, RegisterError, StmAggregateSignatureError,
129-
StmSignatureError,
128+
AggregateSignatureError, AggregationError, MultiSignatureError, RegisterError, SignatureError,
130129
};
131130
pub use key_registration::{ClosedKeyRegistration, KeyRegistration};
132131
pub use parameters::Parameters;

mithril-stm/src/single_signature/signature.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use serde::{Deserialize, Serialize};
99
use crate::bls_multi_signature::BlsSignature;
1010
use crate::eligibility_check::is_lottery_won;
1111
use crate::{
12-
AggregateVerificationKey, Index, Parameters, Stake, StmResult, StmSignatureError,
13-
VerificationKey,
12+
AggregateVerificationKey, Index, Parameters, SignatureError, Stake, StmResult, VerificationKey,
1413
};
1514
use anyhow::{Context, anyhow};
1615

@@ -57,15 +56,13 @@ impl SingleSignature {
5756
) -> StmResult<()> {
5857
for &index in &self.indexes {
5958
if index > params.m {
60-
return Err(anyhow!(StmSignatureError::IndexBoundFailed(
61-
index, params.m
62-
)));
59+
return Err(anyhow!(SignatureError::IndexBoundFailed(index, params.m)));
6360
}
6461

6562
let ev = self.sigma.evaluate_dense_mapping(msg, index);
6663

6764
if !is_lottery_won(params.phi_f, ev, *stake, *total_stake) {
68-
return Err(anyhow!(StmSignatureError::LotteryLost));
65+
return Err(anyhow!(SignatureError::LotteryLost));
6966
}
7067
}
7168

@@ -99,15 +96,15 @@ impl SingleSignature {
9996
pub fn from_bytes<D: Clone + Digest + FixedOutput>(bytes: &[u8]) -> StmResult<SingleSignature> {
10097
let mut u64_bytes = [0u8; 8];
10198

102-
u64_bytes.copy_from_slice(bytes.get(0..8).ok_or(StmSignatureError::SerializationError)?);
99+
u64_bytes.copy_from_slice(bytes.get(0..8).ok_or(SignatureError::SerializationError)?);
103100
let nr_indexes = u64::from_be_bytes(u64_bytes) as usize;
104101

105102
let mut indexes = Vec::new();
106103
for i in 0..nr_indexes {
107104
u64_bytes.copy_from_slice(
108105
bytes
109106
.get(8 + i * 8..16 + i * 8)
110-
.ok_or(StmSignatureError::SerializationError)?,
107+
.ok_or(SignatureError::SerializationError)?,
111108
);
112109
indexes.push(u64::from_be_bytes(u64_bytes));
113110
}
@@ -116,13 +113,13 @@ impl SingleSignature {
116113
let sigma = BlsSignature::from_bytes(
117114
bytes
118115
.get(offset..offset + 48)
119-
.ok_or(StmSignatureError::SerializationError)?,
116+
.ok_or(SignatureError::SerializationError)?,
120117
)?;
121118

122119
u64_bytes.copy_from_slice(
123120
bytes
124121
.get(offset + 48..offset + 56)
125-
.ok_or(StmSignatureError::SerializationError)?,
122+
.ok_or(SignatureError::SerializationError)?,
126123
);
127124
let signer_index = u64::from_be_bytes(u64_bytes);
128125

mithril-stm/src/single_signature/signature_registered_party.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize, Serializer, ser::SerializeTuple};
33

44
use crate::StmResult;
55
use crate::key_registration::RegisteredParty;
6-
use crate::{SingleSignature, StmSignatureError};
6+
use crate::{SignatureError, SingleSignature};
77

88
/// Signature with its registered party.
99
#[derive(Debug, Clone, Hash, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
@@ -31,10 +31,10 @@ impl SingleSignatureWithRegisteredParty {
3131
bytes: &[u8],
3232
) -> StmResult<SingleSignatureWithRegisteredParty> {
3333
let reg_party = RegisteredParty::from_bytes(
34-
bytes.get(0..104).ok_or(StmSignatureError::SerializationError)?,
34+
bytes.get(0..104).ok_or(SignatureError::SerializationError)?,
3535
)?;
3636
let sig = SingleSignature::from_bytes::<D>(
37-
bytes.get(104..).ok_or(StmSignatureError::SerializationError)?,
37+
bytes.get(104..).ok_or(SignatureError::SerializationError)?,
3838
)?;
3939

4040
Ok(SingleSignatureWithRegisteredParty { sig, reg_party })

0 commit comments

Comments
 (0)