-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c44fb8
commit 2aadb8f
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,7 @@ harness = false | |
[[bench]] | ||
name = "snr" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "dpa" | ||
harness = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use muscat::dpa::{dpa, Dpa}; | ||
use muscat::leakage::sbox; | ||
use ndarray::{Array1, Array2}; | ||
use ndarray_rand::rand::{rngs::StdRng, SeedableRng}; | ||
use ndarray_rand::rand_distr::Uniform; | ||
use ndarray_rand::RandomExt; | ||
|
||
fn selection_function(metadata: Array1<u8>, guess: usize) -> usize { | ||
sbox(metadata[1] ^ guess as u8).into() | ||
} | ||
|
||
fn dpa_sequential(leakages: &Array2<f32>, plaintexts: &Array2<u8>) -> Dpa<Array1<u8>> { | ||
let mut dpa = Dpa::new(leakages.shape()[1], 256, selection_function); | ||
|
||
for i in 0..leakages.shape()[0] { | ||
dpa.update(leakages.row(i), plaintexts.row(i).to_owned()); | ||
} | ||
|
||
dpa.finalize(); | ||
|
||
dpa | ||
} | ||
|
||
fn dpa_parallel(leakages: &Array2<f32>, plaintexts: &Array2<u8>) -> Dpa<Array1<u8>> { | ||
dpa( | ||
leakages.view(), | ||
plaintexts | ||
.rows() | ||
.into_iter() | ||
.map(|x| x.to_owned()) | ||
.collect::<Array1<Array1<u8>>>() | ||
.view(), | ||
256, | ||
selection_function, | ||
500, | ||
) | ||
} | ||
|
||
fn bench_dpa(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("dpa"); | ||
|
||
group.measurement_time(std::time::Duration::from_secs(60)); | ||
|
||
for nb_traces in [1000, 2000, 5000].into_iter() { | ||
let leakages = Array2::random_using((nb_traces, 5000), Uniform::new(-2., 2.), &mut rng); | ||
let plaintexts = | ||
Array2::random_using((nb_traces, 16), Uniform::new_inclusive(0, 255), &mut rng); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("sequential", nb_traces), | ||
&(&leakages, &plaintexts), | ||
|b, (leakages, plaintexts)| b.iter(|| dpa_sequential(leakages, plaintexts)), | ||
); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("parallel", nb_traces), | ||
&(&leakages, &plaintexts), | ||
|b, (leakages, plaintexts)| b.iter(|| dpa_parallel(leakages, plaintexts)), | ||
); | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_dpa); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters