Skip to content

Commit

Permalink
Cleanup the Arc and add test for code in README (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlepoint authored Sep 6, 2023
1 parent 6cded69 commit 8cb13e1
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 133 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/fhe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ zeroize_derive.workspace = true
ndarray.workspace = true
protobuf.workspace = true
thiserror.workspace = true
doc-comment = "0.3.3"

[dev-dependencies]
criterion.workspace = true
Expand Down
25 changes: 12 additions & 13 deletions crates/fhe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,33 @@ One ciphertext encrypts the value `20` using the secret key, and one ciphertext
use fhe::bfv::{BfvParametersBuilder, Ciphertext, Encoding, Plaintext, PublicKey, SecretKey};
use fhe_traits::*;
use rand::{rngs::OsRng, thread_rng};
use std::sync::Arc;
use std::error::Error;

fn main() {
let parameters = Arc::new(
BfvParametersBuilder::new()
fn main() -> Result<(), Box<dyn Error>> {
let parameters = BfvParametersBuilder::new()
.set_degree(2048)
.set_moduli(&[0x3fffffff000001])
.set_plaintext_modulus(1 << 10)
.build()
.unwrap(),
);
.build_arc()?;
let mut rng = thread_rng();

let secret_key = SecretKey::random(&parameters, &mut OsRng);
let public_key = PublicKey::new(&secret_key, &mut rng);

let plaintext_1 = Plaintext::try_encode(&[20_u64], Encoding::poly(), &parameters).unwrap();
let plaintext_2 = Plaintext::try_encode(&[-7_i64], Encoding::poly(), &parameters).unwrap();
let plaintext_1 = Plaintext::try_encode(&[20_u64], Encoding::poly(), &parameters)?;
let plaintext_2 = Plaintext::try_encode(&[-7_i64], Encoding::poly(), &parameters)?;

let ciphertext_1: Ciphertext = secret_key.try_encrypt(&plaintext_1, &mut rng).unwrap();
let ciphertext_2: Ciphertext = public_key.try_encrypt(&plaintext_2, &mut rng).unwrap();
let ciphertext_1: Ciphertext = secret_key.try_encrypt(&plaintext_1, &mut rng)?;
let ciphertext_2: Ciphertext = public_key.try_encrypt(&plaintext_2, &mut rng)?;

let result = &ciphertext_1 * &ciphertext_2;

let decrypted_plaintext = secret_key.try_decrypt(&result).unwrap();
let decrypted_vector = Vec::<i64>::try_decode(&decrypted_plaintext, Encoding::poly()).unwrap();
let decrypted_plaintext = secret_key.try_decrypt(&result)?;
let decrypted_vector = Vec::<i64>::try_decode(&decrypted_plaintext, Encoding::poly())?;

assert_eq!(decrypted_vector[0], -140);

Ok(())
}
```

Expand Down
16 changes: 7 additions & 9 deletions crates/fhe/examples/mulpir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use fhe_traits::{
use fhe_util::{ilog2, inverse, transcode_to_bytes};
use indicatif::HumanBytes;
use rand::{rngs::OsRng, thread_rng, RngCore};
use std::{env, error::Error, process::exit, sync::Arc};
use std::{env, error::Error, process::exit};
use util::{
encode_database, generate_database, number_elements_per_plaintext,
timeit::{timeit, timeit_n},
Expand Down Expand Up @@ -124,14 +124,12 @@ fn main() -> Result<(), Box<dyn Error>> {
// Let's generate the BFV parameters structure.
let params = timeit!(
"Parameters generation",
Arc::new(
bfv::BfvParametersBuilder::new()
.set_degree(degree)
.set_plaintext_modulus(plaintext_modulus)
.set_moduli_sizes(&moduli_sizes)
.build()
.unwrap()
)
bfv::BfvParametersBuilder::new()
.set_degree(degree)
.set_plaintext_modulus(plaintext_modulus)
.set_moduli_sizes(&moduli_sizes)
.build_arc()
.unwrap()
);

// Proprocess the database on the server side: the database will be reshaped
Expand Down
14 changes: 6 additions & 8 deletions crates/fhe/examples/sealpir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,12 @@ fn main() -> Result<(), Box<dyn Error>> {
// Let's generate the BFV parameters structure.
let params = timeit!(
"Parameters generation",
Arc::new(
bfv::BfvParametersBuilder::new()
.set_degree(degree)
.set_plaintext_modulus(plaintext_modulus)
.set_moduli_sizes(&moduli_sizes)
.build()
.unwrap()
)
bfv::BfvParametersBuilder::new()
.set_degree(degree)
.set_plaintext_modulus(plaintext_modulus)
.set_moduli_sizes(&moduli_sizes)
.build_arc()
.unwrap()
);

// Proprocess the database on the server side: the database will be reshaped
Expand Down
18 changes: 9 additions & 9 deletions crates/fhe/src/bfv/ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ mod tests {
use fhe_traits::FheDecrypter;
use fhe_traits::{DeserializeParametrized, FheEncoder, FheEncrypter, Serialize};
use rand::thread_rng;
use std::{error::Error, sync::Arc};
use std::error::Error;

#[test]
fn proto_conversion() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(1, 8)),
Arc::new(BfvParameters::default(6, 8)),
BfvParameters::default_arc(1, 8),
BfvParameters::default_arc(6, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let v = params.plaintext.random_vec(params.degree(), &mut rng);
Expand All @@ -226,8 +226,8 @@ mod tests {
fn serialize() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(1, 8)),
Arc::new(BfvParameters::default(6, 8)),
BfvParameters::default_arc(1, 8),
BfvParameters::default_arc(6, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let v = params.plaintext.random_vec(params.degree(), &mut rng);
Expand All @@ -243,8 +243,8 @@ mod tests {
fn new() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(1, 8)),
Arc::new(BfvParameters::default(6, 8)),
BfvParameters::default_arc(1, 8),
BfvParameters::default_arc(6, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let v = params.plaintext.random_vec(params.degree(), &mut rng);
Expand Down Expand Up @@ -281,8 +281,8 @@ mod tests {
fn mod_switch_to_last_level() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(1, 8)),
Arc::new(BfvParameters::default(6, 8)),
BfvParameters::default_arc(1, 8),
BfvParameters::default_arc(6, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let v = params.plaintext.random_vec(params.degree(), &mut rng);
Expand Down
30 changes: 15 additions & 15 deletions crates/fhe/src/bfv/keys/evaluation_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,12 @@ mod tests {
use fhe_util::ilog2;
use itertools::izip;
use rand::thread_rng;
use std::{cmp::min, error::Error, sync::Arc};
use std::{cmp::min, error::Error};

#[test]
fn builder() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let params = Arc::new(BfvParameters::default(6, 8));
let params = BfvParameters::default_arc(6, 8);
let sk = SecretKey::random(&params, &mut rng);

let max_level = params.max_level();
Expand Down Expand Up @@ -582,8 +582,8 @@ mod tests {
fn inner_sum() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(5, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(5, 8),
] {
for _ in 0..25 {
for ciphertext_level in 0..=params.max_level() {
Expand Down Expand Up @@ -626,8 +626,8 @@ mod tests {
fn row_rotation() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(5, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(5, 8),
] {
for _ in 0..50 {
for ciphertext_level in 0..=params.max_level() {
Expand Down Expand Up @@ -671,8 +671,8 @@ mod tests {
fn column_rotation() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(5, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(5, 8),
] {
let row_size = params.degree() >> 1;
for _ in 0..50 {
Expand Down Expand Up @@ -728,8 +728,8 @@ mod tests {
fn expansion() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(5, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(5, 8),
] {
let log_degree = 64 - 1 - params.degree().leading_zeros();
for _ in 0..15 {
Expand Down Expand Up @@ -784,9 +784,9 @@ mod tests {
fn proto_conversion() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(1, 8)),
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(5, 8)),
BfvParameters::default_arc(1, 8),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(5, 8),
] {
let sk = SecretKey::random(&params, &mut rng);

Expand Down Expand Up @@ -830,8 +830,8 @@ mod tests {
fn serialize() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(1, 8)),
Arc::new(BfvParameters::default(6, 8)),
BfvParameters::default_arc(1, 8),
BfvParameters::default_arc(6, 8),
] {
let sk = SecretKey::random(&params, &mut rng);

Expand Down
10 changes: 5 additions & 5 deletions crates/fhe/src/bfv/keys/galois_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ mod tests {
};
use fhe_traits::{FheDecoder, FheDecrypter, FheEncoder, FheEncrypter};
use rand::thread_rng;
use std::{error::Error, sync::Arc};
use std::error::Error;

#[test]
fn relinearization() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(3, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(3, 8),
] {
for _ in 0..30 {
let sk = SecretKey::random(&params, &mut rng);
Expand Down Expand Up @@ -188,8 +188,8 @@ mod tests {
fn proto_conversion() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(4, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(4, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let gk = GaloisKey::new(&sk, 9, 0, 0, &mut rng)?;
Expand Down
12 changes: 6 additions & 6 deletions crates/fhe/src/bfv/keys/key_switching_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,14 @@ mod tests {
};
use num_bigint::BigUint;
use rand::thread_rng;
use std::{error::Error, sync::Arc};
use std::error::Error;

#[test]
fn constructor() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(3, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(3, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let ctx = params.ctx_at_level(0)?;
Expand All @@ -301,7 +301,7 @@ mod tests {
#[test]
fn key_switch() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [Arc::new(BfvParameters::default(6, 8))] {
for params in [BfvParameters::default_arc(6, 8)] {
for _ in 0..100 {
let sk = SecretKey::random(&params, &mut rng);
let ctx = params.ctx_at_level(0)?;
Expand Down Expand Up @@ -340,8 +340,8 @@ mod tests {
fn proto_conversion() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(3, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(3, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let ctx = params.ctx_at_level(0)?;
Expand Down
12 changes: 6 additions & 6 deletions crates/fhe/src/bfv/keys/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ mod tests {
use crate::bfv::{parameters::BfvParameters, Encoding, Plaintext, SecretKey};
use fhe_traits::{DeserializeParametrized, FheDecrypter, FheEncoder, FheEncrypter, Serialize};
use rand::thread_rng;
use std::{error::Error, sync::Arc};
use std::error::Error;

#[test]
fn keygen() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let params = Arc::new(BfvParameters::default(1, 8));
let params = BfvParameters::default_arc(1, 8);
let sk = SecretKey::random(&params, &mut rng);
let pk = PublicKey::new(&sk, &mut rng);
assert_eq!(pk.par, params);
Expand All @@ -163,8 +163,8 @@ mod tests {
fn encrypt_decrypt() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(1, 8)),
Arc::new(BfvParameters::default(6, 8)),
BfvParameters::default_arc(1, 8),
BfvParameters::default_arc(6, 8),
] {
for level in 0..params.max_level() {
for _ in 0..20 {
Expand Down Expand Up @@ -192,8 +192,8 @@ mod tests {
fn test_serialize() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(1, 8)),
Arc::new(BfvParameters::default(6, 8)),
BfvParameters::default_arc(1, 8),
BfvParameters::default_arc(6, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let pk = PublicKey::new(&sk, &mut rng);
Expand Down
10 changes: 5 additions & 5 deletions crates/fhe/src/bfv/keys/relinearization_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ mod tests {
use fhe_math::rq::{traits::TryConvertFrom as TryConvertFromPoly, Poly, Representation};
use fhe_traits::{FheDecoder, FheDecrypter};
use rand::thread_rng;
use std::{error::Error, sync::Arc};
use std::error::Error;

#[test]
fn relinearization() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [Arc::new(BfvParameters::default(6, 8))] {
for params in [BfvParameters::default_arc(6, 8)] {
for _ in 0..100 {
let sk = SecretKey::random(&params, &mut rng);
let rk = RelinearizationKey::new(&sk, &mut rng)?;
Expand Down Expand Up @@ -230,7 +230,7 @@ mod tests {
#[test]
fn relinearization_leveled() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [Arc::new(BfvParameters::default(5, 8))] {
for params in [BfvParameters::default_arc(5, 8)] {
for ciphertext_level in 0..3 {
for key_level in 0..ciphertext_level {
for _ in 0..10 {
Expand Down Expand Up @@ -294,8 +294,8 @@ mod tests {
fn proto_conversion() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
for params in [
Arc::new(BfvParameters::default(6, 8)),
Arc::new(BfvParameters::default(3, 8)),
BfvParameters::default_arc(6, 8),
BfvParameters::default_arc(3, 8),
] {
let sk = SecretKey::random(&params, &mut rng);
let rk = RelinearizationKey::new(&sk, &mut rng)?;
Expand Down
Loading

0 comments on commit 8cb13e1

Please sign in to comment.