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

Do not store max_corr and rank, unify cpa struct, check for processor compatibility #14

Merged
merged 10 commits into from
Jul 15, 2024
Prev Previous commit
Next Next commit
Remove redundant Cpa struct
TrAyZeN committed Jun 13, 2024
commit 9e14ac45a5f32e00f21b7f0a67dd1fd8b5016227
2 changes: 1 addition & 1 deletion benches/cpa.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ pub fn leakage_model_normal(value: ArrayView1<usize>, guess: usize) -> usize {
hw(sbox((value[1] ^ guess) as u8) as usize)
}

fn cpa_normal_sequential(leakages: &Array2<f64>, plaintexts: &Array2<u8>) -> cpa_normal::Cpa {
fn cpa_normal_sequential(leakages: &Array2<f64>, plaintexts: &Array2<u8>) -> Cpa {
let chunk_size = 500;

let mut cpa =
5 changes: 3 additions & 2 deletions src/cpa.rs
Original file line number Diff line number Diff line change
@@ -47,11 +47,12 @@ where
.finalize()
}

#[derive(Debug)]
pub struct Cpa {
/// Guess range upper excluded bound
guess_range: usize,
pub(crate) guess_range: usize,
/// Pearson correlation coefficients
corr: Array2<f32>,
pub(crate) corr: Array2<f32>,
}

impl Cpa {
41 changes: 1 addition & 40 deletions src/cpa_normal.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use ndarray::{Array1, Array2, ArrayView1, ArrayView2, Axis};
use rayon::iter::{ParallelBridge, ParallelIterator};
use std::{iter::zip, ops::Add};

use crate::util::{argsort_by, max_per_row};
use crate::cpa::Cpa;

/// Computes the [`Cpa`] of the given traces using [`CpaProcessor`].
///
@@ -42,45 +42,6 @@ where
.finalize()
}

pub struct Cpa {
/// Guess range upper excluded bound
guess_range: usize,
/// Pearson correlation coefficients
corr: Array2<f32>,
}

impl Cpa {
pub fn rank(&self) -> Array1<usize> {
let rank = argsort_by(&self.max_corr().to_vec()[..], f32::total_cmp);

Array1::from_vec(rank)
}

pub fn corr(&self) -> ArrayView2<f32> {
self.corr.view()
}

pub fn best_guess(&self) -> usize {
let max_corr = self.max_corr();

let mut best_guess_corr = 0.0;
let mut best_guess = 0;

for guess in 0..self.guess_range {
if max_corr[guess] > best_guess_corr {
best_guess_corr = max_corr[guess];
best_guess = guess;
}
}

best_guess
}

pub fn max_corr(&self) -> Array1<f32> {
max_per_row(self.corr.view())
}
}

pub struct CpaProcessor<F>
where
F: Fn(ArrayView1<usize>, usize) -> usize,