From 91ccf99acfb5eaa63447387b9f8cd00d19b94075 Mon Sep 17 00:00:00 2001 From: Janmajaya Mall Date: Fri, 12 Jan 2024 17:34:21 +0530 Subject: [PATCH] add lib and license --- Cargo.lock | 2 -- Cargo.toml | 2 +- LICENSE | 21 ++++++++++++++ src/lib.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 15 ++++++++-- 5 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 LICENSE create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 133a039..8c038a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,7 +17,6 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bfv" version = "0.1.0" -source = "git+https://github.com/Janmajayamall/bfv?branch=pk#34090532ce04ee6c143753edab11f902c057837c" dependencies = [ "concrete-ntt", "crypto-bigint", @@ -344,7 +343,6 @@ dependencies = [ [[package]] name = "traits" version = "0.1.0" -source = "git+https://github.com/Janmajayamall/bfv?branch=pk#34090532ce04ee6c143753edab11f902c057837c" [[package]] name = "unicode-ident" diff --git a/Cargo.toml b/Cargo.toml index 8d005dc..2d55af9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bfv = {git = "https://github.com/Janmajayamall/bfv", branch = "pk"} +bfv = {path = "./../bfv/bfv"} rand = "0.8.5" itertools = "0.10.5" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ff7657e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Gaussian + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..9e4b008 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,83 @@ +use bfv::{ + BfvParameters, Ciphertext, CollectivePublicKeyGenerator, CollectiveRlkGenerator, + CollectiveRlkGeneratorState, Poly, SecretKey, +}; +use rand::thread_rng; + +static CRS_PK: [u8; 32] = [0u8; 32]; +static CRS_RLK: [u8; 32] = [0u8; 32]; + +fn params() -> BfvParameters { + let mut params = BfvParameters::new(&[30, 30], 65537, 1 << 15); + params.enable_hybrid_key_switching(&[30]); + params.enable_pke(); + params +} + +struct PrivateOutputAPostState0 { + s_pk_a: SecretKey, + s_rlk_a: SecretKey, +} +struct MessageAToBPostState0 { + share_pk_a: Poly, + share_rlk_a: (Vec, Vec), +} + +struct PrivateOutputBPostState0 { + s_pk_b: SecretKey, +} +struct MessageBToAPostState1 {} + +struct MessageAToBPostState2 {} + +struct MessageBToAPostState3 {} + +fn state0() -> (PrivateOutputAPostState0, MessageAToBPostState0) { + let params = params(); + let mut rng = thread_rng(); + let s_pk_a = SecretKey::random_with_params(¶ms, &mut rng); + let s_rlk_a = CollectiveRlkGenerator::init_state(¶ms, &mut rng); + + let share_pk_a = + CollectivePublicKeyGenerator::generate_share(¶ms, &s_pk_a, CRS_PK, &mut rng); + + let share_rlk_a = + CollectiveRlkGenerator::generate_share_1(¶ms, &s_pk_a, &s_rlk_a, CRS_RLK, 0, &mut rng); + + let message_a_to_b = MessageAToBPostState0 { + share_pk_a, + share_rlk_a, + }; + + let private_state_a = PrivateOutputAPostState0 { + s_pk_a, + s_rlk_a: s_rlk_a.0.clone(), + }; + + (private_state_a, message_a_to_b) +} + +fn state1(input: MessageAToBPostState0) -> () { + let params = params(); + let mut rng = thread_rng(); + let s_pk_b = SecretKey::random_with_params(¶ms, &mut rng); + let s_rlk_b = CollectiveRlkGenerator::init_state(¶ms, &mut rng); + + let share_pk_b = + CollectivePublicKeyGenerator::generate_share(¶ms, &s_pk_b, CRS_PK, &mut rng); + + let share_rlk_b = + CollectiveRlkGenerator::generate_share_1(¶ms, &s_pk_b, &s_rlk_b, CRS_RLK, 0, &mut rng); + + // collective public key + let collective_pk_shares = vec![share_pk_b.clone(), input.share_pk_a]; + let collecitve_pk = CollectivePublicKeyGenerator::aggregate_shares_and_finalise( + ¶ms, + &collective_pk_shares, + CRS_PK, + ); +} + +fn state2() {} + +fn state3() {} diff --git a/src/main.rs b/src/main.rs index 41d40b8..9dbc7e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use bfv::{ BfvParameters, CollectiveDecryption, CollectivePublicKeyGenerator, CollectiveRlkGenerator, - Encoding, EvaluationKey, Evaluator, PartySecret, Plaintext, Poly, PublicKey, SecretKey, + Encoding, EvaluationKey, Evaluator, MHEDebugger, Plaintext, Poly, PublicKey, SecretKey, }; use itertools::{izip, Itertools}; use rand::{distributions::Uniform, thread_rng, Rng}; @@ -36,8 +36,8 @@ fn plain_psi(parties: &[Party]) -> Vec { } fn main() { - let mut params = BfvParameters::new(&[50, 50, 50], 65537, 1 << 15); - params.enable_hybrid_key_switching(&[50, 50]); + let mut params = BfvParameters::new(&[30, 30], 65537, 1 << 15); + params.enable_hybrid_key_switching(&[30]); params.enable_pke(); let hw = 21000; @@ -130,6 +130,15 @@ fn main() { let ct1ct2 = evaluator.mul(&ct1, &ct2); let psi_ct = evaluator.relinearize(&ct1ct2, &evaluation_key); + unsafe { + let secrets = parties.iter().map(|s| s.secret.clone()).collect_vec(); + dbg!(MHEDebugger::measure_noise( + &secrets, + evaluator.params(), + &psi_ct + )); + } + // Collective decryption // // Each party can independently generate their share to decrypt `psi_ct`. Unless the other party has evaluated the FHE circuit maliciously, they should be able to decrypt `psi_ct` after receiving other party's share. // As it is often the case in MPC, the last party to send their share has leverage to not send the share and prevent the other party from learning PSI output.