From 4cd1fa8c383d32bf9d9e0a5f180429f205d08dc9 Mon Sep 17 00:00:00 2001 From: Robert Kelly Date: Sat, 12 May 2018 15:42:27 -0400 Subject: [PATCH 1/3] refactored seed generation --- src/signature.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/signature.rs b/src/signature.rs index fca8dfb234da27..93395b5f32cc8c 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -3,6 +3,7 @@ use generic_array::GenericArray; use generic_array::typenum::{U32, U64}; use rand::{ChaChaRng, Rng, SeedableRng}; +use rayon::prelude::*; use ring::error::Unspecified; use ring::rand::SecureRandom; use ring::signature::Ed25519KeyPair; @@ -68,11 +69,27 @@ impl GenKeys { KeyPair::generate_pkcs8(self).unwrap().to_vec() } - pub fn gen_n_keys(&self, n_keys: i64, tokens_per_user: i64) -> Vec<(Vec, i64)> { - let users: Vec<_> = (0..n_keys) + pub fn gen_n_seeds(&self, n_seeds: i64) -> Vec<[u8; 16]> { + let mut rng = self.generator.borrow_mut(); + + let seeds = (0..n_seeds) .into_iter() .map(|_| { - let pkcs8 = self.new_key(); + let seed: [u8; 16] = rng.gen(); + seed + }) + .collect(); + seeds + } + + pub fn gen_n_keys(&self, n_keys: i64, tokens_per_user: i64) -> Vec<(Vec, i64)> { + let keys = self.gen_n_seeds(n_keys); + + let users: Vec<_> = keys + .into_par_iter() + .map(|seed| { + let new: GenKeys = GenKeys::new(&seed[..]); + let pkcs8 = KeyPair::generate_pkcs8(&new).unwrap().to_vec(); (pkcs8, tokens_per_user) }) .collect(); From 6149c2fcb56620178c3f2583d5ba823e1025ee90 Mon Sep 17 00:00:00 2001 From: Robert Kelly Date: Sat, 12 May 2018 18:08:08 -0400 Subject: [PATCH 2/3] added benchmarks for two GenKeys --- src/signature.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/src/signature.rs b/src/signature.rs index 93395b5f32cc8c..786c048e1aae6a 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -69,6 +69,45 @@ impl GenKeys { KeyPair::generate_pkcs8(self).unwrap().to_vec() } + pub fn gen_n_keys(&self, n_keys: i64, tokens_per_user: i64) -> Vec<(Vec, i64)> { + let users: Vec<_> = (0..n_keys) + .into_iter() + .map(|_| { + let pkcs8 = self.new_key(); + (pkcs8, tokens_per_user) + }) + .collect(); + users + } +} + +impl SecureRandom for GenKeys { + fn fill(&self, dest: &mut [u8]) -> Result<(), Unspecified> { + let mut rng = self.generator.borrow_mut(); + rng.fill_bytes(dest); + Ok(()) + } +} + +pub struct GenKeys2 { + // This is necessary because the rng needs to mutate its state to remain + // deterministic, and the fill trait requires an immuatble reference to self + generator: RefCell, +} + +impl GenKeys2 { + pub fn new(seed_values: &[u8]) -> GenKeys2 { + let seed: &[u8] = &seed_values[..]; + let rng: ChaChaRng = SeedableRng::from_seed(unsafe { mem::transmute(seed) }); + GenKeys2 { + generator: RefCell::new(rng), + } + } + + pub fn new_key(&self) -> Vec { + KeyPair::generate_pkcs8(self).unwrap().to_vec() + } + pub fn gen_n_seeds(&self, n_seeds: i64) -> Vec<[u8; 16]> { let mut rng = self.generator.borrow_mut(); @@ -88,7 +127,7 @@ impl GenKeys { let users: Vec<_> = keys .into_par_iter() .map(|seed| { - let new: GenKeys = GenKeys::new(&seed[..]); + let new: GenKeys2 = GenKeys2::new(&seed[..]); let pkcs8 = KeyPair::generate_pkcs8(&new).unwrap().to_vec(); (pkcs8, tokens_per_user) }) @@ -97,7 +136,7 @@ impl GenKeys { } } -impl SecureRandom for GenKeys { +impl SecureRandom for GenKeys2 { fn fill(&self, dest: &mut [u8]) -> Result<(), Unspecified> { let mut rng = self.generator.borrow_mut(); rng.fill_bytes(dest); @@ -105,12 +144,30 @@ impl SecureRandom for GenKeys { } } -#[cfg(test)] + +#[cfg(all(feature = "unstable", test))] mod tests { + extern crate test; + + use self::test::Bencher; use super::*; use std::collections::HashSet; use std::iter::FromIterator; + #[bench] + fn bench_gen_keys(b: &mut Bencher) { + let seed: &[_] = &[1, 2, 3, 4]; + let rnd = GenKeys::new(seed); + b.iter(|| rnd.gen_n_keys(100, 1)); + } + + #[bench] + fn bench_gen_keys2(b: &mut Bencher) { + let seed: &[_] = &[1, 2, 3, 4]; + let rnd = GenKeys2::new(seed); + b.iter(|| rnd.gen_n_keys(100, 1)); + } + #[test] fn test_new_key_is_redundant() { let seed: &[_] = &[1, 2, 3, 4]; From ff5e1c635f7bf049569232033f78d299ad27b903 Mon Sep 17 00:00:00 2001 From: Robert Kelly Date: Sat, 12 May 2018 18:18:18 -0400 Subject: [PATCH 3/3] increased iterations --- src/signature.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/signature.rs b/src/signature.rs index 786c048e1aae6a..15771e280a596f 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -158,14 +158,14 @@ mod tests { fn bench_gen_keys(b: &mut Bencher) { let seed: &[_] = &[1, 2, 3, 4]; let rnd = GenKeys::new(seed); - b.iter(|| rnd.gen_n_keys(100, 1)); + b.iter(|| rnd.gen_n_keys(1000, 1)); } #[bench] fn bench_gen_keys2(b: &mut Bencher) { let seed: &[_] = &[1, 2, 3, 4]; let rnd = GenKeys2::new(seed); - b.iter(|| rnd.gen_n_keys(100, 1)); + b.iter(|| rnd.gen_n_keys(1000, 1)); } #[test]