From 8bc171fa113b78d6366f489285c2948ebbb97cc0 Mon Sep 17 00:00:00 2001 From: Jacob Zhong Date: Mon, 18 Apr 2022 11:19:25 -0400 Subject: [PATCH] Minor fixes --- bench/benches/bench.rs | 28 +++++++++++++--------------- src/primality.rs | 2 ++ src/traits.rs | 7 +++++-- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/bench/benches/bench.rs b/bench/benches/bench.rs index 095f7d9..ebf7109 100644 --- a/bench/benches/bench.rs +++ b/bench/benches/bench.rs @@ -30,6 +30,13 @@ pub fn bench_is_prime(c: &mut Criterion) { .count() }) }); + group.bench_function("number-theory", |b| { + b.iter(|| { + numbers() + .filter(|&n| NumberTheory::is_prime(&n)) + .count() + }) + }); #[cfg(feature = "num-primes")] group.bench_function("num-primes", |b| { b.iter(|| { @@ -52,15 +59,6 @@ pub fn bench_is_prime(c: &mut Criterion) { .count() }) }); - group.bench_function("number-theory", |b| { - b.iter(|| { - numbers() - .filter(|&n| NumberTheory::is_prime(&n)) - .count() - }) - }); - // TODO(v0.4.1): why is number-theory faster? difference in modular power? - // we can add a markdown of performance after figuring out this group.finish(); @@ -232,30 +230,30 @@ pub fn bench_prime_gen(c: &mut Criterion) { let mut group = c.benchmark_group("prime generation (256 bits)"); group.sample_size(10).sampling_mode(SamplingMode::Flat); - let mut gen = rand::thread_rng(); + let mut rng = rand::thread_rng(); group.bench_function("num-prime (this crate)", |b| { - b.iter(|| -> num_bigint::BigUint { gen.gen_prime(256, None) }) + b.iter(|| -> num_bigint::BigUint { rng.gen_prime(256, None) }) }); group.bench_function("num-primes", |b| { b.iter(|| Generator::new_prime(256)) }); group.bench_function("glass_pumpkin", |b| { - b.iter(|| gprime::from_rng(256, &mut gen)) + b.iter(|| gprime::from_rng(256, &mut rng)) }); group.finish(); let mut group = c.benchmark_group("safe prime generation (256 bits)"); group.sample_size(10).sampling_mode(SamplingMode::Flat); - let mut gen = rand::thread_rng(); + let mut rng = rand::thread_rng(); group.bench_function("num-prime (this crate)", |b| { - b.iter(|| -> num_bigint::BigUint { gen.gen_safe_prime(256) }) + b.iter(|| -> num_bigint::BigUint { rng.gen_safe_prime(256) }) }); group.bench_function("num-primes", |b| { b.iter(|| Generator::safe_prime(256)) }); group.bench_function("glass_pumpkin", |b| { - b.iter(|| safe_gprime::from_rng(256, &mut gen)) + b.iter(|| safe_gprime::from_rng(256, &mut rng)) }); group.finish(); } diff --git a/src/primality.rs b/src/primality.rs index 625e314..686a8f5 100644 --- a/src/primality.rs +++ b/src/primality.rs @@ -128,6 +128,7 @@ where for<'r> &'r T: RefNum + std::ops::Shr + ModularUnaryOps<&'r T, Output = T>, { + #[inline] fn is_prp(&self, base: Self) -> bool { if self < &Self::one() { return false; @@ -136,6 +137,7 @@ where base.powm(&tm1, self).is_one() } + #[inline] fn is_sprp(&self, base: Self) -> bool { self.test_sprp(base).either(|v| v, |_| false) } diff --git a/src/traits.rs b/src/traits.rs index e1f3382..e8e045a 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -80,6 +80,9 @@ impl BitOr for Primality { #[derive(Debug, Clone, Copy)] #[non_exhaustive] pub struct PrimalityTestConfig { + // TODO: add option to divides small primes in the table + // and this option should be enabled if the probabilistic test is used for strict config + /// Number of strong probable prime test, starting from base 2 pub sprp_trials: usize, @@ -98,8 +101,8 @@ impl Default for PrimalityTestConfig { /// composites with little computation fn default() -> Self { Self { - sprp_trials: 2, - sprp_random_trials: 2, + sprp_trials: 2, // test base 2 and 3 + sprp_random_trials: 3, // choose other 3 random bases slprp_test: false, eslprp_test: false, }