Skip to content

Commit

Permalink
Split cpa processor and result
Browse files Browse the repository at this point in the history
  • Loading branch information
TrAyZeN authored and kingofpayne committed May 3, 2024
1 parent 2aadb8f commit 5235c4c
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 133 deletions.
15 changes: 6 additions & 9 deletions benches/cpa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use muscat::cpa::{self, Cpa};
use muscat::cpa::{self, Cpa, CpaProcessor};
use muscat::cpa_normal;
use muscat::leakage::{hw, sbox};
use ndarray::{Array2, ArrayView1, Axis};
Expand All @@ -13,7 +13,7 @@ pub fn leakage_model(value: usize, guess: usize) -> usize {
}

fn cpa_sequential(leakages: &Array2<f64>, plaintexts: &Array2<u8>) -> Cpa {
let mut cpa = Cpa::new(leakages.shape()[1], 256, 0, leakage_model);
let mut cpa = CpaProcessor::new(leakages.shape()[1], 256, 0, leakage_model);

for i in 0..leakages.shape()[0] {
cpa.update(
Expand All @@ -22,9 +22,7 @@ fn cpa_sequential(leakages: &Array2<f64>, plaintexts: &Array2<u8>) -> Cpa {
);
}

cpa.finalize();

cpa
cpa.finalize()
}

pub fn leakage_model_normal(value: ArrayView1<usize>, guess: usize) -> usize {
Expand All @@ -34,7 +32,8 @@ pub fn leakage_model_normal(value: ArrayView1<usize>, guess: usize) -> usize {
fn cpa_normal_sequential(leakages: &Array2<f64>, plaintexts: &Array2<u8>) -> cpa_normal::Cpa {
let chunk_size = 500;

let mut cpa = cpa_normal::Cpa::new(leakages.shape()[1], chunk_size, 256, leakage_model_normal);
let mut cpa =
cpa_normal::CpaProcessor::new(leakages.shape()[1], chunk_size, 256, leakage_model_normal);

for (leakages_chunk, plaintexts_chunk) in zip(
leakages.axis_chunks_iter(Axis(0), chunk_size),
Expand All @@ -43,9 +42,7 @@ fn cpa_normal_sequential(leakages: &Array2<f64>, plaintexts: &Array2<u8>) -> cpa
cpa.update(leakages_chunk.map(|&x| x as f32).view(), plaintexts_chunk);
}

cpa.finalize();

cpa
cpa.finalize()
}

fn bench_cpa(c: &mut Criterion) {
Expand Down
18 changes: 9 additions & 9 deletions examples/cpa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use indicatif::ProgressIterator;
use muscat::cpa_normal::*;
use muscat::cpa_normal::CpaProcessor;
use muscat::leakage::{hw, sbox};
use muscat::util::{progress_bar, read_array2_from_npy_file, save_array2};
use ndarray::*;
Expand Down Expand Up @@ -30,11 +30,11 @@ fn cpa() -> Result<()> {
let plaintext = read_array2_from_npy_file::<FormatMetadata>(&dir_p)?;
let len_traces = leakages.shape()[0];

let mut cpa_parallel = ((0..len_traces).step_by(batch))
let cpa_parallel = ((0..len_traces).step_by(batch))
.progress_with(progress_bar(len_traces))
.par_bridge()
.map(|row_number| {
let mut cpa = Cpa::new(size, batch, guess_range, leakage_model);
let mut cpa = CpaProcessor::new(size, batch, guess_range, leakage_model);
let range_rows = row_number..row_number + batch;
let range_samples = start_sample..end_sample;
let sample_traces = leakages
Expand All @@ -45,14 +45,14 @@ fn cpa() -> Result<()> {
cpa
})
.reduce(
|| Cpa::new(size, batch, guess_range, leakage_model),
|| CpaProcessor::new(size, batch, guess_range, leakage_model),
|x, y| x + y,
);

cpa_parallel.finalize();
println!("Guessed key = {}", cpa_parallel.pass_guess());
let cpa = cpa_parallel.finalize();
println!("Guessed key = {}", cpa.pass_guess());

save_array2("results/corr.npy", cpa_parallel.pass_corr_array().view())?;
save_array2("results/corr.npy", cpa.pass_corr_array().view())?;

Ok(())
}
Expand All @@ -68,7 +68,7 @@ fn success() -> Result<()> {
let nfiles = 13; // Number of files in the directory. TBD: Automating this value
let rank_traces = 1000;

let mut cpa = Cpa::new(size, batch, guess_range, leakage_model);
let mut cpa = CpaProcessor::new(size, batch, guess_range, leakage_model);

cpa.success_traces(rank_traces);
for i in (0..nfiles).progress() {
Expand All @@ -89,7 +89,7 @@ fn success() -> Result<()> {
}
}

cpa.finalize();
let cpa = cpa.finalize();
println!("Guessed key = {}", cpa.pass_guess());

// save corr key curves in npy
Expand Down
16 changes: 8 additions & 8 deletions examples/cpa_partioned.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use indicatif::ProgressIterator;
use muscat::cpa::*;
use muscat::cpa::CpaProcessor;
use muscat::leakage::{hw, sbox};
use muscat::util::{progress_bar, read_array2_from_npy_file, save_array};
use rayon::prelude::{ParallelBridge, ParallelIterator};
Expand All @@ -23,7 +23,7 @@ fn cpa() -> Result<()> {
let nfiles = 5; // Number of files in the directory. TBD: Automating this value

/* Parallel operation using multi-threading on batches */
let mut cpa = (0..nfiles)
let cpa = (0..nfiles)
.progress_with(progress_bar(nfiles))
.map(|n| {
let dir_l = format!("{folder}/l{n}.npy");
Expand All @@ -34,7 +34,7 @@ fn cpa() -> Result<()> {
})
.par_bridge()
.map(|batch| {
let mut c = Cpa::new(size, guess_range, target_byte, leakage_model);
let mut c = CpaProcessor::new(size, guess_range, target_byte, leakage_model);
let len_leakage = batch.0.shape()[0];
for i in 0..len_leakage {
c.update(
Expand All @@ -45,15 +45,15 @@ fn cpa() -> Result<()> {
c
})
.reduce(
|| Cpa::new(size, guess_range, target_byte, leakage_model),
|a: Cpa, b| a + b,
|| CpaProcessor::new(size, guess_range, target_byte, leakage_model),
|a: CpaProcessor, b| a + b,
);

cpa.finalize();
println!("Guessed key = {}", cpa.pass_guess());
let cpa_result = cpa.finalize();
println!("Guessed key = {}", cpa_result.pass_guess());

// save corr key curves in npy
save_array("../results/corr.npy", &cpa.pass_corr_array())?;
save_array("../results/corr.npy", &cpa_result.pass_corr_array())?;

Ok(())
}
Expand Down
15 changes: 8 additions & 7 deletions examples/rank.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use indicatif::ProgressIterator;
use muscat::cpa::*;
use muscat::cpa::CpaProcessor;
use muscat::leakage::{hw, sbox};
use muscat::util::{progress_bar, read_array2_from_npy_file, save_array};
use ndarray::*;
use ndarray::s;
use rayon::prelude::{ParallelBridge, ParallelIterator};

// traces format
Expand All @@ -22,7 +22,7 @@ fn rank() -> Result<()> {
let folder = String::from("../../data");
let nfiles = 5;
let chunk = 3000;
let mut rank = Cpa::new(size, guess_range, target_byte, leakage_model);
let mut rank = CpaProcessor::new(size, guess_range, target_byte, leakage_model);
for file in (0..nfiles).progress_with(progress_bar(nfiles)) {
let dir_l = format!("{folder}/l{file}.npy");
let dir_p = format!("{folder}/p{file}.npy");
Expand All @@ -38,8 +38,8 @@ fn rank() -> Result<()> {
let x = (0..chunk)
.par_bridge()
.fold(
|| Cpa::new(size, guess_range, target_byte, leakage_model),
|mut r: Cpa, n| {
|| CpaProcessor::new(size, guess_range, target_byte, leakage_model),
|mut r: CpaProcessor, n| {
r.update(
l_sample.row(n).map(|l| *l as usize).view(),
p_sample.row(n).map(|p| *p as usize).view(),
Expand All @@ -48,14 +48,15 @@ fn rank() -> Result<()> {
},
)
.reduce(
|| Cpa::new(size, guess_range, target_byte, leakage_model),
|| CpaProcessor::new(size, guess_range, target_byte, leakage_model),
|lhs, rhs| lhs + rhs,
);
rank = rank + x;
rank.finalize();
}
}

let rank = rank.finalize();

// save rank key curves in npy
save_array("../results/rank.npy", &rank.pass_rank())?;

Expand Down
Loading

0 comments on commit 5235c4c

Please sign in to comment.