Skip to content

Commit

Permalink
Add performance tests against glass-pumpkin
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Oct 4, 2024
1 parent 0d91624 commit c529dd2
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ openssl = { version = "0.10.39", optional = true, features = ["vendored"] }
rug = { version = "1.26", default-features = false, features = [
"integer",
], optional = true }
glass_pumpkin = { version = "1", optional = true }

[dev-dependencies]
# need `crypto-bigint` with `alloc` to test `BoxedUint`
Expand All @@ -37,6 +38,7 @@ default = ["default-rng"]
default-rng = ["rand_core/getrandom"]
tests-openssl = ["openssl"]
tests-gmp = ["rug/std"]
tests-glass-pumpkin = ["glass_pumpkin"]
tests-exhaustive = []
tests-all = ["tests-openssl", "tests-gmp", "tests-exhaustive"]

Expand Down
113 changes: 113 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,118 @@ fn bench_openssl(c: &mut Criterion) {
#[cfg(not(feature = "tests-openssl"))]
fn bench_openssl(_c: &mut Criterion) {}

#[cfg(feature = "tests-glass-pumpkin")]
fn bench_glass_pumpkin(c: &mut Criterion) {
use crypto_bigint::Limb;
use crypto_primes::hazmat::{lucas_test, AStarBase, LucasCheck, MillerRabin, Primality};

fn required_checks(bits: u32) -> usize {
((bits as f64).log2() as usize) + 5
}

fn reference_from_rng(bit_length: u32, rng: &mut impl CryptoRngCore) -> BoxedUint {
loop {
let start = random_odd_integer::<BoxedUint>(
rng,
NonZeroU32::new(bit_length).unwrap(),
bit_length,
);
let sieve = Sieve::new(start.as_ref(), NonZeroU32::new(bit_length).unwrap(), false);
for num in sieve {
let odd_num = &Odd::new(num.clone()).unwrap();

let mr = MillerRabin::new(odd_num);
if (0..required_checks(bit_length))
.any(|_| !mr.test_random_base(rng).is_probably_prime())
{
continue;
}

match lucas_test(odd_num, AStarBase, LucasCheck::Strong) {
Primality::Composite => continue,
Primality::Prime => return num,
_ => {}
}

return num;
}
}
}

fn reference_safe_from_rng(bit_length: u32, rng: &mut impl CryptoRngCore) -> BoxedUint {
loop {
let start = random_odd_integer::<BoxedUint>(
rng,
NonZeroU32::new(bit_length).unwrap(),
bit_length,
);
let sieve = Sieve::new(start.as_ref(), NonZeroU32::new(bit_length).unwrap(), true);
for num in sieve {
let odd_num = &Odd::new(num.clone()).unwrap();

let limbs: &[Limb] = num.as_ref();
if limbs[0].0 & 3 != 3 {
continue;
}

let half = num.wrapping_shr_vartime(1);
let odd_half = &Odd::new(half.clone()).unwrap();

let checks = required_checks(bit_length) - 5;

let mr = MillerRabin::new(odd_num);
if (0..checks).any(|_| !mr.test_random_base(rng).is_probably_prime()) {
continue;
}

let mr = MillerRabin::new(odd_half);
if (0..checks).any(|_| !mr.test_random_base(rng).is_probably_prime()) {
continue;
}

if lucas_test(odd_num, AStarBase, LucasCheck::Strong) == Primality::Composite {
continue;
}

match lucas_test(odd_half, AStarBase, LucasCheck::Strong) {
Primality::Composite => continue,
Primality::Prime => return num,
_ => {}
}

return num;
}
}
}

let mut group = c.benchmark_group("glass-pumpkin");

let mut rng = make_rng();
group.bench_function("(U1024) Random prime (crypto-primes reference)", |b| {
b.iter(|| reference_from_rng(1024, &mut rng))
});

let mut rng = make_rng();
group.bench_function("(U1024) Random prime", |b| {
b.iter(|| glass_pumpkin::prime::from_rng(1024, &mut rng))
});

group.sample_size(20);

let mut rng = make_rng();
group.bench_function("(U1024) Random safe prime (crypto-primes reference)", |b| {
b.iter(|| reference_safe_from_rng(1024, &mut rng))
});

let mut rng = make_rng();
group.bench_function("(U1024) Random safe prime", |b| {
b.iter(|| glass_pumpkin::safe_prime::from_rng(1024, &mut rng))
});
}

#[cfg(not(feature = "tests-glass-pumpkin"))]
fn bench_glass_pumpkin(_c: &mut Criterion) {}

criterion_group!(
benches,
bench_sieve,
Expand All @@ -382,5 +494,6 @@ criterion_group!(
bench_presets,
bench_gmp,
bench_openssl,
bench_glass_pumpkin,
);
criterion_main!(benches);

0 comments on commit c529dd2

Please sign in to comment.