From a60e1d58939fe7c39c3652d5b801830381d7ab93 Mon Sep 17 00:00:00 2001 From: TrAyZeN <1810leo@gmail.com> Date: Mon, 18 Mar 2024 17:22:45 +0100 Subject: [PATCH] Remove unneeded casts --- src/cpa.rs | 84 ++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/src/cpa.rs b/src/cpa.rs index 2f8d6a5..2b2ec5f 100644 --- a/src/cpa.rs +++ b/src/cpa.rs @@ -9,9 +9,9 @@ pub struct Cpa { sig_keys: Array1, values: Array1, a_l: Array2, - target_byte: i32, + target_byte: usize, len_leakages: usize, - guess_range: i32, + guess_range: usize, corr: Array2, max_corr: Array2, rank_slice: Array2, @@ -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, } @@ -53,27 +53,25 @@ impl Cpa { self.go(trace, plaintext.clone(), self.guess_range); } - pub fn gen_values(&mut self, metadata: Array1, _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, 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, metadata: Array1, _guess_range: i32) { + pub fn go(&mut self, trace: Array1, metadata: Array1, 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]; } } @@ -81,26 +79,24 @@ impl Cpa { /* 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::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 = (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); @@ -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 = Array2::zeros((self._guess_range as usize, 1)); + // let mut max_256: Array2 = 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() @@ -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]; } @@ -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; } } @@ -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,