Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark the CCTV RSA vector #66

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[[bench]]
name = "bench"
harness = false

[[bench]]
name = "cctv"
harness = false
89 changes: 89 additions & 0 deletions benches/cctv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::io::BufRead;

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

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

struct HardcodedSieve {
candidates: Vec<String>,
idx: usize,
}
impl HardcodedSieve {
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
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
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
/// testing. The series of candidates in the test vecotr is an average representative sequence of
/// candidates that can be tested across different implementations. There are two primes in the
/// file, the first at line 354 and the other on the last line. Unless there's a bug, the second
/// half of the vector is not traversed in this benchmark.
///
/// [1]: https://github.com/C2SP/CCTV
fn bench_cctv(c: &mut Criterion) {
let mut group = c.benchmark_group("CCTV RSA 1024-bit candidates");
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| {
fjarri marked this conversation as resolved.
Show resolved Hide resolved
b.iter_batched(
|| candidates.clone(),
|candidates| {
for candidate in candidates {
if is_prime_with_rng(&mut rng, &candidate) {
break;
}
}
},
BatchSize::SmallInput,
);
});

group.bench_function("factory API", |b| {
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
b.iter_batched(
HardcodedSieve::new,
|sieve| sieve_and_find(&mut rng, sieve, is_prime_with_rng),
BatchSize::SmallInput,
);
});
}

criterion_group!(benches, bench_cctv,);
criterion_main!(benches);
Loading
Loading