Skip to content

Commit 64aad5f

Browse files
committed
contexts added
1 parent b2e3a49 commit 64aad5f

File tree

6 files changed

+61
-18
lines changed

6 files changed

+61
-18
lines changed

mithril-stm/src/aggregate_signature/basic_verifier.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::anyhow;
1+
use anyhow::{Context, anyhow};
22
use std::collections::{BTreeMap, HashMap, HashSet};
33

44
use crate::bls_multi_signature::{BlsSignature, BlsVerificationKey};
@@ -64,7 +64,8 @@ impl BasicVerifier {
6464
for sig_reg in signatures {
6565
sig_reg
6666
.sig
67-
.check_indices(parameters, &sig_reg.reg_party.1, msg, total_stake)?;
67+
.check_indices(parameters, &sig_reg.reg_party.1, msg, total_stake)
68+
.with_context(|| "Preliminary verification for basic verifier failed.")?;
6869
for &index in &sig_reg.sig.indexes {
6970
unique_indices.insert(index);
7071
nr_indices += 1;
@@ -238,13 +239,15 @@ impl BasicVerifier {
238239
parameters,
239240
msg,
240241
&sig_reg_list,
241-
)?;
242+
)
243+
.with_context(|| "Basic verification failed during selection of unique k indices.")?;
242244

243245
Self::preliminary_verify(&self.total_stake, &unique_sigs, parameters, msg)?;
244246

245247
let (sigs, vks) = Self::collect_signatures_verification_keys(&unique_sigs);
246248

247-
BlsSignature::verify_aggregate(msg.to_vec().as_slice(), &vks, &sigs)?;
249+
BlsSignature::verify_aggregate(msg.to_vec().as_slice(), &vks, &sigs)
250+
.with_context(|| "Basic verifier failed in multisignature verification.")?;
248251

249252
Ok(())
250253
}

mithril-stm/src/aggregate_signature/clerk.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
Index, Parameters, Signer, SingleSignature, Stake, StmResult, VerificationKey,
44
aggregate_signature::ConcatenationProof,
55
};
6+
use anyhow::Context;
67
use blake2::digest::{Digest, FixedOutput};
78

89
#[cfg(feature = "future_proof_system")]
@@ -80,7 +81,12 @@ impl<D: Digest + Clone + FixedOutput + Send + Sync> Clerk<D> {
8081
) -> StmResult<AggregateSignature<D>> {
8182
match aggregate_signature_type {
8283
AggregateSignatureType::Concatenation => Ok(AggregateSignature::Concatenation(
83-
ConcatenationProof::aggregate_signatures(self, sigs, msg)?,
84+
ConcatenationProof::aggregate_signatures(self, sigs, msg).with_context(|| {
85+
format!(
86+
"Signatures failed to aggregate for type {}",
87+
AggregateSignatureType::Concatenation
88+
)
89+
})?,
8490
)),
8591
#[cfg(feature = "future_proof_system")]
8692
AggregateSignatureType::Future => Err(anyhow!(

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use blake2::digest::{Digest, FixedOutput};
23

34
use serde::{Deserialize, Serialize};
@@ -52,6 +53,9 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> ConcatenationProof<D> {
5253
&clerk.params,
5354
&msgp,
5455
&sig_reg_list,
56+
)
57+
.with_context(
58+
|| "Failed to aggregate unique signatures during selection for the k indices.",
5559
)?;
5660

5761
unique_sigs.sort_unstable();
@@ -90,7 +94,8 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> ConcatenationProof<D> {
9094
&self.signatures,
9195
parameters,
9296
&msgp,
93-
)?;
97+
)
98+
.with_context(|| "Preliminary verification of aggregate signatures failed.")?;
9499

95100
let leaves = self
96101
.signatures
@@ -99,7 +104,8 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> ConcatenationProof<D> {
99104
.collect::<Vec<RegisteredParty>>();
100105

101106
avk.get_merkle_tree_batch_commitment()
102-
.verify_leaves_membership_from_batch_path(&leaves, &self.batch_proof)?;
107+
.verify_leaves_membership_from_batch_path(&leaves, &self.batch_proof)
108+
.with_context(|| "Batch proof is invalid in preliminary verification.")?;
103109

104110
Ok(BasicVerifier::collect_signatures_verification_keys(
105111
&self.signatures,
@@ -119,9 +125,12 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> ConcatenationProof<D> {
119125
parameters: &Parameters,
120126
) -> StmResult<()> {
121127
let msgp = avk.get_merkle_tree_batch_commitment().concatenate_with_message(msg);
122-
let (sigs, vks) = self.preliminary_verify(msg, avk, parameters)?;
128+
let (sigs, vks) = self
129+
.preliminary_verify(msg, avk, parameters)
130+
.with_context(|| "Aggregate signature verification failed")?;
123131

124-
BlsSignature::verify_aggregate(msgp.as_slice(), &vks, &sigs)?;
132+
BlsSignature::verify_aggregate(msgp.as_slice(), &vks, &sigs)
133+
.with_context(|| "Aggregate signature verification failed")?;
125134
Ok(())
126135
}
127136

mithril-stm/src/bls_multi_signature/signature.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::anyhow;
1+
use anyhow::{Context, anyhow};
22
use std::{cmp::Ordering, iter::Sum};
33

44
use blake2::{Blake2b, Blake2b512, Digest};
@@ -143,7 +143,7 @@ impl BlsSignature {
143143
vks: &[BlsVerificationKey],
144144
sigs: &[BlsSignature],
145145
) -> StmResult<()> {
146-
let (aggr_vk, aggr_sig) = Self::aggregate(vks, sigs)?;
146+
let (aggr_vk, aggr_sig) = Self::aggregate(vks, sigs).with_context(|| "Multi signature verification failed in aggregation of verification keys and signatures.")?;
147147

148148
blst_error_to_stm_error(
149149
aggr_sig.0.verify(

mithril-stm/src/merkle_tree/commitment.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66
use crate::StmResult;
77
use crate::error::MerkleTreeError;
88
use crate::merkle_tree::{MerkleBatchPath, MerklePath, MerkleTreeLeaf, parent, sibling};
9-
use anyhow::anyhow;
9+
use anyhow::{Context, anyhow};
1010
/// `MerkleTree` commitment.
1111
/// This structure differs from `MerkleTree` in that it does not contain all elements, which are not always necessary.
1212
/// Instead, it only contains the root of the tree.
@@ -208,7 +208,14 @@ impl<D: Digest + FixedOutput> MerkleTreeBatchCommitment<D> {
208208
if ordered_indices[i] & 1 == 0 {
209209
new_hashes.push(
210210
D::new()
211-
.chain(values.first().ok_or(MerkleTreeError::SerializationError)?)
211+
.chain(
212+
values
213+
.first()
214+
.ok_or(MerkleTreeError::SerializationError)
215+
.with_context(|| {
216+
format!("Could not verify leave membership from batch path for idx = {} and ordered_indices[{}]", idx, i)
217+
})?,
218+
)
212219
.chain(&leaves[i])
213220
.finalize()
214221
.to_vec(),
@@ -225,7 +232,16 @@ impl<D: Digest + FixedOutput> MerkleTreeBatchCommitment<D> {
225232
new_hashes.push(
226233
D::new()
227234
.chain(&leaves[i])
228-
.chain(values.first().ok_or(MerkleTreeError::SerializationError)?)
235+
.chain(
236+
values
237+
.first()
238+
.ok_or(MerkleTreeError::SerializationError)
239+
.with_context(|| {
240+
format!(
241+
"Could not verify leave membership from batch path for idx = {} where sibling < nr_nodes", idx
242+
)
243+
})?,
244+
)
229245
.finalize()
230246
.to_vec(),
231247
);

mithril-stm/src/single_signature/signature.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
AggregateVerificationKey, Index, Parameters, Stake, StmResult, StmSignatureError,
1313
VerificationKey,
1414
};
15-
use anyhow::anyhow;
15+
use anyhow::{Context, anyhow};
1616

1717
/// Signature created by a single party who has won the lottery.
1818
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -37,7 +37,13 @@ impl SingleSignature {
3737
msg: &[u8],
3838
) -> StmResult<()> {
3939
let msgp = avk.get_merkle_tree_batch_commitment().concatenate_with_message(msg);
40-
self.basic_verify(params, pk, stake, &msgp, &avk.get_total_stake())?;
40+
self.basic_verify(params, pk, stake, &msgp, &avk.get_total_stake())
41+
.with_context(|| {
42+
format!(
43+
"Single signature verification failed for signer index {}.",
44+
self.signer_index
45+
)
46+
})?;
4147
Ok(())
4248
}
4349

@@ -148,8 +154,11 @@ impl SingleSignature {
148154
msg: &[u8],
149155
total_stake: &Stake,
150156
) -> StmResult<()> {
151-
self.sigma.verify(msg, pk)?;
152-
self.check_indices(params, stake, msg, total_stake)?;
157+
self.sigma
158+
.verify(msg, pk)
159+
.with_context(|| "Basic verification of single signature failed.")?;
160+
self.check_indices(params, stake, msg, total_stake)
161+
.with_context(|| "Basic verification of single signature failed.")?;
153162

154163
Ok(())
155164
}

0 commit comments

Comments
 (0)