Skip to content

Commit 925bfe8

Browse files
authored
Merge pull request #484 from input-output-hk/signature-without-path
Single signature without merkle path
2 parents f688512 + a3cad9b commit 925bfe8

File tree

17 files changed

+737
-289
lines changed

17 files changed

+737
-289
lines changed

clippy

Whitespace-only changes.

demo/protocol-demo/src/demonstrator.rs

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use hex::ToHex;
2-
use rand_chacha::ChaCha20Rng;
3-
use rand_core::{RngCore, SeedableRng};
2+
use rand_core::{CryptoRng, RngCore};
43
use serde::{Deserialize, Serialize};
54
use std::collections::HashMap;
65
use std::env;
@@ -42,14 +41,16 @@ struct MultiSignatureArtifact {
4241
}
4342

4443
/// Party represents a signing protocol participant
45-
#[derive(Debug)]
44+
#[derive(Debug, Clone)]
4645
pub struct Party {
4746
/// Party's identifier
4847
party_id: ProtocolPartyId,
4948
/// Party's stake
5049
stake: ProtocolStake,
5150
/// Protocol parameters
5251
params: Option<ProtocolParameters>,
52+
/// Protocol initializer
53+
initializer: Option<ProtocolInitializerNotCertified>,
5354
/// Protocol signer
5455
signer: Option<ProtocolSigner>,
5556
/// Protocol clerk
@@ -66,6 +67,7 @@ impl Party {
6667
party_id: format!("{}", party_id) as ProtocolPartyId,
6768
stake: stake as ProtocolStake,
6869
params: None,
70+
initializer: None,
6971
signer: None,
7072
clerk: None,
7173
msigs: HashMap::new(),
@@ -105,10 +107,13 @@ impl Party {
105107
}
106108
let closed_reg = key_reg.close();
107109

108-
let seed = [0u8; 32];
109-
let mut rng = ChaCha20Rng::from_seed(seed);
110-
let p = ProtocolInitializerNotCertified::setup(self.params.unwrap(), self.stake, &mut rng);
111-
self.signer = Some(p.new_signer(closed_reg).unwrap());
110+
let signer = self
111+
.initializer
112+
.clone()
113+
.unwrap()
114+
.new_signer(closed_reg)
115+
.unwrap();
116+
self.signer = Some(signer);
112117
self.clerk = Some(ProtocolClerk::from_signer(self.signer.as_ref().unwrap()));
113118
}
114119

@@ -296,10 +301,8 @@ pub struct Demonstrator {
296301

297302
impl Demonstrator {
298303
/// Demonstrator factory
299-
pub fn new(config: &crate::Config) -> Self {
304+
pub fn new<R: RngCore + CryptoRng>(config: &crate::Config, rng: &mut R) -> Self {
300305
// Generate parties
301-
let seed = [0u8; 32];
302-
let mut rng = ChaCha20Rng::from_seed(seed);
303306
let parties = (0..config.nparties)
304307
.into_iter()
305308
.map(|party_id| Party::new(party_id, 1 + rng.next_u64() % 999))
@@ -328,7 +331,7 @@ pub trait ProtocolDemonstrator {
328331
fn establish(&mut self);
329332

330333
/// Initialization phase of the protocol
331-
fn initialize(&mut self);
334+
fn initialize<R: RngCore + CryptoRng>(&mut self, rng: &mut R);
332335

333336
/// Issue certificates
334337
fn issue_certificates(&mut self);
@@ -349,7 +352,7 @@ impl ProtocolDemonstrator for Demonstrator {
349352
}
350353

351354
/// Initialization phase of the protocol
352-
fn initialize(&mut self) {
355+
fn initialize<R: RngCore + CryptoRng>(&mut self, rng: &mut R) {
353356
// Retrieve protocol parameters
354357
let mut verifier = Verifier::new();
355358
verifier.update_params(&self.params.unwrap());
@@ -358,25 +361,17 @@ impl ProtocolDemonstrator for Demonstrator {
358361
}
359362

360363
// Register keys
361-
let seed = [0u8; 32];
362-
let mut rng = ChaCha20Rng::from_seed(seed);
363-
let players = self
364-
.parties
365-
.iter()
366-
.map(|party| (party.party_id.to_owned(), party.stake))
367-
.collect::<Vec<_>>();
368364
let mut players_artifacts = Vec::new();
369-
for (party_id, stake) in players {
365+
for party in self.parties.iter_mut() {
370366
let protocol_initializer =
371-
ProtocolInitializerNotCertified::setup(self.params.unwrap(), stake, &mut rng);
372-
let verification_key: ProtocolSignerVerificationKey =
373-
protocol_initializer.verification_key();
367+
ProtocolInitializerNotCertified::setup(self.params.unwrap(), party.stake, rng);
374368
players_artifacts.push(PlayerArtifact {
375-
party_id,
376-
stake,
377-
verification_key: key_encode_hex(verification_key).unwrap(),
378-
initializer: key_encode_hex(protocol_initializer).unwrap(),
379-
})
369+
party_id: party.clone().party_id,
370+
stake: party.stake,
371+
verification_key: key_encode_hex(protocol_initializer.verification_key()).unwrap(),
372+
initializer: key_encode_hex(protocol_initializer.clone()).unwrap(),
373+
});
374+
party.initializer = Some(protocol_initializer);
380375
}
381376
let players_with_keys = players_artifacts
382377
.iter()
@@ -487,6 +482,8 @@ pub fn write_artifacts<T: Serialize>(artifact_name: &str, value: &T) {
487482
#[cfg(test)]
488483
mod tests {
489484
use super::*;
485+
use rand_chacha::ChaCha20Rng;
486+
use rand_core::SeedableRng;
490487

491488
fn setup_protocol_parameters() -> ProtocolParameters {
492489
ProtocolParameters {
@@ -510,14 +507,18 @@ mod tests {
510507
#[test]
511508
fn test_demonstrator_new() {
512509
let config = default_config();
513-
let demo = Demonstrator::new(&config);
510+
let seed = [0u8; 32];
511+
let mut rng = ChaCha20Rng::from_seed(seed);
512+
let demo = Demonstrator::new(&config, &mut rng);
514513
assert_eq!(demo.config, config);
515514
}
516515

517516
#[test]
518517
fn test_demonstrator_establish() {
519518
let config = default_config();
520-
let mut demo = Demonstrator::new(&config);
519+
let seed = [0u8; 32];
520+
let mut rng = ChaCha20Rng::from_seed(seed);
521+
let mut demo = Demonstrator::new(&config, &mut rng);
521522
demo.establish();
522523
assert_eq!(demo.params.unwrap().m, config.m);
523524
assert_eq!(demo.params.unwrap().k, config.k);
@@ -527,9 +528,11 @@ mod tests {
527528
#[test]
528529
fn test_demonstrator_initialize() {
529530
let config = default_config();
530-
let mut demo = Demonstrator::new(&config);
531+
let seed = [0u8; 32];
532+
let mut rng = ChaCha20Rng::from_seed(seed);
533+
let mut demo = Demonstrator::new(&config, &mut rng);
531534
demo.establish();
532-
demo.initialize();
535+
demo.initialize(&mut rng);
533536
assert_eq!(demo.parties.len(), config.nparties);
534537
assert_eq!(demo.messages.len(), config.nmessages);
535538
for party in demo.parties {
@@ -543,9 +546,11 @@ mod tests {
543546
#[test]
544547
fn test_demonstrator_issue_certificates_ok() {
545548
let config = default_config();
546-
let mut demo = Demonstrator::new(&config);
549+
let seed = [0u8; 32];
550+
let mut rng = ChaCha20Rng::from_seed(seed);
551+
let mut demo = Demonstrator::new(&config, &mut rng);
547552
demo.establish();
548-
demo.initialize();
553+
demo.initialize(&mut rng);
549554
demo.issue_certificates();
550555
assert_eq!(demo.parties.len(), config.nparties);
551556
assert_eq!(demo.messages.len(), config.nmessages);
@@ -559,9 +564,11 @@ mod tests {
559564
let mut config = default_config();
560565
config.k = 10000;
561566
config.m = 10;
562-
let mut demo = Demonstrator::new(&config);
567+
let seed = [0u8; 32];
568+
let mut rng = ChaCha20Rng::from_seed(seed);
569+
let mut demo = Demonstrator::new(&config, &mut rng);
563570
demo.establish();
564-
demo.initialize();
571+
demo.initialize(&mut rng);
565572
demo.issue_certificates();
566573
assert_eq!(demo.parties.len(), config.nparties);
567574
assert_eq!(demo.messages.len(), config.nmessages);
@@ -573,9 +580,11 @@ mod tests {
573580
#[test]
574581
fn test_demonstrator_verify_certificates_ok() {
575582
let config = default_config();
576-
let mut demo = Demonstrator::new(&config);
583+
let seed = [0u8; 32];
584+
let mut rng = ChaCha20Rng::from_seed(seed);
585+
let mut demo = Demonstrator::new(&config, &mut rng);
577586
demo.establish();
578-
demo.initialize();
587+
demo.initialize(&mut rng);
579588
demo.issue_certificates();
580589
assert_eq!(demo.parties.len(), config.nparties);
581590
assert_eq!(demo.messages.len(), config.nmessages);
@@ -587,9 +596,11 @@ mod tests {
587596
let mut config = default_config();
588597
config.k = 10000;
589598
config.m = 10;
590-
let mut demo = Demonstrator::new(&config);
599+
let seed = [0u8; 32];
600+
let mut rng = ChaCha20Rng::from_seed(seed);
601+
let mut demo = Demonstrator::new(&config, &mut rng);
591602
demo.establish();
592-
demo.initialize();
603+
demo.initialize(&mut rng);
593604
demo.issue_certificates();
594605
assert_eq!(demo.parties.len(), config.nparties);
595606
assert_eq!(demo.messages.len(), config.nmessages);

demo/protocol-demo/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ mod demonstrator;
22

33
use crate::demonstrator::{Demonstrator, ProtocolDemonstrator};
44
use clap::Parser;
5+
use rand_chacha::ChaCha20Rng;
6+
use rand_core::SeedableRng;
57

68
/// Simple demonstration of the Mithril protocol
79
#[derive(Parser, Debug, PartialEq, Clone, Copy)]
@@ -40,15 +42,17 @@ fn main() {
4042
/////////////////////
4143

4244
println!("\n>> Protocol establish phase");
43-
let mut mithril_protocol = Demonstrator::new(&config);
45+
let seed = [0u8; 32];
46+
let mut rng = ChaCha20Rng::from_seed(seed);
47+
let mut mithril_protocol = Demonstrator::new(&config, &mut rng);
4448
mithril_protocol.establish();
4549

4650
//////////////////////////
4751
// initialization phase //
4852
/////////////////////////
4953

5054
println!("\n>> Protocol initialize phase:");
51-
mithril_protocol.initialize();
55+
mithril_protocol.initialize(&mut rng);
5256

5357
//////////////////////
5458
// operations phase //

mithril-aggregator/tests/certificate_chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async fn certificate_chain() {
1313
let protocol_parameters = ProtocolParameters {
1414
k: 5,
1515
m: 100,
16-
phi_f: 0.65,
16+
phi_f: 0.95,
1717
};
1818
let mut tester = RuntimeTester::build(protocol_parameters.clone()).await;
1919

mithril-aggregator/tests/create_certificate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async fn create_certificate() {
1010
let protocol_parameters = ProtocolParameters {
1111
k: 5,
1212
m: 100,
13-
phi_f: 0.65,
13+
phi_f: 0.95,
1414
};
1515
let mut tester = RuntimeTester::build(protocol_parameters.clone()).await;
1616

mithril-aggregator/tests/test_extensions/runtime_tester.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ impl RuntimeTester {
221221
e
222222
)
223223
})?;
224+
} else {
225+
panic!(
226+
"Signer '{}' could not sign. \
227+
This test is based on the assumption that every signer signs everytime. \
228+
Possible fix: relax the protocol parameters or give more stakes to this signer.",
229+
signer_with_stake.party_id
230+
);
224231
}
225232
}
226233

mithril-common/src/crypto_helper/cardano/key_certification.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use mithril::key_reg::{ClosedKeyReg, KeyReg};
1313
use mithril::stm::{Stake, StmInitializer, StmParameters, StmSigner, StmVerificationKeyPoP};
1414
use mithril::RegisterError;
1515

16-
use blake2::{digest::consts::U32, Blake2b, Digest};
16+
use blake2::{
17+
digest::{consts::U32, FixedOutput},
18+
Blake2b, Digest,
19+
};
1720
use kes_summed_ed25519::kes::{Sum6Kes, Sum6KesSig};
1821
use kes_summed_ed25519::traits::{KesSig, KesSk};
1922
use rand_core::{CryptoRng, RngCore};
@@ -255,7 +258,7 @@ impl KeyRegWrapper {
255258

256259
/// Finalize the key registration.
257260
/// This function disables `KeyReg::register`, consumes the instance of `self`, and returns a `ClosedKeyReg`.
258-
pub fn close<D: Digest>(self) -> ClosedKeyReg<D> {
261+
pub fn close<D: Digest + FixedOutput>(self) -> ClosedKeyReg<D> {
259262
self.stm_key_reg.close()
260263
}
261264
}

mithril-common/src/crypto_helper/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub type ProtocolClerk = StmClerk<D>;
4949
pub type ProtocolKeyRegistration = KeyRegWrapper;
5050

5151
/// Alias of [MithrilCore:StmSig](struct@mithril::stm::StmSig).
52-
pub type ProtocolSingleSignature = StmSig<D>;
52+
pub type ProtocolSingleSignature = StmSig;
5353

5454
/// Alias of [MithrilCore:StmAggrSig](struct@mithril::stm::StmAggrSig).
5555
pub type ProtocolMultiSignature = StmAggrSig<D>;

0 commit comments

Comments
 (0)