Skip to content

Commit

Permalink
mp-spdz-rs: fhe: Setup params using CurveGroup's scalar field modulus
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Apr 4, 2024
1 parent b31b08f commit 0a9232d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions mp-spdz-rs/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod ffi_inner {
type FHE_Params;
fn new_fhe_params(n_mults: i32, drown_sec: i32) -> UniquePtr<FHE_Params>;
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<bigint>;

// `FHE Keys`
Expand Down
18 changes: 9 additions & 9 deletions mp-spdz-rs/src/fhe/ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<C: CurveGroup> Mul<&Plaintext<C>> for &Ciphertext<C> {
#[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;
Expand Down Expand Up @@ -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, &params);
let ciphertext = encrypt_int(val1, &keypair, &params);
Expand All @@ -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, &params);
let ciphertext = encrypt_int(val1, &keypair, &params);
Expand All @@ -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, &params);
let ciphertext2 = encrypt_int(val2, &keypair, &params);
Expand All @@ -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, &params);
let ciphertext2 = encrypt_int(val2, &keypair, &params);
Expand Down
9 changes: 5 additions & 4 deletions mp-spdz-rs/src/fhe/params.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -31,9 +31,10 @@ impl<C: CurveGroup> BGVParams<C> {
let mut inner = new_fhe_params(n_mults as i32, DEFAULT_DROWN_SEC);

// Generate the parameters
let bits = Scalar::<C>::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 }
}

Expand Down
14 changes: 7 additions & 7 deletions mp-spdz-rs/src/fhe/plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<C: CurveGroup> Mul<&Plaintext<C>> for &Plaintext<C> {

#[cfg(test)]
mod tests {
use rand::{thread_rng, RngCore};
use rand::thread_rng;

use super::*;
use crate::TestCurve;
Expand All @@ -101,8 +101,8 @@ mod tests {
fn test_add() {
let mut rng = thread_rng();
let params = get_params();
let val1: Scalar<TestCurve> = rng.next_u64().into();
let val2: Scalar<TestCurve> = rng.next_u32().into();
let val1: Scalar<TestCurve> = Scalar::random(&mut rng);
let val2: Scalar<TestCurve> = Scalar::random(&mut rng);

let mut plaintext1 = Plaintext::new(&params);
let mut plaintext2 = Plaintext::new(&params);
Expand All @@ -118,8 +118,8 @@ mod tests {
fn test_sub() {
let mut rng = thread_rng();
let params = get_params();
let val1: Scalar<TestCurve> = rng.next_u64().into();
let val2: Scalar<TestCurve> = rng.next_u32().into();
let val1: Scalar<TestCurve> = Scalar::random(&mut rng);
let val2: Scalar<TestCurve> = Scalar::random(&mut rng);

let mut plaintext1 = Plaintext::new(&params);
let mut plaintext2 = Plaintext::new(&params);
Expand All @@ -135,8 +135,8 @@ mod tests {
fn test_mul() {
let mut rng = thread_rng();
let params = get_params();
let val1: Scalar<TestCurve> = rng.next_u64().into();
let val2: Scalar<TestCurve> = rng.next_u64().into();
let val1: Scalar<TestCurve> = Scalar::random(&mut rng);
let val2: Scalar<TestCurve> = Scalar::random(&mut rng);

let mut plaintext1 = Plaintext::new(&params);
let mut plaintext2 = Plaintext::new(&params);
Expand Down

0 comments on commit 0a9232d

Please sign in to comment.