Skip to content

Commit

Permalink
Remove unneeded casts
Browse files Browse the repository at this point in the history
  • Loading branch information
TrAyZeN committed Mar 27, 2024
1 parent a1a2ae3 commit a60e1d5
Showing 1 changed file with 41 additions and 43 deletions.
84 changes: 41 additions & 43 deletions src/cpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub struct Cpa {
sig_keys: Array1<usize>,
values: Array1<usize>,
a_l: Array2<usize>,
target_byte: i32,
target_byte: usize,
len_leakages: usize,
guess_range: i32,
guess_range: usize,
corr: Array2<f32>,
max_corr: Array2<f32>,
rank_slice: Array2<f32>,
Expand All @@ -23,23 +23,23 @@ pub struct Cpa {
impl Cpa {
pub fn new(
size: usize,
guess_range: i32,
target_byte: i32,
guess_range: usize,
target_byte: usize,
f: fn(usize, usize) -> usize,
) -> Self {
Self {
len_samples: size,
a_l: Array2::zeros((guess_range as usize, size)),
target_byte: target_byte,
guess_range: guess_range,
a_l: Array2::zeros((guess_range, size)),
target_byte,
guess_range,
sum_leakages: Array1::zeros(size),
sig_leakages: Array1::zeros(size),
sum_keys: Array1::zeros(guess_range as usize),
sig_keys: Array1::zeros(guess_range as usize),
values: Array1::zeros(guess_range as usize),
corr: Array2::zeros((guess_range as usize, size)),
max_corr: Array2::zeros((guess_range as usize, 1)),
rank_slice: Array2::zeros((guess_range as usize, 1)),
sum_keys: Array1::zeros(guess_range),
sig_keys: Array1::zeros(guess_range),
values: Array1::zeros(guess_range),
corr: Array2::zeros((guess_range, size)),
max_corr: Array2::zeros((guess_range, 1)),
rank_slice: Array2::zeros((guess_range, 1)),
leakage_func: f,
len_leakages: 0,
}
Expand All @@ -53,54 +53,50 @@ impl Cpa {
self.go(trace, plaintext.clone(), self.guess_range);
}

pub fn gen_values(&mut self, metadata: Array1<usize>, _guess_range: i32, _target_key: i32) {
for guess in 0.._guess_range {
self.values[guess as usize] =
(self.leakage_func)(metadata[_target_key as usize], guess as usize) as usize;
pub fn gen_values(&mut self, metadata: Array1<usize>, guess_range: usize, target_key: usize) {
for guess in 0..guess_range {
self.values[guess] = (self.leakage_func)(metadata[target_key], guess);
}
}

pub fn go(&mut self, _trace: Array1<usize>, metadata: Array1<usize>, _guess_range: i32) {
pub fn go(&mut self, trace: Array1<usize>, metadata: Array1<usize>, guess_range: usize) {
for i in 0..self.len_samples {
self.sum_leakages[i] += _trace[i] as usize;
self.sig_leakages[i] += (_trace[i] * _trace[i]) as usize;
self.sum_leakages[i] += trace[i];
self.sig_leakages[i] += trace[i] * trace[i];
}

for guess in 0.._guess_range {
self.sum_keys[guess as usize] += self.values[guess as usize] as usize;
self.sig_keys[guess as usize] +=
(self.values[guess as usize] * self.values[guess as usize]) as usize;
for guess in 0..guess_range {
self.sum_keys[guess] += self.values[guess];
self.sig_keys[guess] += self.values[guess] * self.values[guess];
}
let partition: usize = metadata[self.target_byte as usize] as usize;
let partition: usize = metadata[self.target_byte];
for i in 0..self.len_samples {
self.a_l[[partition, i]] += _trace[i] as usize;
self.a_l[[partition, i]] += trace[i];
}
}

pub fn finalize(&mut self) {
/* This function finalizes the calculation after feeding the
overall traces */

let shape_p = self.guess_range as usize;
let shape_p = self.guess_range;
let mut p: ndarray::ArrayBase<ndarray::OwnedRepr<usize>, ndarray::Dim<[usize; 2]>> =
Array2::zeros((shape_p, shape_p));
for i in 0..self.guess_range {
for x in 0..self.guess_range {
p[[x as usize, i as usize]] = (self.leakage_func)(x as usize, i as usize) as usize;
p[[x, i]] = (self.leakage_func)(x, i);
}
}
for i in 0..self.guess_range {
let _sigkeys = self.sig_keys[i as usize] as f32 / self.len_leakages as f32;
let _sumkeys = self.sum_keys[i as usize] as f32 / self.len_leakages as f32;
let _sigkeys = self.sig_keys[i] as f32 / self.len_leakages as f32;
let _sumkeys = self.sum_keys[i] as f32 / self.len_leakages as f32;
let lower1: f32 = _sigkeys - (_sumkeys * _sumkeys);
/* Parallel operation using multi-threading */
let tmp: Vec<f32> = (0..self.len_samples)
.into_par_iter()
.map(|x| {
let _sumleakages =
self.sum_leakages[x as usize] as f32 / self.len_leakages as f32;
let _sigleakages =
self.sig_leakages[x as usize] as f32 / self.len_leakages as f32;
let _sumleakages = self.sum_leakages[x] as f32 / self.len_leakages as f32;
let _sigleakages = self.sig_leakages[x] as f32 / self.len_leakages as f32;
let slice_a = self.a_l.slice(s![.., x]);
let slice_b = p.slice(s![.., i]);
let summult: i32 = self.sum_mult(slice_a, slice_b);
Expand All @@ -113,16 +109,16 @@ impl Cpa {
.collect();

for z in 0..self.len_samples {
self.corr[[i as usize, z]] = tmp[z];
self.corr[[i, z]] = tmp[z];
}
}
self.calculation();
}

pub fn calculation(&mut self) {
// let mut max_256: Array2<f32> = Array2::zeros((self._guess_range as usize, 1));
// let mut max_256: Array2<f32> = Array2::zeros((self.guess_range as usize, 1));
for i in 0..self.guess_range {
let row = self.corr.row(i as usize);
let row = self.corr.row(i);
// Calculating the max value in the row
let max_value = row
.into_iter()
Expand All @@ -134,7 +130,7 @@ impl Cpa {
tmp
})
.unwrap();
self.max_corr[[i as usize, 0]] = *max_value;
self.max_corr[[i, 0]] = *max_value;
}
self.rank_slice = concatenate![Axis(1), self.rank_slice, self.max_corr];
}
Expand All @@ -147,12 +143,13 @@ impl Cpa {
self.corr.clone()
}

pub fn pass_guess(&self) -> i32 {
let mut init_value: f32 = 0.0;
let mut guess: i32 = 0;
pub fn pass_guess(&self) -> usize {
let mut init_value = 0.0;
let mut guess = 0;

for i in 0..self.guess_range {
if self.max_corr[[i as usize, 0]] > init_value {
init_value = self.max_corr[[i as usize, 0]];
if self.max_corr[[i, 0]] > init_value {
init_value = self.max_corr[[i, 0]];
guess = i;
}
}
Expand All @@ -167,6 +164,7 @@ impl Cpa {

impl Add for Cpa {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self {
sum_leakages: self.sum_leakages + rhs.sum_leakages,
Expand Down

0 comments on commit a60e1d5

Please sign in to comment.