From de4a5e9bfcef74fbd348da262c42626d7c26af27 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Fri, 30 May 2025 15:50:01 +0200 Subject: [PATCH 1/2] refactored secret key --- backend/src/scalar_znx_dft_ops.rs | 21 +++- core/benches/external_product_glwe_fft64.rs | 27 ++-- core/benches/keyswitch_glwe_fft64.rs | 39 ++---- core/src/automorphism.rs | 49 +++----- core/src/gglwe_ciphertext.rs | 60 +++------ core/src/ggsw_ciphertext.rs | 19 +-- core/src/glwe_ciphertext.rs | 36 +++--- core/src/glwe_ciphertext_fourier.rs | 17 ++- core/src/{keys.rs => glwe_keys.rs} | 93 ++++---------- core/src/glwe_ops.rs | 5 +- core/src/glwe_packing.rs | 2 +- core/src/glwe_plaintext.rs | 7 +- core/src/keyswitch_key.rs | 15 +-- core/src/lib.rs | 28 ++--- core/src/tensor_key.rs | 41 +++--- core/src/test_fft64/automorphism_key.rs | 34 ++--- core/src/test_fft64/gglwe.rs | 119 ++++++------------ core/src/test_fft64/ggsw.rs | 129 ++++++++----------- core/src/test_fft64/glwe.rs | 130 +++++++------------- core/src/test_fft64/glwe_fourier.rs | 75 ++++------- core/src/test_fft64/glwe_packing.rs | 22 +--- core/src/test_fft64/tensor_key.rs | 43 +++---- core/src/test_fft64/trace.rs | 20 +-- core/src/trace.rs | 7 +- 24 files changed, 358 insertions(+), 680 deletions(-) rename core/src/{keys.rs => glwe_keys.rs} (62%) diff --git a/backend/src/scalar_znx_dft_ops.rs b/backend/src/scalar_znx_dft_ops.rs index 6bb7039..6d227c7 100644 --- a/backend/src/scalar_znx_dft_ops.rs +++ b/backend/src/scalar_znx_dft_ops.rs @@ -2,8 +2,8 @@ use crate::ffi::svp; use crate::ffi::vec_znx_dft::vec_znx_dft_t; use crate::znx_base::{ZnxInfos, ZnxView, ZnxViewMut}; use crate::{ - Backend, FFT64, Module, ScalarZnxDft, ScalarZnxDftOwned, ScalarZnxDftToMut, ScalarZnxDftToRef, ScalarZnxToRef, VecZnxDft, - VecZnxDftToMut, VecZnxDftToRef, + Backend, FFT64, Module, ScalarZnx, ScalarZnxDft, ScalarZnxDftOwned, ScalarZnxDftToMut, ScalarZnxDftToRef, ScalarZnxToMut, + ScalarZnxToRef, Scratch, VecZnxDft, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef, VecZnxOps, }; pub trait ScalarZnxDftAlloc { @@ -28,6 +28,11 @@ pub trait ScalarZnxDftOps { where R: VecZnxDftToMut, A: ScalarZnxDftToRef; + + fn svp_idft(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch) + where + R: ScalarZnxToMut, + A: ScalarZnxDftToRef; } impl ScalarZnxDftAlloc for Module { @@ -45,6 +50,18 @@ impl ScalarZnxDftAlloc for Module { } impl ScalarZnxDftOps for Module { + fn svp_idft(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch) + where + R: ScalarZnxToMut, + A: ScalarZnxDftToRef, + { + let res_mut: &mut ScalarZnx<&mut [u8]> = &mut res.to_mut(); + let a_ref: &ScalarZnxDft<&[u8], FFT64> = &a.to_ref(); + let (mut vec_znx_big, scratch1) = scratch.tmp_vec_znx_big(self, 1, 1); + self.vec_znx_idft(&mut vec_znx_big, 0, a_ref, a_col, scratch1); + self.vec_znx_copy(res_mut, res_col, &vec_znx_big.to_vec_znx_small(), 0); + } + fn svp_prepare(&self, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: ScalarZnxDftToMut, diff --git a/core/benches/external_product_glwe_fft64.rs b/core/benches/external_product_glwe_fft64.rs index bb9f268..2a51387 100644 --- a/core/benches/external_product_glwe_fft64.rs +++ b/core/benches/external_product_glwe_fft64.rs @@ -1,10 +1,5 @@ use backend::{FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned}; -use core::{ - elem::Infos, - ggsw_ciphertext::GGSWCiphertext, - glwe_ciphertext::GLWECiphertext, - keys::{SecretKey, SecretKeyFourier}, -}; +use core::{GGSWCiphertext, GLWECiphertext, GLWESecret, Infos}; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use sampling::source::Source; use std::hint::black_box; @@ -55,15 +50,13 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_ggsw.encrypt_sk( &module, &pt_rgsw, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -72,7 +65,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { ct_glwe_in.encrypt_zero_sk( &module, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -143,15 +136,13 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_ggsw.encrypt_sk( &module, &pt_rgsw, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -160,7 +151,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { ct_glwe.encrypt_zero_sk( &module, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, diff --git a/core/benches/keyswitch_glwe_fft64.rs b/core/benches/keyswitch_glwe_fft64.rs index cee1bd5..1841d5b 100644 --- a/core/benches/keyswitch_glwe_fft64.rs +++ b/core/benches/keyswitch_glwe_fft64.rs @@ -1,10 +1,5 @@ use backend::{FFT64, Module, ScratchOwned}; -use core::{ - elem::Infos, - glwe_ciphertext::GLWECiphertext, - keys::{SecretKey, SecretKeyFourier}, - keyswitch_key::GLWESwitchingKey, -}; +use core::{GLWECiphertext, GLWESecret, GLWESwitchingKey, Infos}; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use sampling::source::Source; use std::hint::black_box; @@ -57,20 +52,16 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank_in); - sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_in); - sk_in_dft.dft(&module, &sk_in); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_in); + sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank_out); - sk_out.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out); + sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs); ksk.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -79,7 +70,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { ct_in.encrypt_zero_sk( &module, - &sk_in_dft, + &sk_in, &mut source_xa, &mut source_xe, sigma, @@ -150,20 +141,16 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank); - sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_in_dft.dft(&module, &sk_in); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk_in.fill_ternary_prob(&&module, 0.5, &mut source_xs); - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank); - sk_out.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk_out.fill_ternary_prob(&&module, 0.5, &mut source_xs); ksk.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -172,7 +159,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { ct.encrypt_zero_sk( &module, - &sk_in_dft, + &sk_in, &mut source_xa, &mut source_xe, sigma, diff --git a/core/src/automorphism.rs b/core/src/automorphism.rs index 28daa56..339535c 100644 --- a/core/src/automorphism.rs +++ b/core/src/automorphism.rs @@ -1,12 +1,9 @@ -use backend::{ - Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, ScalarZnxDftAlloc, ScalarZnxDftOps, ScalarZnxOps, Scratch, VecZnx, - VecZnxDftOps, VecZnxOps, ZnxZero, -}; +use backend::{Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, ScalarZnxOps, Scratch, VecZnx, VecZnxDftOps, VecZnxOps, ZnxZero}; use sampling::source::Source; use crate::{ - GGLWECiphertext, GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWESwitchingKey, GetRow, Infos, ScratchCore, - SecretKey, SetRow, + GGLWECiphertext, GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWESecret, GLWESwitchingKey, GetRow, Infos, + ScratchCore, SetRow, }; pub struct AutomorphismKey { @@ -87,7 +84,7 @@ impl + AsRef<[u8]>> SetRow for AutomorphismKey { impl AutomorphismKey, FFT64> { pub fn generate_from_sk_scratch_space(module: &Module, basek: usize, k: usize, rank: usize) -> usize { - GGLWECiphertext::generate_from_sk_scratch_space(module, basek, k, rank) + module.bytes_of_scalar_znx_dft(rank) + GGLWECiphertext::generate_from_sk_scratch_space(module, basek, k, rank) + GLWESecret::bytes_of(module, rank) } pub fn generate_from_pk_scratch_space(module: &Module, _basek: usize, _k: usize, _rank: usize) -> usize { @@ -167,7 +164,7 @@ impl + AsRef<[u8]>> AutomorphismKey { &mut self, module: &Module, p: i64, - sk: &SecretKey, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -191,31 +188,21 @@ impl + AsRef<[u8]>> AutomorphismKey { ) } - let (mut sk_out_dft, scratch_1) = scratch.tmp_sk_fourier(module, sk.rank()); + let (mut sk_out, scratch_1) = scratch.tmp_sk(module, sk.rank()); + (0..self.rank()).for_each(|i| { + module.scalar_znx_automorphism( + module.galois_element_inv(p), + &mut sk_out.data, + i, + &sk.data, + i, + ); + }); - { - (0..self.rank()).for_each(|i| { - let (mut sk_inv_auto, _) = scratch_1.tmp_scalar_znx(module, 1); - module.scalar_znx_automorphism( - module.galois_element_inv(p), - &mut sk_inv_auto, - 0, - &sk.data, - i, - ); - module.svp_prepare(&mut sk_out_dft.data, i, &sk_inv_auto, 0); - }); - } + sk_out.prep_fourier(module); - self.key.generate_from_sk( - module, - &sk, - &sk_out_dft, - source_xa, - source_xe, - sigma, - scratch_1, - ); + self.key + .generate_from_sk(module, &sk, &sk_out, source_xa, source_xe, sigma, scratch_1); self.p = p; } diff --git a/core/src/gglwe_ciphertext.rs b/core/src/gglwe_ciphertext.rs index c18f08c..0ab99ed 100644 --- a/core/src/gglwe_ciphertext.rs +++ b/core/src/gglwe_ciphertext.rs @@ -4,7 +4,7 @@ use backend::{ }; use sampling::source::Source; -use crate::{GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext, GetRow, Infos, SecretKeyFourier, SetRow, derive_size}; +use crate::{GLWECiphertext, GLWECiphertextFourier, GLWESecret, GetRow, Infos, ScratchCore, SetRow, derive_size}; pub struct GGLWECiphertext { pub(crate) data: MatZnxDft, @@ -75,7 +75,7 @@ impl + AsRef<[u8]>> GGLWECiphertext { &mut self, module: &Module, pt: &ScalarZnx, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -84,9 +84,9 @@ impl + AsRef<[u8]>> GGLWECiphertext { #[cfg(debug_assertions)] { assert_eq!(self.rank_in(), pt.cols()); - assert_eq!(self.rank_out(), sk_dft.rank()); + assert_eq!(self.rank_out(), sk.rank()); assert_eq!(self.n(), module.n()); - assert_eq!(sk_dft.n(), module.n()); + assert_eq!(sk.n(), module.n()); assert_eq!(pt.n(), module.n()); assert!( scratch.available() @@ -101,34 +101,14 @@ impl + AsRef<[u8]>> GGLWECiphertext { } let rows: usize = self.rows(); - let size: usize = self.size(); let basek: usize = self.basek(); let k: usize = self.k(); + let rank_in: usize = self.rank_in(); + let rank_out: usize = self.rank_out(); - let cols_in: usize = self.rank_in(); - let cols_out: usize = self.rank_out() + 1; - - let (tmp_znx_pt, scrach_1) = scratch.tmp_vec_znx(module, 1, size); - let (tmp_znx_ct, scrach_2) = scrach_1.tmp_vec_znx(module, cols_out, size); - let (tmp_znx_dft_ct, scratch_3) = scrach_2.tmp_vec_znx_dft(module, cols_out, size); - - let mut vec_znx_pt: GLWEPlaintext<&mut [u8]> = GLWEPlaintext { - data: tmp_znx_pt, - basek, - k, - }; - - let mut vec_znx_ct: GLWECiphertext<&mut [u8]> = GLWECiphertext { - data: tmp_znx_ct, - basek, - k, - }; - - let mut vec_znx_ct_dft: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier { - data: tmp_znx_dft_ct, - basek, - k, - }; + let (mut tmp_pt, scrach_1) = scratch.tmp_glwe_pt(module, basek, k); + let (mut tmp_ct, scrach_2) = scrach_1.tmp_glwe_ct(module, basek, k, rank_out); + let (mut tmp_ct_dft, scratch_3) = scrach_2.tmp_glwe_fourier(module, basek, k, rank_out); // For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns // @@ -141,29 +121,21 @@ impl + AsRef<[u8]>> GGLWECiphertext { // // (-(a*s) + s0, a) // (-(b*s) + s1, b) - (0..cols_in).for_each(|col_i| { + (0..rank_in).for_each(|col_i| { (0..rows).for_each(|row_i| { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - vec_znx_pt.data.zero(); // zeroes for next iteration - module.vec_znx_add_scalar_inplace(&mut vec_znx_pt.data, 0, row_i, pt, col_i); // Selects the i-th - module.vec_znx_normalize_inplace(basek, &mut vec_znx_pt.data, 0, scratch_3); + tmp_pt.data.zero(); // zeroes for next iteration + module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, row_i, pt, col_i); // Selects the i-th + module.vec_znx_normalize_inplace(basek, &mut tmp_pt.data, 0, scratch_3); // rlwe encrypt of vec_znx_pt into vec_znx_ct - vec_znx_ct.encrypt_sk( - module, - &vec_znx_pt, - sk_dft, - source_xa, - source_xe, - sigma, - scratch_3, - ); + tmp_ct.encrypt_sk(module, &tmp_pt, sk, source_xa, source_xe, sigma, scratch_3); // Switch vec_znx_ct into DFT domain - vec_znx_ct.dft(module, &mut vec_znx_ct_dft); + tmp_ct.dft(module, &mut tmp_ct_dft); // Stores vec_znx_dft_ct into thw i-th row of the MatZnxDft - module.vmp_prepare_row(&mut self.data, row_i, col_i, &vec_znx_ct_dft.data); + self.set_row(module, row_i, col_i, &tmp_ct_dft); }); }); } diff --git a/core/src/ggsw_ciphertext.rs b/core/src/ggsw_ciphertext.rs index 6de21c9..062954c 100644 --- a/core/src/ggsw_ciphertext.rs +++ b/core/src/ggsw_ciphertext.rs @@ -6,15 +6,8 @@ use backend::{ use sampling::source::Source; use crate::{ - ScratchCore, - automorphism::AutomorphismKey, - elem::{GetRow, Infos, SetRow}, - glwe_ciphertext::GLWECiphertext, - glwe_ciphertext_fourier::GLWECiphertextFourier, - keys::SecretKeyFourier, - keyswitch_key::GLWESwitchingKey, - tensor_key::TensorKey, - utils::derive_size, + AutomorphismKey, GLWECiphertext, GLWECiphertextFourier, GLWESecret, GLWESwitchingKey, GetRow, Infos, ScratchCore, SetRow, + TensorKey, derive_size, }; pub struct GGSWCiphertext { @@ -189,7 +182,7 @@ impl + AsRef<[u8]>> GGSWCiphertext { &mut self, module: &Module, pt: &ScalarZnx, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -197,10 +190,10 @@ impl + AsRef<[u8]>> GGSWCiphertext { ) { #[cfg(debug_assertions)] { - assert_eq!(self.rank(), sk_dft.rank()); + assert_eq!(self.rank(), sk.rank()); assert_eq!(self.n(), module.n()); assert_eq!(pt.n(), module.n()); - assert_eq!(sk_dft.n(), module.n()); + assert_eq!(sk.n(), module.n()); } let basek: usize = self.basek(); @@ -223,7 +216,7 @@ impl + AsRef<[u8]>> GGSWCiphertext { tmp_ct.encrypt_sk_private( module, Some((&tmp_pt, col_j)), - sk_dft, + sk, source_xa, source_xe, sigma, diff --git a/core/src/glwe_ciphertext.rs b/core/src/glwe_ciphertext.rs index 50d988d..d5f8c47 100644 --- a/core/src/glwe_ciphertext.rs +++ b/core/src/glwe_ciphertext.rs @@ -6,16 +6,8 @@ use backend::{ use sampling::source::Source; use crate::{ - SIX_SIGMA, - automorphism::AutomorphismKey, - elem::{Infos, SetMetaData}, - ggsw_ciphertext::GGSWCiphertext, - glwe_ciphertext_fourier::GLWECiphertextFourier, - glwe_ops::GLWEOps, - glwe_plaintext::GLWEPlaintext, - keys::{GLWEPublicKey, SecretDistribution, SecretKeyFourier}, - keyswitch_key::GLWESwitchingKey, - utils::derive_size, + AutomorphismKey, GGSWCiphertext, GLWECiphertextFourier, GLWEOps, GLWEPlaintext, GLWEPublicKey, GLWESecret, GLWESwitchingKey, + Infos, SIX_SIGMA, SecretDistribution, SetMetaData, derive_size, }; pub struct GLWECiphertext { @@ -205,7 +197,7 @@ impl + AsMut<[u8]>> GLWECiphertext { &mut self, module: &Module, pt: &GLWEPlaintext, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -214,7 +206,7 @@ impl + AsMut<[u8]>> GLWECiphertext { self.encrypt_sk_private( module, Some((pt, 0)), - sk_dft, + sk, source_xa, source_xe, sigma, @@ -225,7 +217,7 @@ impl + AsMut<[u8]>> GLWECiphertext { pub fn encrypt_zero_sk>( &mut self, module: &Module, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -234,7 +226,7 @@ impl + AsMut<[u8]>> GLWECiphertext { self.encrypt_sk_private( module, None::<(&GLWEPlaintext>, usize)>, - sk_dft, + sk, source_xa, source_xe, sigma, @@ -573,7 +565,7 @@ impl + AsMut<[u8]>> GLWECiphertext { &mut self, module: &Module, pt: Option<(&GLWEPlaintext, usize)>, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -581,8 +573,8 @@ impl + AsMut<[u8]>> GLWECiphertext { ) { #[cfg(debug_assertions)] { - assert_eq!(self.rank(), sk_dft.rank()); - assert_eq!(sk_dft.n(), module.n()); + assert_eq!(self.rank(), sk.rank()); + assert_eq!(sk.n(), module.n()); assert_eq!(self.n(), module.n()); if let Some((pt, col)) = pt { assert_eq!(pt.n(), module.n()); @@ -615,7 +607,7 @@ impl + AsMut<[u8]>> GLWECiphertext { // c[i] = norm(IDFT(DFT(c[i]) * DFT(s[i]))) module.vec_znx_dft(&mut ci_dft, 0, &self.data, i); - module.svp_apply_inplace(&mut ci_dft, 0, &sk_dft.data, i - 1); + module.svp_apply_inplace(&mut ci_dft, 0, &sk.data_fourier, i - 1); let ci_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(ci_dft); // use c[0] as buffer, which is overwritten later by the normalization step @@ -730,15 +722,15 @@ impl> GLWECiphertext { &self, module: &Module, pt: &mut GLWEPlaintext, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, scratch: &mut Scratch, ) { #[cfg(debug_assertions)] { - assert_eq!(self.rank(), sk_dft.rank()); + assert_eq!(self.rank(), sk.rank()); assert_eq!(self.n(), module.n()); assert_eq!(pt.n(), module.n()); - assert_eq!(sk_dft.n(), module.n()); + assert_eq!(sk.n(), module.n()); } let cols: usize = self.rank() + 1; @@ -751,7 +743,7 @@ impl> GLWECiphertext { // ci_dft = DFT(a[i]) * DFT(s[i]) let (mut ci_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, self.size()); // TODO optimize size when pt << ct module.vec_znx_dft(&mut ci_dft, 0, &self.data, i); - module.svp_apply_inplace(&mut ci_dft, 0, &sk_dft.data, i - 1); + module.svp_apply_inplace(&mut ci_dft, 0, &sk.data_fourier, i - 1); let ci_big = module.vec_znx_idft_consume(ci_dft); // c0_big += a[i] * s[i] diff --git a/core/src/glwe_ciphertext_fourier.rs b/core/src/glwe_ciphertext_fourier.rs index 3ae7704..92864d3 100644 --- a/core/src/glwe_ciphertext_fourier.rs +++ b/core/src/glwe_ciphertext_fourier.rs @@ -4,10 +4,7 @@ use backend::{ }; use sampling::source::Source; -use crate::{ - ScratchCore, elem::Infos, ggsw_ciphertext::GGSWCiphertext, glwe_ciphertext::GLWECiphertext, glwe_plaintext::GLWEPlaintext, - keys::SecretKeyFourier, keyswitch_key::GLWESwitchingKey, utils::derive_size, -}; +use crate::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, GLWESwitchingKey, Infos, ScratchCore, derive_size}; pub struct GLWECiphertextFourier { pub data: VecZnxDft, @@ -126,14 +123,14 @@ impl + AsRef<[u8]>> GLWECiphertextFourier pub fn encrypt_zero_sk>( &mut self, module: &Module, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, scratch: &mut Scratch, ) { let (mut tmp_ct, scratch1) = scratch.tmp_glwe_ct(module, self.basek(), self.k(), self.rank()); - tmp_ct.encrypt_zero_sk(module, sk_dft, source_xa, source_xe, sigma, scratch1); + tmp_ct.encrypt_zero_sk(module, sk, source_xa, source_xe, sigma, scratch1); tmp_ct.dft(module, self); } @@ -219,15 +216,15 @@ impl> GLWECiphertextFourier { &self, module: &Module, pt: &mut GLWEPlaintext, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, scratch: &mut Scratch, ) { #[cfg(debug_assertions)] { - assert_eq!(self.rank(), sk_dft.rank()); + assert_eq!(self.rank(), sk.rank()); assert_eq!(self.n(), module.n()); assert_eq!(pt.n(), module.n()); - assert_eq!(sk_dft.n(), module.n()); + assert_eq!(sk.n(), module.n()); } let cols = self.rank() + 1; @@ -238,7 +235,7 @@ impl> GLWECiphertextFourier { { (1..cols).for_each(|i| { let (mut ci_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, self.size()); // TODO optimize size when pt << ct - module.svp_apply(&mut ci_dft, 0, &sk_dft.data, i - 1, &self.data, i); + module.svp_apply(&mut ci_dft, 0, &sk.data_fourier, i - 1, &self.data, i); let ci_big: VecZnxBig<&mut [u8], FFT64> = module.vec_znx_idft_consume(ci_dft); module.vec_znx_big_add_inplace(&mut pt_big, 0, &ci_big, 0); }); diff --git a/core/src/keys.rs b/core/src/glwe_keys.rs similarity index 62% rename from core/src/keys.rs rename to core/src/glwe_keys.rs index beabef9..8f04408 100644 --- a/core/src/keys.rs +++ b/core/src/glwe_keys.rs @@ -4,7 +4,7 @@ use backend::{ }; use sampling::source::Source; -use crate::{elem::Infos, glwe_ciphertext_fourier::GLWECiphertextFourier}; +use crate::{GLWECiphertextFourier, Infos}; #[derive(Clone, Copy, Debug)] pub(crate) enum SecretDistribution { @@ -14,25 +14,27 @@ pub(crate) enum SecretDistribution { NONE, } -pub struct SecretKey { +pub struct GLWESecret { pub(crate) data: ScalarZnx, + pub(crate) data_fourier: ScalarZnxDft, pub(crate) dist: SecretDistribution, } -impl SecretKey> { - pub fn alloc(module: &Module, rank: usize) -> Self { +impl GLWESecret, B> { + pub fn alloc(module: &Module, rank: usize) -> Self { Self { data: module.new_scalar_znx(rank), + data_fourier: module.new_scalar_znx_dft(rank), dist: SecretDistribution::NONE, } } - pub fn bytes_of(module: &Module, rank: usize) -> usize { - module.bytes_of_scalar_znx(rank + 1) + pub fn bytes_of(module: &Module, rank: usize) -> usize { + module.bytes_of_scalar_znx(rank) + module.bytes_of_scalar_znx_dft(rank) } } -impl SecretKey { +impl GLWESecret { pub fn n(&self) -> usize { self.data.n() } @@ -46,18 +48,20 @@ impl SecretKey { } } -impl + AsRef<[u8]>> SecretKey { - pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) { +impl + AsRef<[u8]>> GLWESecret { + pub fn fill_ternary_prob(&mut self, module: &Module, prob: f64, source: &mut Source) { (0..self.rank()).for_each(|i| { self.data.fill_ternary_prob(i, prob, source); }); + self.prep_fourier(module); self.dist = SecretDistribution::TernaryProb(prob); } - pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) { + pub fn fill_ternary_hw(&mut self, module: &Module, hw: usize, source: &mut Source) { (0..self.rank()).for_each(|i| { self.data.fill_ternary_hw(i, hw, source); }); + self.prep_fourier(module); self.dist = SecretDistribution::TernaryFixed(hw); } @@ -65,58 +69,11 @@ impl + AsRef<[u8]>> SecretKey { self.data.zero(); self.dist = SecretDistribution::ZERO; } -} - -pub struct SecretKeyFourier { - pub(crate) data: ScalarZnxDft, - pub(crate) dist: SecretDistribution, -} - -impl SecretKeyFourier { - pub fn n(&self) -> usize { - self.data.n() - } - - pub fn log_n(&self) -> usize { - self.data.log_n() - } - - pub fn rank(&self) -> usize { - self.data.cols() - } -} - -impl SecretKeyFourier, B> { - pub fn alloc(module: &Module, rank: usize) -> Self { - Self { - data: module.new_scalar_znx_dft(rank), - dist: SecretDistribution::NONE, - } - } - - pub fn bytes_of(module: &Module, rank: usize) -> usize { - module.bytes_of_scalar_znx_dft(rank + 1) - } -} - -impl + AsMut<[u8]>> SecretKeyFourier { - pub fn dft>(&mut self, module: &Module, sk: &SecretKey) { - #[cfg(debug_assertions)] - { - match sk.dist { - SecretDistribution::NONE => panic!("invalid sk: SecretDistribution::NONE"), - _ => {} - } - - assert_eq!(self.n(), module.n()); - assert_eq!(sk.n(), module.n()); - assert_eq!(self.rank(), sk.rank()); - } + pub(crate) fn prep_fourier(&mut self, module: &Module) { (0..self.rank()).for_each(|i| { - module.svp_prepare(&mut self.data, i, &sk.data, i); + module.svp_prepare(&mut self.data_fourier, i, &self.data, i); }); - self.dist = sk.dist; } } @@ -164,15 +121,15 @@ impl + AsMut<[u8]>> GLWEPublicKey { pub fn generate_from_sk>( &mut self, module: &Module, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, ) { #[cfg(debug_assertions)] { - match sk_dft.dist { - SecretDistribution::NONE => panic!("invalid sk_dft: SecretDistribution::NONE"), + match sk.dist { + SecretDistribution::NONE => panic!("invalid sk: SecretDistribution::NONE"), _ => {} } } @@ -185,14 +142,8 @@ impl + AsMut<[u8]>> GLWEPublicKey { self.rank(), )); - self.data.encrypt_zero_sk( - module, - sk_dft, - source_xa, - source_xe, - sigma, - scratch.borrow(), - ); - self.dist = sk_dft.dist; + self.data + .encrypt_zero_sk(module, sk, source_xa, source_xe, sigma, scratch.borrow()); + self.dist = sk.dist; } } diff --git a/core/src/glwe_ops.rs b/core/src/glwe_ops.rs index d3c5648..2beffe6 100644 --- a/core/src/glwe_ops.rs +++ b/core/src/glwe_ops.rs @@ -1,9 +1,6 @@ use backend::{FFT64, Module, Scratch, VecZnx, VecZnxOps, ZnxZero}; -use crate::{ - elem::{Infos, SetMetaData}, - glwe_ciphertext::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef}, -}; +use crate::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, Infos, SetMetaData}; pub trait GLWEOps: GLWECiphertextToMut + Infos + SetMetaData { fn add(&mut self, module: &Module, a: &A, b: &B) diff --git a/core/src/glwe_packing.rs b/core/src/glwe_packing.rs index 3b6cdc0..e4d6f9d 100644 --- a/core/src/glwe_packing.rs +++ b/core/src/glwe_packing.rs @@ -1,4 +1,4 @@ -use crate::{ScratchCore, automorphism::AutomorphismKey, elem::Infos, glwe_ciphertext::GLWECiphertext, glwe_ops::GLWEOps}; +use crate::{AutomorphismKey, GLWECiphertext, GLWEOps, Infos, ScratchCore}; use std::collections::HashMap; use backend::{FFT64, Module, Scratch}; diff --git a/core/src/glwe_plaintext.rs b/core/src/glwe_plaintext.rs index 3ffe32c..0aa846f 100644 --- a/core/src/glwe_plaintext.rs +++ b/core/src/glwe_plaintext.rs @@ -1,11 +1,6 @@ use backend::{Backend, FFT64, Module, VecZnx, VecZnxAlloc, VecZnxToMut, VecZnxToRef}; -use crate::{ - elem::{Infos, SetMetaData}, - glwe_ciphertext::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef}, - glwe_ops::GLWEOps, - utils::derive_size, -}; +use crate::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEOps, Infos, SetMetaData, derive_size}; pub struct GLWEPlaintext { pub data: VecZnx, diff --git a/core/src/keyswitch_key.rs b/core/src/keyswitch_key.rs index 10a06c0..0362cd8 100644 --- a/core/src/keyswitch_key.rs +++ b/core/src/keyswitch_key.rs @@ -1,14 +1,7 @@ use backend::{Backend, FFT64, MatZnxDft, MatZnxDftOps, Module, Scratch, ZnxZero}; use sampling::source::Source; -use crate::{ - ScratchCore, - elem::{GetRow, Infos, SetRow}, - gglwe_ciphertext::GGLWECiphertext, - ggsw_ciphertext::GGSWCiphertext, - glwe_ciphertext_fourier::GLWECiphertextFourier, - keys::{SecretKey, SecretKeyFourier}, -}; +use crate::{GGLWECiphertext, GGSWCiphertext, GLWECiphertextFourier, GLWESecret, GetRow, Infos, ScratchCore, SetRow}; pub struct GLWESwitchingKey(pub(crate) GGLWECiphertext); @@ -144,8 +137,8 @@ impl + AsRef<[u8]>> GLWESwitchingKey { pub fn generate_from_sk, DataSkOut: AsRef<[u8]>>( &mut self, module: &Module, - sk_in: &SecretKey, - sk_out_dft: &SecretKeyFourier, + sk_in: &GLWESecret, + sk_out: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -154,7 +147,7 @@ impl + AsRef<[u8]>> GLWESwitchingKey { self.0.encrypt_sk( module, &sk_in.data, - sk_out_dft, + sk_out, source_xa, source_xe, sigma, diff --git a/core/src/lib.rs b/core/src/lib.rs index ab27539..3854e21 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -4,10 +4,10 @@ pub mod gglwe_ciphertext; pub mod ggsw_ciphertext; pub mod glwe_ciphertext; pub mod glwe_ciphertext_fourier; +pub mod glwe_keys; pub mod glwe_ops; pub mod glwe_packing; pub mod glwe_plaintext; -pub mod keys; pub mod keyswitch_key; pub mod tensor_key; #[cfg(test)] @@ -24,10 +24,10 @@ pub use gglwe_ciphertext::*; pub use ggsw_ciphertext::*; pub use glwe_ciphertext::*; pub use glwe_ciphertext_fourier::*; +pub use glwe_keys::*; pub use glwe_ops::*; pub use glwe_packing::*; pub use glwe_plaintext::*; -pub use keys::*; pub use keyswitch_key::*; pub use tensor_key::*; @@ -64,8 +64,7 @@ pub trait ScratchCore { k: usize, rank: usize, ) -> (GLWECiphertextFourier<&mut [u8], B>, &mut Self); - fn tmp_sk(&mut self, module: &Module, rank: usize) -> (SecretKey<&mut [u8]>, &mut Self); - fn tmp_sk_fourier(&mut self, module: &Module, rank: usize) -> (SecretKeyFourier<&mut [u8], B>, &mut Self); + fn tmp_sk(&mut self, module: &Module, rank: usize) -> (GLWESecret<&mut [u8], B>, &mut Self); fn tmp_glwe_pk( &mut self, module: &Module, @@ -184,25 +183,16 @@ impl ScratchCore for Scratch { ) } - fn tmp_sk(&mut self, module: &Module, rank: usize) -> (SecretKey<&mut [u8]>, &mut Self) { - let (data, scratch) = self.tmp_scalar_znx(module, rank + 1); + fn tmp_sk(&mut self, module: &Module, rank: usize) -> (GLWESecret<&mut [u8], FFT64>, &mut Self) { + let (data, scratch) = self.tmp_scalar_znx(module, rank); + let (data_fourier, scratch1) = scratch.tmp_scalar_znx_dft(module, rank); ( - SecretKey { + GLWESecret { data, + data_fourier, dist: SecretDistribution::NONE, }, - scratch, - ) - } - - fn tmp_sk_fourier(&mut self, module: &Module, rank: usize) -> (SecretKeyFourier<&mut [u8], FFT64>, &mut Self) { - let (data, scratch) = self.tmp_scalar_znx_dft(module, rank); - ( - SecretKeyFourier { - data, - dist: SecretDistribution::NONE, - }, - scratch, + scratch1, ) } diff --git a/core/src/tensor_key.rs b/core/src/tensor_key.rs index 47c90eb..fca6b2e 100644 --- a/core/src/tensor_key.rs +++ b/core/src/tensor_key.rs @@ -1,11 +1,7 @@ -use backend::{Backend, FFT64, MatZnxDft, Module, ScalarZnx, ScalarZnxDftAlloc, ScalarZnxDftOps, Scratch, VecZnxDftOps}; +use backend::{Backend, FFT64, MatZnxDft, Module, ScalarZnxDftOps, Scratch}; use sampling::source::Source; -use crate::{ - elem::Infos, - keys::{SecretKey, SecretKeyFourier}, - keyswitch_key::GLWESwitchingKey, -}; +use crate::{GLWESecret, GLWESwitchingKey, Infos, ScratchCore}; pub struct TensorKey { pub(crate) keys: Vec>, @@ -59,7 +55,7 @@ impl TensorKey { impl TensorKey, FFT64> { pub fn generate_from_sk_scratch_space(module: &Module, basek: usize, k: usize, rank: usize) -> usize { - module.bytes_of_scalar_znx_dft(1) + GLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank) + GLWESecret::bytes_of(module, 1) + GLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank) } } @@ -67,7 +63,7 @@ impl + AsRef<[u8]>> TensorKey { pub fn generate_from_sk>( &mut self, module: &Module, - sk_dft: &SecretKeyFourier, + sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, @@ -75,29 +71,28 @@ impl + AsRef<[u8]>> TensorKey { ) { #[cfg(debug_assertions)] { - assert_eq!(self.rank(), sk_dft.rank()); + assert_eq!(self.rank(), sk.rank()); assert_eq!(self.n(), module.n()); - assert_eq!(sk_dft.n(), module.n()); + assert_eq!(sk.n(), module.n()); } let rank: usize = self.rank(); + let (mut sk_ij, scratch1) = scratch.tmp_sk(module, 1); + (0..rank).for_each(|i| { (i..rank).for_each(|j| { - let (mut sk_ij_dft, scratch1) = scratch.tmp_scalar_znx_dft(module, 1); - module.svp_apply(&mut sk_ij_dft, 0, &sk_dft.data, i, &sk_dft.data, j); - let sk_ij: ScalarZnx<&mut [u8]> = module - .vec_znx_idft_consume(sk_ij_dft.as_vec_znx_dft()) - .to_vec_znx_small() - .to_scalar_znx(); - let sk_ij: SecretKey<&mut [u8]> = SecretKey { - data: sk_ij, - dist: sk_dft.dist, - }; - - self.at_mut(i, j).generate_from_sk( - module, &sk_ij, sk_dft, source_xa, source_xe, sigma, scratch1, + module.svp_apply( + &mut sk_ij.data_fourier, + 0, + &sk.data_fourier, + i, + &sk.data_fourier, + j, ); + module.svp_idft(&mut sk_ij.data, 0, &sk_ij.data_fourier, 0, scratch1); + self.at_mut(i, j) + .generate_from_sk(module, &sk_ij, sk, source_xa, source_xe, sigma, scratch1); }); }) } diff --git a/core/src/test_fft64/automorphism_key.rs b/core/src/test_fft64/automorphism_key.rs index 546e13f..784a2da 100644 --- a/core/src/test_fft64/automorphism_key.rs +++ b/core/src/test_fft64/automorphism_key.rs @@ -2,11 +2,7 @@ use backend::{FFT64, Module, ScalarZnxOps, ScratchOwned, Stats, VecZnxOps}; use sampling::source::Source; use crate::{ - automorphism::AutomorphismKey, - elem::{GetRow, Infos}, - glwe_ciphertext_fourier::GLWECiphertextFourier, - glwe_plaintext::GLWEPlaintext, - keys::{SecretKey, SecretKeyFourier}, + AutomorphismKey, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GetRow, Infos, test_fft64::gglwe::log2_std_noise_gglwe_product, }; @@ -44,11 +40,8 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize, | AutomorphismKey::automorphism_scratch_space(&module, basek, k_ksk, k_ksk, k_ksk, rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 auto_key_in.generate_from_sk( @@ -78,7 +71,7 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize, let mut ct_glwe_dft: GLWECiphertextFourier, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ksk, rank); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&module, basek, k_ksk); - let mut sk_auto: SecretKey> = SecretKey::alloc(&module, rank); + let mut sk_auto: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk (0..rank).for_each(|i| { module.scalar_znx_automorphism( @@ -90,14 +83,13 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize, ); }); - let mut sk_auto_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_auto_dft.dft(&module, &sk_auto); + sk_auto.prep_fourier(&module); (0..auto_key_out.rank_in()).for_each(|col_i| { (0..auto_key_out.rows()).for_each(|row_i| { auto_key_out.get_row(&module, row_i, col_i, &mut ct_glwe_dft); - ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto_dft, scratch.borrow()); + ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto, scratch.borrow()); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk.data, col_i); let noise_have: f64 = pt.data.std(0, basek).log2(); @@ -141,11 +133,8 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk | AutomorphismKey::automorphism_inplace_scratch_space(&module, basek, k_ksk, k_ksk, rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 auto_key.generate_from_sk( @@ -175,7 +164,7 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk let mut ct_glwe_dft: GLWECiphertextFourier, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ksk, rank); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&module, basek, k_ksk); - let mut sk_auto: SecretKey> = SecretKey::alloc(&module, rank); + let mut sk_auto: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk (0..rank).for_each(|i| { module.scalar_znx_automorphism( @@ -187,14 +176,13 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk ); }); - let mut sk_auto_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_auto_dft.dft(&module, &sk_auto); + sk_auto.prep_fourier(&module); (0..auto_key.rank_in()).for_each(|col_i| { (0..auto_key.rows()).for_each(|row_i| { auto_key.get_row(&module, row_i, col_i, &mut ct_glwe_dft); - ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto_dft, scratch.borrow()); + ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto, scratch.borrow()); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk.data, col_i); let noise_have: f64 = pt.data.std(0, basek).log2(); diff --git a/core/src/test_fft64/gglwe.rs b/core/src/test_fft64/gglwe.rs index e23e2d5..1f722ba 100644 --- a/core/src/test_fft64/gglwe.rs +++ b/core/src/test_fft64/gglwe.rs @@ -2,12 +2,7 @@ use backend::{FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToMut, ScratchO use sampling::source::Source; use crate::{ - elem::{GetRow, Infos}, - ggsw_ciphertext::GGSWCiphertext, - glwe_ciphertext_fourier::GLWECiphertextFourier, - glwe_plaintext::GLWEPlaintext, - keys::{SecretKey, SecretKeyFourier}, - keyswitch_key::GLWESwitchingKey, + GGSWCiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GetRow, Infos, test_fft64::ggsw::noise_ggsw_product, }; @@ -88,22 +83,16 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in | GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k_ksk), ); - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank_in); - sk_in.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_in); + sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_in); - sk_in_dft.dft(&module, &sk_in); - - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank_out); - sk_out.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out); + sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs); ksk.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -116,7 +105,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in (0..ksk.rank_in()).for_each(|col_i| { (0..ksk.rows()).for_each(|row_i| { ksk.get_row(&module, row_i, col_i, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow()); + ct_glwe_fourier.decrypt(&module, &mut pt, &sk_out, scratch.borrow()); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i); let std_pt: f64 = pt.data.std(0, basek) * (k_ksk as f64).exp2(); assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt); @@ -161,29 +150,20 @@ fn test_key_switch( ), ); - let mut sk0: SecretKey> = SecretKey::alloc(&module, rank_in_s0s1); - sk0.fill_ternary_prob(0.5, &mut source_xs); + let mut sk0: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_in_s0s1); + sk0.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk0_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_in_s0s1); - sk0_dft.dft(&module, &sk0); + let mut sk1: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1); + sk1.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk1: SecretKey> = SecretKey::alloc(&module, rank_out_s0s1); - sk1.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk1_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1); - sk1_dft.dft(&module, &sk1); - - let mut sk2: SecretKey> = SecretKey::alloc(&module, rank_out_s1s2); - sk2.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk2_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s1s2); - sk2_dft.dft(&module, &sk2); + let mut sk2: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out_s1s2); + sk2.fill_ternary_prob(&module, 0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe_s0s1.generate_from_sk( &module, &sk0, - &sk1_dft, + &sk1, &mut source_xa, &mut source_xe, sigma, @@ -194,7 +174,7 @@ fn test_key_switch( ct_gglwe_s1s2.generate_from_sk( &module, &sk1, - &sk2_dft, + &sk2, &mut source_xa, &mut source_xe, sigma, @@ -211,7 +191,7 @@ fn test_key_switch( (0..ct_gglwe_s0s2.rank_in()).for_each(|col_i| { (0..ct_gglwe_s0s2.rows()).for_each(|row_i| { ct_gglwe_s0s2.get_row(&module, row_i, col_i, &mut ct_glwe_dft); - ct_glwe_dft.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow()); + ct_glwe_dft.decrypt(&module, &mut pt, &sk2, scratch.borrow()); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk0.data, col_i); let noise_have: f64 = pt.data.std(0, basek).log2(); @@ -263,29 +243,20 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, ), ); - let mut sk0: SecretKey> = SecretKey::alloc(&module, rank_in_s0s1); - sk0.fill_ternary_prob(0.5, &mut source_xs); + let mut sk0: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_in_s0s1); + sk0.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk0_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_in_s0s1); - sk0_dft.dft(&module, &sk0); + let mut sk1: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1); + sk1.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk1: SecretKey> = SecretKey::alloc(&module, rank_out_s0s1); - sk1.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk1_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1); - sk1_dft.dft(&module, &sk1); - - let mut sk2: SecretKey> = SecretKey::alloc(&module, rank_out_s0s1); - sk2.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk2_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1); - sk2_dft.dft(&module, &sk2); + let mut sk2: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1); + sk2.fill_ternary_prob(&module, 0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe_s0s1.generate_from_sk( &module, &sk0, - &sk1_dft, + &sk1, &mut source_xa, &mut source_xe, sigma, @@ -296,7 +267,7 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, ct_gglwe_s1s2.generate_from_sk( &module, &sk1, - &sk2_dft, + &sk2, &mut source_xa, &mut source_xe, sigma, @@ -315,7 +286,7 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, (0..ct_gglwe_s0s2.rank_in()).for_each(|col_i| { (0..ct_gglwe_s0s2.rows()).for_each(|row_i| { ct_gglwe_s0s2.get_row(&module, row_i, col_i, &mut ct_glwe_dft); - ct_glwe_dft.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow()); + ct_glwe_dft.decrypt(&module, &mut pt, &sk2, scratch.borrow()); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk0.data, col_i); let noise_have: f64 = pt.data.std(0, basek).log2(); @@ -375,23 +346,17 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_ pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r} - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank_in); - sk_in.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_in); + sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_in); - sk_in_dft.dft(&module, &sk_in); - - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank_out); - sk_out.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out); + sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe_in.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -401,7 +366,7 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_ ct_rgsw.encrypt_sk( &module, &pt_rgsw, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -435,7 +400,7 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_ (0..rank_in).for_each(|col_i| { (0..ct_gglwe_out.rows()).for_each(|row_i| { ct_gglwe_out.get_row(&module, row_i, col_i, &mut ct_glwe_dft); - ct_glwe_dft.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow()); + ct_glwe_dft.decrypt(&module, &mut pt, &sk_out, scratch.borrow()); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i); let noise_have: f64 = pt.data.std(0, basek).log2(); @@ -496,23 +461,17 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6 pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r} - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank_in); - sk_in.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_in); + sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_in); - sk_in_dft.dft(&module, &sk_in); - - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank_out); - sk_out.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out); + sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -522,7 +481,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6 ct_rgsw.encrypt_sk( &module, &pt_rgsw, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -542,7 +501,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6 (0..rank_in).for_each(|col_i| { (0..ct_gglwe.rows()).for_each(|row_i| { ct_gglwe.get_row(&module, row_i, col_i, &mut ct_glwe_dft); - ct_glwe_dft.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow()); + ct_glwe_dft.decrypt(&module, &mut pt, &sk_out, scratch.borrow()); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i); let noise_have: f64 = pt.data.std(0, basek).log2(); diff --git a/core/src/test_fft64/ggsw.rs b/core/src/test_fft64/ggsw.rs index c617a54..0495431 100644 --- a/core/src/test_fft64/ggsw.rs +++ b/core/src/test_fft64/ggsw.rs @@ -5,14 +5,8 @@ use backend::{ use sampling::source::Source; use crate::{ + GGSWCiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GetRow, Infos, TensorKey, automorphism::AutomorphismKey, - elem::{GetRow, Infos}, - ggsw_ciphertext::GGSWCiphertext, - glwe_ciphertext_fourier::GLWECiphertextFourier, - glwe_plaintext::GLWEPlaintext, - keys::{SecretKey, SecretKeyFourier}, - keyswitch_key::GLWESwitchingKey, - tensor_key::TensorKey, }; use super::gglwe::var_noise_gglwe_product; @@ -94,16 +88,13 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize | GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct.encrypt_sk( &module, &pt_scalar, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -121,14 +112,14 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize // mul with sk[col_j-1] if col_j > 0 { module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0); - module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1); + module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1); module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow()); } ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -174,22 +165,16 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64) let var_xs: f64 = 0.5; - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank); - sk_in.fill_ternary_prob(var_xs, &mut source_xs); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk_in.fill_ternary_prob(&module, var_xs, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_in_dft.dft(&module, &sk_in); - - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank); - sk_out.fill_ternary_prob(var_xs, &mut source_xs); - - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk_out.fill_ternary_prob(&module, var_xs, &mut source_xs); ksk.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -197,7 +182,7 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64) ); tsk.generate_from_sk( &module, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -209,7 +194,7 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64) ct_in.encrypt_sk( &module, &pt_scalar, - &sk_in_dft, + &sk_in, &mut source_xa, &mut source_xe, sigma, @@ -229,14 +214,14 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64) // mul with sk[col_j-1] if col_j > 0 { module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0); - module.svp_apply_inplace(&mut pt_dft, 0, &sk_out_dft.data, col_j - 1); + module.svp_apply_inplace(&mut pt_dft, 0, &sk_out.data_fourier, col_j - 1); module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow()); } ct_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow()); + ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -293,22 +278,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig let var_xs: f64 = 0.5; - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank); - sk_in.fill_ternary_prob(var_xs, &mut source_xs); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk_in.fill_ternary_prob(&module, var_xs, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_in_dft.dft(&module, &sk_in); - - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank); - sk_out.fill_ternary_prob(var_xs, &mut source_xs); - - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk_out.fill_ternary_prob(&module, var_xs, &mut source_xs); ksk.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -316,7 +295,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig ); tsk.generate_from_sk( &module, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -328,7 +307,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig ct.encrypt_sk( &module, &pt_scalar, - &sk_in_dft, + &sk_in, &mut source_xa, &mut source_xe, sigma, @@ -348,14 +327,14 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig // mul with sk[col_j-1] if col_j > 0 { module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0); - module.svp_apply_inplace(&mut pt_dft, 0, &sk_out_dft.data, col_j - 1); + module.svp_apply_inplace(&mut pt_dft, 0, &sk_out.data_fourier, col_j - 1); module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow()); } ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow()); + ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -470,11 +449,8 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize, let var_xs: f64 = 0.5; - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(var_xs, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, var_xs, &mut source_xs); auto_key.generate_from_sk( &module, @@ -487,7 +463,7 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize, ); tensor_key.generate_from_sk( &module, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -499,7 +475,7 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize, ct_in.encrypt_sk( &module, &pt_scalar, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -521,14 +497,14 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize, // mul with sk[col_j-1] if col_j > 0 { module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0); - module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1); + module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1); module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow()); } ct_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -583,11 +559,8 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank: let var_xs: f64 = 0.5; - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(var_xs, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, var_xs, &mut source_xs); auto_key.generate_from_sk( &module, @@ -600,7 +573,7 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank: ); tensor_key.generate_from_sk( &module, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -612,7 +585,7 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank: ct.encrypt_sk( &module, &pt_scalar, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -634,14 +607,14 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank: // mul with sk[col_j-1] if col_j > 0 { module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0); - module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1); + module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1); module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow()); } ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -705,16 +678,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize, ), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_ggsw_rhs.encrypt_sk( &module, &pt_ggsw_rhs, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -724,7 +694,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize, ct_ggsw_lhs_in.encrypt_sk( &module, &pt_ggsw_lhs, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -747,13 +717,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize, if col_j > 0 { module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0); - module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1); + module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1); module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow()); } ct_ggsw_lhs_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow()); + ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt.data, 0, &pt_want.data, 0); @@ -818,16 +788,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank | GGSWCiphertext::external_product_inplace_scratch_space(&module, basek, ct_ggsw_lhs.k(), ct_ggsw_rhs.k(), rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_ggsw_rhs.encrypt_sk( &module, &pt_ggsw_rhs, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -837,7 +804,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank ct_ggsw_lhs.encrypt_sk( &module, &pt_ggsw_lhs, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -860,13 +827,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank if col_j > 0 { module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0); - module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1); + module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1); module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0); module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow()); } ct_ggsw_lhs.get_row(&module, row_i, col_j, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow()); + ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt.data, 0, &pt_want.data, 0); diff --git a/core/src/test_fft64/glwe.rs b/core/src/test_fft64/glwe.rs index 921c561..2a2c2b9 100644 --- a/core/src/test_fft64/glwe.rs +++ b/core/src/test_fft64/glwe.rs @@ -6,13 +6,8 @@ use itertools::izip; use sampling::source::Source; use crate::{ + GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWEPublicKey, GLWESecret, Infos, automorphism::AutomorphismKey, - elem::Infos, - ggsw_ciphertext::GGSWCiphertext, - glwe_ciphertext::GLWECiphertext, - glwe_ciphertext_fourier::GLWECiphertextFourier, - glwe_plaintext::GLWEPlaintext, - keys::{GLWEPublicKey, SecretKey, SecretKeyFourier}, keyswitch_key::GLWESwitchingKey, test_fft64::{gglwe::log2_std_noise_gglwe_product, ggsw::noise_ggsw_product}, }; @@ -106,11 +101,8 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma: | GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k()), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); let mut data_want: Vec = vec![0i64; module.n()]; @@ -123,7 +115,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma: ct.encrypt_sk( &module, &pt, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -132,7 +124,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma: pt.data.zero(); - ct.decrypt(&module, &mut pt, &sk_dft, scratch.borrow()); + ct.decrypt(&module, &mut pt, &sk, scratch.borrow()); let mut data_have: Vec = vec![0i64; module.n()]; @@ -161,10 +153,8 @@ fn test_encrypt_zero_sk(log_n: usize, basek: usize, ct_k: usize, sigma: f64, ran let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); let mut ct_dft: GLWECiphertextFourier, FFT64> = GLWECiphertextFourier::alloc(&module, basek, ct_k, rank); @@ -175,13 +165,13 @@ fn test_encrypt_zero_sk(log_n: usize, basek: usize, ct_k: usize, sigma: f64, ran ct_dft.encrypt_zero_sk( &module, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); - ct_dft.decrypt(&module, &mut pt, &sk_dft, scratch.borrow()); + ct_dft.decrypt(&module, &mut pt, &sk, scratch.borrow()); assert!((sigma - pt.data.std(0, basek) * (ct_k as f64).exp2()) <= 0.2); } @@ -197,13 +187,11 @@ fn test_encrypt_pk(log_n: usize, basek: usize, ct_k: usize, k_pk: usize, sigma: let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xu: Source = Source::new([0u8; 32]); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); let mut pk: GLWEPublicKey, FFT64> = GLWEPublicKey::alloc(&module, basek, k_pk, rank); - pk.generate_from_sk(&module, &sk_dft, &mut source_xa, &mut source_xe, sigma); + pk.generate_from_sk(&module, &sk, &mut source_xa, &mut source_xe, sigma); let mut scratch: ScratchOwned = ScratchOwned::new( GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k()) @@ -231,7 +219,7 @@ fn test_encrypt_pk(log_n: usize, basek: usize, ct_k: usize, k_pk: usize, sigma: let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&module, basek, ct_k); - ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0); @@ -289,22 +277,16 @@ fn test_keyswitch( ), ); - let mut sk_in: SecretKey> = SecretKey::alloc(&module, in_rank); - sk_in.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, in_rank); + sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, in_rank); - sk_in_dft.dft(&module, &sk_in); - - let mut sk_out: SecretKey> = SecretKey::alloc(&module, out_rank); - sk_out.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, out_rank); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, out_rank); + sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs); ksk.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -314,7 +296,7 @@ fn test_keyswitch( ct_in.encrypt_sk( &module, &pt_want, - &sk_in_dft, + &sk_in, &mut source_xa, &mut source_xe, sigma, @@ -323,7 +305,7 @@ fn test_keyswitch( ct_out.keyswitch(&module, &ct_in, &ksk, scratch.borrow()); - ct_out.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow()); + ct_out.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -374,22 +356,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize, | GLWECiphertext::keyswitch_inplace_scratch_space(&module, basek, ct_glwe.k(), rank, ct_grlwe.k()), ); - let mut sk0: SecretKey> = SecretKey::alloc(&module, rank); - sk0.fill_ternary_prob(0.5, &mut source_xs); + let mut sk0: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk0.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk0_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk0_dft.dft(&module, &sk0); - - let mut sk1: SecretKey> = SecretKey::alloc(&module, rank); - sk1.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk1_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk1_dft.dft(&module, &sk1); + let mut sk1: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk1.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_grlwe.generate_from_sk( &module, &sk0, - &sk1_dft, + &sk1, &mut source_xa, &mut source_xe, sigma, @@ -399,7 +375,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize, ct_glwe.encrypt_sk( &module, &pt_want, - &sk0_dft, + &sk0, &mut source_xa, &mut source_xe, sigma, @@ -408,7 +384,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize, ct_glwe.keyswitch_inplace(&module, &ct_grlwe, scratch.borrow()); - ct_glwe.decrypt(&module, &mut pt_have, &sk1_dft, scratch.borrow()); + ct_glwe.decrypt(&module, &mut pt_have, &sk1, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -462,17 +438,14 @@ fn test_automorphism( .fill_uniform(basek, 0, pt_want.size(), &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::new( - GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, autokey.k(), rank) + AutomorphismKey::generate_from_sk_scratch_space(&module, basek, autokey.k(), rank) | GLWECiphertext::decrypt_scratch_space(&module, basek, ct_out.k()) | GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k()) | GLWECiphertext::automorphism_scratch_space(&module, basek, ct_out.k(), ct_in.k(), autokey.k(), rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); autokey.generate_from_sk( &module, @@ -487,7 +460,7 @@ fn test_automorphism( ct_in.encrypt_sk( &module, &pt_want, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -495,7 +468,7 @@ fn test_automorphism( ); ct_out.automorphism(&module, &ct_in, &autokey, scratch.borrow()); - ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_normalize_inplace(basek, &mut pt_have.data, 0, scratch.borrow()); @@ -544,17 +517,14 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz .fill_uniform(basek, 0, pt_want.size(), &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::new( - GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, autokey.k(), rank) + AutomorphismKey::generate_from_sk_scratch_space(&module, basek, autokey.k(), rank) | GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k()) | GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k()) | GLWECiphertext::automorphism_inplace_scratch_space(&module, basek, ct.k(), autokey.k(), rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); autokey.generate_from_sk( &module, @@ -569,7 +539,7 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz ct.encrypt_sk( &module, &pt_want, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -577,7 +547,7 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz ); ct.automorphism_inplace(&module, &autokey, scratch.borrow()); - ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_normalize_inplace(basek, &mut pt_have.data, 0, scratch.borrow()); @@ -645,16 +615,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi ), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_ggsw.encrypt_sk( &module, &pt_rgsw, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -664,7 +631,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi ct_glwe_in.encrypt_sk( &module, &pt_want, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -673,7 +640,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi ct_glwe_out.external_product(&module, &ct_glwe_in, &ct_ggsw, scratch.borrow()); - ct_glwe_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct_glwe_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0); @@ -742,16 +709,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k | GLWECiphertext::external_product_inplace_scratch_space(&module, basek, ct_glwe.k(), ct_ggsw.k(), rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_ggsw.encrypt_sk( &module, &pt_rgsw, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -761,7 +725,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k ct_glwe.encrypt_sk( &module, &pt_want, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -770,7 +734,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k ct_glwe.external_product_inplace(&module, &ct_ggsw, scratch.borrow()); - ct_glwe.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct_glwe.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0); diff --git a/core/src/test_fft64/glwe_fourier.rs b/core/src/test_fft64/glwe_fourier.rs index 4213545..ea727ce 100644 --- a/core/src/test_fft64/glwe_fourier.rs +++ b/core/src/test_fft64/glwe_fourier.rs @@ -1,12 +1,5 @@ use crate::{ - GLWEOps, - elem::Infos, - ggsw_ciphertext::GGSWCiphertext, - glwe_ciphertext::GLWECiphertext, - glwe_ciphertext_fourier::GLWECiphertextFourier, - glwe_plaintext::GLWEPlaintext, - keys::{SecretKey, SecretKeyFourier}, - keyswitch_key::GLWESwitchingKey, + GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWEOps, GLWEPlaintext, GLWESecret, GLWESwitchingKey, Infos, test_fft64::{gglwe::log2_std_noise_gglwe_product, ggsw::noise_ggsw_product}, }; use backend::{FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, ZnxViewMut}; @@ -94,22 +87,16 @@ fn test_keyswitch( ), ); - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank_in); - sk_in.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_in); + sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_in); - sk_in_dft.dft(&module, &sk_in); - - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank_out); - sk_out.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank_out); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank_out); + sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs); ksk.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -119,7 +106,7 @@ fn test_keyswitch( ct_glwe_in.encrypt_sk( &module, &pt_want, - &sk_in_dft, + &sk_in, &mut source_xa, &mut source_xe, sigma, @@ -130,7 +117,7 @@ fn test_keyswitch( 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()); - ct_glwe_out.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow()); + ct_glwe_out.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -182,22 +169,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize, | GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, basek, ct_rlwe_dft.k(), ksk.k(), rank), ); - let mut sk_in: SecretKey> = SecretKey::alloc(&module, rank); - sk_in.fill_ternary_prob(0.5, &mut source_xs); + let mut sk_in: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs); - let mut sk_in_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_in_dft.dft(&module, &sk_in); - - let mut sk_out: SecretKey> = SecretKey::alloc(&module, rank); - sk_out.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_out_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_out_dft.dft(&module, &sk_out); + let mut sk_out: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs); ksk.generate_from_sk( &module, &sk_in, - &sk_out_dft, + &sk_out, &mut source_xa, &mut source_xe, sigma, @@ -207,7 +188,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize, ct_glwe.encrypt_sk( &module, &pt_want, - &sk_in_dft, + &sk_in, &mut source_xa, &mut source_xe, sigma, @@ -218,7 +199,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize, ct_rlwe_dft.keyswitch_inplace(&module, &ksk, scratch.borrow()); ct_rlwe_dft.idft(&module, &mut ct_glwe, scratch.borrow()); - ct_glwe.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow()); + ct_glwe.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); @@ -281,16 +262,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi | GLWECiphertextFourier::external_product_scratch_space(&module, basek, ct_out.k(), ct_in.k(), ct_ggsw.k(), rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_ggsw.encrypt_sk( &module, &pt_rgsw, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -300,7 +278,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi ct_in.encrypt_sk( &module, &pt_want, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -311,7 +289,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi ct_out_dft.external_product(&module, &ct_in_dft, &ct_ggsw, scratch.borrow()); ct_out_dft.idft(&module, &mut ct_out, scratch.borrow()); - ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); pt_want.rotate_inplace(&module, k); pt_have.sub_inplace_ab(&module, &pt_want); @@ -381,16 +359,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct | GLWECiphertextFourier::external_product_inplace_scratch_space(&module, basek, ct.k(), ct_ggsw.k(), rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); ct_ggsw.encrypt_sk( &module, &pt_rgsw, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -400,7 +375,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct ct.encrypt_sk( &module, &pt_want, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -411,7 +386,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct ct_rlwe_dft.external_product_inplace(&module, &ct_ggsw, scratch.borrow()); ct_rlwe_dft.idft(&module, &mut ct, scratch.borrow()); - ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); pt_want.rotate_inplace(&module, k); pt_have.sub_inplace_ab(&module, &pt_want); diff --git a/core/src/test_fft64/glwe_packing.rs b/core/src/test_fft64/glwe_packing.rs index a6433c2..6ee38b2 100644 --- a/core/src/test_fft64/glwe_packing.rs +++ b/core/src/test_fft64/glwe_packing.rs @@ -1,11 +1,4 @@ -use crate::{ - automorphism::AutomorphismKey, - glwe_ciphertext::GLWECiphertext, - glwe_ops::GLWEOps, - glwe_packing::StreamPacker, - glwe_plaintext::GLWEPlaintext, - keys::{SecretKey, SecretKeyFourier}, -}; +use crate::{AutomorphismKey, GLWECiphertext, GLWEOps, GLWEPlaintext, GLWESecret, StreamPacker}; use std::collections::HashMap; use backend::{Encoding, FFT64, Module, ScratchOwned, Stats}; @@ -35,11 +28,8 @@ fn packing() { | StreamPacker::scratch_space(&module, basek, ct_k, atk_k, rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&module, basek, ct_k); let mut data: Vec = vec![0i64; module.n()]; @@ -74,7 +64,7 @@ fn packing() { ct.encrypt_sk( &module, &pt, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -87,7 +77,7 @@ fn packing() { ct.encrypt_sk( &module, &pt, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -123,7 +113,7 @@ fn packing() { }); pt_want.data.encode_vec_i64(0, basek, pt_k, &data, 32); - res_i.decrypt(&module, &mut pt, &sk_dft, scratch.borrow()); + res_i.decrypt(&module, &mut pt, &sk, scratch.borrow()); if i & 1 == 0 { pt.sub_inplace_ab(&module, &pt_want); diff --git a/core/src/test_fft64/tensor_key.rs b/core/src/test_fft64/tensor_key.rs index 66d7a34..c6b9b26 100644 --- a/core/src/test_fft64/tensor_key.rs +++ b/core/src/test_fft64/tensor_key.rs @@ -1,15 +1,7 @@ -use backend::{ - FFT64, Module, ScalarZnx, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScratchOwned, Stats, VecZnxDftOps, VecZnxOps, -}; +use backend::{FFT64, Module, ScalarZnxDftOps, ScratchOwned, Stats, VecZnxOps}; use sampling::source::Source; -use crate::{ - elem::{GetRow, Infos}, - glwe_ciphertext_fourier::GLWECiphertextFourier, - glwe_plaintext::GLWEPlaintext, - keys::{SecretKey, SecretKeyFourier}, - tensor_key::TensorKey, -}; +use crate::{GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GetRow, Infos, TensorKey}; #[test] fn encrypt_sk() { @@ -37,15 +29,12 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize rank, )); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&module, 0.5, &mut source_xs); tensor_key.generate_from_sk( &module, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -55,22 +44,26 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize let mut ct_glwe_fourier: GLWECiphertextFourier, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k, rank); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&module, basek, k); + let mut sk_ij = GLWESecret::alloc(&module, 1); + (0..rank).for_each(|i| { (0..rank).for_each(|j| { - let mut sk_ij_dft: ScalarZnxDft, FFT64> = module.new_scalar_znx_dft(1); - module.svp_apply(&mut sk_ij_dft, 0, &sk_dft.data, i, &sk_dft.data, j); - let sk_ij: ScalarZnx> = module - .vec_znx_idft_consume(sk_ij_dft.as_vec_znx_dft()) - .to_vec_znx_small() - .to_scalar_znx(); - + module.svp_apply( + &mut sk_ij.data_fourier, + 0, + &sk.data_fourier, + i, + &sk.data_fourier, + j, + ); + module.svp_idft(&mut sk_ij.data, 0, &sk_ij.data_fourier, 0, scratch.borrow()); (0..tensor_key.rank_in()).for_each(|col_i| { (0..tensor_key.rows()).for_each(|row_i| { tensor_key .at(i, j) .get_row(&module, row_i, col_i, &mut ct_glwe_fourier); - ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow()); - module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij, col_i); + ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow()); + module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i); let std_pt: f64 = pt.data.std(0, basek) * (k as f64).exp2(); assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt); }); diff --git a/core/src/test_fft64/trace.rs b/core/src/test_fft64/trace.rs index b4d8f4e..5025ba6 100644 --- a/core/src/test_fft64/trace.rs +++ b/core/src/test_fft64/trace.rs @@ -3,14 +3,7 @@ use std::collections::HashMap; use backend::{FFT64, FillUniform, Module, ScratchOwned, Stats, VecZnxOps, ZnxView, ZnxViewMut}; use sampling::source::Source; -use crate::{ - automorphism::AutomorphismKey, - elem::Infos, - glwe_ciphertext::GLWECiphertext, - glwe_plaintext::GLWEPlaintext, - keys::{SecretKey, SecretKeyFourier}, - test_fft64::gglwe::var_noise_gglwe_product, -}; +use crate::{AutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, test_fft64::gglwe::var_noise_gglwe_product}; #[test] fn trace_inplace() { @@ -42,11 +35,8 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us | GLWECiphertext::trace_inplace_scratch_space(&module, basek, ct.k(), k_autokey, rank), ); - let mut sk: SecretKey> = SecretKey::alloc(&module, rank); - sk.fill_ternary_prob(0.5, &mut source_xs); - - let mut sk_dft: SecretKeyFourier, FFT64> = SecretKeyFourier::alloc(&module, rank); - sk_dft.dft(&module, &sk); + let mut sk: GLWESecret, FFT64> = GLWESecret::alloc(&module, rank); + sk.fill_ternary_prob(&&module, 0.5, &mut source_xs); let mut data_want: Vec = vec![0i64; module.n()]; @@ -61,7 +51,7 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us ct.encrypt_sk( &module, &pt_have, - &sk_dft, + &sk, &mut source_xa, &mut source_xe, sigma, @@ -89,7 +79,7 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us (0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]); - ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow()); + ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0); module.vec_znx_normalize_inplace(basek, &mut pt_want.data, 0, scratch.borrow()); diff --git a/core/src/trace.rs b/core/src/trace.rs index 5e41757..9414fbe 100644 --- a/core/src/trace.rs +++ b/core/src/trace.rs @@ -2,12 +2,7 @@ use std::collections::HashMap; use backend::{FFT64, Module, Scratch}; -use crate::{ - automorphism::AutomorphismKey, - elem::{Infos, SetMetaData}, - glwe_ciphertext::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef}, - glwe_ops::GLWEOps, -}; +use crate::{AutomorphismKey, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEOps, Infos, SetMetaData}; impl GLWECiphertext> { pub fn trace_galois_elements(module: &Module) -> Vec { From 17995d68fffdd099358c7248290fc9a87f2d58b0 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Wed, 4 Jun 2025 08:40:50 +0200 Subject: [PATCH 2/2] removed tracking of .vscode --- .gitignore | 3 +- .vscode/launch.json | 16 ---------- .vscode/settings.json | 71 ------------------------------------------- 3 files changed, 2 insertions(+), 88 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index ea8c4bf..a46afd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target +/target +/.vscode \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 10efcb2..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "lldb", - "request": "launch", - "name": "Debug", - "program": "${workspaceFolder}/", - "args": [], - "cwd": "${workspaceFolder}" - } - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 5ce22a6..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,71 +0,0 @@ -{ - "cmake.sourceDirectory": "C:/Users/boss_/go/src/github.com/gausslabs/poulpy/backend/spqlios-arithmetic", - "files.associations": { - "vector": "cpp", - "xstring": "cpp", - "xutility": "cpp", - "algorithm": "cpp", - "atomic": "cpp", - "bit": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "cinttypes": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "complex": "cpp", - "concepts": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "exception": "cpp", - "format": "cpp", - "initializer_list": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "iterator": "cpp", - "limits": "cpp", - "locale": "cpp", - "memory": "cpp", - "new": "cpp", - "optional": "cpp", - "ostream": "cpp", - "random": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "string": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "typeinfo": "cpp", - "utility": "cpp", - "xfacet": "cpp", - "xiosbase": "cpp", - "xlocale": "cpp", - "xlocbuf": "cpp", - "xlocinfo": "cpp", - "xlocmes": "cpp", - "xlocmon": "cpp", - "xlocnum": "cpp", - "xloctime": "cpp", - "xmemory": "cpp", - "xtr1common": "cpp", - "vec_znx_arithmetic_private.h": "c", - "reim4_arithmetic.h": "c", - "array": "c", - "string_view": "c" - }, - "github.copilot.enable": { - "*": false, - "plaintext": false, - "markdown": false, - "scminput": false - } -} \ No newline at end of file