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

Split processor and result structure #12

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
12 changes: 5 additions & 7 deletions benches/dpa.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::dpa::{dpa, Dpa};
use muscat::dpa::{dpa, Dpa, DpaProcessor};
use muscat::leakage::sbox;
use ndarray::{Array1, Array2};
use ndarray_rand::rand::{rngs::StdRng, SeedableRng};
Expand All @@ -10,19 +10,17 @@ 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);
fn dpa_sequential(leakages: &Array2<f32>, plaintexts: &Array2<u8>) -> Dpa {
let mut dpa = DpaProcessor::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
dpa.finalize()
}

fn dpa_parallel(leakages: &Array2<f32>, plaintexts: &Array2<u8>) -> Dpa<Array1<u8>> {
fn dpa_parallel(leakages: &Array2<f32>, plaintexts: &Array2<u8>) -> Dpa {
dpa(
leakages.view(),
plaintexts
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
18 changes: 11 additions & 7 deletions examples/dpa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use indicatif::ProgressIterator;
use muscat::dpa::Dpa;
use muscat::dpa::DpaProcessor;
use muscat::leakage::sbox;
use muscat::util::read_array2_from_npy_file;
use ndarray::{s, Array1, Array2};
Expand All @@ -26,7 +26,7 @@ fn dpa() -> Result<()> {
let leakages: Array2<FormatTraces> = read_array2_from_npy_file::<FormatTraces>(&dir_l)?;
let plaintext: Array2<FormatMetadata> = read_array2_from_npy_file::<FormatMetadata>(&dir_p)?;
let len_traces = 20000; //leakages.shape()[0];
let mut dpa = Dpa::new(size, guess_range, leakage_model);
let mut dpa = DpaProcessor::new(size, guess_range, leakage_model);
for i in (0..len_traces).progress() {
let tmp_trace = leakages
.row(i)
Expand All @@ -35,7 +35,7 @@ fn dpa() -> Result<()> {
let tmp_metadata = plaintext.row(i);
dpa.update(tmp_trace.view(), tmp_metadata.to_owned());
}
dpa.finalize();
let dpa = dpa.finalize();
println!("Guessed key = {:02x}", dpa.pass_guess());
// let corr = dpa.pass_corr_array();

Expand All @@ -54,7 +54,7 @@ fn dpa_success() -> Result<()> {
let leakages: Array2<FormatTraces> = read_array2_from_npy_file::<FormatTraces>(&dir_l)?;
let plaintext: Array2<FormatMetadata> = read_array2_from_npy_file::<FormatMetadata>(&dir_p)?;
let len_traces = leakages.shape()[0];
let mut dpa = Dpa::new(size, guess_range, leakage_model);
let mut dpa = DpaProcessor::new(size, guess_range, leakage_model);
let rank_traces: usize = 100;
dpa.assign_rank_traces(rank_traces);

Expand All @@ -67,6 +67,7 @@ fn dpa_success() -> Result<()> {
dpa.update_success(tmp_trace.view(), tmp_metadata);
}

let dpa = dpa.finalize();
println!("Guessed key = {:02x}", dpa.pass_guess());
// let succss = dpa.pass_rank().to_owned();

Expand Down Expand Up @@ -96,17 +97,20 @@ fn dpa_parallel() -> Result<()> {
let tmp_metadata = plaintext
.slice(s![range_rows..range_rows + batch, ..])
.to_owned();
let mut dpa_inner = Dpa::new(size, guess_range, leakage_model);
let mut dpa_inner = DpaProcessor::new(size, guess_range, leakage_model);
for i in 0..batch {
let trace = tmp_leakages.row(i);
let metadata = tmp_metadata.row(i).to_owned();
dpa_inner.update(trace, metadata);
}
dpa_inner
})
.reduce(|| Dpa::new(size, guess_range, leakage_model), |x, y| x + y);
.reduce(
|| DpaProcessor::new(size, guess_range, leakage_model),
|x, y| x + y,
);

dpa.finalize();
let dpa = dpa.finalize();
println!("{:2x}", dpa.pass_guess());
// let corr = dpa.pass_corr_array();

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