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