From 09cddd62fa9b0facf74352f85759d9c021ab3a8d Mon Sep 17 00:00:00 2001 From: Joey Kraut Date: Thu, 4 Apr 2024 15:29:38 -0700 Subject: [PATCH] mp-spdz-rs: fhe: Setup params using `CurveGroup`'s scalar field modulus --- mp-spdz-rs/src/ffi.rs | 1 + mp-spdz-rs/src/fhe/ciphertext.rs | 18 +++++++++--------- mp-spdz-rs/src/fhe/params.rs | 9 +++++---- mp-spdz-rs/src/fhe/plaintext.rs | 14 +++++++------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/mp-spdz-rs/src/ffi.rs b/mp-spdz-rs/src/ffi.rs index ff188e3..8828749 100644 --- a/mp-spdz-rs/src/ffi.rs +++ b/mp-spdz-rs/src/ffi.rs @@ -19,6 +19,7 @@ mod ffi_inner { type FHE_Params; fn new_fhe_params(n_mults: i32, drown_sec: i32) -> UniquePtr; fn basic_generation_mod_prime(self: Pin<&mut FHE_Params>, plaintext_length: i32); + fn param_generation_with_modulus(self: Pin<&mut FHE_Params>, plaintext_modulus: &bigint); fn get_plaintext_mod(params: &FHE_Params) -> UniquePtr; // `FHE Keys` diff --git a/mp-spdz-rs/src/fhe/ciphertext.rs b/mp-spdz-rs/src/fhe/ciphertext.rs index 7ff6995..b541414 100644 --- a/mp-spdz-rs/src/fhe/ciphertext.rs +++ b/mp-spdz-rs/src/fhe/ciphertext.rs @@ -72,7 +72,7 @@ impl Mul<&Plaintext> for &Ciphertext { #[cfg(test)] mod test { use ark_mpc::algebra::Scalar; - use rand::{thread_rng, RngCore}; + use rand::thread_rng; use crate::fhe::{keys::BGVKeypair, params::BGVParams, plaintext::Plaintext}; use crate::TestCurve; @@ -115,8 +115,8 @@ mod test { let (params, mut keypair) = setup_fhe(); // Add a ciphertext with a plaintext - let val1 = rng.next_u64().into(); - let val2 = rng.next_u64().into(); + let val1 = Scalar::random(&mut rng); + let val2 = Scalar::random(&mut rng); let plaintext = plaintext_int(val2, ¶ms); let ciphertext = encrypt_int(val1, &keypair, ¶ms); @@ -138,8 +138,8 @@ mod test { let (params, mut keypair) = setup_fhe(); // Multiply a ciphertext with a plaintext - let val1 = rng.next_u64().into(); - let val2 = rng.next_u64().into(); + let val1 = Scalar::random(&mut rng); + let val2 = Scalar::random(&mut rng); let plaintext = plaintext_int(val2, ¶ms); let ciphertext = encrypt_int(val1, &keypair, ¶ms); @@ -161,8 +161,8 @@ mod test { let (params, mut keypair) = setup_fhe(); // Add two ciphertexts - let val1 = rng.next_u64().into(); - let val2 = rng.next_u64().into(); + let val1 = Scalar::random(&mut rng); + let val2 = Scalar::random(&mut rng); let ciphertext1 = encrypt_int(val1, &keypair, ¶ms); let ciphertext2 = encrypt_int(val2, &keypair, ¶ms); @@ -184,8 +184,8 @@ mod test { let (params, mut keypair) = setup_fhe(); // Multiply two ciphertexts - let val1 = rng.next_u64().into(); - let val2 = rng.next_u64().into(); + let val1 = Scalar::random(&mut rng); + let val2 = Scalar::random(&mut rng); let ciphertext1 = encrypt_int(val1, &keypair, ¶ms); let ciphertext2 = encrypt_int(val2, &keypair, ¶ms); diff --git a/mp-spdz-rs/src/fhe/params.rs b/mp-spdz-rs/src/fhe/params.rs index a3afe68..2a70a05 100644 --- a/mp-spdz-rs/src/fhe/params.rs +++ b/mp-spdz-rs/src/fhe/params.rs @@ -1,12 +1,12 @@ //! FHE setup parameters use ark_ec::CurveGroup; -use ark_mpc::algebra::Scalar; +use ark_ff::{BigInteger, PrimeField}; use std::marker::PhantomData; use cxx::UniquePtr; -use crate::ffi::{new_fhe_params, FHE_Params}; +use crate::ffi::{bigint_from_be_bytes, new_fhe_params, FHE_Params}; /// The default drowning security parameter const DEFAULT_DROWN_SEC: i32 = 128; @@ -31,9 +31,10 @@ impl BGVParams { let mut inner = new_fhe_params(n_mults as i32, DEFAULT_DROWN_SEC); // Generate the parameters - let bits = Scalar::::bit_length() as i32; - inner.pin_mut().basic_generation_mod_prime(bits); + let mut mod_bytes = C::ScalarField::MODULUS.to_bytes_be(); + let mod_bigint = unsafe { bigint_from_be_bytes(mod_bytes.as_mut_ptr(), mod_bytes.len()) }; + inner.pin_mut().param_generation_with_modulus(mod_bigint.as_ref().unwrap()); Self { inner, _phantom: PhantomData } } diff --git a/mp-spdz-rs/src/fhe/plaintext.rs b/mp-spdz-rs/src/fhe/plaintext.rs index e2d1810..52173cb 100644 --- a/mp-spdz-rs/src/fhe/plaintext.rs +++ b/mp-spdz-rs/src/fhe/plaintext.rs @@ -87,7 +87,7 @@ impl Mul<&Plaintext> for &Plaintext { #[cfg(test)] mod tests { - use rand::{thread_rng, RngCore}; + use rand::thread_rng; use super::*; use crate::TestCurve; @@ -101,8 +101,8 @@ mod tests { fn test_add() { let mut rng = thread_rng(); let params = get_params(); - let val1: Scalar = rng.next_u64().into(); - let val2: Scalar = rng.next_u32().into(); + let val1: Scalar = Scalar::random(&mut rng); + let val2: Scalar = Scalar::random(&mut rng); let mut plaintext1 = Plaintext::new(¶ms); let mut plaintext2 = Plaintext::new(¶ms); @@ -118,8 +118,8 @@ mod tests { fn test_sub() { let mut rng = thread_rng(); let params = get_params(); - let val1: Scalar = rng.next_u64().into(); - let val2: Scalar = rng.next_u32().into(); + let val1: Scalar = Scalar::random(&mut rng); + let val2: Scalar = Scalar::random(&mut rng); let mut plaintext1 = Plaintext::new(¶ms); let mut plaintext2 = Plaintext::new(¶ms); @@ -135,8 +135,8 @@ mod tests { fn test_mul() { let mut rng = thread_rng(); let params = get_params(); - let val1: Scalar = rng.next_u64().into(); - let val2: Scalar = rng.next_u64().into(); + let val1: Scalar = Scalar::random(&mut rng); + let val2: Scalar = Scalar::random(&mut rng); let mut plaintext1 = Plaintext::new(¶ms); let mut plaintext2 = Plaintext::new(¶ms);