Skip to content

Commit

Permalink
Add save and load helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
TrAyZeN committed Nov 13, 2024
1 parent 07b0680 commit 7138e57
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"

[features]
progress_bar = ["dep:indicatif"]
quicklog = ["dep:thiserror"]
quicklog = []

[dependencies]
serde_json = "1.0.132"
Expand All @@ -22,7 +22,7 @@ rayon = "1.10.0"
indicatif = { version = "0.17.8", optional = true }
ndarray-npy ="0.9.1"
itertools = "0.13.0"
thiserror = { version = "1.0.58", optional = true }
thiserror = { version = "1.0.58" }
serde = { version = "1.0.214", features = ["derive"] }

[dev-dependencies]
Expand Down
31 changes: 29 additions & 2 deletions src/distinguishers/cpa.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::util::{argmax_by, argsort_by, max_per_row};
use crate::{
util::{argmax_by, argsort_by, max_per_row},
Error,
};
use ndarray::{s, Array1, Array2, ArrayView1, ArrayView2, Axis};
use rayon::{
iter::ParallelBridge,
prelude::{IntoParallelIterator, ParallelIterator},
};
use serde::{Deserialize, Serialize};
use std::{iter::zip, ops::Add};
use std::{fs::File, iter::zip, ops::Add, path::Path};

/// Compute the [`Cpa`] of the given traces using [`CpaProcessor`].
///
Expand Down Expand Up @@ -242,6 +245,30 @@ where
a.dot(&b)
}

/// Save the [`CpaProcessor`] to a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let file = File::create(path)?;
serde_json::to_writer(file, &CpaProcessorSerdeAdapter::from(self))?;

Ok(())
}

/// Load a [`CpaProcessor`] from a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn load<P: AsRef<Path>>(path: P, leakage_func: F) -> Result<Self, Error> {
let file = File::open(path)?;
let p: CpaProcessorSerdeAdapter = serde_json::from_reader(file)?;

Ok(p.with(leakage_func))
}

/// Determine if two [`CpaProcessor`] are compatible for addition.
///
/// If they were created with the same parameters, they are compatible.
Expand Down
28 changes: 26 additions & 2 deletions src/distinguishers/cpa_normal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ndarray::{Array1, Array2, ArrayView1, ArrayView2, Axis};
use rayon::iter::{ParallelBridge, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{iter::zip, ops::Add};
use std::{fs::File, iter::zip, ops::Add, path::Path};

use crate::distinguishers::cpa::Cpa;
use crate::{distinguishers::cpa::Cpa, Error};

/// Compute the [`Cpa`] of the given traces using [`CpaProcessor`].
///
Expand Down Expand Up @@ -203,6 +203,30 @@ where
Cpa { corr }
}

/// Save the [`CpaProcessor`] to a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let file = File::create(path)?;
serde_json::to_writer(file, &CpaProcessorSerdeAdapter::from(self))?;

Ok(())
}

/// Load a [`CpaProcessor`] from a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn load<P: AsRef<Path>>(path: P, leakage_func: F) -> Result<Self, Error> {
let file = File::open(path)?;
let p: CpaProcessorSerdeAdapter = serde_json::from_reader(file)?;

Ok(p.with(leakage_func))
}

/// Determine if two [`CpaProcessor`] are compatible for addition.
///
/// If they were created with the same parameters, they are compatible.
Expand Down
31 changes: 29 additions & 2 deletions src/distinguishers/dpa.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use ndarray::{Array1, Array2, ArrayView1, ArrayView2, Axis};
use rayon::iter::{ParallelBridge, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{iter::zip, marker::PhantomData, ops::Add};
use std::{fs::File, iter::zip, marker::PhantomData, ops::Add, path::Path};

use crate::util::{argmax_by, argsort_by, max_per_row};
use crate::{
util::{argmax_by, argsort_by, max_per_row},
Error,
};

/// Compute the [`Dpa`] of the given traces using [`DpaProcessor`].
///
Expand Down Expand Up @@ -206,6 +209,30 @@ where
}
}

/// Save the [`DpaProcessor`] to a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let file = File::create(path)?;
serde_json::to_writer(file, &DpaProcessorSerdeAdapter::from(self))?;

Ok(())
}

/// Load a [`DpaProcessor`] from a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn load<P: AsRef<Path>>(path: P, selection_function: F) -> Result<Self, Error> {
let file = File::open(path)?;
let p: DpaProcessorSerdeAdapter = serde_json::from_reader(file)?;

Ok(p.with(selection_function))
}

/// Determine if two [`DpaProcessor`] are compatible for addition.
///
/// If they were created with the same parameters, they are compatible.
Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::io;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("Failed to save/load muscat data")]
SaveLoadError(#[from] serde_json::Error),
#[error(transparent)]
IoError(#[from] io::Error),
}
52 changes: 50 additions & 2 deletions src/leakage_detection.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Leakage detection methods
use crate::processors::MeanVar;
use crate::{processors::MeanVar, Error};
use ndarray::{s, Array1, Array2, ArrayView1, ArrayView2, Axis};
use rayon::iter::{ParallelBridge, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{iter::zip, ops::Add};
use std::{fs::File, iter::zip, ops::Add, path::Path};

/// Compute the SNR of the given traces using [`SnrProcessor`].
///
Expand Down Expand Up @@ -150,6 +150,30 @@ impl SnrProcessor {
self.classes_count.len()
}

/// Save the [`SnrProcessor`] to a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let file = File::create(path)?;
serde_json::to_writer(file, self)?;

Ok(())
}

/// Load a [`SnrProcessor`] from a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let file = File::open(path)?;
let p = serde_json::from_reader(file)?;

Ok(p)
}

/// Determine if two [`SnrProcessor`] are compatible for addition.
///
/// If they were created with the same parameters, they are compatible.
Expand Down Expand Up @@ -289,6 +313,30 @@ impl TTestProcessor {
self.mean_var_1.size()
}

/// Save the [`TTestProcessor`] to a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let file = File::create(path)?;
serde_json::to_writer(file, self)?;

Ok(())
}

/// Load a [`TTestProcessor`] from a file.
///
/// # Warning
/// The file format is not stable as muscat is active development. Thus, the format might
/// change between versions.
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let file = File::open(path)?;
let p = serde_json::from_reader(file)?;

Ok(p)
}

/// Determine if two [`TTestProcessor`] are compatible for addition.
///
/// If they were created with the same parameters, they are compatible.
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
pub mod distinguishers;
pub mod error;
pub mod leakage;
pub mod leakage_detection;
pub mod preprocessors;
pub mod processors;
pub mod trace;
pub mod util;

pub use crate::error::Error;

#[cfg(feature = "quicklog")]
pub mod quicklog;

0 comments on commit 7138e57

Please sign in to comment.