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

Check formatting and lint in CI #4

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ name: Rust

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always
Expand All @@ -15,8 +13,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Check formatting
run: cargo fmt --check
- name: Lint
run: cargo clippy
23 changes: 8 additions & 15 deletions examples/cpa.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use indicatif::ProgressIterator;
use muscat::cpa_normal::*;
use muscat::leakage::{hw, sbox};
use muscat::util::{progress_bar, read_array_2_from_npy_file, save_array2};
use indicatif::ProgressIterator;
use ndarray::*;
use rayon::iter::{ParallelBridge, ParallelIterator};
use std::time::{self};


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


// traces format
type FormatTraces = f64;
type FormatMetadata = u8;
Expand All @@ -30,7 +28,8 @@ fn cpa() {
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))
let mut cpa_parallel = ((0..len_traces).step_by(patch))
.progress_with(progress_bar(len_traces))
.map(|row| row)
.par_bridge()
.map(|row_number| {
Expand All @@ -54,7 +53,6 @@ fn cpa() {
save_array2("results/corr.npy", cpa_parallel.pass_corr_array().view());
}


#[allow(dead_code)]
fn success() {
let start_sample: usize = 0;
Expand All @@ -79,9 +77,10 @@ fn success() {
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();
.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);
}
}
Expand All @@ -91,14 +90,8 @@ fn success() {
save_array2("results/success.npy", cpa.pass_rank().view());
}



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




2 changes: 1 addition & 1 deletion examples/rank.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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 indicatif::ProgressIterator;

// traces format
type FormatTraces = i16;
Expand Down
36 changes: 19 additions & 17 deletions src/cpa_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ impl Cpa {
}
}

pub fn update<T: Copy, U: Copy>(
&mut self,
trace_patch: Array2<T>,
plaintext_patch: Array2<U>,
) where
f32: From<T>, usize:From<U>
pub fn update<T: Copy, U: Copy>(&mut self, trace_patch: Array2<T>, plaintext_patch: Array2<U>)
where
f32: From<T>,
usize: From<U>,
{
/* This function updates the internal arrays of the CPA
It accepts trace_patch and plaintext_patch to update them*/
Expand Down Expand Up @@ -98,7 +96,14 @@ impl Cpa {
}
}

pub fn update_success<T: Copy, U: Copy>(&mut self, trace_patch: Array2<T>, plaintext_patch: Array2<U>) where f32: From<T>, usize:From<U> {
pub fn update_success<T: Copy, U: Copy>(
&mut self,
trace_patch: Array2<T>,
plaintext_patch: Array2<U>,
) where
f32: From<T>,
usize: From<U>,
{
/* This function updates the main arrays of the CPA for the success rate*/
self.update(trace_patch, plaintext_patch);
if self.len_leakages % self.rank_traces == 0 {
Expand All @@ -115,24 +120,21 @@ impl Cpa {
/* This function finalizes the calculation after
feeding all stored acc arrays */
let cov_n: Array2<f32> = self.cov.clone() / self.len_leakages as f32;
let avg_keys: Array1<f32> = self.sum_keys.clone()/ self.len_leakages as f32;
let avg_keys: Array1<f32> = self.sum_keys.clone() / self.len_leakages as f32;
let std_key: Array1<f32> = self.sum2_keys.clone() / self.len_leakages as f32;
let avg_leakages: Array1<f32> = self.sum_leakages.clone() / self.len_leakages as f32;
let std_leakages: Array1<f32> = self.sum2_leakages.clone() / self.len_leakages as f32;

for i in 0..self.guess_range as usize {
for x in 0..self.len_samples {
let numerator: f32 = cov_n[[i, x]]
- (avg_keys[i] * avg_leakages[x]);

let denominator_1: f32 = std_key[i]
- (avg_keys[i] * avg_keys[i] );
let numerator: f32 = cov_n[[i, x]] - (avg_keys[i] * avg_leakages[x]);

let denominator_2: f32 = std_leakages[x]
- (avg_leakages[x] * avg_leakages[x]);
if numerator != 0.0{
self.corr[[i as usize, x]] = f32::abs(numerator / f32::sqrt(denominator_1 * denominator_2));
let denominator_1: f32 = std_key[i] - (avg_keys[i] * avg_keys[i]);

let denominator_2: f32 = std_leakages[x] - (avg_leakages[x] * avg_leakages[x]);
if numerator != 0.0 {
self.corr[[i as usize, x]] =
f32::abs(numerator / f32::sqrt(denominator_1 * denominator_2));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod cpa;
pub mod cpa_normal;
pub mod leakage;
pub mod preprocessors;
pub mod processors;
pub mod quicklog;
pub mod trace;
pub mod util;
pub mod preprocessors;
pub mod cpa_normal;
Loading