diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 2833bee..e0ca001 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -1,5 +1,5 @@ use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, + Base2K, Degree, Dnum, Dsize, GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPrepared, Rank, TorusPrecision, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }; @@ -39,7 +39,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct_in.div_ceil(p.base2k.0 * dsize.0).into(); - let gglwe_atk_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { + let gglwe_atk_layout: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n, base2k, k: k_gglwe, @@ -62,7 +62,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ksk: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&gglwe_atk_layout); let mut ct_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 6918ee1..a291f96 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,11 +7,12 @@ use crate::{ ScratchTakeCore, automorphism::glwe_ct::GLWEAutomorphism, layouts::{ - AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE, GetGaloisElement, SetGaloisElement, + GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE, GLWEAutomorphismKey, GetGaloisElement, + SetGaloisElement, }, }; -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn automorphism_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, @@ -19,11 +20,11 @@ impl AutomorphismKey> { K: GGLWEInfos, M: GLWEAutomorphismKeyAutomorphism, { - module.automorphism_key_automorphism_tmp_bytes(res_infos, a_infos, key_infos) + module.glwe_automorphism_key_automorphism_tmp_bytes(res_infos, a_infos, key_infos) } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn automorphism(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where A: GGLWEToRef + GetGaloisElement + GGLWEInfos, @@ -31,7 +32,7 @@ impl AutomorphismKey { Scratch: ScratchTakeCore, M: GLWEAutomorphismKeyAutomorphism, { - module.automorphism_key_automorphism(self, a, key, scratch); + module.glwe_automorphism_key_automorphism(self, a, key, scratch); } pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) @@ -40,7 +41,7 @@ impl AutomorphismKey { Scratch: ScratchTakeCore, M: GLWEAutomorphismKeyAutomorphism, { - module.automorphism_key_automorphism_inplace(self, key, scratch); + module.glwe_automorphism_key_automorphism_inplace(self, key, scratch); } } @@ -53,7 +54,7 @@ pub trait GLWEAutomorphismKeyAutomorphism where Self: GaloisElement + GLWEAutomorphism + VecZnxAutomorphism, { - fn automorphism_key_automorphism_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + fn glwe_automorphism_key_automorphism_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, A: GGLWEInfos, @@ -62,7 +63,7 @@ where self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } - fn automorphism_key_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + fn glwe_automorphism_key_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) where R: GGLWEToMut + SetGaloisElement + GGLWEInfos, A: GGLWEToRef + GetGaloisElement + GGLWEInfos, @@ -118,7 +119,7 @@ where res.set_p((p * key.p()) % (self.cyclotomic_order() as i64)); } - fn automorphism_key_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) + fn glwe_automorphism_key_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GGLWEToMut + SetGaloisElement + GetGaloisElement + GGLWEInfos, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index cf77758..fb54f6d 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ automorphism::glwe_ct::GLWEAutomorphism, layouts::{ GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GetGaloisElement, - prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, + prepared::{GLWETensorKeyPrepared, GLWETensorKeyPreparedToRef}, }, }; @@ -36,7 +36,7 @@ impl GGSW { where A: GGSWToRef, K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, { @@ -46,7 +46,7 @@ impl GGSW { pub fn automorphism_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, { @@ -79,12 +79,12 @@ where R: GGSWToMut, A: GGSWToRef, K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let a: &GGSW<&[u8]> = &a.to_ref(); - let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); assert_eq!(res.dsize(), a.dsize()); assert!(res.dnum() <= a.dnum()); @@ -104,11 +104,11 @@ where where R: GGSWToMut, K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); - let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); // Keyswitch the j-th row of the col 0 for row in 0..res.dnum().as_usize() { diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 2c05af5..7161239 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -54,7 +54,7 @@ impl GLWE { module.glwe_automorphism_sub(self, a, key, scratch); } - pub fn glwe_automorphism_sub_negate(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) + pub fn automorphism_sub_negate(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, A: GLWEToRef, diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs index 87b6791..b33759e 100644 --- a/poulpy-core/src/conversion/gglwe_to_ggsw.rs +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -11,7 +11,7 @@ use crate::{ GLWECopy, ScratchTakeCore, layouts::{ GGLWE, GGLWEInfos, GGLWEToRef, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, - prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, + prepared::{GLWETensorKeyPrepared, GLWETensorKeyPreparedToRef}, }, }; @@ -31,7 +31,7 @@ impl GGSW { where M: GGSWFromGGLWE, G: GGLWEToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { module.ggsw_from_gglwe(self, gglwe, tsk, scratch); @@ -54,12 +54,12 @@ where where R: GGSWToMut, A: GGLWEToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let a: &GGLWE<&[u8]> = &a.to_ref(); - let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); assert_eq!(res.rank(), a.rank_out()); assert_eq!(res.dnum(), a.dnum()); @@ -85,7 +85,7 @@ pub trait GGSWFromGGLWE { where R: GGSWToMut, A: GGLWEToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore; } @@ -158,11 +158,11 @@ where fn ggsw_expand_row(&self, res: &mut R, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); - let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); let basek_in: usize = res.base2k().into(); let basek_tsk: usize = tsk.base2k().into(); diff --git a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs index df57f0f..fb1d567 100644 --- a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs @@ -16,9 +16,9 @@ impl GLWEAutomorphismKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: AutomorphismKeyCompressedEncryptSk, + M: GLWEAutomorphismKeyCompressedEncryptSk, { - module.automorphism_key_compressed_encrypt_sk_tmp_bytes(infos) + module.glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes(infos) } } @@ -34,18 +34,18 @@ impl GLWEAutomorphismKeyCompressed { scratch: &mut Scratch, ) where S: GLWESecretToRef + GLWEInfos, - M: AutomorphismKeyCompressedEncryptSk, + M: GLWEAutomorphismKeyCompressedEncryptSk, { - module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); + module.glwe_automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); } } -pub trait AutomorphismKeyCompressedEncryptSk { - fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyCompressedEncryptSk { + fn glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn automorphism_key_compressed_encrypt_sk( + fn glwe_automorphism_key_compressed_encrypt_sk( &self, res: &mut R, p: i64, @@ -58,12 +58,12 @@ pub trait AutomorphismKeyCompressedEncryptSk { S: GLWESecretToRef + GLWEInfos; } -impl AutomorphismKeyCompressedEncryptSk for Module +impl GLWEAutomorphismKeyCompressedEncryptSk for Module where Self: ModuleN + GaloisElement + VecZnxAutomorphism + GGLWECompressedEncryptSk + GLWESecretPreparedFactory, Scratch: ScratchTakeCore, { - fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -73,7 +73,7 @@ where + GLWESecretPrepared::bytes_of_from_infos(self, infos) } - fn automorphism_key_compressed_encrypt_sk( + fn glwe_automorphism_key_compressed_encrypt_sk( &self, res: &mut R, p: i64, diff --git a/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs index b50b47d..14c9217 100644 --- a/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs @@ -8,10 +8,10 @@ use poulpy_hal::{ }; use crate::{ - GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore, TensorKeyEncryptSk, + GGLWECompressedEncryptSk, GLWETensorKeyEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, Rank, - TensorKeyCompressedAtMut, compressed::GLWETensorKeyCompressed, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESecretToRef, + GLWETensorKeyCompressedAtMut, LWEInfos, Rank, compressed::GLWETensorKeyCompressed, }, }; @@ -19,9 +19,9 @@ impl GLWETensorKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWETensorKeyCompressedEncryptSk, + M: GLWETensorKeyCompressedEncryptSk, { - module.tensor_key_compressed_encrypt_sk_tmp_bytes(infos) + module.glwe_tensor_key_compressed_encrypt_sk_tmp_bytes(infos) } } @@ -35,18 +35,18 @@ impl GLWETensorKeyCompressed { scratch: &mut Scratch, ) where S: GLWESecretToRef + GetDistribution, - M: GGLWETensorKeyCompressedEncryptSk, + M: GLWETensorKeyCompressedEncryptSk, { - module.tensor_key_compressed_encrypt_sk(self, sk, seed_xa, source_xe, scratch); + module.glwe_tensor_key_compressed_encrypt_sk(self, sk, seed_xa, source_xe, scratch); } } -pub trait GGLWETensorKeyCompressedEncryptSk { - fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWETensorKeyCompressedEncryptSk { + fn glwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn tensor_key_compressed_encrypt_sk( + fn glwe_tensor_key_compressed_encrypt_sk( &self, res: &mut R, sk: &S, @@ -55,15 +55,15 @@ pub trait GGLWETensorKeyCompressedEncryptSk { scratch: &mut Scratch, ) where D: DataMut, - R: TensorKeyCompressedAtMut + GGLWEInfos, + R: GLWETensorKeyCompressedAtMut + GGLWEInfos, S: GLWESecretToRef + GetDistribution; } -impl GGLWETensorKeyCompressedEncryptSk for Module +impl GLWETensorKeyCompressedEncryptSk for Module where Self: ModuleN + GGLWECompressedEncryptSk - + TensorKeyEncryptSk + + GLWETensorKeyEncryptSk + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA @@ -75,7 +75,7 @@ where + GLWESecretPreparedFactory, Scratch: ScratchTakeBasic + ScratchTakeCore, { - fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -87,7 +87,7 @@ where + self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) } - fn tensor_key_compressed_encrypt_sk( + fn glwe_tensor_key_compressed_encrypt_sk( &self, res: &mut R, sk: &S, @@ -96,7 +96,7 @@ where scratch: &mut Scratch, ) where D: DataMut, - R: GGLWEInfos + TensorKeyCompressedAtMut, + R: GGLWEInfos + GLWETensorKeyCompressedAtMut, S: GLWESecretToRef + GetDistribution, { let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank()); diff --git a/poulpy-core/src/encryption/glwe_automorphism_key.rs b/poulpy-core/src/encryption/glwe_automorphism_key.rs index cee3163..429a6d7 100644 --- a/poulpy-core/src/encryption/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/glwe_automorphism_key.rs @@ -7,30 +7,30 @@ use poulpy_hal::{ use crate::{ GGLWEEncryptSk, ScratchTakeCore, layouts::{ - AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepared, + GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEAutomorphismKey, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, SetGaloisElement, }, }; -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: AutomorphismKeyEncryptSk, + M: GLWEAutomorphismKeyEncryptSk, { - module.automorphism_key_encrypt_sk_tmp_bytes(infos) + module.glwe_automorphism_key_encrypt_sk_tmp_bytes(infos) } pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWEAutomorphismKeyEncryptPk, + M: GLWEAutomorphismKeyEncryptPk, { - module.automorphism_key_encrypt_pk_tmp_bytes(infos) + module.glwe_automorphism_key_encrypt_pk_tmp_bytes(infos) } } -impl AutomorphismKey +impl GLWEAutomorphismKey where Self: GGLWEToRef, { @@ -44,18 +44,18 @@ where scratch: &mut Scratch, ) where S: GLWESecretToRef, - M: AutomorphismKeyEncryptSk, + M: GLWEAutomorphismKeyEncryptSk, { - module.automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); + module.glwe_automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); } } -pub trait AutomorphismKeyEncryptSk { - fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyEncryptSk { + fn glwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn automorphism_key_encrypt_sk( + fn glwe_automorphism_key_encrypt_sk( &self, res: &mut R, p: i64, @@ -68,12 +68,12 @@ pub trait AutomorphismKeyEncryptSk { S: GLWESecretToRef; } -impl AutomorphismKeyEncryptSk for Module +impl GLWEAutomorphismKeyEncryptSk for Module where Self: GGLWEEncryptSk + VecZnxAutomorphism + GaloisElement + SvpPPolBytesOf + GLWESecretPreparedFactory, Scratch: ScratchTakeCore, { - fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -88,7 +88,7 @@ where .max(GLWESecret::bytes_of_from_infos(infos)) } - fn automorphism_key_encrypt_sk( + fn glwe_automorphism_key_encrypt_sk( &self, res: &mut R, p: i64, @@ -106,10 +106,10 @@ where assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= self.automorphism_key_encrypt_sk_tmp_bytes(res), + scratch.available() >= self.glwe_automorphism_key_encrypt_sk_tmp_bytes(res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", scratch.available(), - self.automorphism_key_encrypt_sk_tmp_bytes(res) + self.glwe_automorphism_key_encrypt_sk_tmp_bytes(res) ); let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); @@ -141,18 +141,18 @@ where } } -pub trait GGLWEAutomorphismKeyEncryptPk { - fn automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyEncryptPk { + fn glwe_automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; } -impl GGLWEAutomorphismKeyEncryptPk for Module +impl GLWEAutomorphismKeyEncryptPk for Module where Self:, Scratch: ScratchTakeCore, { - fn automorphism_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize + fn glwe_automorphism_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize where A: GGLWEInfos, { diff --git a/poulpy-core/src/encryption/glwe_tensor_key.rs b/poulpy-core/src/encryption/glwe_tensor_key.rs index e151b8e..b7afae5 100644 --- a/poulpy-core/src/encryption/glwe_tensor_key.rs +++ b/poulpy-core/src/encryption/glwe_tensor_key.rs @@ -10,22 +10,22 @@ use poulpy_hal::{ use crate::{ GGLWEEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, TensorKeyToMut, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWETensorKey, GLWETensorKeyToMut, LWEInfos, Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedFactory}, }, }; -impl TensorKey> { +impl GLWETensorKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyEncryptSk, + M: GLWETensorKeyEncryptSk, { - module.tensor_key_encrypt_sk_tmp_bytes(infos) + module.glwe_tensor_key_encrypt_sk_tmp_bytes(infos) } } -impl TensorKey { +impl GLWETensorKey { pub fn encrypt_sk( &mut self, module: &M, @@ -34,20 +34,20 @@ impl TensorKey { source_xe: &mut Source, scratch: &mut Scratch, ) where - M: TensorKeyEncryptSk, + M: GLWETensorKeyEncryptSk, S: GLWESecretToRef + GetDistribution + GLWEInfos, Scratch: ScratchTakeCore, { - module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); + module.glwe_tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); } } -pub trait TensorKeyEncryptSk { - fn tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWETensorKeyEncryptSk { + fn glwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn tensor_key_encrypt_sk( + fn glwe_tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, @@ -55,11 +55,11 @@ pub trait TensorKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: TensorKeyToMut, + R: GLWETensorKeyToMut, S: GLWESecretToRef + GetDistribution + GLWEInfos; } -impl TensorKeyEncryptSk for Module +impl GLWETensorKeyEncryptSk for Module where Self: ModuleN + GGLWEEncryptSk @@ -72,7 +72,7 @@ where + VecZnxBigNormalize, Scratch: ScratchTakeCore, { - fn tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -84,7 +84,7 @@ where + GGLWE::encrypt_sk_tmp_bytes(self, infos) } - fn tensor_key_encrypt_sk( + fn glwe_tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, @@ -92,10 +92,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: TensorKeyToMut, + R: GLWETensorKeyToMut, S: GLWESecretToRef + GetDistribution + GLWEInfos, { - let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWETensorKey<&mut [u8]> = &mut res.to_mut(); // let n: RingDegree = sk.n(); let rank: Rank = res.rank_out(); diff --git a/poulpy-core/src/external_product/gglwe.rs b/poulpy-core/src/external_product/gglwe.rs index a9537c1..437cf39 100644 --- a/poulpy-core/src/external_product/gglwe.rs +++ b/poulpy-core/src/external_product/gglwe.rs @@ -3,12 +3,12 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero}; use crate::{ GLWEExternalProduct, ScratchTakeCore, layouts::{ - AutomorphismKey, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GGSWPrepared, GLWEInfos, GLWESwitchingKey, + GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GGSWPrepared, GLWEAutomorphismKey, GLWEInfos, GLWESwitchingKey, prepared::GGSWPreparedToRef, }, }; -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn external_product_tmp_bytes( &self, module: &M, @@ -26,7 +26,7 @@ impl AutomorphismKey> { } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where M: GGLWEExternalProduct, diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 80cf84c..bfcb6a3 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use poulpy_hal::{ api::ModuleLogN, - layouts::{Backend, DataMut, GaloisElement, Module, Scratch, VecZnx}, + layouts::{Backend, DataMut, GaloisElement, Module, Scratch, VecZnx, galois_element}, }; use crate::{ @@ -70,20 +70,25 @@ impl GLWETrace for Module where { } +#[inline(always)] +pub fn trace_galois_elements(log_n: usize, cyclotomic_order: i64) -> Vec { + (0..log_n) + .map(|i| { + if i == 0 { + -1 + } else { + galois_element(1 << (i - 1), cyclotomic_order) + } + }) + .collect() +} + pub trait GLWETrace where Self: ModuleLogN + GaloisElement + GLWEAutomorphism + GLWEShift + GLWECopy, { fn glwe_trace_galois_elements(&self) -> Vec { - (0..self.log_n()) - .map(|i| { - if i == 0 { - -1 - } else { - self.galois_element(1 << (i - 1)) - } - }) - .collect() + trace_galois_elements(self.log_n(), self.cyclotomic_order()) } fn glwe_trace_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize diff --git a/poulpy-core/src/keyswitching/gglwe.rs b/poulpy-core/src/keyswitching/gglwe.rs index 1f9232f..d837002 100644 --- a/poulpy-core/src/keyswitching/gglwe.rs +++ b/poulpy-core/src/keyswitching/gglwe.rs @@ -3,10 +3,10 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; use crate::{ ScratchTakeCore, keyswitching::GLWEKeyswitch, - layouts::{AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWESwitchingKey}, + layouts::{GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWEAutomorphismKey, GLWESwitchingKey}, }; -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, @@ -18,7 +18,7 @@ impl AutomorphismKey> { } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where A: GGLWEToRef + GGLWEToRef, diff --git a/poulpy-core/src/keyswitching/ggsw.rs b/poulpy-core/src/keyswitching/ggsw.rs index 334572d..231b071 100644 --- a/poulpy-core/src/keyswitching/ggsw.rs +++ b/poulpy-core/src/keyswitching/ggsw.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, VecZnx}; use crate::{ GGSWExpandRows, ScratchTakeCore, keyswitching::GLWEKeyswitch, - layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::TensorKeyPreparedToRef}, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::GLWETensorKeyPreparedToRef}, }; impl GGSW> { @@ -30,7 +30,7 @@ impl GGSW { where A: GGSWToRef, K: GGLWEPreparedToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, { @@ -40,7 +40,7 @@ impl GGSW { pub fn keyswitch_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where K: GGLWEPreparedToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, { @@ -91,7 +91,7 @@ where R: GGSWToMut, A: GGSWToRef, K: GGLWEPreparedToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); @@ -113,7 +113,7 @@ where where R: GGSWToMut, K: GGLWEPreparedToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs b/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs index 6760d98..3e0a4b5 100644 --- a/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut, - GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWEDecompress, GLWEInfos, GetGaloisElement, LWEInfos, Rank, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWECompressedToRef, + GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWEAutomorphismKey, GLWEDecompress, GLWEInfos, GetGaloisElement, LWEInfos, Rank, SetGaloisElement, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -154,7 +154,7 @@ where impl AutomorphismKeyDecompress for Module where Self: GLWEDecompress {} -impl AutomorphismKey +impl GLWEAutomorphismKey where Self: SetGaloisElement, { diff --git a/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs b/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs index e8752d3..6939ff2 100644 --- a/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs +++ b/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, - GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, + GLWEInfos, GLWETensorKey, GLWETensorKeyToMut, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -149,11 +149,11 @@ impl WriterTo for GLWETensorKeyCompressed { } } -pub trait TensorKeyCompressedAtRef { +pub trait GLWETensorKeyCompressedAtRef { fn at(&self, i: usize, j: usize) -> &GGLWECompressed; } -impl TensorKeyCompressedAtRef for GLWETensorKeyCompressed { +impl GLWETensorKeyCompressedAtRef for GLWETensorKeyCompressed { fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed { if i > j { std::mem::swap(&mut i, &mut j); @@ -163,11 +163,11 @@ impl TensorKeyCompressedAtRef for GLWETensorKeyCompressed { } } -pub trait TensorKeyCompressedAtMut { +pub trait GLWETensorKeyCompressedAtMut { fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed; } -impl TensorKeyCompressedAtMut for GLWETensorKeyCompressed { +impl GLWETensorKeyCompressedAtMut for GLWETensorKeyCompressed { fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWECompressed { if i > j { std::mem::swap(&mut i, &mut j); @@ -177,16 +177,16 @@ impl TensorKeyCompressedAtMut for GLWETensorKeyCompressed { } } -pub trait TensorKeyDecompress +pub trait GLWETensorKeyDecompress where Self: GGLWEDecompress, { fn decompress_tensor_key(&self, res: &mut R, other: &O) where - R: TensorKeyToMut, - O: TensorKeyCompressedToRef, + R: GLWETensorKeyToMut, + O: GLWETensorKeyCompressedToRef, { - let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWETensorKey<&mut [u8]> = &mut res.to_mut(); let other: &GLWETensorKeyCompressed<&[u8]> = &other.to_ref(); assert_eq!( @@ -203,23 +203,23 @@ where } } -impl TensorKeyDecompress for Module where Self: GGLWEDecompress {} +impl GLWETensorKeyDecompress for Module where Self: GGLWEDecompress {} -impl TensorKey { +impl GLWETensorKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: TensorKeyCompressedToRef, - M: TensorKeyDecompress, + O: GLWETensorKeyCompressedToRef, + M: GLWETensorKeyDecompress, { module.decompress_tensor_key(self, other); } } -pub trait TensorKeyCompressedToMut { +pub trait GLWETensorKeyCompressedToMut { fn to_mut(&mut self) -> GLWETensorKeyCompressed<&mut [u8]>; } -impl TensorKeyCompressedToMut for GLWETensorKeyCompressed +impl GLWETensorKeyCompressedToMut for GLWETensorKeyCompressed where GGLWECompressed: GGLWECompressedToMut, { @@ -230,11 +230,11 @@ where } } -pub trait TensorKeyCompressedToRef { +pub trait GLWETensorKeyCompressedToRef { fn to_ref(&self) -> GLWETensorKeyCompressed<&[u8]>; } -impl TensorKeyCompressedToRef for GLWETensorKeyCompressed +impl GLWETensorKeyCompressedToRef for GLWETensorKeyCompressed where GGLWECompressed: GGLWECompressedToRef, { diff --git a/poulpy-core/src/layouts/glwe_automorphism_key.rs b/poulpy-core/src/layouts/glwe_automorphism_key.rs index c15c2db..a378e6d 100644 --- a/poulpy-core/src/layouts/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/glwe_automorphism_key.rs @@ -11,7 +11,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct AutomorphismKeyLayout { +pub struct GLWEAutomorphismKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -21,7 +21,7 @@ pub struct AutomorphismKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct AutomorphismKey { +pub struct GLWEAutomorphismKey { pub(crate) key: GGLWE, pub(crate) p: i64, } @@ -34,25 +34,25 @@ pub trait SetGaloisElement { fn set_p(&mut self, p: i64); } -impl SetGaloisElement for AutomorphismKey { +impl SetGaloisElement for GLWEAutomorphismKey { fn set_p(&mut self, p: i64) { self.p = p } } -impl GetGaloisElement for AutomorphismKey { +impl GetGaloisElement for GLWEAutomorphismKey { fn p(&self) -> i64 { self.p } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn p(&self) -> i64 { self.p } } -impl LWEInfos for AutomorphismKey { +impl LWEInfos for GLWEAutomorphismKey { fn n(&self) -> Degree { self.key.n() } @@ -70,13 +70,13 @@ impl LWEInfos for AutomorphismKey { } } -impl GLWEInfos for AutomorphismKey { +impl GLWEInfos for GLWEAutomorphismKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for AutomorphismKey { +impl GGLWEInfos for GLWEAutomorphismKey { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -94,7 +94,7 @@ impl GGLWEInfos for AutomorphismKey { } } -impl LWEInfos for AutomorphismKeyLayout { +impl LWEInfos for GLWEAutomorphismKeyLayout { fn base2k(&self) -> Base2K { self.base2k } @@ -108,13 +108,13 @@ impl LWEInfos for AutomorphismKeyLayout { } } -impl GLWEInfos for AutomorphismKeyLayout { +impl GLWEInfos for GLWEAutomorphismKeyLayout { fn rank(&self) -> Rank { self.rank } } -impl GGLWEInfos for AutomorphismKeyLayout { +impl GGLWEInfos for GLWEAutomorphismKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -132,25 +132,25 @@ impl GGLWEInfos for AutomorphismKeyLayout { } } -impl fmt::Debug for AutomorphismKey { +impl fmt::Debug for GLWEAutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for AutomorphismKey { +impl FillUniform for GLWEAutomorphismKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for AutomorphismKey { +impl fmt::Display for GLWEAutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key) } } -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, @@ -166,7 +166,7 @@ impl AutomorphismKey> { } pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - AutomorphismKey { + GLWEAutomorphismKey { key: GGLWE::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } @@ -196,38 +196,38 @@ impl AutomorphismKey> { } } -impl GGLWEToMut for AutomorphismKey { +impl GGLWEToMut for GLWEAutomorphismKey { fn to_mut(&mut self) -> GGLWE<&mut [u8]> { self.key.to_mut() } } -impl GGLWEToRef for AutomorphismKey { +impl GGLWEToRef for GLWEAutomorphismKey { fn to_ref(&self) -> GGLWE<&[u8]> { self.key.to_ref() } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } } -impl ReaderFrom for AutomorphismKey { +impl ReaderFrom for GLWEAutomorphismKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.p = reader.read_u64::()? as i64; self.key.read_from(reader) } } -impl WriterTo for AutomorphismKey { +impl WriterTo for GLWEAutomorphismKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) diff --git a/poulpy-core/src/layouts/glwe_tensor_key.rs b/poulpy-core/src/layouts/glwe_tensor_key.rs index 7f1bb4c..bc0100f 100644 --- a/poulpy-core/src/layouts/glwe_tensor_key.rs +++ b/poulpy-core/src/layouts/glwe_tensor_key.rs @@ -11,7 +11,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct TensorKeyLayout { +pub struct GLWETensorKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -21,11 +21,11 @@ pub struct TensorKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct TensorKey { +pub struct GLWETensorKey { pub(crate) keys: Vec>, } -impl LWEInfos for TensorKey { +impl LWEInfos for GLWETensorKey { fn n(&self) -> Degree { self.keys[0].n() } @@ -43,13 +43,13 @@ impl LWEInfos for TensorKey { } } -impl GLWEInfos for TensorKey { +impl GLWEInfos for GLWETensorKey { fn rank(&self) -> Rank { self.keys[0].rank_out() } } -impl GGLWEInfos for TensorKey { +impl GGLWEInfos for GLWETensorKey { fn rank_in(&self) -> Rank { self.rank_out() } @@ -67,7 +67,7 @@ impl GGLWEInfos for TensorKey { } } -impl LWEInfos for TensorKeyLayout { +impl LWEInfos for GLWETensorKeyLayout { fn n(&self) -> Degree { self.n } @@ -81,13 +81,13 @@ impl LWEInfos for TensorKeyLayout { } } -impl GLWEInfos for TensorKeyLayout { +impl GLWEInfos for GLWETensorKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for TensorKeyLayout { +impl GGLWEInfos for GLWETensorKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -105,13 +105,13 @@ impl GGLWEInfos for TensorKeyLayout { } } -impl fmt::Debug for TensorKey { +impl fmt::Debug for GLWETensorKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for TensorKey { +impl FillUniform for GLWETensorKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() @@ -119,7 +119,7 @@ impl FillUniform for TensorKey { } } -impl fmt::Display for TensorKey { +impl fmt::Display for GLWETensorKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKey)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -129,7 +129,7 @@ impl fmt::Display for TensorKey { } } -impl TensorKey> { +impl GLWETensorKey> { pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, @@ -151,7 +151,7 @@ impl TensorKey> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - TensorKey { + GLWETensorKey { keys: (0..pairs) .map(|_| GGLWE::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), @@ -183,7 +183,7 @@ impl TensorKey> { } } -impl TensorKey { +impl GLWETensorKey { // Returns a mutable reference to GGLWE_{s}(s[i] * s[j]) pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWE { if i > j { @@ -194,7 +194,7 @@ impl TensorKey { } } -impl TensorKey { +impl GLWETensorKey { // Returns a reference to GGLWE_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE { if i > j { @@ -205,7 +205,7 @@ impl TensorKey { } } -impl ReaderFrom for TensorKey { +impl ReaderFrom for GLWETensorKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -221,7 +221,7 @@ impl ReaderFrom for TensorKey { } } -impl WriterTo for TensorKey { +impl WriterTo for GLWETensorKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -231,31 +231,31 @@ impl WriterTo for TensorKey { } } -pub trait TensorKeyToRef { - fn to_ref(&self) -> TensorKey<&[u8]>; +pub trait GLWETensorKeyToRef { + fn to_ref(&self) -> GLWETensorKey<&[u8]>; } -impl TensorKeyToRef for TensorKey +impl GLWETensorKeyToRef for GLWETensorKey where GGLWE: GGLWEToRef, { - fn to_ref(&self) -> TensorKey<&[u8]> { - TensorKey { + fn to_ref(&self) -> GLWETensorKey<&[u8]> { + GLWETensorKey { keys: self.keys.iter().map(|c| c.to_ref()).collect(), } } } -pub trait TensorKeyToMut { - fn to_mut(&mut self) -> TensorKey<&mut [u8]>; +pub trait GLWETensorKeyToMut { + fn to_mut(&mut self) -> GLWETensorKey<&mut [u8]>; } -impl TensorKeyToMut for TensorKey +impl GLWETensorKeyToMut for GLWETensorKey where GGLWE: GGLWEToMut, { - fn to_mut(&mut self) -> TensorKey<&mut [u8]> { - TensorKey { + fn to_mut(&mut self) -> GLWETensorKey<&mut [u8]> { + GLWETensorKey { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } } diff --git a/poulpy-core/src/layouts/lwe.rs b/poulpy-core/src/layouts/lwe.rs index 0900d78..6f8cdce 100644 --- a/poulpy-core/src/layouts/lwe.rs +++ b/poulpy-core/src/layouts/lwe.rs @@ -10,6 +10,9 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; pub trait LWEInfos { fn n(&self) -> Degree; + fn log_n(&self) -> usize { + (u64::BITS - (self.n().as_usize() as u64 - 1).leading_zeros()) as usize + } fn k(&self) -> TorusPrecision; fn max_k(&self) -> TorusPrecision { TorusPrecision(self.k().0 * self.size() as u32) diff --git a/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs b/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs index 7ac936e..bd63c75 100644 --- a/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs @@ -2,15 +2,15 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef, - GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, + GLWEInfos, GLWETensorKey, GLWETensorKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct TensorKeyPrepared { +pub struct GLWETensorKeyPrepared { pub(crate) keys: Vec>, } -impl LWEInfos for TensorKeyPrepared { +impl LWEInfos for GLWETensorKeyPrepared { fn n(&self) -> Degree { self.keys[0].n() } @@ -28,13 +28,13 @@ impl LWEInfos for TensorKeyPrepared { } } -impl GLWEInfos for TensorKeyPrepared { +impl GLWEInfos for GLWETensorKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for TensorKeyPrepared { +impl GGLWEInfos for GLWETensorKeyPrepared { fn rank_in(&self) -> Rank { self.rank_out() } @@ -52,7 +52,7 @@ impl GGLWEInfos for TensorKeyPrepared { } } -pub trait TensorKeyPreparedFactory +pub trait GLWETensorKeyPreparedFactory where Self: GGLWEPreparedFactory, { @@ -63,16 +63,16 @@ where dnum: Dnum, dsize: Dsize, rank: Rank, - ) -> TensorKeyPrepared, B> { + ) -> GLWETensorKeyPrepared, B> { let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); - TensorKeyPrepared { + GLWETensorKeyPrepared { keys: (0..pairs) .map(|_| self.alloc_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } - fn alloc_tensor_key_prepared_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> + fn alloc_tensor_key_prepared_from_infos(&self, infos: &A) -> GLWETensorKeyPrepared, B> where A: GGLWEInfos, { @@ -117,11 +117,11 @@ where fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: TensorKeyPreparedToMut, - O: TensorKeyToRef, + R: GLWETensorKeyPreparedToMut, + O: GLWETensorKeyToRef, { - let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut(); - let other: TensorKey<&[u8]> = other.to_ref(); + let mut res: GLWETensorKeyPrepared<&mut [u8], B> = res.to_mut(); + let other: GLWETensorKey<&[u8]> = other.to_ref(); assert_eq!(res.keys.len(), other.keys.len()); @@ -131,20 +131,20 @@ where } } -impl TensorKeyPreparedFactory for Module where Module: GGLWEPreparedFactory {} +impl GLWETensorKeyPreparedFactory for Module where Module: GGLWEPreparedFactory {} -impl TensorKeyPrepared, B> { +impl GLWETensorKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.alloc_tensor_key_prepared_from_infos(infos) } pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) } @@ -152,20 +152,20 @@ impl TensorKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.bytes_of_tensor_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) } } -impl TensorKeyPrepared { +impl GLWETensorKeyPrepared { // Returns a mutable reference to GGLWE_{s}(s[i] * s[j]) pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEPrepared { if i > j { @@ -176,7 +176,7 @@ impl TensorKeyPrepared { } } -impl TensorKeyPrepared { +impl GLWETensorKeyPrepared { // Returns a reference to GGLWE_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared { if i > j { @@ -187,51 +187,51 @@ impl TensorKeyPrepared { } } -impl TensorKeyPrepared, B> { +impl GLWETensorKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.prepare_tensor_key_tmp_bytes(infos) } } -impl TensorKeyPrepared { +impl GLWETensorKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: TensorKeyToRef, - M: TensorKeyPreparedFactory, + O: GLWETensorKeyToRef, + M: GLWETensorKeyPreparedFactory, { module.prepare_tensor_key(self, other, scratch); } } -pub trait TensorKeyPreparedToMut { - fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; +pub trait GLWETensorKeyPreparedToMut { + fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B>; } -impl TensorKeyPreparedToMut for TensorKeyPrepared +impl GLWETensorKeyPreparedToMut for GLWETensorKeyPrepared where GGLWEPrepared: GGLWEPreparedToMut, { - fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { - TensorKeyPrepared { + fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B> { + GLWETensorKeyPrepared { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } } } -pub trait TensorKeyPreparedToRef { - fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>; +pub trait GLWETensorKeyPreparedToRef { + fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B>; } -impl TensorKeyPreparedToRef for TensorKeyPrepared +impl GLWETensorKeyPreparedToRef for GLWETensorKeyPrepared where GGLWEPrepared: GGLWEPreparedToRef, { - fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { - TensorKeyPrepared { + fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B> { + GLWETensorKeyPrepared { keys: self.keys.iter().map(|c| c.to_ref()).collect(), } } diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index a5c5152..ccad084 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -21,6 +21,7 @@ pub use dist::*; pub use encryption::*; pub use external_product::*; pub use glwe_packing::*; +pub use glwe_trace::*; pub use keyswitching::*; pub use noise::*; pub use scratch::*; diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 880af1b..2220dc4 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,11 +6,11 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPrepared, - GLWEPublicKey, GLWESecret, GLWESwitchingKey, Rank, TensorKey, + Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEAutomorphismKey, GLWEInfos, GLWEPlaintext, + GLWEPrepared, GLWEPublicKey, GLWESecret, GLWESwitchingKey, GLWETensorKey, Rank, prepared::{ GGLWEPrepared, GGSWPrepared, GLWEAutomorphismKeyPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, - GLWESwitchingKeyPrepared, TensorKeyPrepared, + GLWESwitchingKeyPrepared, GLWETensorKeyPrepared, }, }, }; @@ -282,12 +282,12 @@ where ) } - fn take_glwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) + fn take_glwe_automorphism_key(&mut self, infos: &A) -> (GLWEAutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_gglwe(infos); - (AutomorphismKey { key: data, p: 0 }, scratch) + (GLWEAutomorphismKey { key: data, p: 0 }, scratch) } fn take_glwe_automorphism_key_prepared( @@ -304,14 +304,14 @@ where (GLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) } - fn take_glwe_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) + fn take_glwe_tensor_key(&mut self, infos: &A) -> (GLWETensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKey" + "rank_in != rank_out is not supported for GLWETensorKey" ); let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; @@ -331,10 +331,10 @@ where scratch = s; keys.push(gglwe); } - (TensorKey { keys }, scratch) + (GLWETensorKey { keys }, scratch) } - fn take_glwe_tensor_key_prepared(&mut self, module: &M, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_glwe_tensor_key_prepared(&mut self, module: &M, infos: &A) -> (GLWETensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, M: ModuleN + VmpPMatBytesOf, @@ -364,7 +364,7 @@ where scratch = s; keys.push(gglwe); } - (TensorKeyPrepared { keys }, scratch) + (GLWETensorKeyPrepared { keys }, scratch) } } diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 502a59b..c67d87d 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,8 +1,8 @@ use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ - AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, - LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWEAutomorphismKey, GLWESwitchingKey, GLWETensorKey, GLWEToLWESwitchingKey, + LWE, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TorusPrecision, compressed::{ GGLWECompressed, GGSWCompressed, GLWEAutomorphismKeyCompressed, GLWECompressed, GLWESwitchingKeyCompressed, GLWETensorKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECompressed, LWESwitchingKeyCompressed, @@ -69,7 +69,7 @@ fn test_glwe_switching_key_compressed_serialization() { #[test] fn test_automorphism_key_serialization() { - let original: AutomorphismKey> = AutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } @@ -82,7 +82,7 @@ fn test_automorphism_key_compressed_serialization() { #[test] fn test_tensor_key_serialization() { - let original: TensorKey> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GLWETensorKey> = GLWETensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } diff --git a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs index a8d3d53..3f2b94a 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -5,11 +5,11 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GLWEAutomorphismKeyAutomorphism, GLWEDecrypt, ScratchTakeCore, + GLWEAutomorphismKeyAutomorphism, GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEAutomorphismKeyPreparedFactory, GLWEPlaintext, GLWESecret, - GLWESecretPreparedFactory, + GGLWEInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWEPlaintext, + GLWESecret, GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -18,7 +18,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism(module: &Module) where - Module: AutomorphismKeyEncryptSk + Module: GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWEAutomorphismKeyAutomorphism + VecZnxAutomorphism @@ -46,7 +46,7 @@ where let dnum_out: usize = k_out / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_in_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_in_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -55,7 +55,7 @@ where rank: rank.into(), }; - let auto_key_out_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_out_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -64,7 +64,7 @@ where rank: rank.into(), }; - let auto_key_apply_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_apply_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -73,18 +73,18 @@ where rank: rank.into(), }; - let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_in_infos); - let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_out_infos); - let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_apply_infos); + let mut auto_key_in: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_in_infos); + let mut auto_key_out: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_out_infos); + let mut auto_key_apply: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_apply_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos) - | AutomorphismKey::automorphism_tmp_bytes( + GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos) + | GLWEAutomorphismKey::automorphism_tmp_bytes( module, &auto_key_out_infos, &auto_key_in_infos, @@ -187,7 +187,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) where - Module: AutomorphismKeyEncryptSk + Module: GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWEAutomorphismKeyAutomorphism + VecZnxAutomorphism @@ -213,7 +213,7 @@ where let dnum_in: usize = k_in / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_layout: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -222,7 +222,7 @@ where rank: rank.into(), }; - let auto_key_apply_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_apply_layout: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -231,17 +231,17 @@ where rank: rank.into(), }; - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); - let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_apply_layout); + let mut auto_key: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut auto_key_apply: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_apply_layout); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply) - | AutomorphismKey::automorphism_tmp_bytes(module, &auto_key, &auto_key, &auto_key_apply), + GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply) + | GLWEAutomorphismKey::automorphism_tmp_bytes(module, &auto_key, &auto_key, &auto_key_apply), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); diff --git a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs index 384a86b..c3aa3c3 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -5,12 +5,12 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, TensorKeyEncryptSk, + GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, GGSW, GGSWLayout, GLWEAutomorphismKeyPreparedFactory, GLWESecret, GLWESecretPreparedFactory, TensorKey, - TensorKeyLayout, TensorKeyPreparedFactory, - prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, + GGSW, GGSWLayout, GLWEAutomorphismKey, GLWEAutomorphismKeyPreparedFactory, GLWESecret, GLWESecretPreparedFactory, + GLWETensorKey, GLWETensorKeyLayout, GLWETensorKeyPreparedFactory, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, GLWETensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -18,11 +18,11 @@ use crate::{ pub fn test_ggsw_automorphism(module: &Module) where Module: GGSWEncryptSk - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GGSWAutomorphism - + TensorKeyPreparedFactory - + TensorKeyEncryptSk + + GLWETensorKeyPreparedFactory + + GLWETensorKeyEncryptSk + GLWESecretPreparedFactory + VecZnxAutomorphismInplace + GGSWNoise, @@ -64,7 +64,7 @@ where rank: rank.into(), }; - let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { + let tensor_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -73,7 +73,7 @@ where rank: rank.into(), }; - let auto_key_layout: TensorKeyLayout = TensorKeyLayout { + let auto_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -84,8 +84,8 @@ where let mut ct_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_layout); let mut ct_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut tensor_key: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -94,8 +94,8 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ct_in) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) - | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | GLWETensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) | GGSW::automorphism_tmp_bytes(module, &ct_out, &ct_in, &auto_key, &tensor_key), ); @@ -138,8 +138,8 @@ where GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, BE> = - TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: GLWETensorKeyPrepared, BE> = + GLWETensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -177,11 +177,11 @@ where pub fn test_ggsw_automorphism_inplace(module: &Module) where Module: GGSWEncryptSk - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GGSWAutomorphism - + TensorKeyPreparedFactory - + TensorKeyEncryptSk + + GLWETensorKeyPreparedFactory + + GLWETensorKeyEncryptSk + GLWESecretPreparedFactory + VecZnxAutomorphismInplace + GGSWNoise, @@ -211,7 +211,7 @@ where rank: rank.into(), }; - let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { + let tensor_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -220,7 +220,7 @@ where rank: rank.into(), }; - let auto_key_layout: TensorKeyLayout = TensorKeyLayout { + let auto_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -230,8 +230,8 @@ where }; let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut tensor_key: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -240,8 +240,8 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ct) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) - | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | GLWETensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) | GGSW::automorphism_tmp_bytes(module, &ct, &ct, &auto_key, &tensor_key), ); @@ -284,8 +284,8 @@ where GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, BE> = - TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: GLWETensorKeyPrepared, BE> = + GLWETensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs index df9d36d..58f737a 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -5,11 +5,11 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GLWEAutomorphism, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + GLWEAutomorphism, GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedFactory, + GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, + GLWESecret, GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -22,7 +22,7 @@ where + VecZnxFillUniform + GLWEDecrypt + GLWEAutomorphism - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWENoise + VecZnxAutomorphismInplace, @@ -55,7 +55,7 @@ where rank: rank.into(), }; - let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let autokey_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -64,7 +64,7 @@ where dsize: di.into(), }; - let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut autokey: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&autokey_infos); let mut ct_in: GLWE> = GLWE::alloc_from_infos(&ct_in_infos); let mut ct_out: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); @@ -76,7 +76,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) + GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) | GLWE::decrypt_tmp_bytes(module, &ct_out) | GLWE::encrypt_sk_tmp_bytes(module, &ct_in) | GLWE::automorphism_tmp_bytes(module, &ct_out, &ct_in, &autokey), @@ -140,7 +140,7 @@ where + VecZnxFillUniform + GLWEDecrypt + GLWEAutomorphism - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWENoise + VecZnxAutomorphismInplace, @@ -165,7 +165,7 @@ where rank: rank.into(), }; - let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let autokey_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -174,7 +174,7 @@ where dsize: di.into(), }; - let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut autokey: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&autokey_infos); let mut ct: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); @@ -185,7 +185,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) + GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) | GLWE::decrypt_tmp_bytes(module, &ct) | GLWE::encrypt_sk_tmp_bytes(module, &ct) | GLWE::automorphism_tmp_bytes(module, &ct, &ct, &autokey), diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs index 3b642c4..ad456c9 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -5,19 +5,20 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyCompressedEncryptSk, AutomorphismKeyEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, + GGLWEKeyswitch, GLWEAutomorphismKeyCompressedEncryptSk, GLWEAutomorphismKeyEncryptSk, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPreparedFactory, - GLWESwitchingKeyDecompress, compressed::GLWEAutomorphismKeyCompressed, prepared::GLWESecretPrepared, + AutomorphismKeyDecompress, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret, + GLWESecretPreparedFactory, GLWESwitchingKeyDecompress, compressed::GLWEAutomorphismKeyCompressed, + prepared::GLWESecretPrepared, }, noise::GGLWENoise, }; pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) where - Module: AutomorphismKeyEncryptSk + Module: GLWEAutomorphismKeyEncryptSk + GGLWEKeyswitch + GLWESecretPreparedFactory + GLWESwitchingKeyEncryptSk @@ -37,7 +38,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let atk_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -46,13 +47,15 @@ where rank: rank.into(), }; - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWEAutomorphismKey::encrypt_sk_tmp_bytes( + module, &atk_infos, + )); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -89,7 +92,7 @@ where pub fn test_gglwe_automorphism_key_compressed_encrypt_sk(module: &Module) where - Module: AutomorphismKeyCompressedEncryptSk + Module: GLWEAutomorphismKeyCompressedEncryptSk + GGLWEKeyswitch + GLWESecretPreparedFactory + GLWESwitchingKeyEncryptSk @@ -109,7 +112,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let atk_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -150,7 +153,7 @@ where let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); sk_out_prepared.prepare(module, &sk_out); - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs index 9135374..940f917 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -9,18 +9,18 @@ use poulpy_hal::{ }; use crate::{ - GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, + GLWETensorKeyCompressedEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore, decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - Dsize, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWETensorKeyCompressed, TensorKey, TensorKeyLayout, + Dsize, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWETensorKey, GLWETensorKeyCompressed, GLWETensorKeyLayout, prepared::GLWESecretPrepared, }, }; pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where - Module: TensorKeyEncryptSk + Module: GLWETensorKeyEncryptSk + GLWESecretPreparedFactory + GLWEDecrypt + VecZnxDftAlloc @@ -40,7 +40,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos = TensorKeyLayout { + let tensor_key_infos = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -49,13 +49,16 @@ where rank: rank.into(), }; - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key_infos)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWETensorKey::encrypt_sk_tmp_bytes( + module, + &tensor_key_infos, + )); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -111,9 +114,9 @@ where pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where - Module: TensorKeyEncryptSk + Module: GLWETensorKeyEncryptSk + GLWESecretPreparedFactory - + GGLWETensorKeyCompressedEncryptSk + + GLWETensorKeyCompressedEncryptSk + GLWEDecrypt + VecZnxDftAlloc + VecZnxBigAlloc @@ -133,7 +136,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos: TensorKeyLayout = TensorKeyLayout { + let tensor_key_infos: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -162,7 +165,7 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); diff --git a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs index 288eb88..b582d89 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -5,12 +5,12 @@ use poulpy_hal::{ }; use crate::{ - GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, + GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ GGSW, GGSWLayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, - GLWESwitchingKeyPreparedFactory, TensorKey, TensorKeyLayout, TensorKeyPreparedFactory, - prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared}, + GLWESwitchingKeyPreparedFactory, GLWETensorKey, GLWETensorKeyLayout, GLWETensorKeyPreparedFactory, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, GLWETensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -20,10 +20,10 @@ pub fn test_ggsw_keyswitch(module: &Module) where Module: GGSWEncryptSk + GLWESwitchingKeyEncryptSk - + TensorKeyEncryptSk + + GLWETensorKeyEncryptSk + GGSWKeyswitch + GLWESecretPreparedFactory - + TensorKeyPreparedFactory + + GLWETensorKeyPreparedFactory + GLWESwitchingKeyPreparedFactory + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -61,7 +61,7 @@ where rank: rank.into(), }; - let tsk_infos: TensorKeyLayout = TensorKeyLayout { + let tsk_infos: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -82,7 +82,7 @@ where let mut ggsw_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_infos); let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); - let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut tsk: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tsk_infos); let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -93,7 +93,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) - | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) + | GLWETensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) | GGSW::keyswitch_tmp_bytes( module, &ggsw_out_infos, @@ -148,7 +148,7 @@ where GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); ksk_prepared.prepare(module, &ksk, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut tsk_prepared: GLWETensorKeyPrepared, BE> = GLWETensorKeyPrepared::alloc_from_infos(module, &tsk); tsk_prepared.prepare(module, &tsk, scratch.borrow()); ggsw_out.keyswitch( @@ -185,10 +185,10 @@ pub fn test_ggsw_keyswitch_inplace(module: &Module) where Module: GGSWEncryptSk + GLWESwitchingKeyEncryptSk - + TensorKeyEncryptSk + + GLWETensorKeyEncryptSk + GGSWKeyswitch + GLWESecretPreparedFactory - + TensorKeyPreparedFactory + + GLWETensorKeyPreparedFactory + GLWESwitchingKeyPreparedFactory + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -216,7 +216,7 @@ where rank: rank.into(), }; - let tsk_infos: TensorKeyLayout = TensorKeyLayout { + let tsk_infos: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -236,7 +236,7 @@ where }; let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); - let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut tsk: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tsk_infos); let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -247,7 +247,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) - | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) + | GLWETensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) | GGSW::keyswitch_tmp_bytes( module, &ggsw_out_infos, @@ -302,7 +302,7 @@ where GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); ksk_prepared.prepare(module, &ksk, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut tsk_prepared: GLWETensorKeyPrepared, BE> = GLWETensorKeyPrepared::alloc_from_infos(module, &tsk); tsk_prepared.prepare(module, &tsk, scratch.borrow()); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index ec685f4..029e059 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -7,10 +7,10 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore, + GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedFactory, + GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, + GLWESecret, GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, }; @@ -18,7 +18,7 @@ use crate::{ pub fn test_glwe_packing(module: &Module) where Module: GLWEEncryptSk - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWEPacking + GLWESecretPreparedFactory @@ -49,7 +49,7 @@ where rank: rank.into(), }; - let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -60,7 +60,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos), ); @@ -81,7 +81,7 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); let mut auto_keys: HashMap, BE>> = HashMap::new(); - let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); + let mut tmp: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index 4b83808..ed2ed79 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -7,12 +7,12 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore, + GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore, encryption::SIGMA, glwe_trace::GLWETrace, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedFactory, LWEInfos, + GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, + GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::var_noise_gglwe_product, @@ -23,7 +23,7 @@ where Module: GLWETrace + GLWEEncryptSk + GLWEDecrypt - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + VecZnxFillUniform + GLWESecretPreparedFactory @@ -49,7 +49,7 @@ where rank: rank.into(), }; - let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_autokey.into(), @@ -69,7 +69,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_out_infos) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) | GLWE::trace_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &key_infos), ); @@ -98,7 +98,7 @@ where let mut auto_keys: HashMap, BE>> = HashMap::new(); let gal_els: Vec = GLWE::trace_galois_elements(module); - let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); + let mut tmp: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index bc074dd..0556a6f 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -97,18 +97,23 @@ impl ModuleLogN for Module where Self: ModuleN {} impl CyclotomicOrder for Module where Self: ModuleN {} +#[inline(always)] +pub fn galois_element(generator: i64, cyclotomic_order: i64) -> i64 { + if generator == 0 { + return 1; + } + + let g_exp: u64 = mod_exp_u64(GALOISGENERATOR, generator.unsigned_abs() as usize) & (cyclotomic_order - 1) as u64; + g_exp as i64 * generator.signum() +} + pub trait GaloisElement where Self: CyclotomicOrder, { // Returns GALOISGENERATOR^|generator| * sign(generator) fn galois_element(&self, generator: i64) -> i64 { - if generator == 0 { - return 1; - } - - let g_exp: u64 = mod_exp_u64(GALOISGENERATOR, generator.unsigned_abs() as usize) & (self.cyclotomic_order() - 1) as u64; - g_exp as i64 * generator.signum() + galois_element(generator, self.cyclotomic_order()) } // Returns gen^-1 diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index a1f39c6..0d90062 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -2,107 +2,46 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; -use poulpy_core::layouts::{ - AutomorphismKeyLayout, Dsize, GGSW, GGSWLayout, GLWESecret, LWE, LWELayout, LWESecret, TensorKeyLayout, - prepared::PrepareAlloc, +use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWEExternalProduct, LWEEncryptSk, ScratchTakeCore, + layouts::{ + Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWESecret, GLWESecretPreparedFactory, + GLWETensorKeyLayout, LWE, LWELayout, LWESecret, + }, }; use poulpy_hal::{ - api::{ - ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, - SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, - VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, - VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, - TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, - }, + api::{ModuleN, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use poulpy_schemes::tfhe::{ blind_rotation::{ - BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, - BlindRotationKeyInfos, BlindRotationKeyLayout, BlindRotationKeyPrepared, CGGI, + BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyInfos, BlindRotationKeyLayout, CGGI, }, circuit_bootstrapping::{ CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute, + CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, }, }; -pub fn benc_circuit_bootstrapping(c: &mut Criterion, label: &str) +pub fn benc_circuit_bootstrapping(c: &mut Criterion, label: &str) where - Module: ModuleNew - + VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, - BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + Module: ModuleNew + + ModuleN + + GLWESecretPreparedFactory + + GLWEExternalProduct + + GLWEDecrypt + + LWEEncryptSk + + CircuitBootstrappingKeyEncryptSk + + CircuitBootstrappingKeyPreparedFactory + + CirtuitBootstrappingExecute + + GGSWPreparedFactory + + GGSWNoise + + GLWEEncryptSk + + VecZnxRotateInplace, + BlindRotationKey, BRA>: BlindRotationKeyFactory, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { let group_name: String = format!("circuit_bootstrapping::{label}"); @@ -118,81 +57,33 @@ where cbt_infos: CircuitBootstrappingKeyLayout, } - fn runner(params: &Params) -> impl FnMut() + fn runner(params: &Params) -> impl FnMut() where - Module: ModuleNew - + VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, - BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + Module: ModuleNew + + ModuleN + + GLWESecretPreparedFactory + + GLWEExternalProduct + + GLWEDecrypt + + LWEEncryptSk + + CircuitBootstrappingKeyEncryptSk + + CircuitBootstrappingKeyPreparedFactory + + CirtuitBootstrappingExecute + + GGSWPreparedFactory + + GGSWNoise + + GLWEEncryptSk + + VecZnxRotateInplace, + BlindRotationKey, BRA>: BlindRotationKeyFactory, /* TODO find a way to remove this bound or move it to CBT KEY */ + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { // Scratch space (4MB) - let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); + let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); let n_glwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_glwe(); let n_lwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_lwe(); let rank: poulpy_core::layouts::Rank = params.cbt_infos.layout_brk.rank; - let module: Module = Module::::new(n_glwe.as_u32() as u64); + let module: Module = Module::::new(n_glwe.as_u32() as u64); let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([1u8; 32]); @@ -208,19 +99,20 @@ where let ct_lwe: LWE> = LWE::alloc_from_infos(¶ms.lwe_infos); // Circuit bootstrapping evaluation key - let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::alloc_from_infos(¶ms.cbt_infos); + cbt_key.encrypt_sk( &module, &sk_lwe, &sk_glwe, - ¶ms.cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); let mut res: GGSW> = GGSW::alloc_from_infos(¶ms.ggsw_infos); - let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); - + let mut cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, BE> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(&module, ¶ms.cbt_infos); + cbt_prepared.prepare(&module, &cbt_key, scratch.borrow()); move || { cbt_prepared.execute_to_constant( &module, @@ -261,7 +153,7 @@ where dnum: 3_u32.into(), rank: 2_u32.into(), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), @@ -269,7 +161,7 @@ where dsize: Dsize(1), rank: 2_u32.into(), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), @@ -280,7 +172,7 @@ where }, }] { let id: BenchmarkId = BenchmarkId::from_parameter(params.name.clone()); - let mut runner = runner::(¶ms); + let mut runner = runner::(¶ms); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 3383114..a7c56bd 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/poulpy-schemes/examples/circuit_bootstrapping.rs @@ -1,9 +1,9 @@ use poulpy_core::{ - GLWEOperations, + GLWENormalize, layouts::{ - AutomorphismKeyLayout, GGSW, GGSWLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWEInfos, LWELayout, - LWEPlaintext, LWESecret, TensorKeyLayout, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWLayout, GLWE, GLWEAutomorphismKeyLayout, GLWELayout, GLWEPlaintext, GLWESecret, GLWETensorKeyLayout, LWE, + LWEInfos, LWELayout, LWEPlaintext, LWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, }; use std::time::Instant; @@ -22,10 +22,7 @@ use poulpy_hal::{ use poulpy_schemes::tfhe::{ blind_rotation::{BlindRotationKeyLayout, CGGI}, - circuit_bootstrapping::{ - CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute, - }, + circuit_bootstrapping::{CircuitBootstrappingKey, CircuitBootstrappingKeyLayout, CircuitBootstrappingKeyPrepared}, }; fn main() { @@ -89,7 +86,7 @@ fn main() { dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_trace.into(), @@ -97,7 +94,7 @@ fn main() { dsize: 1_u32.into(), rank: rank.into(), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -145,7 +142,8 @@ fn main() { // sk_glwe.fill_zero(); // GLWE secret prepared (opaque backend dependant write only struct) - let sk_glwe_prepared: GLWESecretPrepared, BackendImpl> = sk_glwe.prepare_alloc(&module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BackendImpl> = GLWESecretPrepared::alloc(&module, rank.into()); + sk_glwe_prepared.prepare(&module, &sk_glwe); // Plaintext value to circuit bootstrap let data: i64 = 1 % (1 << k_lwe_pt); @@ -175,23 +173,26 @@ fn main() { let now: Instant = Instant::now(); // Circuit bootstrapping evaluation key - let cbt_key: CircuitBootstrappingKey, CGGI> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey, CGGI> = CircuitBootstrappingKey::alloc_from_infos(&cbt_infos); + + cbt_key.encrypt_sk( &module, &sk_lwe, &sk_glwe, - &cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); + println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); // Output GGSW let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) - let cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = - cbt_key.prepare_alloc(&module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(&module, &cbt_infos); + cbt_prepared.prepare(&module, &cbt_key, scratch.borrow()); // Apply circuit bootstrapping: LWE(data * 2^{- (k_lwe_pt + 2)}) -> GGSW(data) let now: Instant = Instant::now(); @@ -234,7 +235,7 @@ fn main() { .for_each(|(x, y)| *y = (x % (1 << (k_glwe_pt - 1))) as i64 - (1 << (k_glwe_pt - 2))); pt_glwe.encode_vec_i64(&data_vec, (k_lwe_pt + 2).into()); - pt_glwe.normalize_inplace(&module, scratch.borrow()); + module.glwe_normalize_inplace(&mut pt_glwe, scratch.borrow()); println!("{}", pt_glwe); @@ -249,7 +250,8 @@ fn main() { ); // Prepare GGSW output of circuit bootstrapping (opaque backend dependant write only struct) - let res_prepared: GGSWPrepared, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); + let mut res_prepared: GGSWPrepared, BackendImpl> = GGSWPrepared::alloc_from_infos(&module, &res); + res_prepared.prepare(&module, &res, scratch.borrow()); // Apply GLWE x GGSW ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs index 830a829..ee3f454 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs @@ -29,7 +29,7 @@ impl BlindRotationKeyFactory for BlindRotationKey { } } -impl BlindRotationKeyEncryptSk for Module +impl BlindRotationKeyEncryptSk for Module where Self: GGSWEncryptSk, Scratch: ScratchTakeCore, diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs index b711f16..fe3e795 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs @@ -15,7 +15,7 @@ use crate::tfhe::blind_rotation::{ utils::set_xai_plus_y, }; -impl BlindRotationKeyPreparedFactory for Module +impl BlindRotationKeyPreparedFactory for Module where Self: GGSWPreparedFactory + SvpPPolAlloc + SvpPrepare, { diff --git a/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs index 99a6e2e..b250e07 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs @@ -10,7 +10,7 @@ use poulpy_core::{ use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey}; -pub trait BlindRotationKeyEncryptSk { +pub trait BlindRotationKeyEncryptSk { fn blind_rotation_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos; @@ -43,7 +43,7 @@ impl BlindRotationKey { S0: GLWESecretPreparedToRef + GLWEInfos, S1: LWESecretToRef + LWEInfos + GetDistribution, Scratch: ScratchTakeCore, - M: BlindRotationKeyEncryptSk, + M: BlindRotationKeyEncryptSk, { module.blind_rotation_key_encrypt_sk(self, sk_glwe, sk_lwe, source_xa, source_xe, scratch); } @@ -53,7 +53,7 @@ impl BlindRotationKey, BRA> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGSWInfos, - M: BlindRotationKeyEncryptSk, + M: BlindRotationKeyEncryptSk, { module.blind_rotation_key_encrypt_sk_tmp_bytes(infos) } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs index 5830c84..c18e71c 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs @@ -3,13 +3,13 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Scratch, SvpPPol}; use std::marker::PhantomData; use poulpy_core::{ - Distribution, ScratchTakeCore, + Distribution, layouts::{Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared}, }; use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyInfos}; -pub trait BlindRotationKeyPreparedFactory { +pub trait BlindRotationKeyPreparedFactory { fn blind_rotation_key_prepared_alloc(&self, infos: &A) -> BlindRotationKeyPrepared, BRA, BE> where A: BlindRotationKeyInfos; @@ -21,27 +21,23 @@ pub trait BlindRotationKeyPreparedFactory { scratch: &mut Scratch, ) where DM: DataMut, - DR: DataRef, - Scratch: ScratchTakeCore; + DR: DataRef; } impl BlindRotationKeyPrepared, BRA, BE> { pub fn alloc(module: &M, infos: &A) -> Self where A: BlindRotationKeyInfos, - M: BlindRotationKeyPreparedFactory, + M: BlindRotationKeyPreparedFactory, { module.blind_rotation_key_prepared_alloc(infos) } } -impl BlindRotationKeyPrepared -where - Scratch: ScratchTakeCore, -{ +impl BlindRotationKeyPrepared { pub fn prepare(&mut self, module: &M, other: &BlindRotationKey, scratch: &mut Scratch) where - M: BlindRotationKeyPreparedFactory, + M: BlindRotationKeyPreparedFactory, { module.blind_rotation_key_prepare(self, other, scratch); } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 8d0a017..1651987 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -24,14 +24,13 @@ pub fn test_blind_rotation( block_size: usize, extension_factor: usize, ) where - M: BlindRotationKeyEncryptSk - + BlindRotationKeyPreparedFactory + M: BlindRotationKeyEncryptSk + + BlindRotationKeyPreparedFactory + BlindRotationExecute - + GLWESecretPreparedFactory - + BlindRotationExecute - + LWEEncryptSk + LookupTableFactory - + GLWEDecrypt, + + GLWESecretPreparedFactory + + GLWEDecrypt + + LWEEncryptSk, BlindRotationKey, BRA>: BlindRotationKeyFactory, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index a8b7212..9838fe0 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -1,185 +1,182 @@ use std::collections::HashMap; use poulpy_hal::{ - api::{ - ScratchAvailable, TakeMatZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice, - VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, - VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ToOwnedDeep}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, + api::{ModuleLogN, ModuleN, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ToOwnedDeep}, }; use poulpy_core::{ - GLWEOperations, TakeGGLWE, TakeGLWE, - layouts::{Dsize, GGLWELayout, GGSWInfos, GLWEInfos, LWEInfos}, + GGSWFromGGLWE, GLWEPacking, GLWETrace, ScratchTakeCore, + layouts::{Dsize, GGLWELayout, GGSWInfos, GGSWToMut, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, LWEToRef}, }; -use poulpy_core::glwe_packing; use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::GLWEAutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{ - BlincRotationExecute, BlindRotationAlgo, BlindRotationKeyPrepared, LookUpTable, LookUpTableRotationDirection, + BlindRotationAlgo, BlindRotationExecute, LookUpTableLayout, LookUpTableRotationDirection, LookupTable, LookupTableFactory, }, - circuit_bootstrapping::{CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute}, + circuit_bootstrapping::CircuitBootstrappingKeyPrepared, }; -impl CirtuitBootstrappingExecute for CircuitBootstrappingKeyPrepared -where - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + VecZnxNormalize, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - Scratch: TakeVecZnx - + TakeVecZnxDftSlice - + TakeVecZnxBig - + TakeVecZnxDft - + TakeMatZnx - + ScratchAvailable - + TakeVecZnxSlice - + TakeSlice, - BlindRotationKeyPrepared: BlincRotationExecute, -{ - fn execute_to_constant( +pub trait CirtuitBootstrappingExecute { + fn circuit_bootstrapping_execute_to_constant( &self, - module: &Module, - res: &mut GGSW, - lwe: &LWE, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared, log_domain: usize, extension_factor: usize, - scratch: &mut Scratch, - ) { + scratch: &mut Scratch, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef; + + #[allow(clippy::too_many_arguments)] + fn circuit_bootstrapping_execute_to_exponent( + &self, + log_gap_out: usize, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef; +} + +impl CircuitBootstrappingKeyPrepared { + pub fn execute_to_constant( + &self, + module: &M, + res: &mut R, + lwe: &L, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) where + M: CirtuitBootstrappingExecute, + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + { + module.circuit_bootstrapping_execute_to_constant(res, lwe, self, log_domain, extension_factor, scratch); + } + + pub fn execute_to_exponent( + &self, + module: &M, + log_gap_out: usize, + res: &mut R, + lwe: &L, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) where + M: CirtuitBootstrappingExecute, + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + { + module.circuit_bootstrapping_execute_to_exponent( + log_gap_out, + res, + lwe, + self, + log_domain, + extension_factor, + scratch, + ); + } +} + +impl CirtuitBootstrappingExecute for Module +where + Self: ModuleN + LookupTableFactory + BlindRotationExecute + GLWETrace + GLWEPacking + GGSWFromGGLWE, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, +{ + fn circuit_bootstrapping_execute_to_constant( + &self, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef, + { circuit_bootstrap_core( false, - module, + self, 0, res, lwe, log_domain, extension_factor, - self, + key, scratch, ); } - fn execute_to_exponent( + fn circuit_bootstrapping_execute_to_exponent( &self, - module: &Module, log_gap_out: usize, - res: &mut GGSW, - lwe: &LWE, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared, log_domain: usize, extension_factor: usize, - scratch: &mut Scratch, - ) { + scratch: &mut Scratch, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef, + { circuit_bootstrap_core( true, - module, + self, log_gap_out, res, lwe, log_domain, extension_factor, - self, + key, scratch, ); } } #[allow(clippy::too_many_arguments)] -pub fn circuit_bootstrap_core( +pub fn circuit_bootstrap_core( to_exponent: bool, - module: &Module, + module: &M, log_gap_out: usize, - res: &mut GGSW, - lwe: &LWE, + res: &mut R, + lwe: &L, log_domain: usize, extension_factor: usize, - key: &CircuitBootstrappingKeyPrepared, - scratch: &mut Scratch, + key: &CircuitBootstrappingKeyPrepared, + scratch: &mut Scratch, ) where - DRes: DataMut, - DLwe: DataRef, - DBrk: DataRef, - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxRotate - + VecZnxNormalize, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - Scratch: TakeVecZnxDftSlice - + TakeVecZnxBig - + TakeVecZnxDft - + TakeVecZnx - + ScratchAvailable - + TakeVecZnxSlice - + TakeMatZnx - + TakeSlice, - BlindRotationKeyPrepared: BlincRotationExecute, + R: GGSWToMut, + L: LWEToRef, + D: DataRef, + M: ModuleN + LookupTableFactory + BlindRotationExecute + GLWETrace + GLWEPacking + GGSWFromGGLWE, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - use poulpy_core::layouts::LWEInfos; + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let lwe: &LWE<&[u8]> = &lwe.to_ref(); - assert_eq!(res.n(), key.brk.n()); - assert_eq!(lwe.base2k(), key.brk.base2k()); - assert_eq!(res.base2k(), key.brk.base2k()); - } + assert_eq!(res.n(), key.brk.n()); + assert_eq!(lwe.base2k(), key.brk.base2k()); + assert_eq!(res.base2k(), key.brk.base2k()); let n: usize = res.n().into(); let base2k: usize = res.base2k().into(); @@ -203,8 +200,15 @@ pub fn circuit_bootstrap_core( }); } + let lut_infos: LookUpTableLayout = LookUpTableLayout { + n: module.n().into(), + extension_factor, + k: (base2k * dnum).into(), + base2k: base2k.into(), + }; + // Lut precision, basically must be able to hold the decomposition power basis of the GGSW - let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, base2k * dnum, extension_factor); + let mut lut: LookupTable = LookupTable::alloc(&lut_infos); lut.set(module, &f, base2k * dnum); if to_exponent { @@ -212,7 +216,7 @@ pub fn circuit_bootstrap_core( } // TODO: separate GGSW k from output of blind rotation k - let (mut res_glwe, scratch_1) = scratch.take_glwe_ct(res); + let (mut res_glwe, scratch_1) = scratch.take_glwe(res); let gglwe_infos: GGLWELayout = GGLWELayout { n: n.into(), @@ -252,7 +256,7 @@ pub fn circuit_bootstrap_core( } if i < dnum { - res_glwe.rotate_inplace(module, -(gap as i64), scratch_2); + module.glwe_rotate_inplace(-(gap as i64), &mut res_glwe, scratch_2); } }); @@ -261,46 +265,24 @@ pub fn circuit_bootstrap_core( } #[allow(clippy::too_many_arguments)] -fn post_process( - module: &Module, - res: &mut GLWE, - a: &GLWE, +fn post_process( + module: &M, + res: &mut R, + a: &A, log_gap_in: usize, log_gap_out: usize, log_domain: usize, - auto_keys: &HashMap, B>>, - scratch: &mut Scratch, + auto_keys: &HashMap, BE>>, + scratch: &mut Scratch, ) where - DataRes: DataMut, - DataA: DataRef, - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotate - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + R: GLWEToMut, + A: GLWEToRef, + M: ModuleLogN + GLWETrace + GLWEPacking, + Scratch: ScratchTakeCore, { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let log_n: usize = module.log_n(); let mut cts: HashMap>> = HashMap::new(); @@ -326,7 +308,7 @@ fn post_process( for i in 0..steps { if i != 0 { - res.rotate_inplace(module, -(1 << log_gap_in), scratch); + module.glwe_rotate_inplace(-(1 << log_gap_in), res, scratch); } cts_vec.push(res.to_owned_deep()); } @@ -335,7 +317,8 @@ fn post_process( cts.insert(i * (1 << log_gap_out), ct); } - glwe_packing(module, &mut cts, log_gap_out, auto_keys, scratch); + module.glwe_pack(&mut cts, log_gap_out, auto_keys, scratch); + let packed: &mut GLWE> = cts.remove(&0).unwrap(); res.trace( module, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 81f99e6..172d430 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,41 +1,38 @@ -use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWE, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey, - TensorKeyLayout, - prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, +use poulpy_core::{ + GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, GetDistribution, ScratchTakeCore, + layouts::{ + GGLWEInfos, GGSWInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecretPreparedFactory, + GLWESecretToRef, GLWETensorKey, GLWETensorKeyLayout, LWEInfos, LWESecretToRef, prepared::GLWESecretPrepared, + }, + trace_galois_elements, }; use std::collections::HashMap; use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, - VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Data, DataRef, Module, Scratch}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::tfhe::blind_rotation::{ - BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyInfos, - BlindRotationKeyLayout, BlindRotationKeyPrepared, + BlindRotationAlgo, BlindRotationKey, BlindRotationKeyEncryptSk, BlindRotationKeyFactory, BlindRotationKeyInfos, + BlindRotationKeyLayout, }; pub trait CircuitBootstrappingKeyInfos { fn brk_infos(&self) -> BlindRotationKeyLayout; - fn atk_infos(&self) -> AutomorphismKeyLayout; - fn tsk_infos(&self) -> TensorKeyLayout; + fn atk_infos(&self) -> GLWEAutomorphismKeyLayout; + fn tsk_infos(&self) -> GLWETensorKeyLayout; } #[derive(Debug, Clone, Copy)] pub struct CircuitBootstrappingKeyLayout { pub layout_brk: BlindRotationKeyLayout, - pub layout_atk: AutomorphismKeyLayout, - pub layout_tsk: TensorKeyLayout, + pub layout_atk: GLWEAutomorphismKeyLayout, + pub layout_tsk: GLWETensorKeyLayout, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { - fn atk_infos(&self) -> AutomorphismKeyLayout { + fn atk_infos(&self) -> GLWEAutomorphismKeyLayout { self.layout_atk } @@ -43,96 +40,114 @@ impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { self.layout_brk } - fn tsk_infos(&self) -> TensorKeyLayout { + fn tsk_infos(&self) -> GLWETensorKeyLayout { self.layout_tsk } } -pub trait CircuitBootstrappingKeyEncryptSk { +pub trait CircuitBootstrappingKeyEncryptSk { #[allow(clippy::too_many_arguments)] - fn encrypt_sk( - module: &Module, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, - cbt_infos: &INFOS, + fn circuit_bootstrapping_key_encrypt_sk( + &self, + res: &mut CircuitBootstrappingKey, + sk_lwe: &S0, + sk_glwe: &S1, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, - ) -> Self + scratch: &mut Scratch, + ) where + D: DataMut, + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution; +} + +impl CircuitBootstrappingKey, BRA> { + pub fn alloc_from_infos(infos: &A) -> Self where - INFOS: CircuitBootstrappingKeyInfos, - DLwe: DataRef, - DGlwe: DataRef; + A: CircuitBootstrappingKeyInfos, + BlindRotationKey, BRA>: BlindRotationKeyFactory, + { + let atk_infos: &GLWEAutomorphismKeyLayout = &infos.atk_infos(); + let brk_infos: &BlindRotationKeyLayout = &infos.brk_infos(); + let trk_infos: &GLWETensorKeyLayout = &infos.tsk_infos(); + let gal_els: Vec = trace_galois_elements(atk_infos.log_n(), 2 * atk_infos.n().as_usize() as i64); + + Self { + brk: , BRA> as BlindRotationKeyFactory>::blind_rotation_key_alloc(brk_infos), + atk: gal_els + .iter() + .map(|&gal_el| { + let key = GLWEAutomorphismKey::alloc_from_infos(atk_infos); + (gal_el, key) + }) + .collect(), + tsk: GLWETensorKey::alloc_from_infos(trk_infos), + } + } } pub struct CircuitBootstrappingKey { pub(crate) brk: BlindRotationKey, - pub(crate) tsk: TensorKey>, - pub(crate) atk: HashMap>>, + pub(crate) tsk: GLWETensorKey>, + pub(crate) atk: HashMap>>, } -impl CircuitBootstrappingKeyEncryptSk for CircuitBootstrappingKey, BRA> -where - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, - Module: SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxAutomorphism, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, -{ - fn encrypt_sk( - module: &Module, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, - cbt_infos: &INFOS, +impl CircuitBootstrappingKey { + pub fn encrypt_sk( + &mut self, + module: &M, + sk_lwe: &S0, + sk_glwe: &S1, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, - ) -> Self - where - INFOS: CircuitBootstrappingKeyInfos, - DLwe: DataRef, - DGlwe: DataRef, + scratch: &mut Scratch, + ) where + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution, + M: CircuitBootstrappingKeyEncryptSk, { - assert_eq!(sk_lwe.n(), cbt_infos.brk_infos().n_lwe()); - assert_eq!(sk_glwe.n(), cbt_infos.brk_infos().n_glwe()); - assert_eq!(sk_glwe.n(), cbt_infos.atk_infos().n()); - assert_eq!(sk_glwe.n(), cbt_infos.tsk_infos().n()); + module.circuit_bootstrapping_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); + } +} - let atk_infos: AutomorphismKeyLayout = cbt_infos.atk_infos(); - let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos(); - let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); +impl CircuitBootstrappingKeyEncryptSk for Module +where + Self: GLWETensorKeyEncryptSk + + BlindRotationKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + + GLWESecretPreparedFactory, + Scratch: ScratchTakeCore, +{ + fn circuit_bootstrapping_key_encrypt_sk( + &self, + res: &mut CircuitBootstrappingKey, + sk_lwe: &S0, + sk_glwe: &S1, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + D: DataMut, + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution, + { + let brk_infos: &BlindRotationKeyLayout = &res.brk_infos(); + let atk_infos: &GLWEAutomorphismKeyLayout = &res.atk_infos(); + let tsk_infos: &GLWETensorKeyLayout = &res.tsk_infos(); - let mut auto_keys: HashMap>> = HashMap::new(); - let gal_els: Vec = GLWE::trace_galois_elements(module); - gal_els.iter().for_each(|gal_el| { - let mut key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); - key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch); - auto_keys.insert(*gal_el, key); - }); + assert_eq!(sk_lwe.n(), brk_infos.n_lwe()); + assert_eq!(sk_glwe.n(), brk_infos.n_glwe()); + assert_eq!(sk_glwe.n(), atk_infos.n()); + assert_eq!(sk_glwe.n(), tsk_infos.n()); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch); + for (p, atk) in res.atk.iter_mut() { + atk.encrypt_sk(self, *p, sk_glwe, source_xa, source_xe, scratch); + } - let mut brk: BlindRotationKey, BRA> = BlindRotationKey::, BRA>::alloc(&brk_infos); - brk.encrypt_sk( - module, + let sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(self, brk_infos.rank()); + + res.brk.encrypt_sk( + self, &sk_glwe_prepared, sk_lwe, source_xa, @@ -140,27 +155,15 @@ where scratch, ); - let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&trk_infos); - tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); - - Self { - brk, - atk: auto_keys, - tsk, - } + res.tsk + .encrypt_sk(self, sk_glwe, source_xa, source_xe, scratch); } } -pub struct CircuitBootstrappingKeyPrepared { - pub(crate) brk: BlindRotationKeyPrepared, - pub(crate) tsk: TensorKeyPrepared, B>, - pub(crate) atk: HashMap, B>>, -} - -impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared { - fn atk_infos(&self) -> AutomorphismKeyLayout { +impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKey { + fn atk_infos(&self) -> GLWEAutomorphismKeyLayout { let (_, atk) = self.atk.iter().next().expect("atk is empty"); - AutomorphismKeyLayout { + GLWEAutomorphismKeyLayout { n: atk.n(), base2k: atk.base2k(), k: atk.k(), @@ -181,8 +184,8 @@ impl CircuitBootstrappingKeyInfo } } - fn tsk_infos(&self) -> TensorKeyLayout { - TensorKeyLayout { + fn tsk_infos(&self) -> GLWETensorKeyLayout { + GLWETensorKeyLayout { n: self.tsk.n(), base2k: self.tsk.base2k(), k: self.tsk.k(), @@ -192,22 +195,3 @@ impl CircuitBootstrappingKeyInfo } } } - -impl PrepareAlloc, BRA, B>> - for CircuitBootstrappingKey -where - Module: VmpPMatAlloc + VmpPrepare, - BlindRotationKey: PrepareAlloc, BRA, B>>, - TensorKey: PrepareAlloc, B>>, - AutomorphismKey: PrepareAlloc, B>>, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> CircuitBootstrappingKeyPrepared, BRA, B> { - let brk: BlindRotationKeyPrepared, BRA, B> = self.brk.prepare_alloc(module, scratch); - let tsk: TensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); - let mut atk: HashMap, B>> = HashMap::new(); - for (key, value) in &self.atk { - atk.insert(*key, value.prepare_alloc(module, scratch)); - } - CircuitBootstrappingKeyPrepared { brk, tsk, atk } - } -} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_compressed.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_compressed.rs new file mode 100644 index 0000000..223c193 --- /dev/null +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_compressed.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; + +use poulpy_core::layouts::{GLWEAutomorphismKeyCompressed, GLWETensorKeyCompressed}; +use poulpy_hal::layouts::Data; + +use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyCompressed}; + +#[allow(dead_code)] +pub struct CircuitBootstrappingKey { + pub(crate) brk: BlindRotationKeyCompressed, + pub(crate) tsk: GLWETensorKeyCompressed>, + pub(crate) atk: HashMap>>, +} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs new file mode 100644 index 0000000..db39a45 --- /dev/null +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs @@ -0,0 +1,136 @@ +use poulpy_core::{ + layouts::{ + GGLWEInfos, GGSWInfos, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWEInfos, GLWETensorKeyLayout, + GLWETensorKeyPreparedFactory, LWEInfos, + prepared::{GLWEAutomorphismKeyPrepared, GLWETensorKeyPrepared}, + }, + trace_galois_elements, +}; +use std::collections::HashMap; + +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; + +use crate::tfhe::{ + blind_rotation::{ + BlindRotationAlgo, BlindRotationKeyInfos, BlindRotationKeyLayout, BlindRotationKeyPrepared, + BlindRotationKeyPreparedFactory, + }, + circuit_bootstrapping::{CircuitBootstrappingKey, CircuitBootstrappingKeyInfos}, +}; + +impl CircuitBootstrappingKeyPrepared, BRA, BE> { + pub fn alloc_from_infos(module: &M, infos: &A) -> CircuitBootstrappingKeyPrepared, BRA, BE> + where + A: CircuitBootstrappingKeyInfos, + M: CircuitBootstrappingKeyPreparedFactory, + { + module.circuit_bootstrapping_key_prepared_alloc_from_infos(infos) + } +} + +impl CircuitBootstrappingKeyPrepared { + pub fn prepare(&mut self, module: &M, other: &CircuitBootstrappingKey, scratch: &mut Scratch) + where + DR: DataRef, + M: CircuitBootstrappingKeyPreparedFactory, + { + module.circuit_bootstrapping_key_prepare(self, other, scratch); + } +} + +impl CircuitBootstrappingKeyPreparedFactory for Module where + Self: Sized + + BlindRotationKeyPreparedFactory + + GLWETensorKeyPreparedFactory + + GLWEAutomorphismKeyPreparedFactory +{ +} + +pub trait CircuitBootstrappingKeyPreparedFactory +where + Self: Sized + + BlindRotationKeyPreparedFactory + + GLWETensorKeyPreparedFactory + + GLWEAutomorphismKeyPreparedFactory, +{ + fn circuit_bootstrapping_key_prepared_alloc_from_infos( + &self, + infos: &A, + ) -> CircuitBootstrappingKeyPrepared, BRA, BE> + where + A: CircuitBootstrappingKeyInfos, + { + let atk_infos: &GLWEAutomorphismKeyLayout = &infos.atk_infos(); + let gal_els: Vec = trace_galois_elements(atk_infos.log_n(), 2 * atk_infos.n().as_usize() as i64); + + CircuitBootstrappingKeyPrepared { + brk: BlindRotationKeyPrepared::alloc(self, &infos.brk_infos()), + tsk: GLWETensorKeyPrepared::alloc_from_infos(self, &infos.tsk_infos()), + atk: gal_els + .iter() + .map(|&gal_el| { + let key = GLWEAutomorphismKeyPrepared::alloc_from_infos(self, atk_infos); + (gal_el, key) + }) + .collect(), + } + } + fn circuit_bootstrapping_key_prepare( + &self, + res: &mut CircuitBootstrappingKeyPrepared, + other: &CircuitBootstrappingKey, + scratch: &mut Scratch, + ) where + DM: DataMut, + DR: DataRef, + { + res.brk.prepare(self, &other.brk, scratch); + res.tsk.prepare(self, &other.tsk, scratch); + + for (k, a) in res.atk.iter_mut() { + a.prepare(self, other.atk.get(k).unwrap(), scratch); + } + } +} + +pub struct CircuitBootstrappingKeyPrepared { + pub(crate) brk: BlindRotationKeyPrepared, + pub(crate) tsk: GLWETensorKeyPrepared, B>, + pub(crate) atk: HashMap, B>>, +} + +impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared { + fn atk_infos(&self) -> GLWEAutomorphismKeyLayout { + let (_, atk) = self.atk.iter().next().expect("atk is empty"); + GLWEAutomorphismKeyLayout { + n: atk.n(), + base2k: atk.base2k(), + k: atk.k(), + dnum: atk.dnum(), + dsize: atk.dsize(), + rank: atk.rank(), + } + } + + fn brk_infos(&self) -> BlindRotationKeyLayout { + BlindRotationKeyLayout { + n_glwe: self.brk.n_glwe(), + n_lwe: self.brk.n_lwe(), + base2k: self.brk.base2k(), + k: self.brk.k(), + dnum: self.brk.dnum(), + rank: self.brk.rank(), + } + } + + fn tsk_infos(&self) -> GLWETensorKeyLayout { + GLWETensorKeyLayout { + n: self.tsk.n(), + base2k: self.tsk.base2k(), + k: self.tsk.k(), + dnum: self.tsk.dnum(), + dsize: self.tsk.dsize(), + rank: self.tsk.rank(), + } + } +} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs index ed3b6a1..e857cd0 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -1,36 +1,12 @@ mod circuit; mod key; +mod key_compressed; +mod key_prepared; -//[cfg(tests)] -//pub mod tests; +#[cfg(test)] +pub mod tests; pub use circuit::*; pub use key::*; - -use poulpy_core::layouts::{GGSW, LWE}; - -use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; - -pub trait CirtuitBootstrappingExecute { - fn execute_to_constant( - &self, - module: &Module, - res: &mut GGSW, - lwe: &LWE, - log_domain: usize, - extension_factor: usize, - scratch: &mut Scratch, - ); - - #[allow(clippy::too_many_arguments)] - fn execute_to_exponent( - &self, - module: &Module, - log_gap_out: usize, - res: &mut GGSW, - lwe: &LWE, - log_domain: usize, - extension_factor: usize, - scratch: &mut Scratch, - ); -} +// pub use key_compressed::*; +pub use key_prepared::*; 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 cfe238f..77469d1 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -1,108 +1,49 @@ use std::time::Instant; use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, - VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, - ZnNormalizeInplace, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned, ZnxView, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, - TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, - }, + api::{ModuleN, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + layouts::{Backend, ScalarZnx, Scratch, ScratchOwned, ZnxView, ZnxViewMut}, source::Source, }; use crate::tfhe::{ - blind_rotation::{ - BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, - BlindRotationKeyLayout, BlindRotationKeyPrepared, - }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyLayout}, circuit_bootstrapping::{ CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute, + CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, }, }; -use poulpy_core::layouts::{AutomorphismKeyLayout, Dsize, GGSWLayout, LWELayout, TensorKeyLayout, prepared::PrepareAlloc}; +use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWEExternalProduct, LWEEncryptSk, ScratchTakeCore, + layouts::{ + Dsize, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWESecretPreparedFactory, GLWETensorKeyLayout, + LWELayout, + }, +}; use poulpy_core::layouts::{ GGSW, GLWE, GLWEPlaintext, GLWESecret, LWE, LWEPlaintext, LWESecret, prepared::{GGSWPrepared, GLWESecretPrepared}, }; -pub fn test_circuit_bootstrapping_to_exponent(module: &Module) +pub fn test_circuit_bootstrapping_to_exponent(module: &M) where - Module: VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, - BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + M: ModuleN + + GLWESecretPreparedFactory + + GLWEExternalProduct + + GLWEDecrypt + + LWEEncryptSk + + CircuitBootstrappingKeyEncryptSk + + CircuitBootstrappingKeyPreparedFactory + + CirtuitBootstrappingExecute + + GGSWPreparedFactory + + GGSWNoise + + GLWEEncryptSk + + VecZnxRotateInplace, + BlindRotationKey, BRA>: BlindRotationKeyFactory, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { let n_glwe: usize = module.n(); let base2k: usize = 17; @@ -141,7 +82,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -149,7 +90,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -168,7 +109,7 @@ where rank: rank.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 23); + let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 23); let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([1u8; 32]); @@ -180,7 +121,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_glwe_prepared.prepare(module, &sk_glwe); let data: i64 = 1; @@ -193,22 +135,27 @@ where ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now(); - let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::alloc_from_infos(&cbt_infos); + println!("CBT-ALLOC: {} ms", now.elapsed().as_millis()); + + let now: Instant = Instant::now(); + cbt_key.encrypt_sk( module, &sk_lwe, &sk_glwe, - &cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); - println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); + println!("CBT-ENCRYPT: {} ms", now.elapsed().as_millis()); let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let log_gap_out = 1; - let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, BE> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(module, &cbt_infos); + cbt_prepared.prepare(module, &cbt_key, scratch.borrow()); let now: Instant = Instant::now(); cbt_prepared.execute_to_exponent( @@ -247,7 +194,8 @@ where scratch.borrow(), ); - let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let mut res_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &res); + res_prepared.prepare(module, &res, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); @@ -260,71 +208,23 @@ where assert_eq!(pt_res.data.at(0, 0), pt_want); } -pub fn test_circuit_bootstrapping_to_constant(module: &Module) +pub fn test_circuit_bootstrapping_to_constant(module: &M) where - Module: VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, - BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + M: ModuleN + + GLWESecretPreparedFactory + + GLWEExternalProduct + + GLWEDecrypt + + LWEEncryptSk + + CircuitBootstrappingKeyEncryptSk + + CircuitBootstrappingKeyPreparedFactory + + CirtuitBootstrappingExecute + + GGSWPreparedFactory + + GGSWNoise + + GLWEEncryptSk + + VecZnxRotateInplace, + BlindRotationKey, BRA>: BlindRotationKeyFactory, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { let n_glwe: usize = module.n(); let base2k: usize = 14; @@ -363,7 +263,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -371,7 +271,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -390,7 +290,7 @@ where rank: rank.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 23); + let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 23); let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([1u8; 32]); @@ -402,7 +302,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_glwe_prepared.prepare(module, &sk_glwe); let data: i64 = 1; @@ -415,20 +316,25 @@ where ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now(); - let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::alloc_from_infos(&cbt_infos); + println!("CBT-ALLOC: {} ms", now.elapsed().as_millis()); + + let now: Instant = Instant::now(); + cbt_key.encrypt_sk( module, &sk_lwe, &sk_glwe, - &cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); - println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); + println!("CBT-ENCRYPT: {} ms", now.elapsed().as_millis()); let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); - let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, BE> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(module, &cbt_infos); + cbt_prepared.prepare(module, &cbt_key, scratch.borrow()); let now: Instant = Instant::now(); cbt_prepared.execute_to_constant( @@ -460,7 +366,8 @@ where scratch.borrow(), ); - let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let mut res_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &res); + res_prepared.prepare(module, &res, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/fft64.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/fft64.rs new file mode 100644 index 0000000..a1fdab2 --- /dev/null +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/fft64.rs @@ -0,0 +1,21 @@ +use poulpy_backend::cpu_fft64_ref::FFT64Ref; +use poulpy_hal::{api::ModuleNew, layouts::Module}; + +use crate::tfhe::{ + blind_rotation::CGGI, + circuit_bootstrapping::tests::circuit_bootstrapping::{ + test_circuit_bootstrapping_to_constant, test_circuit_bootstrapping_to_exponent, + }, +}; + +#[test] +fn test_to_constant_cggi() { + let module: Module = Module::::new(256); + test_circuit_bootstrapping_to_constant::(&module); +} + +#[test] +fn test_to_exponent_cggi() { + let module: Module = Module::::new(256); + test_circuit_bootstrapping_to_exponent::(&module); +} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs deleted file mode 100644 index 3661f81..0000000 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs +++ /dev/null @@ -1,21 +0,0 @@ -use poulpy_backend::cpu_spqlios::FFT64Spqlios; -use poulpy_hal::{api::ModuleNew, layouts::Module}; - -use crate::tfhe::{ - blind_rotation::CGGI, - circuit_bootstrapping::tests::circuit_bootstrapping::{ - test_circuit_bootstrapping_to_constant, test_circuit_bootstrapping_to_exponent, - }, -}; - -#[test] -fn test_to_constant() { - let module: Module = Module::::new(256); - test_circuit_bootstrapping_to_constant::(&module); -} - -#[test] -fn test_to_exponent() { - let module: Module = Module::::new(256); - test_circuit_bootstrapping_to_exponent::(&module); -} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs deleted file mode 100644 index aebaafb..0000000 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod fft64; diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs deleted file mode 100644 index f2bc1d4..0000000 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod cpu_spqlios; diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs index 22f8f4f..893a6be 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs @@ -1,3 +1,3 @@ pub mod circuit_bootstrapping; -mod implementation; +mod fft64; diff --git a/poulpy-schemes/src/tfhe/mod.rs b/poulpy-schemes/src/tfhe/mod.rs index cc2dbe9..b91a937 100644 --- a/poulpy-schemes/src/tfhe/mod.rs +++ b/poulpy-schemes/src/tfhe/mod.rs @@ -1,3 +1,3 @@ // pub mod bdd_arithmetic; pub mod blind_rotation; -//pub mod circuit_bootstrapping; +pub mod circuit_bootstrapping;