Skip to content

Commit 2614ed3

Browse files
authored
Merge pull request #2373 from input-output-hk/curiecrypt/organize-stm
Organize STM code
2 parents 6408d5c + b15c070 commit 2614ed3

File tree

8 files changed

+614
-621
lines changed

8 files changed

+614
-621
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-stm/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.3.40 (18-03-2025)
9+
10+
### Changed
11+
12+
- Moved implementation blocks under their respective structures.
13+
- Ordered property tests.
14+
815
## 0.3.18 (11-04-2024)
916

1017
- Deprecate `portable` feature:

mithril-stm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-stm"
3-
version = "0.3.39"
3+
version = "0.3.40"
44
edition = { workspace = true }
55
authors = { workspace = true }
66
homepage = { workspace = true }

mithril-stm/src/error.rs

Lines changed: 104 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ pub enum MultiSignatureError {
3939
VerificationKeyInfinity(Box<VerificationKey>),
4040
}
4141

42+
/// Error types related to merkle trees.
43+
#[derive(Debug, Clone, thiserror::Error)]
44+
pub enum MerkleTreeError<D: Digest + FixedOutput> {
45+
/// Serialization error
46+
#[error("Serialization of a merkle tree failed")]
47+
SerializationError,
48+
49+
/// Invalid merkle path
50+
#[error("Path does not verify against root")]
51+
PathInvalid(Path<D>),
52+
53+
/// Invalid merkle batch path
54+
#[error("Batch path does not verify against root")]
55+
BatchPathInvalid(BatchPath<D>),
56+
}
57+
4258
/// Errors which can be output by Mithril single signature verification.
4359
#[derive(Debug, Clone, thiserror::Error)]
4460
pub enum StmSignatureError {
@@ -67,28 +83,39 @@ pub enum StmSignatureError {
6783
SerializationError,
6884
}
6985

70-
/// Errors which can be output by Mithril aggregate verification.
71-
#[derive(Debug, Clone, thiserror::Error)]
72-
pub enum StmAggregateSignatureError<D: Digest + FixedOutput> {
73-
/// The IVK is invalid after aggregating the keys
74-
#[error("Aggregated key does not correspond to the expected key.")]
75-
IvkInvalid(Box<VerificationKey>),
76-
77-
/// This error occurs when the the serialization of the raw bytes failed
78-
#[error("Invalid bytes")]
79-
SerializationError,
86+
impl From<MultiSignatureError> for StmSignatureError {
87+
fn from(e: MultiSignatureError) -> Self {
88+
match e {
89+
MultiSignatureError::SerializationError => Self::SerializationError,
90+
MultiSignatureError::SignatureInvalid(e) => Self::SignatureInvalid(e),
91+
MultiSignatureError::BatchInvalid => unreachable!(),
92+
MultiSignatureError::KeyInvalid(_) => unreachable!(),
93+
MultiSignatureError::AggregateSignatureInvalid => unreachable!(),
94+
MultiSignatureError::SignatureInfinity(_) => unreachable!(),
95+
MultiSignatureError::VerificationKeyInfinity(_) => unreachable!(),
96+
}
97+
}
98+
}
8099

81-
/// Invalid merkle batch path
82-
#[error("Batch path does not verify against root")]
83-
PathInvalid(BatchPath<D>),
100+
impl<D: Digest + FixedOutput> From<MerkleTreeError<D>> for StmSignatureError {
101+
fn from(e: MerkleTreeError<D>) -> Self {
102+
match e {
103+
MerkleTreeError::SerializationError => Self::SerializationError,
104+
_ => unreachable!(),
105+
}
106+
}
107+
}
84108

85-
/// Batch verification of STM aggregate signatures failed
86-
#[error("Batch verification of STM aggregate signatures failed")]
87-
BatchInvalid,
109+
/// Error types for aggregation.
110+
#[derive(Debug, Clone, thiserror::Error)]
111+
pub enum AggregationError {
112+
/// Not enough signatures were collected, got this many instead.
113+
#[error("Not enough signatures. Got only {0} out of {1}.")]
114+
NotEnoughSignatures(u64, u64),
88115

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

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

114-
/// Error types for aggregation.
115-
#[derive(Debug, Clone, thiserror::Error)]
116-
pub enum AggregationError {
117-
/// Not enough signatures were collected, got this many instead.
118-
#[error("Not enough signatures. Got only {0} out of {1}.")]
119-
NotEnoughSignatures(u64, u64),
120-
121-
/// This error happens when we try to convert a u64 to a usize and it does not fit
122-
#[error("Invalid usize conversion")]
123-
UsizeConversionInvalid,
124-
}
125-
126-
/// Error types related to merkle trees.
127-
#[derive(Debug, Clone, thiserror::Error)]
128-
pub enum MerkleTreeError<D: Digest + FixedOutput> {
129-
/// Serialization error
130-
#[error("Serialization of a merkle tree failed")]
131-
SerializationError,
132-
133-
/// Invalid merkle path
134-
#[error("Path does not verify against root")]
135-
PathInvalid(Path<D>),
136-
137-
/// Invalid merkle batch path
138-
#[error("Batch path does not verify against root")]
139-
BatchPathInvalid(BatchPath<D>),
140-
}
141-
142-
/// Errors which can be outputted by key registration.
143-
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
144-
pub enum RegisterError {
145-
/// This key has already been registered by a participant
146-
#[error("This key has already been registered.")]
147-
KeyRegistered(Box<VerificationKey>),
148-
149-
/// Verification key is the infinity
150-
#[error("Verification key is the infinity")]
151-
VerificationKeyInfinity(Box<VerificationKey>),
152-
153-
/// The supplied key is not valid
154-
#[error("The verification of correctness of the supplied key is invalid.")]
155-
KeyInvalid(Box<VerificationKeyPoP>),
156-
157-
/// Serialization error
158-
#[error("Serialization error")]
159-
SerializationError,
160-
161-
/// UnregisteredInitializer error
162-
#[error("Initializer not registered. Cannot participate as a signer.")]
163-
UnregisteredInitializer,
141+
impl From<AggregationError> for CoreVerifierError {
142+
fn from(e: AggregationError) -> Self {
143+
match e {
144+
AggregationError::NotEnoughSignatures(e, _e) => Self::NoQuorum(e, e),
145+
AggregationError::UsizeConversionInvalid => unreachable!(),
146+
}
147+
}
164148
}
165149

166-
impl From<MultiSignatureError> for StmSignatureError {
150+
impl From<MultiSignatureError> for CoreVerifierError {
167151
fn from(e: MultiSignatureError) -> Self {
168152
match e {
169-
MultiSignatureError::SerializationError => Self::SerializationError,
170-
MultiSignatureError::SignatureInvalid(e) => Self::SignatureInvalid(e),
153+
MultiSignatureError::AggregateSignatureInvalid => Self::AggregateSignatureInvalid,
171154
MultiSignatureError::BatchInvalid => unreachable!(),
155+
MultiSignatureError::SerializationError => unreachable!(),
172156
MultiSignatureError::KeyInvalid(_) => unreachable!(),
173-
MultiSignatureError::AggregateSignatureInvalid => unreachable!(),
157+
MultiSignatureError::SignatureInvalid(_e) => unreachable!(),
174158
MultiSignatureError::SignatureInfinity(_) => unreachable!(),
175159
MultiSignatureError::VerificationKeyInfinity(_) => unreachable!(),
176160
}
177161
}
178162
}
179163

180-
impl<D: Digest + FixedOutput> From<MerkleTreeError<D>> for StmSignatureError {
181-
fn from(e: MerkleTreeError<D>) -> Self {
182-
match e {
183-
MerkleTreeError::SerializationError => Self::SerializationError,
184-
_ => unreachable!(),
185-
}
164+
impl From<StmSignatureError> for CoreVerifierError {
165+
fn from(e: StmSignatureError) -> Self {
166+
CoreVerifierError::IndividualSignatureInvalid(e)
186167
}
187168
}
188169

170+
/// Errors which can be output by Mithril aggregate verification.
171+
#[derive(Debug, Clone, thiserror::Error)]
172+
pub enum StmAggregateSignatureError<D: Digest + FixedOutput> {
173+
/// The IVK is invalid after aggregating the keys
174+
#[error("Aggregated key does not correspond to the expected key.")]
175+
IvkInvalid(Box<VerificationKey>),
176+
177+
/// This error occurs when the the serialization of the raw bytes failed
178+
#[error("Invalid bytes")]
179+
SerializationError,
180+
181+
/// Invalid merkle batch path
182+
#[error("Batch path does not verify against root")]
183+
PathInvalid(BatchPath<D>),
184+
185+
/// Batch verification of STM aggregate signatures failed
186+
#[error("Batch verification of STM aggregate signatures failed")]
187+
BatchInvalid,
188+
189+
/// `CoreVerifier` check failed
190+
#[error("Core verification error: {0}")]
191+
CoreVerificationError(#[source] CoreVerifierError),
192+
}
193+
189194
impl<D: Digest + FixedOutput> From<MerkleTreeError<D>> for StmAggregateSignatureError<D> {
190195
fn from(e: MerkleTreeError<D>) -> Self {
191196
match e {
@@ -233,33 +238,28 @@ impl<D: Digest + FixedOutput> From<StmSignatureError> for StmAggregateSignatureE
233238
}
234239
}
235240

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

245-
impl From<MultiSignatureError> for CoreVerifierError {
246-
fn from(e: MultiSignatureError) -> Self {
247-
match e {
248-
MultiSignatureError::AggregateSignatureInvalid => Self::AggregateSignatureInvalid,
249-
MultiSignatureError::BatchInvalid => unreachable!(),
250-
MultiSignatureError::SerializationError => unreachable!(),
251-
MultiSignatureError::KeyInvalid(_) => unreachable!(),
252-
MultiSignatureError::SignatureInvalid(_e) => unreachable!(),
253-
MultiSignatureError::SignatureInfinity(_) => unreachable!(),
254-
MultiSignatureError::VerificationKeyInfinity(_) => unreachable!(),
255-
}
256-
}
257-
}
248+
/// Verification key is the infinity
249+
#[error("Verification key is the infinity")]
250+
VerificationKeyInfinity(Box<VerificationKey>),
258251

259-
impl From<StmSignatureError> for CoreVerifierError {
260-
fn from(e: StmSignatureError) -> Self {
261-
CoreVerifierError::IndividualSignatureInvalid(e)
262-
}
252+
/// The supplied key is not valid
253+
#[error("The verification of correctness of the supplied key is invalid.")]
254+
KeyInvalid(Box<VerificationKeyPoP>),
255+
256+
/// Serialization error
257+
#[error("Serialization error")]
258+
SerializationError,
259+
260+
/// UnregisteredInitializer error
261+
#[error("Initializer not registered. Cannot participate as a signer.")]
262+
UnregisteredInitializer,
263263
}
264264

265265
impl From<MultiSignatureError> for RegisterError {

mithril-stm/src/key_reg.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@ pub struct KeyReg {
1919
keys: HashMap<VerificationKey, Stake>,
2020
}
2121

22-
/// Structure generated out of a closed registration containing the registered parties, total stake, and the merkle tree.
23-
/// One can only get a global `avk` out of a closed key registration.
24-
#[derive(Clone, Debug, PartialEq, Eq)]
25-
pub struct ClosedKeyReg<D: Digest> {
26-
/// Ordered list of registered parties.
27-
pub reg_parties: Vec<RegParty>,
28-
/// Total stake of the registered parties.
29-
pub total_stake: Stake,
30-
/// Unique public key out of the key registration instance.
31-
pub merkle_tree: Arc<MerkleTree<D>>,
32-
}
33-
3422
impl KeyReg {
3523
/// Initialize an empty `KeyReg`.
3624
pub fn init() -> Self {
@@ -78,6 +66,18 @@ impl KeyReg {
7866
}
7967
}
8068

69+
/// Structure generated out of a closed registration containing the registered parties, total stake, and the merkle tree.
70+
/// One can only get a global `avk` out of a closed key registration.
71+
#[derive(Clone, Debug, PartialEq, Eq)]
72+
pub struct ClosedKeyReg<D: Digest> {
73+
/// Ordered list of registered parties.
74+
pub reg_parties: Vec<RegParty>,
75+
/// Total stake of the registered parties.
76+
pub total_stake: Stake,
77+
/// Unique public key out of the key registration instance.
78+
pub merkle_tree: Arc<MerkleTree<D>>,
79+
}
80+
8181
#[cfg(test)]
8282
mod tests {
8383
use super::*;

0 commit comments

Comments
 (0)