Skip to content

Commit 3cf6fa1

Browse files
committed
bench: use seeded RNG for glass_pumpkin prime generation benchmarks
Use glass_pumpkin's from_rng() API with a seeded ChaCha8Rng instead of new() which uses OsRng internally, making these benchmarks deterministic. num-primes doesn't expose an RNG parameter so it remains non-deterministic.
1 parent 75e7ea8 commit 3cf6fa1

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bench/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ num-bigint = { version = "0.4", features = ["rand"] }
1414
num-prime = { path = "..", features = ["big-int"] }
1515
criterion = { version = "4.3.0", package = "codspeed-criterion-compat" }
1616
rand = "0.8"
17+
rand_chacha = "0.10"
18+
rand_core = "0.10"
1719
num-primes = { version = "0.3.0", optional = true }
1820
primal-check = "0.3.1"
1921
is_prime = "2.0.7"
@@ -24,5 +26,3 @@ number-theory = "0.0.6"
2426

2527
[features]
2628
default = ["num-primes"]
27-
28-

bench/benches/bench.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use num_prime::{nt_funcs, RandPrime};
1010
use num_primes::{Generator, Verification};
1111
use rand::rngs::StdRng;
1212
use rand::SeedableRng;
13+
use rand_chacha::ChaCha8Rng;
14+
use rand_core::SeedableRng as SeedableRng2;
1315

1416
#[cfg(target_arch = "x86_64")]
1517
use number_theory::NumberTheory;
@@ -221,8 +223,12 @@ pub fn bench_prime_gen(c: &mut Criterion) {
221223
group.bench_function("num-prime (this crate)", |b| {
222224
b.iter(|| -> num_bigint::BigUint { rng.gen_prime(256, None) })
223225
});
226+
// Note: num-primes uses thread_rng() internally, so this benchmark is not deterministic
224227
group.bench_function("num-primes", |b| b.iter(|| Generator::new_prime(256)));
225-
group.bench_function("glass_pumpkin", |b| b.iter(|| gprime::new(256)));
228+
let mut rng_gp = ChaCha8Rng::seed_from_u64(257);
229+
group.bench_function("glass_pumpkin", |b| {
230+
b.iter(|| gprime::from_rng(256, &mut rng_gp))
231+
});
226232
group.finish();
227233

228234
let mut group = c.benchmark_group("safe prime generation (256 bits)");
@@ -232,8 +238,12 @@ pub fn bench_prime_gen(c: &mut Criterion) {
232238
group.bench_function("num-prime (this crate)", |b| {
233239
b.iter(|| -> num_bigint::BigUint { rng.gen_safe_prime(256) })
234240
});
241+
// Note: num-primes uses thread_rng() internally, so this benchmark is not deterministic
235242
group.bench_function("num-primes", |b| b.iter(|| Generator::safe_prime(256)));
236-
group.bench_function("glass_pumpkin", |b| b.iter(|| safe_gprime::new(256)));
243+
let mut rng_gp = ChaCha8Rng::seed_from_u64(513);
244+
group.bench_function("glass_pumpkin", |b| {
245+
b.iter(|| safe_gprime::from_rng(256, &mut rng_gp))
246+
});
237247
group.finish();
238248
}
239249

0 commit comments

Comments
 (0)