Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions mithril-aggregator/src/multi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,19 @@ impl MultiSigner for MultiSignerImpl {
self.aggregate_signature_type,
) {
Ok(multi_signature) => Ok(Some(multi_signature)),
Err(ProtocolAggregationError::NotEnoughSignatures(actual, expected)) => {
warn!(
self.logger,
"Could not compute multi-signature: Not enough signatures. Got only {actual} out of {expected}."
);
Ok(None)
}
Err(err) => Err(anyhow!(err).context(format!(
"Multi Signer can not create multi-signature for entity type '{:?}'",
open_message.signed_entity_type
))),
Err(err) => match err.downcast_ref::<ProtocolAggregationError>() {
Some(ProtocolAggregationError::NotEnoughSignatures(actual, expected)) => {
warn!(
self.logger,
"Could not compute multi-signature: Not enough signatures. Got only {actual} out of {expected}."
);
Ok(None)
}
_ => Err(anyhow!(err).context(format!(
"Multi Signer can not create multi-signature for entity type '{:?}'",
open_message.signed_entity_type
))),
},
}
}
}
Expand Down
30 changes: 14 additions & 16 deletions mithril-common/src/crypto_helper/cardano/key_certification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::{collections::HashMap, sync::Arc};

use anyhow::anyhow;
use blake2::{
Blake2b, Digest,
digest::{FixedOutput, consts::U32},
Expand Down Expand Up @@ -173,13 +174,8 @@ impl StmInitializerWrapper {
/// * the current total stake (according to the registration service)
/// # Error
/// This function fails if the initializer is not registered.
pub fn new_signer(
self,
closed_reg: ClosedKeyRegistration<D>,
) -> Result<Signer<D>, ProtocolRegistrationErrorWrapper> {
self.stm_initializer
.create_signer(closed_reg)
.map_err(ProtocolRegistrationErrorWrapper::CoreRegister)
pub fn new_signer(self, closed_reg: ClosedKeyRegistration<D>) -> StdResult<Signer<D>> {
self.stm_initializer.create_signer(closed_reg)
}

/// Convert to bytes
Expand All @@ -199,7 +195,7 @@ impl StmInitializerWrapper {
/// Convert a slice of bytes to an `StmInitializerWrapper`
/// # Error
/// The function fails if the given string of bytes is not of required size.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, RegisterError> {
pub fn from_bytes(bytes: &[u8]) -> StdResult<Self> {
let stm_initializer =
Initializer::from_bytes(bytes.get(..256).ok_or(RegisterError::SerializationError)?)?;
let bytes = bytes.get(256..).ok_or(RegisterError::SerializationError)?;
Expand Down Expand Up @@ -250,7 +246,7 @@ impl KeyRegWrapper {
kes_sig: Option<ProtocolSignerVerificationKeySignature>, // Used for only for testing when SPO pool id is not certified
kes_period: Option<KesPeriod>,
pk: ProtocolSignerVerificationKey,
) -> Result<ProtocolPartyId, ProtocolRegistrationErrorWrapper> {
) -> StdResult<ProtocolPartyId> {
let pool_id_bech32: ProtocolPartyId = if let Some(opcert) = opcert {
let signature = kes_sig.ok_or(ProtocolRegistrationErrorWrapper::KesSignatureMissing)?;
let kes_period =
Expand All @@ -264,9 +260,11 @@ impl KeyRegWrapper {
.compute_protocol_party_id()
.map_err(|_| ProtocolRegistrationErrorWrapper::PoolAddressEncoding)?
} else {
return Err(ProtocolRegistrationErrorWrapper::KesSignatureInvalid(
kes_period,
opcert.get_start_kes_period(),
return Err(anyhow!(
ProtocolRegistrationErrorWrapper::KesSignatureInvalid(
kes_period,
opcert.get_start_kes_period(),
)
));
}
} else {
Expand All @@ -277,12 +275,12 @@ impl KeyRegWrapper {
};

if let Some(&stake) = self.stake_distribution.get(&pool_id_bech32) {
self.stm_key_reg
.register(stake, pk.into())
.map_err(ProtocolRegistrationErrorWrapper::CoreRegister)?;
self.stm_key_reg.register(stake, pk.into())?;
return Ok(pool_id_bech32);
}
Err(ProtocolRegistrationErrorWrapper::PartyIdNonExisting)
Err(anyhow!(
ProtocolRegistrationErrorWrapper::PartyIdNonExisting
))
}

/// Finalize the key registration.
Expand Down
10 changes: 5 additions & 5 deletions mithril-common/src/crypto_helper/codec/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod binary_mithril_stm {

impl TryFromBytes for SingleSignature {
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
Self::from_bytes::<D>(bytes).map_err(|e| e.into())
Self::from_bytes::<D>(bytes)
}
}

Expand All @@ -73,7 +73,7 @@ mod binary_mithril_stm {

impl TryFromBytes for SingleSignatureWithRegisteredParty {
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
Self::from_bytes::<D>(bytes).map_err(|e| e.into())
Self::from_bytes::<D>(bytes)
}
}

Expand All @@ -97,7 +97,7 @@ mod binary_mithril_stm {

impl TryFromBytes for VerificationKey {
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
Self::from_bytes(bytes).map_err(|e| e.into())
Self::from_bytes(bytes)
}
}

Expand All @@ -109,7 +109,7 @@ mod binary_mithril_stm {

impl TryFromBytes for VerificationKeyProofOfPossession {
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
Self::from_bytes(bytes).map_err(|e| e.into())
Self::from_bytes(bytes)
}
}

Expand Down Expand Up @@ -139,7 +139,7 @@ mod binary_mithril_stm {

impl TryFromBytes for Initializer {
fn try_from_bytes(bytes: &[u8]) -> StdResult<Self> {
Self::from_bytes(bytes).map_err(|e| e.into())
Self::from_bytes(bytes)
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions mithril-common/src/protocol/multi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use mithril_stm::{AggregateSignatureType, Parameters};

use crate::{
StdResult,
crypto_helper::{
ProtocolAggregateVerificationKey, ProtocolAggregationError, ProtocolClerk,
ProtocolMultiSignature,
},
crypto_helper::{ProtocolAggregateVerificationKey, ProtocolClerk, ProtocolMultiSignature},
entities::SingleSignature,
protocol::ToMessage,
};
Expand All @@ -31,7 +28,7 @@ impl MultiSigner {
single_signatures: &[SingleSignature],
message: &T,
aggregate_signature_type: AggregateSignatureType,
) -> Result<ProtocolMultiSignature, ProtocolAggregationError> {
) -> StdResult<ProtocolMultiSignature> {
let protocol_signatures: Vec<_> = single_signatures
.iter()
.map(|single_signature| single_signature.to_protocol_signature())
Expand Down Expand Up @@ -94,9 +91,10 @@ impl MultiSigner {

#[cfg(test)]
mod test {
use mithril_stm::StmSignatureError;
use mithril_stm::MultiSignatureError;

use crate::{
crypto_helper::ProtocolAggregationError,
entities::{ProtocolMessage, ProtocolMessagePartKey, ProtocolParameters},
protocol::SignerBuilder,
test::{
Expand Down Expand Up @@ -129,7 +127,10 @@ mod test {
);

assert!(
matches!(error, ProtocolAggregationError::NotEnoughSignatures(_, _)),
matches!(
error.downcast_ref::<ProtocolAggregationError>(),
Some(ProtocolAggregationError::NotEnoughSignatures(_, _))
),
"Expected ProtocolAggregationError::NotEnoughSignatures, got: {error:?}"
)
}
Expand Down Expand Up @@ -194,8 +195,8 @@ mod test {
"Verify single signature should fail if the signer isn't in the registered parties",
);

match error.downcast_ref::<StmSignatureError>() {
Some(StmSignatureError::SignatureInvalid(_)) => (),
match error.downcast_ref::<MultiSignatureError>() {
Some(MultiSignatureError::SignatureInvalid(_)) => (),
_ => panic!("Expected an SignatureInvalid error, got: {error:?}"),
}
}
Expand All @@ -220,8 +221,8 @@ mod test {
.verify_single_signature(&ProtocolMessage::default(), &single_signature)
.expect_err("Verify single signature should fail");

match error.downcast_ref::<StmSignatureError>() {
Some(StmSignatureError::SignatureInvalid(_)) => (),
match error.downcast_ref::<MultiSignatureError>() {
Some(MultiSignatureError::SignatureInvalid(_)) => (),
_ => panic!("Expected an SignatureInvalid error, got: {error:?}"),
}
}
Expand Down
12 changes: 5 additions & 7 deletions mithril-common/src/protocol/signer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod test {
use mithril_stm::RegisterError;

use crate::{
crypto_helper::{KesSignerStandard, ProtocolRegistrationErrorWrapper},
crypto_helper::KesSignerStandard,
test::{builder::MithrilFixtureBuilder, double::fake_data},
};

Expand Down Expand Up @@ -220,8 +220,8 @@ mod test {
"We should not be able to construct a signer builder if a signer registration fail",
);

match error.downcast_ref::<ProtocolRegistrationErrorWrapper>() {
Some(ProtocolRegistrationErrorWrapper::CoreRegister(_)) => (),
match error.downcast_ref::<RegisterError>() {
Some(RegisterError::KeyRegistered { .. }) => (),
_ => panic!("Expected an CoreRegister error, got: {error:?}"),
}
}
Expand Down Expand Up @@ -264,10 +264,8 @@ mod test {
"We should not be able to construct a single signer from a not registered party",
);

match error.downcast_ref::<ProtocolRegistrationErrorWrapper>() {
Some(ProtocolRegistrationErrorWrapper::CoreRegister(
RegisterError::UnregisteredInitializer,
)) => (),
match error.downcast_ref::<RegisterError>() {
Some(RegisterError::UnregisteredInitializer) => (),
_ => panic!(
"Expected an ProtocolRegistrationErrorWrapper::CoreRegister error, got: {error:?}"
),
Expand Down
6 changes: 6 additions & 0 deletions mithril-stm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.6.0 (11-19-2025)

### Changed

- Stm error handling is done with `anyhow`.

## 0.5.5 (10-13-2025)

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion mithril-stm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-stm"
version = "0.5.5"
version = "0.6.0"
edition = { workspace = true }
authors = { workspace = true }
homepage = { workspace = true }
Expand All @@ -21,6 +21,7 @@ benchmark-internals = [] # For benchmarking multi_sig
future_proof_system = [] # For activating future proof systems

[dependencies]
anyhow = { workspace = true }
blake2 = "0.10.6"
# Enforce blst portable feature for runtime detection of Intel ADX instruction set.
blst = { version = "0.3.16", features = ["portable"] }
Expand Down
23 changes: 13 additions & 10 deletions mithril-stm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,19 @@ match msig {
println!("Aggregate ok");
assert!(aggr.verify(&msg, &clerk.compute_aggregate_verification_key(), &params).is_ok());
}
Err(AggregationError::NotEnoughSignatures(n, k)) => {
println!("Not enough signatures");
assert!(n < params.k && k == params.k)
}
Err(AggregationError::UsizeConversionInvalid) => {
println!("Invalid usize conversion");
}
Err(AggregationError::UnsupportedProofSystem(aggregate_signature_type)) => {
println!("Unsupported proof system: {:?}", aggregate_signature_type);
}
Err(error) => match error.downcast_ref::<AggregationError>() {
Some(AggregationError::NotEnoughSignatures(n, k)) => {
println!("Not enough signatures");
assert!(n < &params.k && k == &params.k)
},

Some(AggregationError::UnsupportedProofSystem(aggregate_signature_type)) => {
println!("Unsupported proof system: {:?}", aggregate_signature_type);
},
_ => {
println!("Unexpected error during aggregation: {:?}", error);
}
},
}
```

Expand Down
Loading
Loading