Skip to content

Commit

Permalink
Remove leakage model from processors
Browse files Browse the repository at this point in the history
  • Loading branch information
TrAyZeN committed Dec 9, 2024
1 parent 504c11c commit b750437
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 331 deletions.
14 changes: 9 additions & 5 deletions benches/cpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ pub fn leakage_model(value: usize, guess: usize) -> usize {
}

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

for i in 0..traces.shape()[0] {
cpa.update(
traces.row(i).map(|&x| x as usize).view(),
plaintexts.row(i).map(|&y| y as usize).view(),
leakage_model,
);
}

cpa.finalize()
cpa.finalize(leakage_model)
}

pub fn leakage_model_normal(value: ArrayView1<usize>, guess: usize) -> usize {
Expand All @@ -32,14 +33,17 @@ pub fn leakage_model_normal(value: ArrayView1<usize>, guess: usize) -> usize {
fn cpa_normal_sequential(traces: &Array2<f64>, plaintexts: &Array2<u8>) -> Cpa {
let batch_size = 500;

let mut cpa =
cpa_normal::CpaProcessor::new(traces.shape()[1], batch_size, 256, leakage_model_normal);
let mut cpa = cpa_normal::CpaProcessor::new(traces.shape()[1], batch_size, 256);

for (trace_batch, plaintext_batch) in zip(
traces.axis_chunks_iter(Axis(0), batch_size),
plaintexts.axis_chunks_iter(Axis(0), batch_size),
) {
cpa.update(trace_batch.map(|&x| x as f32).view(), plaintext_batch);
cpa.update(
trace_batch.map(|&x| x as f32).view(),
plaintext_batch,
leakage_model_normal,
);
}

cpa.finalize()
Expand Down
4 changes: 2 additions & 2 deletions benches/dpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ fn selection_function(metadata: ArrayView1<u8>, guess: usize) -> bool {
}

fn dpa_sequential(traces: &Array2<f32>, plaintexts: &Array2<u8>) -> Dpa {
let mut dpa = DpaProcessor::new(traces.shape()[1], 256, selection_function);
let mut dpa = DpaProcessor::new(traces.shape()[1], 256);

for i in 0..traces.shape()[0] {
dpa.update(traces.row(i), plaintexts.row(i));
dpa.update(traces.row(i), plaintexts.row(i), selection_function);
}

dpa.finalize()
Expand Down
13 changes: 5 additions & 8 deletions examples/cpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ fn cpa() -> Result<()> {
.progress_with(progress_bar(len_traces))
.par_bridge()
.map(|row_number| {
let mut cpa = CpaProcessor::new(size, batch, guess_range, leakage_model);
let mut cpa = CpaProcessor::new(size, batch, guess_range);
let range_rows = row_number..row_number + batch;
let range_samples = start_sample..end_sample;
let sample_traces = traces
.slice(s![range_rows.clone(), range_samples])
.map(|l| *l as f32);
let sample_metadata = plaintext.slice(s![range_rows, ..]).map(|p| *p as usize);
cpa.update(sample_traces.view(), sample_metadata.view());
cpa.update(sample_traces.view(), sample_metadata.view(), leakage_model);
cpa
})
.reduce(
|| CpaProcessor::new(size, batch, guess_range, leakage_model),
|x, y| x + y,
);
.reduce(|| CpaProcessor::new(size, batch, guess_range), |x, y| x + y);

let cpa = cpa_parallel.finalize();
println!("Guessed key = {}", cpa.best_guess());
Expand All @@ -69,7 +66,7 @@ fn success() -> Result<()> {
let nfiles = 13; // Number of files in the directory. TBD: Automating this value
let rank_traces = 1000;

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

let mut rank = Array1::zeros(guess_range);
let mut processed_traces = 0;
Expand All @@ -87,7 +84,7 @@ fn success() -> Result<()> {
.map(|l| *l as f32);
let sample_metadata = plaintext.slice(s![range_rows, range_metadata]);

cpa.update(sample_traces.view(), sample_metadata);
cpa.update(sample_traces.view(), sample_metadata, leakage_model);
processed_traces += sample_traces.len();
if processed_traces % rank_traces == 0 {
// rank can be saved to get its evolution
Expand Down
7 changes: 4 additions & 3 deletions examples/cpa_partioned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ fn cpa() -> Result<()> {
})
.par_bridge()
.map(|batch| {
let mut c = CpaProcessor::new(size, guess_range, target_byte, leakage_model);
let mut c = CpaProcessor::new(size, guess_range, target_byte);
for i in 0..batch.0.shape()[0] {
c.update(
batch.0.row(i).map(|x| *x as usize).view(),
batch.1.row(i).map(|y| *y as usize).view(),
leakage_model,
);
}
c
})
.reduce(
|| CpaProcessor::new(size, guess_range, target_byte, leakage_model),
|| CpaProcessor::new(size, guess_range, target_byte),
|a, b| a + b,
);

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

// save corr key curves in npy
Expand Down
21 changes: 11 additions & 10 deletions examples/dpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ fn dpa() -> Result<()> {
let traces = read_array2_from_npy_file::<FormatTraces>(&dir_l)?;
let plaintext = read_array2_from_npy_file::<FormatMetadata>(&dir_p)?;
let len_traces = 20000; //traces.shape()[0];
let mut dpa_proc = DpaProcessor::new(size, guess_range, selection_function);
let mut dpa_proc = DpaProcessor::new(size, guess_range);
for i in (0..len_traces).progress() {
let tmp_trace = traces
.row(i)
.slice(s![start_sample..end_sample])
.mapv(|t| t as f32);
let tmp_metadata = plaintext.row(i);
dpa_proc.update(tmp_trace.view(), tmp_metadata.to_owned());
dpa_proc.update(
tmp_trace.view(),
tmp_metadata.to_owned(),
selection_function,
);
}
let dpa = dpa_proc.finalize();
println!("Guessed key = {:02x}", dpa.best_guess());
Expand All @@ -53,7 +57,7 @@ fn dpa_success() -> Result<()> {
let traces = read_array2_from_npy_file::<FormatTraces>(&dir_l)?;
let plaintext = read_array2_from_npy_file::<FormatMetadata>(&dir_p)?;
let len_traces = traces.shape()[0];
let mut dpa_proc = DpaProcessor::new(size, guess_range, selection_function);
let mut dpa_proc = DpaProcessor::new(size, guess_range);
let rank_traces: usize = 100;

let mut rank = Array1::zeros(guess_range);
Expand All @@ -63,7 +67,7 @@ fn dpa_success() -> Result<()> {
.slice(s![start_sample..end_sample])
.mapv(|t| t as f32);
let tmp_metadata = plaintext.row(i).to_owned();
dpa_proc.update(tmp_trace.view(), tmp_metadata);
dpa_proc.update(tmp_trace.view(), tmp_metadata, selection_function);

if i % rank_traces == 0 {
// rank can be saved to get its evolution
Expand Down Expand Up @@ -102,18 +106,15 @@ fn dpa_parallel() -> Result<()> {
.slice(s![range_rows..range_rows + batch, ..])
.to_owned();

let mut dpa_inner = DpaProcessor::new(size, guess_range, selection_function);
let mut dpa_inner = DpaProcessor::new(size, guess_range);
for i in 0..batch {
let trace = tmp_traces.row(i);
let metadata = tmp_metadata.row(i).to_owned();
dpa_inner.update(trace, metadata);
dpa_inner.update(trace, metadata, selection_function);
}
dpa_inner
})
.reduce(
|| DpaProcessor::new(size, guess_range, selection_function),
|x, y| x + y,
)
.reduce(|| DpaProcessor::new(size, guess_range), |x, y| x + y)
.finalize();

println!("{:2x}", dpa.best_guess());
Expand Down
9 changes: 5 additions & 4 deletions examples/rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn rank() -> Result<()> {
let folder = String::from("../../data");
let nfiles = 5;
let batch_size = 3000;
let mut rank = CpaProcessor::new(size, guess_range, target_byte, leakage_model);
let mut rank = CpaProcessor::new(size, guess_range, target_byte);
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 @@ -37,24 +37,25 @@ fn rank() -> Result<()> {
let x = (0..batch_size)
.par_bridge()
.fold(
|| CpaProcessor::new(size, guess_range, target_byte, leakage_model),
|| CpaProcessor::new(size, guess_range, target_byte),
|mut r, n| {
r.update(
l_sample.row(n).map(|l| *l as usize).view(),
p_sample.row(n).map(|p| *p as usize).view(),
leakage_model,
);
r
},
)
.reduce(
|| CpaProcessor::new(size, guess_range, target_byte, leakage_model),
|| CpaProcessor::new(size, guess_range, target_byte),
|lhs, rhs| lhs + rhs,
);
rank = rank + x;
}
}

let rank = rank.finalize();
let rank = rank.finalize(leakage_model);

// save rank key curves in npy
save_array("../results/rank.npy", &rank.rank().map(|&x| x as u64))?;
Expand Down
Loading

0 comments on commit b750437

Please sign in to comment.