From b09a06e4ff5bdbbe897903092155b19093015260 Mon Sep 17 00:00:00 2001 From: TrAyZeN Date: Wed, 9 Oct 2024 15:58:43 +0200 Subject: [PATCH] Add ttest benchmark --- Cargo.toml | 4 ++++ benches/ttest.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 benches/ttest.rs diff --git a/Cargo.toml b/Cargo.toml index 4debf7d..61b8de3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,3 +46,7 @@ harness = false [[bench]] name = "dpa" harness = false + +[[bench]] +name = "ttest" +harness = false diff --git a/benches/ttest.rs b/benches/ttest.rs new file mode 100644 index 0000000..6b9c932 --- /dev/null +++ b/benches/ttest.rs @@ -0,0 +1,51 @@ +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use muscat::leakage_detection::{ttest, TTestProcessor}; +use ndarray::{Array1, Array2}; +use ndarray_rand::rand::{rngs::StdRng, SeedableRng}; +use ndarray_rand::rand_distr::{Standard, Uniform}; +use ndarray_rand::RandomExt; + +fn ttest_sequential(traces: &Array2, trace_classes: &Array1) -> Array1 { + let mut ttest = TTestProcessor::new(traces.shape()[1]); + + for i in 0..traces.shape()[0] { + ttest.process(traces.row(i), trace_classes[i]); + } + + ttest.ttest() +} + +fn ttest_parallel(traces: &Array2, trace_classes: &Array1) -> Array1 { + ttest(traces.view(), trace_classes.view(), 500) +} + +fn bench_ttest(c: &mut Criterion) { + // Seed rng to get the same output each run + let mut rng = StdRng::seed_from_u64(0); + + let mut group = c.benchmark_group("ttest"); + + group.measurement_time(std::time::Duration::from_secs(60)); + + for num_traces in [5000, 10000, 25000].into_iter() { + let leakages = Array2::random_using((num_traces, 5000), Uniform::new(-200, 200), &mut rng); + let plaintexts = Array1::random_using(num_traces, Standard, &mut rng); + + group.bench_with_input( + BenchmarkId::new("sequential", num_traces), + &(&leakages, &plaintexts), + |b, (leakages, plaintexts)| b.iter(|| ttest_sequential(leakages, plaintexts)), + ); + + group.bench_with_input( + BenchmarkId::new("parallel", num_traces), + &(&leakages, &plaintexts), + |b, (leakages, plaintexts)| b.iter(|| ttest_parallel(leakages, plaintexts)), + ); + } + + group.finish(); +} + +criterion_group!(benches, bench_ttest); +criterion_main!(benches);