Commit daac01b7 authored by Jakub Klemsa's avatar Jakub Klemsa
Browse files

WIP: constants moved to default setup, values used in code (pass setup).

parent a75c1c82
......@@ -4,65 +4,6 @@ use concrete::*;
#[cfg(feature = "manual")]
use crate::params::*;
// Experiment Setup
/// Number of Benchmark Repetitions
pub const N_REPEAT: usize = 500;
/// Number of top BS times to be considered in single V_0 corrections search
#[cfg(feature = "numerical")]
pub const N_TOP_BS_TIMES: usize = 3;
/// Number of top V_max'es to be considered per BS time
#[cfg(feature = "numerical")]
pub const N_TOP_V_MAXES: usize = 2;
/// V_0 correction
/// When this is set 1.0, it has no effect
/// It is applied as is, for variance it might make sense to use 1/eta^2 (only the part related to V_0)
#[cfg(feature = "numerical")]
// pub const V0_CORR: [f64; 12] = [1.0, 1.1, 1.2, 1.3, 1.5, 1.75, 2.0, 2.3, 2.7, 3.2, 3.8, 4.5];
pub const V0_CORR: [f64; 10] = [0.826, 0.909, 1.0, 1.1, 1.210, 1.331, 1.464, 1.611, 1.772, 1.949]; //
// pub const V0_CORR: [f64; 4] = [0.621, 0.683, 0.751, 0.826]; // used for 126|5|2
// pub const V0_CORR: [f64; 6] = [1.0, 1.2, 1.44, 1.73, 2.07, 2.49];
// pub const V0_CORR: [f64; 1] = [1.0];
/// Security Parameter(s)
///
/// Derived from LWE Estimates:
///
/// - ( 90, 0.0335) (new; was 0.0328)
/// - (112, 0.0275) (new; was 0.0271)
/// - ( 80, 0.0380)
/// - (128, 0.0235)
///
#[cfg(feature = "numerical")]
#[allow(non_upper_case_globals)]
// pub const LAMBDA_S_LAMBDAs: [(usize, f64); 1] = [(128, 0.0235)];
// pub const LAMBDA_S_LAMBDAs: [(usize, f64); 1] = [(112, 0.0274)];
// pub const LAMBDA_S_LAMBDAs: [(usize, f64); 1] = [( 80, 0.0380)];
// pub const LAMBDA_S_LAMBDAs: [(usize, f64); 3] = [( 80, 0.0380), (127, 0.0238), (128, 0.0235)];
// alternative s_lambda's
pub const LAMBDA_S_LAMBDAs: [(usize, f64); 1] = [(125, 0.0241)];
// pub const LAMBDA_S_LAMBDAs: [(usize, f64); 1] = [(129, 0.0231)];
// pub const LAMBDA_S_LAMBDAs: [(usize, f64); 1] = [(126, 0.0239)];
/// Plaintext Precision(s), Quadratic Weight(s)
#[cfg(feature = "numerical")]
#[allow(non_upper_case_globals)]
// pub const PI_POW_2DELTAs: [(usize, usize); 1] = [(2, 2)];
// pub const PI_POW_2DELTAs: [(usize, usize); 1] = [(2, 3)];
// pub const PI_POW_2DELTAs: [(usize, usize); 1] = [(5, 2)]; // Demo Z/8Z by Zama
// pub const PI_POW_2DELTAs: [(usize, usize); 1] = [(6, 2)]; // Demo Z/16Z by Zama
// pub const PI_POW_2DELTAs: [(usize, usize); 1] = [(5, 20)];
// pub const PI_POW_2DELTAs: [(usize, usize); 2] = [(2, 3), (5, 20)];
// pub const PI_POW_2DELTAs: [(usize, usize); 2] = [(2, 2), (5, 20)];
// pub const PI_POW_2DELTAs: [(usize, usize); 4] = [(2, 2), (5, 20), (5, 2), (6, 2)];
pub const PI_POW_2DELTAs: [(usize, usize); 2] = [(5, 20), (6, 2)];
// pub const PI_POW_2DELTAs: [(usize, usize); 1] = [(2, 2)];
// pub const PI_POW_2DELTAs: [(usize, usize); 1] = [(5, 2)];
// Ranges
......
......@@ -18,7 +18,6 @@ mod misc;
use params::*;
#[cfg(feature = "manual")]
use constants_param_ranges::*;
use setup::*;
// resolve mutually exclusive features
......@@ -39,13 +38,14 @@ const NOISES_FILE_EXT: &str = "dat";
fn main() -> Result<(), Box<dyn Error>> {
// load setup
//TODO take setup filename as argument
let stp = Setup::new("setup.yaml")?;
//DBG
println!("(?) ==== SETUP LOAD END ====\n");
// generate all params
#[cfg(feature = "numerical")]
let all_pars = Params::gen_all();
let all_pars = Params::gen_all(&stp);
#[cfg(feature = "manual")]
let all_pars = MAN_PARAMS;
......@@ -89,7 +89,7 @@ fn main() -> Result<(), Box<dyn Error>> {
task_str, par_i+1, par_grp.len(), par_gi+1, all_pars.len(),
).purple());
println!("{:?}\n", par);
par.bench_p(&mut res_file, &mut nss_file)?;
par.bench_p(&stp, &mut res_file, &mut nss_file)?;
}
// (B) sort by numeric estimate & bench the best ones
......@@ -99,11 +99,11 @@ fn main() -> Result<(), Box<dyn Error>> {
task_str, par_i+1, par_grp.len(), par_gi+1, all_pars.len(),
).purple());
println!("{:?}\n", par);
par.bench_p(&mut res_file, &mut nss_file)?;
par.bench_p(&stp, &mut res_file, &mut nss_file)?;
}
//~ if let Some(num_best) = par_grp.iter().min_by(|p,q| p.num_bs_time_ms().partial_cmp(&q.num_bs_time_ms()).unwrap() ) {
//~ println!("{:?}\n", num_best);
//~ num_best.bench_p(&mut res_file, &mut nss_file)?;
//~ num_best.bench_p(&stp, &mut res_file, &mut nss_file)?;
//~ } else {
//~ eprintln!("(!) Minimum search by numerically estimated BS time failed.\n");
//~ }
......
......@@ -10,7 +10,7 @@ use crate::*;
impl Params {
pub fn bench_p(&self, res_file: &mut File, nss_file: &mut File) -> Result<(), Box<dyn Error>> {
pub fn bench_p(&self, stp: &Setup, res_file: &mut File, nss_file: &mut File) -> Result<(), Box<dyn Error>> {
let key = Keys::new(&self)?;
// get keys's filenames & respective sizes in bytes
......@@ -31,9 +31,9 @@ impl Params {
let mut _t: f64 = 0.0;
simple_duration!(
["Bench {}× BS", N_REPEAT],
["Bench {}× BS", stp.n_repeat],
[
for _ in 0..N_REPEAT {
for _ in 0..stp.n_repeat {
// prepare empty ciphertext for aggregation
// kreténi smazali zero_with_encoder a ikdyž ho sem zkopíruju, stejně je to pomrdaný ptž ani s encoderem se to neumí sečíst a vysere se to
let c = LWE::encrypt_uint(
......@@ -153,7 +153,7 @@ impl Params {
// write params & results
if let Err(e) = write!(res_file, "- :params:\n{}\n :bench:\n :n_runs: {}\n :time:\n :br_time_median_ms: {}\n :br_time_min_ms: {}\n :ks_time_median_ms: {}\n :ks_time_min_ms: {}\n :key_size:\n :sk_size: {}\n :bsk_size: {}\n :ksk_size: {}\n :noise:\n :meas_V_0: {}\n :calc_V_0_concrete: {}\n :calc_V_0_formula: {}\n :noise_max_abs: {}\n",
self.to_yaml(2),
N_REPEAT,
stp.n_repeat,
br_time_median_ms,
br_time_min_ms,
ks_time_median_ms,
......
......@@ -4,18 +4,19 @@ use colored::Colorize;
use crate::params::*;
#[cfg(feature = "manual")]
use crate::constants_param_ranges::*;
use crate::setup::*;
impl Params {
/// Generate params grouped by inputs: lambda, pi, 2Delta
pub fn gen_all() -> Vec<Vec<Params>> {
pub fn gen_all(stp: &Setup) -> Vec<Vec<Params>> {
// vector of groups of params
let mut all_params = Vec::new();
// loop input parameters
for lambda_s_lambda in LAMBDA_S_LAMBDAs {
for pi_2_delta in PI_POW_2DELTAs {
for &lambda_s_lambda in stp.lambda_s_lambdas {
for &pi_2_delta in stp.pi_pow_2deltas {
println!("{}", format!("\n(i) Generating {}-bit params for π = {}, 2^2∆ = {}.",
lambda_s_lambda.0, pi_2_delta.0, pi_2_delta.1,
......@@ -35,7 +36,7 @@ impl Params {
let Delta2 = (pow_2Delta as f64).log2();
// loop v0_corr
for v0_corr in V0_CORR {
for &v0_corr in stp.v0_corr {
let mut par_v0_corr_subgrp = Vec::new();
......@@ -205,7 +206,7 @@ impl Params {
for (i, p) in par_v0_corr_subgrp.iter().enumerate() {
// hitting new (N,n ; l,t) group (new BS time)
if p.num_bs_time_ms() != last_bs_time {
if bs_time_groups >= N_TOP_BS_TIMES {
if bs_time_groups >= stp.n_top_bs_times {
del_from_idx = i;
break;
}
......@@ -245,8 +246,8 @@ impl Params {
// sort by V_max
bst_grp.sort_by(|p,q| p.v_max_ins().partial_cmp(&q.v_max_ins()).unwrap() );
// take N_TOP_V_MAXES best
if bst_grp.len() > N_TOP_V_MAXES {
bst_grp.drain(N_TOP_V_MAXES..);
if bst_grp.len() > stp.n_top_v_maxes {
bst_grp.drain(stp.n_top_v_maxes..);
}
// update count
par_cnt += bst_grp.len();
......
......@@ -2,26 +2,96 @@ use std::error::Error;
use std::path::Path;
use std::fs;
use crate::constants_param_ranges::*;
extern crate yaml_rust;
use yaml_rust::YamlLoader; // YamlEmitter
#[derive(Clone,Debug)]
pub struct Setup {
pub struct Setup<'a> {
// experiment setup
pub v0_corr: &'a [f64],
pub lambda_s_lambdas: &'a [(usize, f64)],
pub pi_pow_2deltas: &'a [(usize, usize)],
// experiment parameters
pub n_repeat: usize,
//~ pub n_top_bs_times: usize,
//~ pub n_top_v_maxes: usize,
pub n_top_bs_times: usize,
pub n_top_v_maxes: usize,
// ranges
}
impl Setup {
// =============================================================================
//
// Default Setup
//
pub const SETUP_DEF: Setup = Setup {
// -------------------------------------------------------------------------
// Experiment Setup
//
/// V_0 correction (numerical mode only)
/// When this is set 1.0, it has no effect
/// [1.0, 1.1, 1.2, 1.3, 1.5, 1.75, 2.0, 2.3, 2.7, 3.2, 3.8, 4.5];
/// [0.826, 0.909, 1.0, 1.1, 1.210, 1.331, 1.464, 1.611, 1.772, 1.949]; //
/// [0.621, 0.683, 0.751, 0.826]; // used for 126|5|2
/// [1.0, 1.2, 1.44, 1.73, 2.07, 2.49];
/// [1.0];
v0_corr: &[1.0],
/// Security Parameter(s) (numerical mode only)
///
/// - ( 90, 0.0335) (new; was 0.0328)
/// - (112, 0.0275) (new; was 0.0271)
/// - ( 80, 0.0380)
/// - (128, 0.0235)
///
/// [(128, 0.0235)];
/// [(112, 0.0274)];
/// [( 80, 0.0380)];
/// [( 80, 0.0380), (127, 0.0238), (128, 0.0235)];
/// alternative s_lambda's
/// [(125, 0.0241)];
/// [(129, 0.0231)];
/// [(126, 0.0239)];
lambda_s_lambdas: &[( 80, 0.0380)],
/// Plaintext Precision(s), Quadratic Weight(s) (numerical mode only)
/// [(2, 2)];
/// [(2, 3)];
/// [(5, 2)]; // Demo Z/8Z by Zama
/// [(6, 2)]; // Demo Z/16Z by Zama
/// [(5, 20)];
/// [(2, 3), (5, 20)];
/// [(2, 2), (5, 20)];
/// [(2, 2), (5, 20), (5, 2), (6, 2)];
/// [(5, 20), (6, 2)];
/// [(2, 2)];
/// [(5, 2)];
pi_pow_2deltas: &[(5, 2)],
// -------------------------------------------------------------------------
// Experiment Parameters
//
/// Number of Benchmark Repetitions
n_repeat: 500,
/// Number of top BS times to be considered in single V_0 corrections search (numerical mode only)
n_top_bs_times: 2,
/// Number of top V_max'es to be considered per BS time (numerical mode only)
n_top_v_maxes: 3,
};
// =============================================================================
//
// Load Setup from YAML file
//
impl Setup<'_> {
/// Load experiment setup (or use default values)
pub fn new(setup_filename: &str) -> Result<Setup, Box<dyn Error>> {
// create Setup struct with default values
let mut stp = SETUP_DEFAULT.clone();
let mut stp = SETUP_DEF.clone();
if Path::new(setup_filename).is_file() {
println!("(i) Loading setup from '{}' ...", setup_filename);
......@@ -36,8 +106,12 @@ impl Setup {
if let Some(n_repeat) = setup_map["n_repeat"].as_i64() {
stp.n_repeat = n_repeat as usize;
} else {
//DBG
}
if let Some(n_top_bs_times) = setup_map["n_top_bs_times"].as_i64() {
stp.n_top_bs_times = n_top_bs_times as usize;
}
if let Some(n_top_v_maxes) = setup_map["n_top_v_maxes"].as_i64() {
stp.n_top_v_maxes = n_top_v_maxes as usize;
}
//DBG
......@@ -50,7 +124,3 @@ impl Setup {
Ok(stp)
}
}
pub const SETUP_DEFAULT: Setup = Setup {
n_repeat: N_REPEAT,
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment