Skip to content

Commit e798a29

Browse files
committed
feat(stm): implement future proof system placeholder
This will be removed when a new proof system is implemented. In the mean time it structures the code for ease of implementation later.
1 parent a2ec273 commit e798a29

File tree

3 files changed

+91
-22
lines changed

3 files changed

+91
-22
lines changed

mithril-stm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ crate-type = ["lib", "cdylib", "staticlib"]
1717
default = ["rug-backend"]
1818
rug-backend = ["rug/default"]
1919
num-integer-backend = ["num-bigint", "num-rational", "num-traits"]
20-
benchmark-internals = [] # For benchmarking multi_sig
20+
benchmark-internals = [] # For benchmarking multi_sig
21+
future_proof_system = [] # For activating future proof systems
2122

2223
[dependencies]
2324
blake2 = "0.10.6"

mithril-stm/src/aggregate_signature/signature.rs

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use super::ConcatenationProof;
1616
pub enum AggregateSignatureType {
1717
/// Concatenation proof system.
1818
Concatenation,
19+
/// Future proof system. Not suitable for production.
20+
#[cfg(feature = "future_proof_system")]
21+
Future,
1922
}
2023

2124
impl AggregateSignatureType {
@@ -25,6 +28,8 @@ impl AggregateSignatureType {
2528
pub fn to_bytes_encoding_prefix(&self) -> u8 {
2629
match self {
2730
AggregateSignatureType::Concatenation => 0,
31+
#[cfg(feature = "future_proof_system")]
32+
AggregateSignatureType::Future => 255,
2833
}
2934
}
3035

@@ -34,6 +39,8 @@ impl AggregateSignatureType {
3439
pub fn from_bytes_encoding_prefix(byte: u8) -> Option<Self> {
3540
match byte {
3641
0 => Some(AggregateSignatureType::Concatenation),
42+
#[cfg(feature = "future_proof_system")]
43+
255 => Some(AggregateSignatureType::Future),
3744
_ => None,
3845
}
3946
}
@@ -45,6 +52,8 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> From<&AggregateSignature<D>>
4552
fn from(aggr_sig: &AggregateSignature<D>) -> Self {
4653
match aggr_sig {
4754
AggregateSignature::Concatenation(_) => AggregateSignatureType::Concatenation,
55+
#[cfg(feature = "future_proof_system")]
56+
AggregateSignature::Future => AggregateSignatureType::Future,
4857
}
4958
}
5059
}
@@ -53,6 +62,8 @@ impl Display for AggregateSignatureType {
5362
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5463
match self {
5564
AggregateSignatureType::Concatenation => write!(f, "Concatenation"),
65+
#[cfg(feature = "future_proof_system")]
66+
AggregateSignatureType::Future => write!(f, "Future"),
5667
}
5768
}
5869
}
@@ -63,9 +74,16 @@ impl Display for AggregateSignatureType {
6374
serialize = "MerkleBatchPath<D>: Serialize",
6475
deserialize = "MerkleBatchPath<D>: Deserialize<'de>"
6576
))]
66-
#[serde(untagged)]
6777
pub enum AggregateSignature<D: Clone + Digest + FixedOutput + Send + Sync> {
78+
/// A future proof system.
79+
#[cfg(feature = "future_proof_system")]
80+
Future,
81+
6882
/// Concatenation proof system.
83+
// The 'untagged' attribute is required for backward compatibility.
84+
// It implies that this variant is placed at the end of the enum.
85+
// It will be removed when the support for JSON hex encoding is dropped in the calling crates.
86+
#[serde(untagged)]
6987
Concatenation(ConcatenationProof<D>),
7088
}
7189

@@ -78,9 +96,13 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
7896
parameters: &Parameters,
7997
) -> Result<(), StmAggregateSignatureError<D>> {
8098
match self {
81-
AggregateSignature::Concatenation(stm_aggr_sig) => {
82-
stm_aggr_sig.verify(msg, avk, parameters)
99+
AggregateSignature::Concatenation(concatenation_proof) => {
100+
concatenation_proof.verify(msg, avk, parameters)
83101
}
102+
#[cfg(feature = "future_proof_system")]
103+
AggregateSignature::Future => Err(StmAggregateSignatureError::UnsupportedProofSystem(
104+
self.into(),
105+
)),
84106
}
85107
}
86108

@@ -98,21 +120,33 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
98120
});
99121
stm_signatures
100122
.into_iter()
101-
.try_for_each(
102-
|(stm_aggr_sig_type, stm_aggr_sigs)| match stm_aggr_sig_type {
103-
AggregateSignatureType::Concatenation => ConcatenationProof::batch_verify(
104-
&stm_aggr_sigs
123+
.try_for_each(|(aggregate_signature_type, aggregate_signatures)| {
124+
match aggregate_signature_type {
125+
AggregateSignatureType::Concatenation => {
126+
let aggregate_signatures_length = aggregate_signatures.len();
127+
let concatenation_proofs = aggregate_signatures
105128
.into_iter()
106-
.filter_map(|s| match s {
107-
Self::Concatenation(stm_aggr_sig) => Some(stm_aggr_sig),
108-
})
109-
.collect::<Vec<_>>(),
110-
msgs,
111-
avks,
112-
parameters,
113-
),
114-
},
115-
)
129+
.filter_map(|s| s.to_concatenation_proof().cloned())
130+
.collect::<Vec<_>>();
131+
if concatenation_proofs.len() != aggregate_signatures_length {
132+
return Err(StmAggregateSignatureError::BatchInvalid);
133+
}
134+
135+
ConcatenationProof::batch_verify(
136+
&concatenation_proofs,
137+
msgs,
138+
avks,
139+
parameters,
140+
)
141+
}
142+
#[cfg(feature = "future_proof_system")]
143+
AggregateSignatureType::Future => {
144+
Err(StmAggregateSignatureError::UnsupportedProofSystem(
145+
aggregate_signature_type,
146+
))
147+
}
148+
}
149+
})
116150
.map_err(|_| StmAggregateSignatureError::BatchInvalid)
117151
}
118152

@@ -127,6 +161,8 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
127161
AggregateSignature::Concatenation(concatenation_proof) => {
128162
concatenation_proof.to_bytes()
129163
}
164+
#[cfg(feature = "future_proof_system")]
165+
AggregateSignature::Future => vec![],
130166
};
131167
aggregate_signature_bytes.append(&mut proof_bytes);
132168

@@ -143,22 +179,45 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
143179
AggregateSignatureType::Concatenation => Ok(AggregateSignature::Concatenation(
144180
ConcatenationProof::from_bytes(proof_bytes)?,
145181
)),
182+
#[cfg(feature = "future_proof_system")]
183+
AggregateSignatureType::Future => Ok(AggregateSignature::Future),
184+
}
185+
}
186+
187+
/// If the aggregate signature is a concatenation proof, return it.
188+
pub fn to_concatenation_proof(&self) -> Option<&ConcatenationProof<D>> {
189+
match self {
190+
AggregateSignature::Concatenation(proof) => Some(proof),
191+
#[cfg(feature = "future_proof_system")]
192+
AggregateSignature::Future => None,
146193
}
147194
}
148195

149196
/// Extract the list of signatures.
150197
// TODO: transfer this function to the concatenation proof ? Some proofs might not fully carry this information
151198
pub fn signatures(&self) -> Vec<SingleSignatureWithRegisteredParty> {
152199
match self {
153-
AggregateSignature::Concatenation(stm_aggr_sig) => stm_aggr_sig.signatures.clone(),
200+
AggregateSignature::Concatenation(concatenation_proof) => {
201+
concatenation_proof.signatures.clone()
202+
}
203+
#[cfg(feature = "future_proof_system")]
204+
AggregateSignature::Future(concatenation_proof) => {
205+
concatenation_proof.signatures.clone()
206+
}
154207
}
155208
}
156209

157210
/// Extract the list of unique merkle tree nodes that covers path for all signatures.
158211
// TODO: transfer this function to the concatenation proof
159212
pub fn batch_proof(&self) -> MerkleBatchPath<D> {
160213
match self {
161-
AggregateSignature::Concatenation(stm_aggr_sig) => stm_aggr_sig.batch_proof.clone(),
214+
AggregateSignature::Concatenation(concatenation_proof) => {
215+
concatenation_proof.batch_proof.clone()
216+
}
217+
#[cfg(feature = "future_proof_system")]
218+
AggregateSignature::Future(concatenation_proof) => {
219+
concatenation_proof.batch_proof.clone()
220+
}
162221
}
163222
}
164223

@@ -167,8 +226,12 @@ impl<D: Clone + Digest + FixedOutput + Send + Sync> AggregateSignature<D> {
167226
#[cfg(test)]
168227
pub(crate) fn set_batch_proof(&mut self, batch_proof: MerkleBatchPath<D>) {
169228
match self {
170-
AggregateSignature::Concatenation(stm_aggr_sig) => {
171-
stm_aggr_sig.batch_proof = batch_proof
229+
AggregateSignature::Concatenation(concatenation_proof) => {
230+
concatenation_proof.batch_proof = batch_proof
231+
}
232+
#[cfg(feature = "future_proof_system")]
233+
AggregateSignature::Future(concatenation_proof) => {
234+
concatenation_proof.batch_proof = batch_proof
172235
}
173236
}
174237
}

mithril-stm/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use blake2::digest::{Digest, FixedOutput};
33
use blst::BLST_ERROR;
44

5+
use crate::aggregate_signature::AggregateSignatureType;
56
use crate::bls_multi_signature::{
67
BlsSignature, BlsVerificationKey, BlsVerificationKeyProofOfPossession,
78
};
@@ -190,6 +191,10 @@ pub enum StmAggregateSignatureError<D: Digest + FixedOutput> {
190191
/// `CoreVerifier` check failed
191192
#[error("Core verification error: {0}")]
192193
CoreVerificationError(#[source] CoreVerifierError),
194+
195+
/// The proof system used in the aggregate signature is not supported
196+
#[error("Unsupported proof system: {0}")]
197+
UnsupportedProofSystem(AggregateSignatureType),
193198
}
194199

195200
impl<D: Digest + FixedOutput> From<MerkleTreeError<D>> for StmAggregateSignatureError<D> {

0 commit comments

Comments
 (0)