diff --git a/iris-mpc-common/Cargo.toml b/iris-mpc-common/Cargo.toml index 3bb0122a6..e7a43e6f9 100644 --- a/iris-mpc-common/Cargo.toml +++ b/iris-mpc-common/Cargo.toml @@ -46,3 +46,11 @@ float_eq = "1" [[bin]] name = "key-manager" path = "src/bin/key_manager.rs" + +[[bin]] +name = "shares-encoding" +path = "src/bin/shares_encoding.rs" + +[[bin]] +name = "e2e-input-transform" +path = "src/bin/e2e_input_transform.rs" diff --git a/iris-mpc-common/src/bin/e2e_input_transform.rs b/iris-mpc-common/src/bin/e2e_input_transform.rs new file mode 100644 index 000000000..0cc064714 --- /dev/null +++ b/iris-mpc-common/src/bin/e2e_input_transform.rs @@ -0,0 +1,114 @@ +use base64::{engine::general_purpose, Engine}; +use iris_mpc_common::{ + galois_engine::degree4::GaloisRingIrisCodeShare, iris_db::iris::IrisCodeArray, +}; +use rand::SeedableRng; +use serde::{Deserialize, Serialize}; +use std::{ + fs::File, + io::{Read, Write}, +}; + +const RNG_SEED: u64 = 42; // Replace with your seed value + +#[derive(Debug, Serialize, Deserialize)] +struct Signup { + signup_id: String, + iris_code_left: String, + iris_code_shares_left: Option<[String; 3]>, + mask_code_left: String, + mask_code_shares_left: Option<[String; 3]>, + iris_code_right: String, + iris_code_shares_right: Option<[String; 3]>, + mask_code_right: String, + mask_code_shares_right: Option<[String; 3]>, + matched_with: Vec, + whitelisted: bool, + closest_signups: Vec, + zkp: String, + idcomm: String, +} + +impl Signup { + fn add_shares(&mut self) { + let mut rng = rand::rngs::StdRng::seed_from_u64(RNG_SEED); + let iris_code_left = IrisCodeArray::from_base64(&self.iris_code_left).unwrap(); + let mask_code_left = IrisCodeArray::from_base64(&self.mask_code_left).unwrap(); + let iris_code_right = IrisCodeArray::from_base64(&self.iris_code_right).unwrap(); + let mask_code_right = IrisCodeArray::from_base64(&self.mask_code_right).unwrap(); + + // Encode iris code shares left + let shares_iris_code_left = + GaloisRingIrisCodeShare::encode_iris_code(&iris_code_left, &mask_code_left, &mut rng); + self.iris_code_shares_left = Some(get_share_strings(&shares_iris_code_left)); + + // Encode iris code shares right + let shares_iris_code_right = + GaloisRingIrisCodeShare::encode_iris_code(&iris_code_right, &mask_code_right, &mut rng); + self.iris_code_shares_right = Some(get_share_strings(&shares_iris_code_right)); + + // Encode mask code shares left + let shares_mask_code_left = + GaloisRingIrisCodeShare::encode_mask_code(&mask_code_left, &mut rng); + self.mask_code_shares_left = Some(get_share_strings(&shares_mask_code_left)); + + // Encode mask code shares right + let shares_mask_code_right = + GaloisRingIrisCodeShare::encode_mask_code(&mask_code_right, &mut rng); + self.mask_code_shares_right = Some(get_share_strings(&shares_mask_code_right)); + } +} + +#[derive(Debug, Serialize, Deserialize)] +struct Match { + signup_id: String, + distance_left: f64, + distance_right: f64, + manipulation_type: String, + normalization_used: bool, +} + +fn get_share_strings(shares_iris_code_left: &[GaloisRingIrisCodeShare; 3]) -> [String; 3] { + let mut shares: [String; 3] = Default::default(); + for i in 0..3 { + shares[i] = + general_purpose::STANDARD.encode(bytemuck::cast_slice(&shares_iris_code_left[i].coefs)); + } + shares +} + +fn read_json_file(file_path: &str) -> serde_json::Result> { + let mut file = File::open(file_path).expect("File not found"); + let mut data = String::new(); + file.read_to_string(&mut data) + .expect("Unable to read string"); + + let signups: Vec = serde_json::from_str(&data)?; + Ok(signups) +} + +fn write_json_file(file_path: &str, signups: &Vec) -> std::io::Result<()> { + let json_data = serde_json::to_string_pretty(signups).expect("Serialization failed"); + let mut file = File::create(file_path)?; + file.write_all(json_data.as_bytes())?; + Ok(()) +} + +fn main() { + let input_file = "./iris-mpc-common/src/bin/data/ss_e2e.json"; + let output_file = "./iris-mpc-common/src/bin/data/generated_ss_e2e_shares.json"; + let mut signups = read_json_file(input_file).unwrap(); + for signup in &mut signups { + println!("Calculating shares for signup {:?}", signup.signup_id); + signup.add_shares(); + } + write_json_file( + "./iris-mpc-common/src/bin/data/generated_ss_e2e_shares.json", + &signups, + ) + .unwrap(); + println!( + "Share calculation completed. Results written to {:?}", + output_file + ); +} diff --git a/iris-mpc-common/src/bin/shares_encoding.rs b/iris-mpc-common/src/bin/shares_encoding.rs new file mode 100644 index 000000000..06fd14748 --- /dev/null +++ b/iris-mpc-common/src/bin/shares_encoding.rs @@ -0,0 +1,45 @@ +use clap::Parser; +use iris_mpc_common::{ + galois_engine::degree4::GaloisRingIrisCodeShare, iris_db::iris::IrisCodeArray, +}; +use rand::{prelude::StdRng, SeedableRng}; + +const RNG_SEED: u64 = 42; // Replace with your seed value + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + #[arg(short, long)] + iris_base_64: Option, + + #[arg(short, long)] + mask_base_64: Option, + + #[arg(short, long, env)] + rng_seed: Option, +} + +fn main() { + let args = Args::parse(); + let mut rng = if let Some(seed_rng) = args.rng_seed { + StdRng::seed_from_u64(seed_rng) + } else { + StdRng::seed_from_u64(RNG_SEED) + }; + + let iris_code = if let Some(iris_base_64) = args.iris_base_64 { + IrisCodeArray::from_base64(&iris_base_64).unwrap() + } else { + IrisCodeArray::random_rng(&mut rng) + }; + + let mask_code = if let Some(mask_base_64) = args.mask_base_64 { + IrisCodeArray::from_base64(&mask_base_64).unwrap() + } else { + IrisCodeArray::default() + }; + + let shares = GaloisRingIrisCodeShare::encode_iris_code(&iris_code, &mask_code, &mut rng); + + println!("Generated Shares: {:?}", shares); +}