Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdplm committed Jan 15, 2025
1 parent 1b4acbf commit 15db0ca
Showing 1 changed file with 14 additions and 58 deletions.
72 changes: 14 additions & 58 deletions benches/cctv.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,12 @@
use std::io::BufRead;

use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use crypto_bigint::U1024;
use rand_chacha::ChaCha8Rng;

use crypto_primes::{is_prime_with_rng, sieve_and_find, SieveFactory};
use crypto_primes::is_prime_with_rng;
use rand_core::SeedableRng;

struct HardcodedSieve {
candidates: Vec<String>,
idx: usize,
}

impl HardcodedSieve {
fn new() -> Self {
Self {
candidates: std::fs::read("./benches/rsa.bench.2048.txt")
.expect("file present")
.lines()
.map(|l| l.unwrap().to_owned())
.collect(),
idx: 0,
}
}
}

impl Iterator for HardcodedSieve {
type Item = U1024;
fn next(&mut self) -> Option<Self::Item> {
self.idx += 1;
self.candidates.get(self.idx).map(|str| U1024::from_be_hex(str))
}
}

impl SieveFactory for HardcodedSieve {
type Item = U1024;
type Sieve = Self;
fn make_sieve(
&mut self,
_rng: &mut impl rand_core::CryptoRngCore,
_previous_sieve: Option<&Self::Sieve>,
) -> Option<Self::Sieve> {
Some(Self {
candidates: self.candidates.clone(),
idx: 0,
})
}
}

/// CCTV stands for Community Cryptography Test Vectors[1]. This benchmark uses the
/// "rsa.bench.2048.txt" test vector, which is a file of 708 1024-bit long candidates for prime
/// testing. The series of candidates in the test vecotr is an average representative sequence of
Expand All @@ -58,33 +17,30 @@ impl SieveFactory for HardcodedSieve {
/// [1]: https://github.com/C2SP/CCTV
fn bench_cctv(c: &mut Criterion) {
let mut group = c.benchmark_group("CCTV RSA 1024-bit candidates");
group
.sample_size(10)
.measurement_time(std::time::Duration::from_secs(10));
group.sample_size(10);
let mut rng = ChaCha8Rng::from_seed([123; 32]);
let candidates: Vec<U1024> = std::fs::read("./benches/rsa.bench.2048.txt")
.expect("file present")
.lines()
.map(|candidate_hex| U1024::from_be_hex(&candidate_hex.unwrap()))
.collect();

group.bench_function("manual", |b| {
assert!(
is_prime_with_rng(&mut rng, &candidates[353]),
"Line 354 is a prime. This is a bug."
);
assert!(
is_prime_with_rng(&mut rng, &candidates[707]),
"Line 708 is a prime. This is a bug."
);

group.bench_function("all", |b| {
b.iter(|| {
for candidate in &candidates {
if is_prime_with_rng(&mut rng, candidate) {
break;
}
black_box(is_prime_with_rng(&mut rng, candidate));
}
});
});

group.bench_function("factory API", |b| {
b.iter_batched(
HardcodedSieve::new,
|sieve| sieve_and_find(&mut rng, sieve, is_prime_with_rng),
BatchSize::SmallInput,
);
});
}

criterion_group!(benches, bench_cctv,);
Expand Down

0 comments on commit 15db0ca

Please sign in to comment.