Skip to content

Commit

Permalink
adds full benches
Browse files Browse the repository at this point in the history
  • Loading branch information
supinie committed Jun 4, 2024
1 parent e5dea88 commit fdef438
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ criterion = "0.3" # 0.5.1 latest

[[bench]]
name = "my_benchmark"
harness = false
harness = false
82 changes: 76 additions & 6 deletions benches/my_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,79 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use enc_rust::fibonacci;
use criterion::{criterion_group, criterion_main, Criterion};
use enc_rust::kem::*;

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
pub fn gen_key_benchmark_512(c: &mut Criterion) {
c.bench_function("key_gen_bench_512", |b| {
b.iter(|| generate_key_pair(None, 2))
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
pub fn gen_key_benchmark_768(c: &mut Criterion) {
c.bench_function("key_gen_bench_768", |b| {
b.iter(|| generate_key_pair(None, 3))
});
}

pub fn gen_key_benchmark_1024(c: &mut Criterion) {
c.bench_function("key_gen_bench_1024", |b| {
b.iter(|| generate_key_pair(None, 4))
});
}

pub fn encap_benchmark_512(c: &mut Criterion) {
let (pk, _) = generate_key_pair(None, 2).unwrap();
c.bench_function("encap_benchmark_512", |b| {
b.iter(|| pk.encapsulate(None, None))
});
}

pub fn encap_benchmark_768(c: &mut Criterion) {
let (pk, _) = generate_key_pair(None, 3).unwrap();
c.bench_function("encap_benchmark_768", |b| {
b.iter(|| pk.encapsulate(None, None))
});
}

pub fn encap_benchmark_1024(c: &mut Criterion) {
let (pk, _) = generate_key_pair(None, 4).unwrap();
c.bench_function("encap_benchmark_1024", |b| {
b.iter(|| pk.encapsulate(None, None))
});
}

pub fn decap_benchmark_512(c: &mut Criterion) {
let (pk, sk) = generate_key_pair(None, 2).unwrap();
let (ciphertext_obj, _) = pk.encapsulate(None, None).unwrap();
c.bench_function("decap_benchmark_512", |b| {
b.iter(|| sk.decapsulate(ciphertext_obj.as_bytes()))
});
}

pub fn decap_benchmark_768(c: &mut Criterion) {
let (pk, sk) = generate_key_pair(None, 3).unwrap();
let (ciphertext_obj, _) = pk.encapsulate(None, None).unwrap();
c.bench_function("decap_benchmark_768", |b| {
b.iter(|| sk.decapsulate(ciphertext_obj.as_bytes()))
});
}

pub fn decap_benchmark_1024(c: &mut Criterion) {
let (pk, sk) = generate_key_pair(None, 4).unwrap();
let (ciphertext_obj, _) = pk.encapsulate(None, None).unwrap();
c.bench_function("decap_benchmark_1024", |b| {
b.iter(|| sk.decapsulate(ciphertext_obj.as_bytes()))
});
}

criterion_group!(
benches,
gen_key_benchmark_512,
gen_key_benchmark_768,
gen_key_benchmark_1024,
encap_benchmark_512,
encap_benchmark_768,
encap_benchmark_1024,
decap_benchmark_512,
decap_benchmark_768,
decap_benchmark_1024
);
criterion_main!(benches);
9 changes: 0 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,3 @@ mod tests {
mod sample;
mod vectors;
}

#[inline]
pub fn fibonacci(n: u64) -> u64 {
match n {
0 => 1,
1 => 1,
n => fibonacci(n-1) + fibonacci(n-2),
}
}
2 changes: 1 addition & 1 deletion src/polynomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod sample;
use crate::{
errors::{CrystalsError, PackingError},
field_operations::{barrett_reduce, conditional_sub_q, mont_form, montgomery_reduce},
params::{SecurityLevel, N, POLYBYTES, Q, Q_DIV, Q_I16, Q_U16, Q_I32, Q_U32, SYMBYTES},
params::{SecurityLevel, N, POLYBYTES, Q, Q_DIV, Q_I16, Q_I32, Q_U16, Q_U32, SYMBYTES},
polynomials::ntt::ZETAS,
};
use core::num::TryFromIntError;
Expand Down

0 comments on commit fdef438

Please sign in to comment.