Skip to content

Commit

Permalink
cpa_normal
Browse files Browse the repository at this point in the history
  • Loading branch information
kab-ph committed Mar 18, 2024
1 parent 28cba3e commit 5836426
Show file tree
Hide file tree
Showing 4 changed files with 356 additions and 44 deletions.
128 changes: 85 additions & 43 deletions examples/cpa.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,105 @@
// use simple_bar::ProgressBar;
use cpa::cpa_normal::*;
use cpa::leakage::{hw, sbox};
use cpa::tools::{progress_bar, read_array_2_from_npy_file, write_array};
use indicatif::ProgressIterator;
use muscat::cpa::*;
use muscat::leakage::{hw, sbox};
use muscat::util::{progress_bar, read_array_2_from_npy_file, save_array};
use ndarray::*;
use rayon::prelude::{ParallelBridge, ParallelIterator};
use std::time::Instant;
use rayon::iter::{ParallelBridge, ParallelIterator};
use std::time::{self};

// traces format
type FormatTraces = i16;
type FormatMetadata = i32;

// leakage model
pub fn leakage_model(value: usize, guess: usize) -> usize {
hw(sbox((value ^ guess) as u8) as usize)
pub fn leakage_model(value: ArrayView1<usize>, guess: usize) -> usize {
hw(sbox((value[1] ^ guess) as u8) as usize)
}

// multi-threading cpa

// traces format
type FormatTraces = f64;
type FormatMetadata = u8;

#[allow(dead_code)]
fn cpa() {
let size: usize = 5000; // Number of samples
let start_sample: usize = 0;
let end_sample: usize = 5000;
let size: usize = end_sample - start_sample; // Number of samples
let patch: usize = 500;
let guess_range = 256; // 2**(key length)
let target_byte = 1;
let folder = String::from("../../data"); // Directory of leakages and metadata
let nfiles = 5; // Number of files in the directory. TBD: Automating this value

/* Parallel operation using multi-threading on patches */
let mut cpa = (0..nfiles)
.into_iter()
.progress_with(progress_bar(nfiles))
.map(|n| {
let dir_l = format!("{folder}/l{n}.npy");
let dir_p = format!("{folder}/p{n}.npy");
let leakages: Array2<FormatTraces> = read_array_2_from_npy_file(&dir_l);
let plaintext: Array2<FormatMetadata> = read_array_2_from_npy_file(&dir_p);
(leakages, plaintext)
})
.into_iter()
let folder = String::from("../data/cw");
let dir_l = format!("{folder}/leakages.npy");
let dir_p = format!("{folder}/plaintexts.npy");
let leakages: Array2<FormatTraces> = read_array_2_from_npy_file::<FormatTraces>(&dir_l);
let plaintext: Array2<FormatMetadata> = read_array_2_from_npy_file::<FormatMetadata>(&dir_p);
let len_traces = leakages.shape()[0];
let mut cpa_parallel = ((0..len_traces).step_by(patch)).progress_with(progress_bar(len_traces))
.map(|row| row)
.par_bridge()
.map(|patch| {
let mut c: Cpa = Cpa::new(size, guess_range, target_byte, leakage_model);
let len_leakage = patch.0.shape()[0];
for i in 0..len_leakage {
c.update(
patch.0.row(i).map(|x| *x as usize),
patch.1.row(i).map(|y| *y as usize),
);
}
c
.map(|row_number| {
let mut cpa = Cpa::new(size, patch, guess_range, leakage_model);
let range_rows = row_number..row_number + patch;
let range_samples = start_sample..end_sample;
let sample_traces = leakages
.slice(s![range_rows.clone(), range_samples])
.map(|l| *l as f32);
let sample_metadata: ArrayBase<OwnedRepr<usize>, Dim<[usize; 2]>> =
plaintext.slice(s![range_rows, ..]).map(|p| *p as usize);
cpa.update(sample_traces, sample_metadata);
cpa
})
.reduce(
|| Cpa::new(size, guess_range, target_byte, leakage_model),
|a: Cpa, b| a + b,
|| Cpa::new(size, patch, guess_range, leakage_model),
|x, y| x + y,
);
cpa_parallel.finalize();
println!("Guessed key = {}", cpa_parallel.pass_guess());
write_array("results/corr.npy", cpa_parallel.pass_corr_array().view())
}



#[allow(dead_code)]
fn success() {
let start_sample: usize = 0;
let end_sample: usize = 5000;
let size: usize = end_sample - start_sample; // Number of samples
let patch: usize = 500;
let guess_range = 256; // 2**(key length)
let folder = String::from("../data/log_584012"); // "../../../intenship/scripts/log_584012"
let nfiles = 13; // Number of files in the directory. TBD: Automating this value
let rank_traces: usize = 1000;
let mut cpa = Cpa::new(size, patch, guess_range, leakage_model);
cpa.success_traces(rank_traces);
for i in (0..nfiles).progress() {
let dir_l = format!("{folder}/l/{i}.npy");
let dir_p = format!("{folder}/p/{i}.npy");
let leakages: Array2<FormatTraces> = read_array_2_from_npy_file::<FormatTraces>(&dir_l);
let plaintext: Array2<FormatMetadata> =
read_array_2_from_npy_file::<FormatMetadata>(&dir_p);
let len_leakages = leakages.shape()[0];
for row in (0..len_leakages).step_by(patch) {
let range_samples = start_sample..end_sample;
let range_rows: std::ops::Range<usize> = row..row + patch;
let range_metadat = 0..plaintext.shape()[1];
let sample_traces = leakages
.slice(s![range_rows.clone(), range_samples]).map(|l| *l as f32);
let sample_metadata: Array2<FormatMetadata>= plaintext
.slice(s![range_rows, range_metadat]).to_owned();
cpa.update_success(sample_traces, sample_metadata);
}
}
cpa.finalize();
println!("Guessed key = {}", cpa.pass_guess());
// save corr key curves in npy
save_array("../results/corr.npy", &cpa.pass_corr_array());
write_array("results/success.npy", cpa.pass_rank().view());
}

fn main() {


fn main(){
let t = time::Instant::now();
cpa();
println!("{:?}", t.elapsed());
}




63 changes: 63 additions & 0 deletions examples/cpa_partioned.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// use simple_bar::ProgressBar;
use indicatif::ProgressIterator;
use muscat::cpa::*;
use muscat::leakage::{hw, sbox};
use muscat::util::{progress_bar, read_array_2_from_npy_file, save_array};
use ndarray::*;
use rayon::prelude::{ParallelBridge, ParallelIterator};
use std::time::Instant;

// traces format
type FormatTraces = i16;
type FormatMetadata = i32;

// leakage model
pub fn leakage_model(value: usize, guess: usize) -> usize {
hw(sbox((value ^ guess) as u8) as usize)
}

// multi-threading cpa
fn cpa() {
let size: usize = 5000; // Number of samples
let guess_range = 256; // 2**(key length)
let target_byte = 1;
let folder = String::from("../../data"); // Directory of leakages and metadata
let nfiles = 5; // Number of files in the directory. TBD: Automating this value

/* Parallel operation using multi-threading on patches */
let mut cpa = (0..nfiles)
.into_iter()
.progress_with(progress_bar(nfiles))
.map(|n| {
let dir_l = format!("{folder}/l{n}.npy");
let dir_p = format!("{folder}/p{n}.npy");
let leakages: Array2<FormatTraces> = read_array_2_from_npy_file(&dir_l);
let plaintext: Array2<FormatMetadata> = read_array_2_from_npy_file(&dir_p);
(leakages, plaintext)
})
.into_iter()
.par_bridge()
.map(|patch| {
let mut c: Cpa = Cpa::new(size, guess_range, target_byte, leakage_model);
let len_leakage = patch.0.shape()[0];
for i in 0..len_leakage {
c.update(
patch.0.row(i).map(|x| *x as usize),
patch.1.row(i).map(|y| *y as usize),
);
}
c
})
.reduce(
|| Cpa::new(size, guess_range, target_byte, leakage_model),
|a: Cpa, b| a + b,
);
cpa.finalize();
println!("Guessed key = {}", cpa.pass_guess());
// save corr key curves in npy
save_array("../results/corr.npy", &cpa.pass_corr_array());
}

fn main() {
cpa();
}
Loading

0 comments on commit 5836426

Please sign in to comment.