diff --git a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs index fb1d567..0c15e96 100644 --- a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs @@ -99,6 +99,7 @@ where let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); { let (mut sk_out, _) = scratch_1.take_glwe_secret(self.n().into(), sk.rank()); + sk_out.dist = sk.dist; for i in 0..sk.rank().into() { self.vec_znx_automorphism( self.galois_element_inv(p), diff --git a/poulpy-core/src/encryption/compressed/glwe_switching_key.rs b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs index e1c40ea..b801927 100644 --- a/poulpy-core/src/encryption/compressed/glwe_switching_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs @@ -121,6 +121,8 @@ where } } + sk_out_tmp.dist = sk_out.dist; + self.gglwe_compressed_encrypt_sk(res, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2); *res.input_degree() = sk_in.n(); diff --git a/poulpy-core/src/encryption/glwe.rs b/poulpy-core/src/encryption/glwe.rs index fc59e4b..b032b38 100644 --- a/poulpy-core/src/encryption/glwe.rs +++ b/poulpy-core/src/encryption/glwe.rs @@ -485,18 +485,20 @@ where let ct: &mut VecZnx<&mut [u8]> = &mut res.to_mut(); let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); - #[cfg(debug_assertions)] - { - if compressed { - assert_eq!( - ct.cols(), - 1, - "invalid glwe: compressed tag=true but #cols={} != 1", - ct.cols() - ) - } + if compressed { + assert_eq!( + ct.cols(), + 1, + "invalid glwe: compressed tag=true but #cols={} != 1", + ct.cols() + ) } + assert!( + sk.dist != Distribution::NONE, + "glwe secret distribution is NONE (have you prepared the key?)" + ); + let size: usize = ct.size(); let (mut c0, scratch_1) = scratch.take_vec_znx(self.n(), 1, size); diff --git a/poulpy-core/src/encryption/glwe_automorphism_key.rs b/poulpy-core/src/encryption/glwe_automorphism_key.rs index 429a6d7..4feaeb6 100644 --- a/poulpy-core/src/encryption/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/glwe_automorphism_key.rs @@ -116,6 +116,8 @@ where { let (mut sk_out, _) = scratch_1.take_glwe_secret(sk.n(), sk.rank()); + sk_out.dist = sk.dist; + for i in 0..sk.rank().into() { self.vec_znx_automorphism( self.galois_element_inv(p), diff --git a/poulpy-core/src/encryption/glwe_switching_key.rs b/poulpy-core/src/encryption/glwe_switching_key.rs index 62d60c0..e1467f3 100644 --- a/poulpy-core/src/encryption/glwe_switching_key.rs +++ b/poulpy-core/src/encryption/glwe_switching_key.rs @@ -128,6 +128,8 @@ where } } + sk_out_tmp.dist = sk_out.dist; + self.gglwe_encrypt_sk( res, &sk_in_tmp, diff --git a/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs index 30a46a8..71877a4 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs @@ -101,6 +101,7 @@ where { let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), sk_lwe_as_glwe_prep.rank()); + sk_lwe_as_glwe.dist = sk_lwe.dist; sk_lwe_as_glwe.data.zero(); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_2); diff --git a/poulpy-core/src/encryption/lwe_switching_key.rs b/poulpy-core/src/encryption/lwe_switching_key.rs index 859f0c9..431545b 100644 --- a/poulpy-core/src/encryption/lwe_switching_key.rs +++ b/poulpy-core/src/encryption/lwe_switching_key.rs @@ -111,21 +111,24 @@ where assert!(sk_lwe_out.n().0 <= res.n().0); assert!(res.n() <= self.n() as u32); - let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1)); - let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), Rank(1)); + let (mut sk_glwe_in, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1)); + let (mut sk_glwe_out, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), Rank(1)); - sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); - sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); - self.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2); + sk_glwe_in.dist = sk_lwe_in.dist; + sk_glwe_out.dist = sk_lwe_out.dist; - sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0)); - sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0); - self.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2); + sk_glwe_out.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); + sk_glwe_out.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); + self.vec_znx_automorphism_inplace(-1, &mut sk_glwe_out.data.as_vec_znx_mut(), 0, scratch_2); + + sk_glwe_in.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0)); + sk_glwe_in.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0); + self.vec_znx_automorphism_inplace(-1, &mut sk_glwe_in.data.as_vec_znx_mut(), 0, scratch_2); self.glwe_switching_key_encrypt_sk( res, - &sk_in_glwe, - &sk_out_glwe, + &sk_glwe_in, + &sk_glwe_out, source_xa, source_xe, scratch_2, diff --git a/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs index 524bb1a..af31420 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs @@ -100,6 +100,8 @@ where assert!(sk_lwe.n().0 <= self.n() as u32); let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1)); + sk_lwe_as_glwe.dist = sk_lwe.dist; + sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index bfcb6a3..c2ba15c 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -192,6 +192,8 @@ where ); } } else { + // println!("res: {}", res); + for i in start..end { self.glwe_rsh(1, res, scratch); diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 9838fe0..5b3ea4d 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -6,8 +6,10 @@ use poulpy_hal::{ }; use poulpy_core::{ - GGSWFromGGLWE, GLWEPacking, GLWETrace, ScratchTakeCore, - layouts::{Dsize, GGLWELayout, GGSWInfos, GGSWToMut, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, LWEToRef}, + GGSWFromGGLWE, GLWEDecrypt, GLWEPacking, GLWETrace, ScratchTakeCore, + layouts::{ + Dsize, GGLWELayout, GGSWInfos, GGSWToMut, GLWEInfos, GLWESecretPreparedFactory, GLWEToMut, GLWEToRef, LWEInfos, LWEToRef, + }, }; use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::GLWEAutomorphismKeyPrepared}; @@ -94,7 +96,14 @@ impl CircuitBootstrappingKeyPre impl CirtuitBootstrappingExecute for Module where - Self: ModuleN + LookupTableFactory + BlindRotationExecute + GLWETrace + GLWEPacking + GGSWFromGGLWE, + Self: ModuleN + + LookupTableFactory + + BlindRotationExecute + + GLWETrace + + GLWEPacking + + GGSWFromGGLWE + + GLWESecretPreparedFactory + + GLWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { @@ -167,7 +176,14 @@ pub fn circuit_bootstrap_core( R: GGSWToMut, L: LWEToRef, D: DataRef, - M: ModuleN + LookupTableFactory + BlindRotationExecute + GLWETrace + GLWEPacking + GGSWFromGGLWE, + M: ModuleN + + LookupTableFactory + + BlindRotationExecute + + GLWETrace + + GLWEPacking + + GGSWFromGGLWE + + GLWESecretPreparedFactory + + GLWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { @@ -255,6 +271,12 @@ pub fn circuit_bootstrap_core( tmp_glwe.trace(module, 0, module.log_n(), &res_glwe, &key.atk, scratch_2); } + // let sk_glwe: &poulpy_core::layouts::GLWESecret<&[u8]> = &sk_glwe.to_ref(); + // let sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_glwe.rank()); + // let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&res_glwe); + // res_glwe.decrypt(module, &mut pt, &sk_glwe_prepared, scratch_2); + // println!("pt[{i}]: {}", pt); + if i < dnum { module.glwe_rotate_inplace(-(gap as i64), &mut res_glwe, scratch_2); } diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 172d430..c6b8adc 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,5 +1,5 @@ use poulpy_core::{ - GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, GetDistribution, ScratchTakeCore, + Distribution, GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ GGLWEInfos, GGSWInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecretPreparedFactory, GLWESecretToRef, GLWETensorKey, GLWETensorKeyLayout, LWEInfos, LWESecretToRef, prepared::GLWESecretPrepared, @@ -140,11 +140,14 @@ where assert_eq!(sk_glwe.n(), atk_infos.n()); assert_eq!(sk_glwe.n(), tsk_infos.n()); + assert!(sk_glwe.dist() != &Distribution::NONE); + for (p, atk) in res.atk.iter_mut() { atk.encrypt_sk(self, *p, sk_glwe, source_xa, source_xe, scratch); } - let sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(self, brk_infos.rank()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(self, brk_infos.rank()); + sk_glwe_prepared.prepare(self, sk_glwe); res.brk.encrypt_sk( self, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index 77469d1..59223ea 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -229,7 +229,7 @@ where let n_glwe: usize = module.n(); let base2k: usize = 14; let extension_factor: usize = 1; - let rank: usize = 2; + let rank: usize = 1; let n_lwe: usize = 77; let k_lwe_pt: usize = 1;