Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organize STM code #2373

Merged
merged 3 commits into from
Mar 18, 2025
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

7 changes: 7 additions & 0 deletions mithril-stm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ 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.3.40 (18-03-2025)

### Changed

- Moved implementation blocks under their respective structures.
- Ordered property tests.

## 0.3.18 (11-04-2024)

- Deprecate `portable` feature:
Expand Down
2 changes: 1 addition & 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.3.39"
version = "0.3.40"
edition = { workspace = true }
authors = { workspace = true }
homepage = { workspace = true }
Expand Down
208 changes: 104 additions & 104 deletions mithril-stm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ pub enum MultiSignatureError {
VerificationKeyInfinity(Box<VerificationKey>),
}

/// Error types related to merkle trees.
#[derive(Debug, Clone, thiserror::Error)]
pub enum MerkleTreeError<D: Digest + FixedOutput> {
/// Serialization error
#[error("Serialization of a merkle tree failed")]
SerializationError,

/// Invalid merkle path
#[error("Path does not verify against root")]
PathInvalid(Path<D>),

/// Invalid merkle batch path
#[error("Batch path does not verify against root")]
BatchPathInvalid(BatchPath<D>),
}

/// Errors which can be output by Mithril single signature verification.
#[derive(Debug, Clone, thiserror::Error)]
pub enum StmSignatureError {
Expand Down Expand Up @@ -67,28 +83,39 @@ pub enum StmSignatureError {
SerializationError,
}

/// Errors which can be output by Mithril aggregate verification.
#[derive(Debug, Clone, thiserror::Error)]
pub enum StmAggregateSignatureError<D: Digest + FixedOutput> {
/// The IVK is invalid after aggregating the keys
#[error("Aggregated key does not correspond to the expected key.")]
IvkInvalid(Box<VerificationKey>),

/// This error occurs when the the serialization of the raw bytes failed
#[error("Invalid bytes")]
SerializationError,
impl From<MultiSignatureError> for StmSignatureError {
fn from(e: MultiSignatureError) -> Self {
match e {
MultiSignatureError::SerializationError => Self::SerializationError,
MultiSignatureError::SignatureInvalid(e) => Self::SignatureInvalid(e),
MultiSignatureError::BatchInvalid => unreachable!(),
MultiSignatureError::KeyInvalid(_) => unreachable!(),
MultiSignatureError::AggregateSignatureInvalid => unreachable!(),
MultiSignatureError::SignatureInfinity(_) => unreachable!(),
MultiSignatureError::VerificationKeyInfinity(_) => unreachable!(),
}
}
}

/// Invalid merkle batch path
#[error("Batch path does not verify against root")]
PathInvalid(BatchPath<D>),
impl<D: Digest + FixedOutput> From<MerkleTreeError<D>> for StmSignatureError {
fn from(e: MerkleTreeError<D>) -> Self {
match e {
MerkleTreeError::SerializationError => Self::SerializationError,
_ => unreachable!(),
}
}
}

/// Batch verification of STM aggregate signatures failed
#[error("Batch verification of STM aggregate signatures failed")]
BatchInvalid,
/// Error types for aggregation.
#[derive(Debug, Clone, thiserror::Error)]
pub enum AggregationError {
/// Not enough signatures were collected, got this many instead.
#[error("Not enough signatures. Got only {0} out of {1}.")]
NotEnoughSignatures(u64, u64),

/// `CoreVerifier` check failed
#[error("Core verification error: {0}")]
CoreVerificationError(#[source] CoreVerifierError),
/// This error happens when we try to convert a u64 to a usize and it does not fit
#[error("Invalid usize conversion")]
UsizeConversionInvalid,
}

/// Errors which can be output by `CoreVerifier`.
Expand All @@ -111,81 +138,59 @@ pub enum CoreVerifierError {
IndividualSignatureInvalid(#[source] StmSignatureError),
}

/// Error types for aggregation.
#[derive(Debug, Clone, thiserror::Error)]
pub enum AggregationError {
/// Not enough signatures were collected, got this many instead.
#[error("Not enough signatures. Got only {0} out of {1}.")]
NotEnoughSignatures(u64, u64),

/// This error happens when we try to convert a u64 to a usize and it does not fit
#[error("Invalid usize conversion")]
UsizeConversionInvalid,
}

/// Error types related to merkle trees.
#[derive(Debug, Clone, thiserror::Error)]
pub enum MerkleTreeError<D: Digest + FixedOutput> {
/// Serialization error
#[error("Serialization of a merkle tree failed")]
SerializationError,

/// Invalid merkle path
#[error("Path does not verify against root")]
PathInvalid(Path<D>),

/// Invalid merkle batch path
#[error("Batch path does not verify against root")]
BatchPathInvalid(BatchPath<D>),
}

/// Errors which can be outputted by key registration.
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum RegisterError {
/// This key has already been registered by a participant
#[error("This key has already been registered.")]
KeyRegistered(Box<VerificationKey>),

/// Verification key is the infinity
#[error("Verification key is the infinity")]
VerificationKeyInfinity(Box<VerificationKey>),

/// The supplied key is not valid
#[error("The verification of correctness of the supplied key is invalid.")]
KeyInvalid(Box<VerificationKeyPoP>),

/// Serialization error
#[error("Serialization error")]
SerializationError,

/// UnregisteredInitializer error
#[error("Initializer not registered. Cannot participate as a signer.")]
UnregisteredInitializer,
impl From<AggregationError> for CoreVerifierError {
fn from(e: AggregationError) -> Self {
match e {
AggregationError::NotEnoughSignatures(e, _e) => Self::NoQuorum(e, e),
AggregationError::UsizeConversionInvalid => unreachable!(),
}
}
}

impl From<MultiSignatureError> for StmSignatureError {
impl From<MultiSignatureError> for CoreVerifierError {
fn from(e: MultiSignatureError) -> Self {
match e {
MultiSignatureError::SerializationError => Self::SerializationError,
MultiSignatureError::SignatureInvalid(e) => Self::SignatureInvalid(e),
MultiSignatureError::AggregateSignatureInvalid => Self::AggregateSignatureInvalid,
MultiSignatureError::BatchInvalid => unreachable!(),
MultiSignatureError::SerializationError => unreachable!(),
MultiSignatureError::KeyInvalid(_) => unreachable!(),
MultiSignatureError::AggregateSignatureInvalid => unreachable!(),
MultiSignatureError::SignatureInvalid(_e) => unreachable!(),
MultiSignatureError::SignatureInfinity(_) => unreachable!(),
MultiSignatureError::VerificationKeyInfinity(_) => unreachable!(),
}
}
}

impl<D: Digest + FixedOutput> From<MerkleTreeError<D>> for StmSignatureError {
fn from(e: MerkleTreeError<D>) -> Self {
match e {
MerkleTreeError::SerializationError => Self::SerializationError,
_ => unreachable!(),
}
impl From<StmSignatureError> for CoreVerifierError {
fn from(e: StmSignatureError) -> Self {
CoreVerifierError::IndividualSignatureInvalid(e)
}
}

/// Errors which can be output by Mithril aggregate verification.
#[derive(Debug, Clone, thiserror::Error)]
pub enum StmAggregateSignatureError<D: Digest + FixedOutput> {
/// The IVK is invalid after aggregating the keys
#[error("Aggregated key does not correspond to the expected key.")]
IvkInvalid(Box<VerificationKey>),

/// This error occurs when the the serialization of the raw bytes failed
#[error("Invalid bytes")]
SerializationError,

/// Invalid merkle batch path
#[error("Batch path does not verify against root")]
PathInvalid(BatchPath<D>),

/// Batch verification of STM aggregate signatures failed
#[error("Batch verification of STM aggregate signatures failed")]
BatchInvalid,

/// `CoreVerifier` check failed
#[error("Core verification error: {0}")]
CoreVerificationError(#[source] CoreVerifierError),
}

impl<D: Digest + FixedOutput> From<MerkleTreeError<D>> for StmAggregateSignatureError<D> {
fn from(e: MerkleTreeError<D>) -> Self {
match e {
Expand Down Expand Up @@ -233,33 +238,28 @@ impl<D: Digest + FixedOutput> From<StmSignatureError> for StmAggregateSignatureE
}
}

impl From<AggregationError> for CoreVerifierError {
fn from(e: AggregationError) -> Self {
match e {
AggregationError::NotEnoughSignatures(e, _e) => Self::NoQuorum(e, e),
AggregationError::UsizeConversionInvalid => unreachable!(),
}
}
}
/// Errors which can be outputted by key registration.
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum RegisterError {
/// This key has already been registered by a participant
#[error("This key has already been registered.")]
KeyRegistered(Box<VerificationKey>),

impl From<MultiSignatureError> for CoreVerifierError {
fn from(e: MultiSignatureError) -> Self {
match e {
MultiSignatureError::AggregateSignatureInvalid => Self::AggregateSignatureInvalid,
MultiSignatureError::BatchInvalid => unreachable!(),
MultiSignatureError::SerializationError => unreachable!(),
MultiSignatureError::KeyInvalid(_) => unreachable!(),
MultiSignatureError::SignatureInvalid(_e) => unreachable!(),
MultiSignatureError::SignatureInfinity(_) => unreachable!(),
MultiSignatureError::VerificationKeyInfinity(_) => unreachable!(),
}
}
}
/// Verification key is the infinity
#[error("Verification key is the infinity")]
VerificationKeyInfinity(Box<VerificationKey>),

impl From<StmSignatureError> for CoreVerifierError {
fn from(e: StmSignatureError) -> Self {
CoreVerifierError::IndividualSignatureInvalid(e)
}
/// The supplied key is not valid
#[error("The verification of correctness of the supplied key is invalid.")]
KeyInvalid(Box<VerificationKeyPoP>),

/// Serialization error
#[error("Serialization error")]
SerializationError,

/// UnregisteredInitializer error
#[error("Initializer not registered. Cannot participate as a signer.")]
UnregisteredInitializer,
}

impl From<MultiSignatureError> for RegisterError {
Expand Down
24 changes: 12 additions & 12 deletions mithril-stm/src/key_reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ pub struct KeyReg {
keys: HashMap<VerificationKey, Stake>,
}

/// Structure generated out of a closed registration containing the registered parties, total stake, and the merkle tree.
/// One can only get a global `avk` out of a closed key registration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClosedKeyReg<D: Digest> {
/// Ordered list of registered parties.
pub reg_parties: Vec<RegParty>,
/// Total stake of the registered parties.
pub total_stake: Stake,
/// Unique public key out of the key registration instance.
pub merkle_tree: Arc<MerkleTree<D>>,
}

impl KeyReg {
/// Initialize an empty `KeyReg`.
pub fn init() -> Self {
Expand Down Expand Up @@ -78,6 +66,18 @@ impl KeyReg {
}
}

/// Structure generated out of a closed registration containing the registered parties, total stake, and the merkle tree.
/// One can only get a global `avk` out of a closed key registration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClosedKeyReg<D: Digest> {
/// Ordered list of registered parties.
pub reg_parties: Vec<RegParty>,
/// Total stake of the registered parties.
pub total_stake: Stake,
/// Unique public key out of the key registration instance.
pub merkle_tree: Arc<MerkleTree<D>>,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading