From 5e8b26cfc34f546d1a8860d9e1244d5d50e793f4 Mon Sep 17 00:00:00 2001 From: Marc Brinkmann Date: Wed, 4 Jul 2018 16:43:17 +0200 Subject: [PATCH 1/2] Implemented `Rand` for `SecretKey`. --- src/crypto/mod.rs | 25 ++++++++++++------------- tests/sync_key_gen.rs | 7 ++----- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index b00db79..c3e56f3 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -16,7 +16,7 @@ use clear_on_drop::ClearOnDrop; use init_with::InitWith; use pairing::bls12_381::{Bls12, Fr, FrRepr, G1, G1Affine, G2, G2Affine}; use pairing::{CurveAffine, CurveProjective, Engine, Field, PrimeField}; -use rand::{ChaChaRng, OsRng, Rng, SeedableRng}; +use rand::{ChaChaRng, OsRng, Rand, Rng, SeedableRng}; use ring::digest; use self::error::{ErrorKind, Result}; @@ -129,12 +129,14 @@ impl Default for SecretKey { } } -impl SecretKey { - /// Creates a new secret key. - pub fn new(rng: &mut R) -> Self { +impl Rand for SecretKey { + fn rand(rng: &mut R) -> Self { SecretKey(rng.gen()) } +} +impl SecretKey { + /// Creates a secret key from an existing value pub fn from_value(f: Fr) -> Self { SecretKey(f) } @@ -405,13 +407,12 @@ mod tests { use std::collections::BTreeMap; - use rand; + use rand::{self, random}; #[test] fn test_simple_sig() { - let mut rng = rand::thread_rng(); - let sk0 = SecretKey::new(&mut rng); - let sk1 = SecretKey::new(&mut rng); + let sk0: SecretKey = random(); + let sk1: SecretKey = random(); let pk0 = sk0.public_key(); let msg0 = b"Real news"; let msg1 = b"Fake news"; @@ -464,9 +465,8 @@ mod tests { #[test] fn test_simple_enc() { - let mut rng = rand::thread_rng(); - let sk_bob = SecretKey::new(&mut rng); - let sk_eve = SecretKey::new(&mut rng); + let sk_bob: SecretKey = random(); + let sk_eve: SecretKey = random(); let pk_bob = sk_bob.public_key(); let msg = b"Muffins in the canteen today! Don't tell Eve!"; let ciphertext = pk_bob.encrypt(&msg[..]); @@ -566,8 +566,7 @@ mod tests { fn test_serde() { use bincode; - let mut rng = rand::thread_rng(); - let sk = SecretKey::new(&mut rng); + let sk: SecretKey = random(); let sig = sk.sign("Please sign here: ______"); let pk = sk.public_key(); let ser_pk = bincode::serialize(&pk).expect("serialize public key"); diff --git a/tests/sync_key_gen.rs b/tests/sync_key_gen.rs index 686ecd2..d40cdf7 100644 --- a/tests/sync_key_gen.rs +++ b/tests/sync_key_gen.rs @@ -11,10 +11,8 @@ use hbbft::crypto::{PublicKey, SecretKey}; use hbbft::sync_key_gen::SyncKeyGen; fn test_sync_key_gen_with(threshold: usize, node_num: usize) { - let mut rng = rand::thread_rng(); - // Generate individual key pairs for encryption. These are not suitable for threshold schemes. - let sec_keys: Vec = (0..node_num).map(|_| SecretKey::new(&mut rng)).collect(); + let sec_keys: Vec = (0..node_num).map(|_| rand::random()).collect(); let pub_keys: BTreeMap = sec_keys .iter() .map(|sk| sk.public_key()) @@ -37,8 +35,7 @@ fn test_sync_key_gen_with(threshold: usize, node_num: usize) { let mut accepts = Vec::new(); for (sender_id, proposal) in proposals[..=threshold].iter().enumerate() { for (node_id, node) in nodes.iter_mut().enumerate() { - let accept = node - .handle_propose(&sender_id, proposal.clone().expect("proposal")) + let accept = node.handle_propose(&sender_id, proposal.clone().expect("proposal")) .expect("valid proposal"); // Only the first `threshold + 1` manage to commit their `Accept`s. if node_id <= 2 * threshold { From 60450d2c90d136745b2f94fd9167eaf365c37996 Mon Sep 17 00:00:00 2001 From: Marc Brinkmann Date: Thu, 5 Jul 2018 10:12:57 +0200 Subject: [PATCH 2/2] Reformatted using nightly rustfmt instead of stable rustfmt. --- tests/sync_key_gen.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/sync_key_gen.rs b/tests/sync_key_gen.rs index d40cdf7..e573c8c 100644 --- a/tests/sync_key_gen.rs +++ b/tests/sync_key_gen.rs @@ -35,7 +35,8 @@ fn test_sync_key_gen_with(threshold: usize, node_num: usize) { let mut accepts = Vec::new(); for (sender_id, proposal) in proposals[..=threshold].iter().enumerate() { for (node_id, node) in nodes.iter_mut().enumerate() { - let accept = node.handle_propose(&sender_id, proposal.clone().expect("proposal")) + let accept = node + .handle_propose(&sender_id, proposal.clone().expect("proposal")) .expect("valid proposal"); // Only the first `threshold + 1` manage to commit their `Accept`s. if node_id <= 2 * threshold {