Skip to content

Commit

Permalink
Randomize benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Nov 13, 2024
1 parent 4ea84ca commit d2c4a39
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::num::NonZero;
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use crypto_bigint::{nlimbs, BoxedUint, Integer, Odd, RandomBits, Uint, U1024, U128, U256};
use rand_chacha::ChaCha8Rng;
use rand_core::{CryptoRngCore, OsRng, SeedableRng};
use rand_core::{CryptoRngCore, OsRng, RngCore, SeedableRng};

#[cfg(feature = "tests-gmp")]
use rug::{integer::Order, Integer as GmpInteger};
Expand All @@ -25,6 +25,12 @@ fn make_rng() -> ChaCha8Rng {
ChaCha8Rng::from_seed(*b"01234567890123456789012345678901")
}

fn make_random_rng() -> ChaCha8Rng {
let mut seed = <ChaCha8Rng as SeedableRng>::Seed::default();
OsRng.fill_bytes(&mut seed);
ChaCha8Rng::from_seed(seed)
}

fn random_odd_uint<T: RandomBits + Integer>(rng: &mut impl CryptoRngCore, bit_length: u32) -> Odd<T> {
random_odd_integer::<T>(rng, NonZero::new(bit_length).unwrap())
}
Expand Down Expand Up @@ -282,38 +288,58 @@ fn bench_presets(c: &mut Criterion) {
#[cfg(feature = "multicore")]
fn bench_multicore_presets(c: &mut Criterion) {
let mut group = c.benchmark_group("Presets (multicore)");
let mut rng = make_rng();

group.bench_function("(U128) Random prime", |b| {
b.iter(|| par_generate_prime_with_rng::<U128>(&mut rng, 128, num_cpus::get()))
b.iter_batched(
make_random_rng,
|mut rng| par_generate_prime_with_rng::<U128>(&mut rng, 128, num_cpus::get()),
BatchSize::SmallInput,
)
});

let mut rng = make_rng();
group.bench_function("(U1024) Random prime", |b| {
b.iter(|| par_generate_prime_with_rng::<U1024>(&mut rng, 1024, num_cpus::get()))
b.iter_batched(
make_random_rng,
|mut rng| par_generate_prime_with_rng::<U1024>(&mut rng, 1024, num_cpus::get()),
BatchSize::SmallInput,
)
});

let mut rng = make_rng();
group.bench_function("(U128) Random safe prime", |b| {
b.iter(|| par_generate_safe_prime_with_rng::<U128>(&mut rng, 128, num_cpus::get()))
b.iter_batched(
make_random_rng,
|mut rng| par_generate_safe_prime_with_rng::<U128>(&mut rng, 128, num_cpus::get()),
BatchSize::SmallInput,
)
});

group.sample_size(20);
let mut rng = make_rng();
group.bench_function("(U1024) Random safe prime", |b| {
b.iter(|| par_generate_safe_prime_with_rng::<U1024>(&mut rng, 1024, num_cpus::get()))
b.iter_batched(
make_random_rng,
|mut rng| par_generate_safe_prime_with_rng::<U1024>(&mut rng, 1024, num_cpus::get()),
BatchSize::SmallInput,
)
});

let mut rng = make_rng();
group.bench_function("(Boxed128) Random safe prime", |b| {
b.iter(|| par_generate_safe_prime_with_rng::<BoxedUint>(&mut rng, 128, num_cpus::get()))
b.iter_batched(
make_random_rng,
|mut rng| par_generate_safe_prime_with_rng::<BoxedUint>(&mut rng, 128, num_cpus::get()),
BatchSize::SmallInput,
)
});

group.sample_size(20);
let mut rng = make_rng();
group.bench_function("(Boxed1024) Random safe prime", |b| {
b.iter(|| par_generate_safe_prime_with_rng::<BoxedUint>(&mut rng, 1024, num_cpus::get()))
b.iter_batched(
make_random_rng,
|mut rng| par_generate_safe_prime_with_rng::<BoxedUint>(&mut rng, 1024, num_cpus::get()),
BatchSize::SmallInput,
)
});
}

#[cfg(not(feature = "multicore"))]
fn bench_multicore_presets(_c: &mut Criterion) {}

Expand Down

0 comments on commit d2c4a39

Please sign in to comment.