Skip to content

Commit

Permalink
intermediate before prune
Browse files Browse the repository at this point in the history
  • Loading branch information
naftulikay committed Nov 10, 2023
1 parent df4291f commit 173fa56
Show file tree
Hide file tree
Showing 11 changed files with 586 additions and 33 deletions.
130 changes: 100 additions & 30 deletions example_crypto/benches/rand.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,109 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rand::{thread_rng, RngCore};

fn bench_rand_thread_rng(c: &mut Criterion) {
// stack tests
c.bench_function("rand::thread_rng::stack_32", |b| {
b.iter(|| {
let mut s = [0; 32];
thread_rng().fill_bytes(&mut s);
})
});
c.bench_function("rand::thread_rng::stack_64", |b| {
b.iter(|| {
let mut s = [0; 64];
thread_rng().fill_bytes(&mut s);
})
});
// vec tests
c.bench_function("rand::thread_rng::alloc_32", |b| {
b.iter(|| {
let mut v = vec![0; 32];
thread_rng().fill_bytes(&mut v.as_mut_slice());
})
});
c.bench_function("rand::thread_rng::alloc_64", |b| {
b.iter(|| {
let mut v = vec![0; 64];
thread_rng().fill_bytes(&mut v.as_mut_slice());
})
});
use example_crypto::rng::{
DevRandomBufRng, DevRandomDirectRng, DevUrandomBufRng, DevUrandomDirectRng, OpenSslRng,
RandCrateOs, RandCrateThread, RandGenerator, RandOsRng, RandThreadRng, SysRandomBuffered,
SysRandomDirect, SysUrandomBuffered, SysUrandomDirect,
};

const BUFFER_SIZES: [usize; 9] = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192];

pub struct RngBencher<R: RandGenerator> {
rng: R,
prefix: &'static str,
}

impl<R> RngBencher<R>
where
R: RandGenerator,
{
pub fn new(rng: R, prefix: &'static str) -> Self {
Self { rng, prefix }
}

/// Conduct the benchmark using fixed-size arrays on the stack.
pub fn bench_arr(&mut self, c: &mut Criterion) -> &mut Self {
self.bench_arr_specific::<32>(c);
self.bench_arr_specific::<64>(c);
self.bench_arr_specific::<128>(c);
self.bench_arr_specific::<256>(c);
self.bench_arr_specific::<512>(c);
self.bench_arr_specific::<1024>(c);
self.bench_arr_specific::<2048>(c);
self.bench_arr_specific::<4096>(c);
self.bench_arr_specific::<8192>(c);
self
}

fn bench_arr_specific<const S: usize>(&mut self, c: &mut Criterion) {
c.bench_function(format!("{}::array::{}", self.prefix, S).as_str(), |b| {
b.iter(|| self.rng.generate_array::<S>());
});
}

/// Conduct the benchmarks using fixed-size vectors on the heap.
pub fn bench_vec(&mut self, c: &mut Criterion) -> &mut Self {
self.bench_vec_specific::<32>(c);
self.bench_vec_specific::<64>(c);
self.bench_vec_specific::<128>(c);
self.bench_vec_specific::<256>(c);
self.bench_vec_specific::<512>(c);
self.bench_vec_specific::<1024>(c);
self.bench_vec_specific::<2048>(c);
self.bench_vec_specific::<4096>(c);
self.bench_vec_specific::<8192>(c);
self
}

fn bench_vec_specific<const S: usize>(&mut self, c: &mut Criterion) {
c.bench_function(format!("{}::vec::{}", self.prefix, S).as_str(), |b| {
b.iter(|| self.rng.generate_vec::<S>());
});
}
}

fn bench_fast_rands(c: &mut Criterion) {
// buffered /dev/urandom
RngBencher::new(DevUrandomBufRng::new(), DevUrandomBufRng::PREFIX)
.bench_arr(c)
.bench_vec(c);

// direct /dev/urandom
RngBencher::new(DevUrandomDirectRng::new(), DevUrandomDirectRng::PREFIX)
.bench_arr(c)
.bench_vec(c);

// openssl
RngBencher::new(OpenSslRng::new(), OpenSslRng::PREFIX)
.bench_arr(c)
.bench_vec(c);

// rand: os rng
RngBencher::new(RandOsRng::new(), RandOsRng::PREFIX)
.bench_arr(c)
.bench_vec(c);

// rand: thread rng
RngBencher::new(RandThreadRng::new(), RandThreadRng::PREFIX)
.bench_arr(c)
.bench_vec(c);
}

fn bench_slow_rands(c: &mut Criterion) {
// buffered /dev/random
RngBencher::new(DevRandomBufRng::new(), DevRandomBufRng::PREFIX)
.bench_arr(c)
.bench_vec(c);

// direct /dev/random
RngBencher::new(DevRandomDirectRng::new(), DevRandomDirectRng::PREFIX)
.bench_arr(c)
.bench_vec(c);
}

criterion_group! {
name = rand;
config = Criterion::default();
targets = bench_rand_thread_rng
targets = bench_fast_rands
}

criterion_main!(rand);
38 changes: 36 additions & 2 deletions example_crypto/benches/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use example_crypto::openssl::sign::Ed25519Signer;
use rand::{thread_rng, RngCore};
use std::sync::atomic::{AtomicUsize, Ordering};

fn bench_sign(c: &mut Criterion) {
fn bench_sign_ed25519(c: &mut Criterion) {
// eddsa
c.bench_function("openssl::sign::ed25519", |b| {
const KEY_COUNT: usize = 256;
Expand Down Expand Up @@ -37,10 +37,44 @@ fn bench_sign(c: &mut Criterion) {
});
}

fn bench_sign_ed448(c: &mut Criterion) {
// eddsa
c.bench_function("openssl::sign::ed448", |b| {
const KEY_COUNT: usize = 256;
const DATA_COUNT: usize = 512;

// build 256 signers
let keys: Vec<Ed25519Signer> = (0..KEY_COUNT).map(|_| Ed25519Signer::random()).collect();

let data: Vec<[u8; 32]> = (0..DATA_COUNT)
.map(|_| {
let mut d = [0; 32];
thread_rng().fill_bytes(&mut d);
d
})
.collect();

let (signer_index, data_index) = (AtomicUsize::new(0), AtomicUsize::new(0));

b.iter(|| {
let (current_signer, current_data) = (
signer_index.fetch_add(1, Ordering::AcqRel) % KEY_COUNT,
data_index.fetch_add(1, Ordering::AcqRel) % DATA_COUNT,
);

// get em fast
let _sig = unsafe {
keys.get_unchecked(current_signer)
.sign(data.get_unchecked(current_data))
};
})
});
}

criterion_group! {
name = sign;
config = Criterion::default();
targets = bench_sign
targets = bench_sign_ed25519,
}

criterion_main!(sign);
3 changes: 2 additions & 1 deletion example_crypto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod openssl;
pub mod openssl;
pub mod rng;
1 change: 1 addition & 0 deletions example_crypto/src/openssl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod asymm;
pub mod client_ca;
pub mod keygen;
pub mod sign;
Empty file.
37 changes: 37 additions & 0 deletions example_crypto/src/openssl/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ use openssl::pkey::{Id, PKey, Private};
use openssl::sign::Signer;
use rand::{thread_rng, RngCore};

pub enum EllipticCurve {
Ed25519,
Ed448,
SECP256R1,
SECP384R1,
}

pub struct EdDSASigner {
pub key: PKey<Private>,
}

pub struct Ed25519Signer {
key: PKey<Private>,
}
Expand Down Expand Up @@ -36,3 +47,29 @@ impl Ed25519Signer {
sig
}
}

pub struct Ed448Signer {
key: PKey<Private>,
}

impl Ed448Signer {
/// Generate a new signer with a randomly generated key.
///
/// All values are legal in EdDSA keys, so it's simply generating 32 bytes from the CSPNG.
pub fn random() -> Self {
Self {
key: PKey::generate_ed448().unwrap(),
}
}

pub fn sign(&self, data: &[u8]) -> [u8; 64] {
let mut sig = [0; 64];

let _signature_length = Signer::new_without_digest(&self.key)
.expect("unable to create signer")
.sign_oneshot(&mut sig, data)
.expect("unable to sign data");

sig
}
}
Loading

0 comments on commit 173fa56

Please sign in to comment.