fixed tests for ciphertext fourier

This commit is contained in:
Jean-Philippe Bossuat
2025-05-15 10:45:06 +02:00
parent 67594e2e3f
commit 723a41acd0
9 changed files with 487 additions and 476 deletions

View File

@@ -130,6 +130,4 @@ fn main() {
.for_each(|(i, (a, b))| { .for_each(|(i, (a, b))| {
println!("{}: {} {}", i, a, (*b as f64) / scale); println!("{}: {} {}", i, a, (*b as f64) / scale);
}); });
module.free();
} }

View File

@@ -150,8 +150,7 @@ impl Scratch {
unsafe { &mut *(data as *mut [u8] as *mut Self) } unsafe { &mut *(data as *mut [u8] as *mut Self) }
} }
#[allow(dead_code)] pub fn available(&self) -> usize {
fn available(&self) -> usize {
let ptr: *const u8 = self.data.as_ptr(); let ptr: *const u8 = self.data.as_ptr();
let self_len: usize = self.data.len(); let self_len: usize = self.data.len();
let aligned_offset: usize = ptr.align_offset(DEFAULTALIGN); let aligned_offset: usize = ptr.align_offset(DEFAULTALIGN);

View File

@@ -337,8 +337,6 @@ mod tests {
assert_eq!(a_dft.raw(), b_dft.raw()); assert_eq!(a_dft.raw(), b_dft.raw());
} }
} }
module.free();
} }
#[test] #[test]
@@ -425,7 +423,5 @@ mod tests {
}); });
}); });
}); });
module.free();
} }
} }

View File

@@ -255,6 +255,17 @@ where
assert_eq!(self.n(), module.n()); assert_eq!(self.n(), module.n());
assert_eq!(res.n(), module.n()); assert_eq!(res.n(), module.n());
assert_eq!(a.n(), module.n()); assert_eq!(a.n(), module.n());
assert!(
scratch.available()
>= GGLWECiphertext::prod_with_glwe_scratch_space(
module,
res.size(),
a.size(),
self.size(),
self.rank_in(),
self.rank_out()
)
);
} }
let cols_in: usize = self.rank_in(); let cols_in: usize = self.rank_in();

View File

@@ -98,25 +98,25 @@ impl GLWECiphertextFourier<Vec<u8>, FFT64> {
rank_in: usize, rank_in: usize,
rank_out: usize, rank_out: usize,
) -> usize { ) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space( <GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_fourier_scratch_space(
module, res_size, lhs, rhs, rank_in, rank_out, module, res_size, lhs, rhs, rank_in, rank_out,
) )
} }
pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize, rank: usize) -> usize { pub fn keyswitch_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize, rank: usize) -> usize {
<GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space( <GGLWECiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_fourier_inplace_scratch_space(
module, res_size, rhs, rank, module, res_size, rhs, rank,
) )
} }
pub fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize, rank: usize) -> usize { pub fn external_product_scratch_space(module: &Module<FFT64>, res_size: usize, lhs: usize, rhs: usize, rank: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_scratch_space( <GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_fourier_scratch_space(
module, res_size, lhs, rhs, rank, rank, module, res_size, lhs, rhs, rank, rank,
) )
} }
pub fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize, rank: usize) -> usize { pub fn external_product_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize, rank: usize) -> usize {
<GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_inplace_scratch_space( <GGSWCiphertext<Vec<u8>, FFT64> as VecGLWEProductScratchSpace>::prod_with_glwe_fourier_inplace_scratch_space(
module, res_size, rhs, rank, module, res_size, rhs, rank,
) )
} }

View File

@@ -567,7 +567,7 @@
// }); // });
// }); // });
// } // }
pub(crate) fn noise_rgsw_product( pub(crate) fn noise_ggsw_gglwe_product(
n: f64, n: f64,
log_base2k: usize, log_base2k: usize,
var_xs: f64, var_xs: f64,

View File

@@ -13,7 +13,7 @@ use crate::{
glwe_plaintext::GLWEPlaintext, glwe_plaintext::GLWEPlaintext,
keys::{GLWEPublicKey, SecretKey, SecretKeyFourier}, keys::{GLWEPublicKey, SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey, keyswitch_key::GLWESwitchingKey,
test_fft64::{gglwe::noise_gglwe_product, ggsw::noise_rgsw_product}, test_fft64::{gglwe::noise_gglwe_product, ggsw::noise_ggsw_gglwe_product},
}; };
#[test] #[test]
@@ -58,6 +58,22 @@ fn keyswitch_inplace() {
}); });
} }
#[test]
fn external_product() {
(1..4).for_each(|rank| {
println!("test external_product rank: {}", rank);
test_external_product(12, 12, 60, 45, 60, rank, 3.2);
});
}
#[test]
fn external_product_inplace() {
(1..4).for_each(|rank| {
println!("test external_product rank: {}", rank);
test_external_product_inplace(12, 15, 60, 60, rank, 3.2);
});
}
fn test_encrypt_sk(log_n: usize, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) { fn test_encrypt_sk(log_n: usize, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) {
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n); let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
@@ -399,14 +415,6 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
); );
} }
#[test]
fn external_product() {
(1..4).for_each(|rank| {
println!("test external_product rank: {}", rank);
test_external_product(12, 12, 60, 45, 60, rank, 3.2);
});
}
fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usize, k_ct_out: usize, rank: usize, sigma: f64) { fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usize, k_ct_out: usize, rank: usize, sigma: f64) {
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n); let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
@@ -490,7 +498,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
let var_a0_err: f64 = sigma * sigma; let var_a0_err: f64 = sigma * sigma;
let var_a1_err: f64 = 1f64 / 12f64; let var_a1_err: f64 = 1f64 / 12f64;
let noise_want: f64 = noise_rgsw_product( let noise_want: f64 = noise_ggsw_gglwe_product(
module.n() as f64, module.n() as f64,
basek, basek,
0.5, 0.5,
@@ -512,14 +520,6 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
); );
} }
#[test]
fn external_product_inplace() {
(1..4).for_each(|rank| {
println!("test external_product rank: {}", rank);
test_external_product_inplace(12, 15, 60, 60, rank, 3.2);
});
}
fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct: usize, rank: usize, sigma: f64) { fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct: usize, rank: usize, sigma: f64) {
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n); let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
let rows: usize = (k_ct + basek - 1) / basek; let rows: usize = (k_ct + basek - 1) / basek;
@@ -595,7 +595,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
let var_a0_err: f64 = sigma * sigma; let var_a0_err: f64 = sigma * sigma;
let var_a1_err: f64 = 1f64 / 12f64; let var_a1_err: f64 = 1f64 / 12f64;
let noise_want: f64 = noise_rgsw_product( let noise_want: f64 = noise_ggsw_gglwe_product(
module.n() as f64, module.n() as f64,
basek, basek,
0.5, 0.5,

View File

@@ -1,438 +1,445 @@
// use crate::{ use crate::{
// elem::Infos, elem::Infos,
// ggsw_ciphertext::GGSWCiphertext, ggsw_ciphertext::GGSWCiphertext,
// glwe_ciphertext::GLWECiphertext, glwe_ciphertext::GLWECiphertext,
// glwe_ciphertext_fourier::GLWECiphertextFourier, glwe_ciphertext_fourier::GLWECiphertextFourier,
// glwe_plaintext::GLWEPlaintext, glwe_plaintext::GLWEPlaintext,
// keys::{SecretKey, SecretKeyFourier}, keys::{SecretKey, SecretKeyFourier},
// keyswitch_key::GLWESwitchingKey, keyswitch_key::GLWESwitchingKey,
// test_fft64::{gglwe::noise_grlwe_rlwe_product, ggsw::noise_rgsw_product}, test_fft64::{gglwe::noise_gglwe_product, ggsw::noise_ggsw_gglwe_product},
// }; };
// use base2k::{FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, VecZnxToMut, ZnxViewMut}; use base2k::{FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, VecZnxToMut, ZnxViewMut};
// use sampling::source::Source; use sampling::source::Source;
//
// #[test] #[test]
// fn keyswitch() { fn keyswitch() {
// let module: Module<FFT64> = Module::<FFT64>::new(2048); (1..4).for_each(|rank_in| {
// let log_base2k: usize = 12; (1..4).for_each(|rank_out| {
// let log_k_grlwe: usize = 60; println!("test keyswitch rank_in: {} rank_out: {}", rank_in, rank_out);
// let log_k_rlwe_in: usize = 45; test_keyswitch(12, 12, 60, 45, 60, rank_in, rank_out, 3.2);
// let log_k_rlwe_out: usize = 60; });
// let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; });
// }
// let rank: usize = 1;
// #[test]
// let sigma: f64 = 3.2; fn keyswitch_inplace() {
// (1..4).for_each(|rank| {
// let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> = println!("test keyswitch_inplace rank: {}", rank);
// GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank); test_keyswitch_inplace(12, 12, 60, 45, rank, 3.2);
// let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, rank); });
// let mut ct_rlwe_in_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = }
// GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in, rank);
// let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out, rank); #[test]
// let mut ct_rlwe_out_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = fn external_product() {
// GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_out, rank); (1..4).for_each(|rank| {
// let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in); println!("test external_product rank: {}", rank);
// let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out); test_external_product(12, 12, 60, 45, 60, rank, 3.2);
// });
// let mut source_xs: Source = Source::new([0u8; 32]); }
// let mut source_xe: Source = Source::new([0u8; 32]);
// let mut source_xa: Source = Source::new([0u8; 32]); #[test]
// fn external_product_inplace() {
// Random input plaintext (1..4).for_each(|rank| {
// pt_want println!("test external_product rank: {}", rank);
// .data test_external_product_inplace(12, 15, 60, 60, rank, 3.2);
// .fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa); });
// }
// let mut scratch: ScratchOwned = ScratchOwned::new(
// GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size()) fn test_keyswitch(
// | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size()) log_n: usize,
// | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe_in.size()) basek: usize,
// | GLWECiphertextFourier::keyswitch_scratch_space( k_ksk: usize,
// &module, k_ct_in: usize,
// ct_rlwe_out.size(), k_ct_out: usize,
// ct_rlwe_in.size(), rank_in: usize,
// ct_grlwe.size(), rank_out: usize,
// ), sigma: f64,
// ); ) {
// let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
// let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
// sk0.fill_ternary_prob(0.5, &mut source_xs); let rows: usize = (k_ct_in + basek - 1) / basek;
//
// let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank); let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, basek, k_ksk, rows, rank_in, rank_out);
// sk0_dft.dft(&module, &sk0); let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct_in, rank_in);
// let mut ct_glwe_dft_in: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ct_in, rank_in);
// let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank); let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct_out, rank_out);
// sk1.fill_ternary_prob(0.5, &mut source_xs); let mut ct_glwe_dft_out: GLWECiphertextFourier<Vec<u8>, FFT64> =
// GLWECiphertextFourier::new(&module, basek, k_ct_out, rank_out);
// let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ct_in);
// sk1_dft.dft(&module, &sk1); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ct_out);
//
// ct_grlwe.encrypt_sk( let mut source_xs: Source = Source::new([0u8; 32]);
// &module, let mut source_xe: Source = Source::new([0u8; 32]);
// &sk0.data, let mut source_xa: Source = Source::new([0u8; 32]);
// &sk1_dft,
// &mut source_xa, // Random input plaintext
// &mut source_xe, pt_want
// sigma, .data
// scratch.borrow(), .fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
// );
// let mut scratch: ScratchOwned = ScratchOwned::new(
// ct_rlwe_in.encrypt_sk( GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ksk.size())
// &module, | GLWECiphertext::decrypt_scratch_space(&module, ct_glwe_out.size())
// &pt_want, | GLWECiphertext::encrypt_sk_scratch_space(&module, ct_glwe_in.size())
// &sk0_dft, | GLWECiphertextFourier::keyswitch_scratch_space(
// &mut source_xa, &module,
// &mut source_xe, ct_glwe_out.size(),
// sigma, ct_glwe_in.size(),
// scratch.borrow(), ksk.size(),
// ); rank_in,
// rank_out,
// ct_rlwe_in.dft(&module, &mut ct_rlwe_in_dft); ),
// ct_rlwe_out_dft.keyswitch(&module, &ct_rlwe_in_dft, &ct_grlwe, scratch.borrow()); );
// ct_rlwe_out_dft.idft(&module, &mut ct_rlwe_out, scratch.borrow());
// let mut sk_in: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_in);
// ct_rlwe_out.decrypt(&module, &mut pt_have, &sk1_dft, scratch.borrow()); sk_in.fill_ternary_prob(0.5, &mut source_xs);
//
// module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0); let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_in);
// sk_in_dft.dft(&module, &sk_in);
// let noise_have: f64 = pt_have.data.std(0, log_base2k).log2();
// let noise_want: f64 = noise_grlwe_rlwe_product( let mut sk_out: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_out);
// module.n() as f64, sk_out.fill_ternary_prob(0.5, &mut source_xs);
// log_base2k,
// 0.5, let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_out);
// 0.5, sk_out_dft.dft(&module, &sk_out);
// 0f64,
// sigma * sigma, ksk.encrypt_sk(
// 0f64, &module,
// log_k_rlwe_in, &sk_in,
// log_k_grlwe, &sk_out_dft,
// ); &mut source_xa,
// &mut source_xe,
// assert!( sigma,
// (noise_have - noise_want).abs() <= 0.1, scratch.borrow(),
// "{} {}", );
// noise_have,
// noise_want ct_glwe_in.encrypt_sk(
// ); &module,
// } &pt_want,
// &sk_in_dft,
// #[test] &mut source_xa,
// fn keyswich_inplace() { &mut source_xe,
// let module: Module<FFT64> = Module::<FFT64>::new(2048); sigma,
// let log_base2k: usize = 12; scratch.borrow(),
// let log_k_grlwe: usize = 60; );
// let log_k_rlwe: usize = 45;
// let rows: usize = (log_k_rlwe + log_base2k - 1) / log_base2k; ct_glwe_in.dft(&module, &mut ct_glwe_dft_in);
// let rank: usize = 1; ct_glwe_dft_out.keyswitch(&module, &ct_glwe_dft_in, &ksk, scratch.borrow());
// ct_glwe_dft_out.idft(&module, &mut ct_glwe_out, scratch.borrow());
// let sigma: f64 = 3.2;
// ct_glwe_out.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
// let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> =
// GLWESwitchingKey::new(&module, log_base2k, log_k_grlwe, rows, rank, rank); module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
// let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe, rank);
// let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = let noise_have: f64 = pt_have.data.std(0, basek).log2();
// GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe, rank); let noise_want: f64 = noise_gglwe_product(
// let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe); module.n() as f64,
// let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe); basek,
// 0.5,
// let mut source_xs: Source = Source::new([0u8; 32]); 0.5,
// let mut source_xe: Source = Source::new([0u8; 32]); 0f64,
// let mut source_xa: Source = Source::new([0u8; 32]); sigma * sigma,
// 0f64,
// Random input plaintext rank_in as f64,
// pt_want k_ct_in,
// .data k_ksk,
// .fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa); );
//
// let mut scratch: ScratchOwned = ScratchOwned::new( assert!(
// GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size()) (noise_have - noise_want).abs() <= 0.1,
// | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size()) "{} {}",
// | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe.size()) noise_have,
// | GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, ct_rlwe_dft.size(), ct_grlwe.size()), noise_want
// ); );
// }
// let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
// sk0.fill_ternary_prob(0.5, &mut source_xs); fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize, rank: usize, sigma: f64) {
// let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
// let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank); let rows: usize = (k_ct + basek - 1) / basek;
// sk0_dft.dft(&module, &sk0);
// let mut ksk: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, basek, k_ksk, rows, rank, rank);
// let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank); let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct, rank);
// sk1.fill_ternary_prob(0.5, &mut source_xs); let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ct, rank);
// let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ct);
// let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ct);
// sk1_dft.dft(&module, &sk1);
// let mut source_xs: Source = Source::new([0u8; 32]);
// ct_grlwe.encrypt_sk( let mut source_xe: Source = Source::new([0u8; 32]);
// &module, let mut source_xa: Source = Source::new([0u8; 32]);
// &sk0.data,
// &sk1_dft, // Random input plaintext
// &mut source_xa, pt_want
// &mut source_xe, .data
// sigma, .fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
// scratch.borrow(),
// ); let mut scratch: ScratchOwned = ScratchOwned::new(
// GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ksk.size())
// ct_rlwe.encrypt_sk( | GLWECiphertext::decrypt_scratch_space(&module, ct_glwe.size())
// &module, | GLWECiphertext::encrypt_sk_scratch_space(&module, ct_glwe.size())
// &pt_want, | GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, ct_rlwe_dft.size(), ksk.size(), rank),
// &sk0_dft, );
// &mut source_xa,
// &mut source_xe, let mut sk_in: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
// sigma, sk_in.fill_ternary_prob(0.5, &mut source_xs);
// scratch.borrow(),
// ); let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
// sk_in_dft.dft(&module, &sk_in);
// ct_rlwe.dft(&module, &mut ct_rlwe_dft);
// ct_rlwe_dft.keyswitch_inplace(&module, &ct_grlwe, scratch.borrow()); let mut sk_out: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
// ct_rlwe_dft.idft(&module, &mut ct_rlwe, scratch.borrow()); sk_out.fill_ternary_prob(0.5, &mut source_xs);
//
// ct_rlwe.decrypt(&module, &mut pt_have, &sk1_dft, scratch.borrow()); let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
// sk_out_dft.dft(&module, &sk_out);
// module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
// ksk.encrypt_sk(
// let noise_have: f64 = pt_have.data.std(0, log_base2k).log2(); &module,
// let noise_want: f64 = noise_grlwe_rlwe_product( &sk_in,
// module.n() as f64, &sk_out_dft,
// log_base2k, &mut source_xa,
// 0.5, &mut source_xe,
// 0.5, sigma,
// 0f64, scratch.borrow(),
// sigma * sigma, );
// 0f64,
// log_k_rlwe, ct_glwe.encrypt_sk(
// log_k_grlwe, &module,
// ); &pt_want,
// &sk_in_dft,
// assert!( &mut source_xa,
// (noise_have - noise_want).abs() <= 0.1, &mut source_xe,
// "{} {}", sigma,
// noise_have, scratch.borrow(),
// noise_want );
// );
// } ct_glwe.dft(&module, &mut ct_rlwe_dft);
// ct_rlwe_dft.keyswitch_inplace(&module, &ksk, scratch.borrow());
// #[test] ct_rlwe_dft.idft(&module, &mut ct_glwe, scratch.borrow());
// fn external_product() {
// let module: Module<FFT64> = Module::<FFT64>::new(2048); ct_glwe.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
// let log_base2k: usize = 12;
// let log_k_grlwe: usize = 60; module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
// let log_k_rlwe_in: usize = 45;
// let log_k_rlwe_out: usize = 60; let noise_have: f64 = pt_have.data.std(0, basek).log2();
// let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; let noise_want: f64 = noise_gglwe_product(
// let rank: usize = 1; module.n() as f64,
// basek,
// let sigma: f64 = 3.2; 0.5,
// 0.5,
// let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows, rank); 0f64,
// let mut ct_rlwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, rank); sigma * sigma,
// let mut ct_rlwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_out, rank); 0f64,
// let mut ct_rlwe_dft_in: GLWECiphertextFourier<Vec<u8>, FFT64> = rank as f64,
// GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in, rank); k_ct,
// let mut ct_rlwe_dft_out: GLWECiphertextFourier<Vec<u8>, FFT64> = k_ksk,
// GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_out, rank); );
// let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
// let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in); assert!(
// let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out); (noise_have - noise_want).abs() <= 0.1,
// "{} {}",
// let mut source_xs: Source = Source::new([0u8; 32]); noise_have,
// let mut source_xe: Source = Source::new([0u8; 32]); noise_want
// let mut source_xa: Source = Source::new([0u8; 32]); );
// }
// Random input plaintext
// pt_want fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usize, k_ct_out: usize, rank: usize, sigma: f64) {
// .data let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
// .fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa);
// let rows: usize = (k_ct_in + basek - 1) / basek;
// pt_want.to_mut().at_mut(0, 0)[1] = 1;
// let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, k_ggsw, rows, rank);
// let k: usize = 1; let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct_in, rank);
// let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct_out, rank);
// pt_rgsw.raw_mut()[k] = 1; // X^{k} let mut ct_in_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ct_in, rank);
// let mut ct_out_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ct_out, rank);
// let mut scratch: ScratchOwned = ScratchOwned::new( let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
// GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size()) let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ct_in);
// | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe_out.size()) let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ct_out);
// | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe_in.size())
// | GLWECiphertext::external_product_scratch_space( let mut source_xs: Source = Source::new([0u8; 32]);
// &module, let mut source_xe: Source = Source::new([0u8; 32]);
// ct_rlwe_out.size(), let mut source_xa: Source = Source::new([0u8; 32]);
// ct_rlwe_in.size(),
// ct_rgsw.size(), // Random input plaintext
// ), pt_want
// ); .data
// .fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
// let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
// sk.fill_ternary_prob(0.5, &mut source_xs); pt_want.to_mut().at_mut(0, 0)[1] = 1;
//
// let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank); let k: usize = 1;
// sk_dft.dft(&module, &sk);
// pt_rgsw.raw_mut()[k] = 1; // X^{k}
// ct_rgsw.encrypt_sk(
// &module, let mut scratch: ScratchOwned = ScratchOwned::new(
// &pt_rgsw, GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
// &sk_dft, | GLWECiphertext::decrypt_scratch_space(&module, ct_out.size())
// &mut source_xa, | GLWECiphertext::encrypt_sk_scratch_space(&module, ct_in.size())
// &mut source_xe, | GLWECiphertextFourier::external_product_scratch_space(&module, ct_out.size(), ct_in.size(), ct_rgsw.size(), rank),
// sigma, );
// scratch.borrow(),
// ); let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
// sk.fill_ternary_prob(0.5, &mut source_xs);
// ct_rlwe_in.encrypt_sk(
// &module, let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
// &pt_want, sk_dft.dft(&module, &sk);
// &sk_dft,
// &mut source_xa, ct_rgsw.encrypt_sk(
// &mut source_xe, &module,
// sigma, &pt_rgsw,
// scratch.borrow(), &sk_dft,
// ); &mut source_xa,
// &mut source_xe,
// ct_rlwe_in.dft(&module, &mut ct_rlwe_dft_in); sigma,
// ct_rlwe_dft_out.external_product(&module, &ct_rlwe_dft_in, &ct_rgsw, scratch.borrow()); scratch.borrow(),
// ct_rlwe_dft_out.idft(&module, &mut ct_rlwe_out, scratch.borrow()); );
//
// ct_rlwe_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); ct_in.encrypt_sk(
// &module,
// module.vec_znx_rotate_inplace(k as i64, &mut pt_want, 0); &pt_want,
// &sk_dft,
// module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0); &mut source_xa,
// &mut source_xe,
// let noise_have: f64 = pt_have.data.std(0, log_base2k).log2(); sigma,
// scratch.borrow(),
// let var_gct_err_lhs: f64 = sigma * sigma; );
// let var_gct_err_rhs: f64 = 0f64;
// ct_in.dft(&module, &mut ct_in_dft);
// let var_msg: f64 = 1f64 / module.n() as f64; // X^{k} ct_out_dft.external_product(&module, &ct_in_dft, &ct_rgsw, scratch.borrow());
// let var_a0_err: f64 = sigma * sigma; ct_out_dft.idft(&module, &mut ct_out, scratch.borrow());
// let var_a1_err: f64 = 1f64 / 12f64;
// ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
// let noise_want: f64 = noise_rgsw_product(
// module.n() as f64, module.vec_znx_rotate_inplace(k as i64, &mut pt_want, 0);
// log_base2k,
// 0.5, module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
// var_msg,
// var_a0_err, let noise_have: f64 = pt_have.data.std(0, basek).log2();
// var_a1_err,
// var_gct_err_lhs, let var_gct_err_lhs: f64 = sigma * sigma;
// var_gct_err_rhs, let var_gct_err_rhs: f64 = 0f64;
// log_k_rlwe_in,
// log_k_grlwe, let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
// ); let var_a0_err: f64 = sigma * sigma;
// let var_a1_err: f64 = 1f64 / 12f64;
// assert!(
// (noise_have - noise_want).abs() <= 0.1, let noise_want: f64 = noise_ggsw_gglwe_product(
// "{} {}", module.n() as f64,
// noise_have, basek,
// noise_want 0.5,
// ); var_msg,
// } var_a0_err,
// var_a1_err,
// #[test] var_gct_err_lhs,
// fn external_product_inplace() { var_gct_err_rhs,
// let module: Module<FFT64> = Module::<FFT64>::new(2048); rank as f64,
// let log_base2k: usize = 12; k_ct_in,
// let log_k_grlwe: usize = 60; k_ggsw,
// let log_k_rlwe_in: usize = 45; );
// let log_k_rlwe_out: usize = 60;
// let rows: usize = (log_k_rlwe_in + log_base2k - 1) / log_base2k; assert!(
// let rank: usize = 1; (noise_have - noise_want).abs() <= 0.1,
// "{} {}",
// let sigma: f64 = 3.2; noise_have,
// noise_want
// let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, log_base2k, log_k_grlwe, rows, rank); );
// let mut ct_rlwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, log_base2k, log_k_rlwe_in, rank); }
// let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> =
// GLWECiphertextFourier::new(&module, log_base2k, log_k_rlwe_in, rank); fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct: usize, rank: usize, sigma: f64) {
// let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1); let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
// let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_in); let rows: usize = (k_ct + basek - 1) / basek;
// let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, log_base2k, log_k_rlwe_out);
// let mut ct_ggsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, k_ggsw, rows, rank);
// let mut source_xs: Source = Source::new([0u8; 32]); let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::new(&module, basek, k_ct, rank);
// let mut source_xe: Source = Source::new([0u8; 32]); let mut ct_rlwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ct, rank);
// let mut source_xa: Source = Source::new([0u8; 32]); let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
// let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ct);
// Random input plaintext let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ct);
// pt_want
// .data let mut source_xs: Source = Source::new([0u8; 32]);
// .fill_uniform(log_base2k, 0, pt_want.size(), &mut source_xa); let mut source_xe: Source = Source::new([0u8; 32]);
// let mut source_xa: Source = Source::new([0u8; 32]);
// pt_want.to_mut().at_mut(0, 0)[1] = 1;
// // Random input plaintext
// let k: usize = 1; pt_want
// .data
// pt_rgsw.raw_mut()[k] = 1; // X^{k} .fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
//
// let mut scratch: ScratchOwned = ScratchOwned::new( pt_want.to_mut().at_mut(0, 0)[1] = 1;
// GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size())
// | GLWECiphertext::decrypt_scratch_space(&module, ct_rlwe.size()) let k: usize = 1;
// | GLWECiphertext::encrypt_sk_scratch_space(&module, rank, ct_rlwe.size())
// | GLWECiphertext::external_product_inplace_scratch_space(&module, ct_rlwe.size(), ct_rgsw.size()), pt_rgsw.raw_mut()[k] = 1; // X^{k}
// );
// let mut scratch: ScratchOwned = ScratchOwned::new(
// let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank); GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_ggsw.size())
// sk.fill_ternary_prob(0.5, &mut source_xs); | GLWECiphertext::decrypt_scratch_space(&module, ct.size())
// | GLWECiphertext::encrypt_sk_scratch_space(&module, ct.size())
// let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank); | GLWECiphertextFourier::external_product_inplace_scratch_space(&module, ct.size(), ct_ggsw.size(), rank),
// sk_dft.dft(&module, &sk); );
//
// ct_rgsw.encrypt_sk( let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
// &module, sk.fill_ternary_prob(0.5, &mut source_xs);
// &pt_rgsw,
// &sk_dft, let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
// &mut source_xa, sk_dft.dft(&module, &sk);
// &mut source_xe,
// sigma, ct_ggsw.encrypt_sk(
// scratch.borrow(), &module,
// ); &pt_rgsw,
// &sk_dft,
// ct_rlwe.encrypt_sk( &mut source_xa,
// &module, &mut source_xe,
// &pt_want, sigma,
// &sk_dft, scratch.borrow(),
// &mut source_xa, );
// &mut source_xe,
// sigma, ct.encrypt_sk(
// scratch.borrow(), &module,
// ); &pt_want,
// &sk_dft,
// ct_rlwe.dft(&module, &mut ct_rlwe_dft); &mut source_xa,
// ct_rlwe_dft.external_product_inplace(&module, &ct_rgsw, scratch.borrow()); &mut source_xe,
// ct_rlwe_dft.idft(&module, &mut ct_rlwe, scratch.borrow()); sigma,
// scratch.borrow(),
// ct_rlwe.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); );
//
// module.vec_znx_rotate_inplace(k as i64, &mut pt_want, 0); ct.dft(&module, &mut ct_rlwe_dft);
// ct_rlwe_dft.external_product_inplace(&module, &ct_ggsw, scratch.borrow());
// module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0); ct_rlwe_dft.idft(&module, &mut ct, scratch.borrow());
//
// let noise_have: f64 = pt_have.data.std(0, log_base2k).log2(); ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
//
// let var_gct_err_lhs: f64 = sigma * sigma; module.vec_znx_rotate_inplace(k as i64, &mut pt_want, 0);
// let var_gct_err_rhs: f64 = 0f64;
// module.vec_znx_sub_ab_inplace(&mut pt_have, 0, &pt_want, 0);
// let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
// let var_a0_err: f64 = sigma * sigma; let noise_have: f64 = pt_have.data.std(0, basek).log2();
// let var_a1_err: f64 = 1f64 / 12f64;
// let var_gct_err_lhs: f64 = sigma * sigma;
// let noise_want: f64 = noise_rgsw_product( let var_gct_err_rhs: f64 = 0f64;
// module.n() as f64,
// log_base2k, let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
// 0.5, let var_a0_err: f64 = sigma * sigma;
// var_msg, let var_a1_err: f64 = 1f64 / 12f64;
// var_a0_err,
// var_a1_err, let noise_want: f64 = noise_ggsw_gglwe_product(
// var_gct_err_lhs, module.n() as f64,
// var_gct_err_rhs, basek,
// log_k_rlwe_in, 0.5,
// log_k_grlwe, var_msg,
// ); var_a0_err,
// var_a1_err,
// assert!( var_gct_err_lhs,
// (noise_have - noise_want).abs() <= 0.1, var_gct_err_rhs,
// "{} {}", rank as f64,
// noise_have, k_ct,
// noise_want k_ggsw,
// ); );
// }
assert!(
(noise_have - noise_want).abs() <= 0.1,
"{} {}",
noise_have,
noise_want
);
}

View File

@@ -23,7 +23,7 @@ pub(crate) trait VecGLWEProductScratchSpace {
Self::prod_with_glwe_scratch_space(module, res_size, res_size, rhs, rank, rank) Self::prod_with_glwe_scratch_space(module, res_size, res_size, rhs, rank, rank)
} }
fn prod_with_glwe_dft_scratch_space( fn prod_with_glwe_fourier_scratch_space(
module: &Module<FFT64>, module: &Module<FFT64>,
res_size: usize, res_size: usize,
lhs: usize, lhs: usize,
@@ -32,11 +32,11 @@ pub(crate) trait VecGLWEProductScratchSpace {
rank_out: usize, rank_out: usize,
) -> usize { ) -> usize {
(Self::prod_with_glwe_scratch_space(module, res_size, lhs, rhs, rank_in, rank_out) | module.vec_znx_idft_tmp_bytes()) (Self::prod_with_glwe_scratch_space(module, res_size, lhs, rhs, rank_in, rank_out) | module.vec_znx_idft_tmp_bytes())
+ module.bytes_of_vec_znx(rank_in, lhs) + module.bytes_of_vec_znx(rank_in + 1, lhs)
+ module.bytes_of_vec_znx(rank_out, res_size) + module.bytes_of_vec_znx(rank_out + 1, res_size)
} }
fn prod_with_glwe_dft_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize, rank: usize) -> usize { fn prod_with_glwe_fourier_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize, rank: usize) -> usize {
(Self::prod_with_glwe_inplace_scratch_space(module, res_size, rhs, rank) | module.vec_znx_idft_tmp_bytes()) (Self::prod_with_glwe_inplace_scratch_space(module, res_size, rhs, rank) | module.vec_znx_idft_tmp_bytes())
+ module.bytes_of_vec_znx(rank + 1, res_size) + module.bytes_of_vec_znx(rank + 1, res_size)
} }
@@ -49,13 +49,13 @@ pub(crate) trait VecGLWEProductScratchSpace {
rank_in: usize, rank_in: usize,
rank_out: usize, rank_out: usize,
) -> usize { ) -> usize {
Self::prod_with_glwe_dft_scratch_space(module, res_size, lhs, rhs, rank_in, rank_out) Self::prod_with_glwe_fourier_scratch_space(module, res_size, lhs, rhs, rank_in, rank_out)
+ module.bytes_of_vec_znx_dft(rank_in + 1, lhs) + module.bytes_of_vec_znx_dft(rank_in + 1, lhs)
+ module.bytes_of_vec_znx_dft(rank_out + 1, res_size) + module.bytes_of_vec_znx_dft(rank_out + 1, res_size)
} }
fn prod_with_vec_glwe_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize, rank: usize) -> usize { fn prod_with_vec_glwe_inplace_scratch_space(module: &Module<FFT64>, res_size: usize, rhs: usize, rank: usize) -> usize {
Self::prod_with_glwe_dft_inplace_scratch_space(module, res_size, rhs, rank) Self::prod_with_glwe_fourier_inplace_scratch_space(module, res_size, rhs, rank)
+ module.bytes_of_vec_znx_dft(rank + 1, res_size) + module.bytes_of_vec_znx_dft(rank + 1, res_size)
} }
} }