From 252eda36fe0c0e5e5c8b630d7f333b4862b12c6c Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 15:32:52 +0200 Subject: [PATCH] keyswitch tests --- poulpy-core/src/automorphism/gglwe_atk.rs | 104 +++++++++--------- poulpy-core/src/automorphism/ggsw_ct.rs | 19 ++-- poulpy-core/src/automorphism/glwe_ct.rs | 57 +++++----- poulpy-core/src/conversion/gglwe_to_ggsw.rs | 6 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 12 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 12 +- poulpy-core/src/decryption/lwe_ct.rs | 20 ++-- poulpy-core/src/decryption/mod.rs | 2 +- .../src/encryption/compressed/gglwe_atk.rs | 47 +++++--- .../src/encryption/compressed/gglwe_ksk.rs | 24 ++-- .../src/encryption/compressed/gglwe_tsk.rs | 50 +++++---- .../src/encryption/compressed/ggsw_ct.rs | 37 +++---- poulpy-core/src/encryption/gglwe_atk.rs | 45 +++++--- poulpy-core/src/encryption/gglwe_ksk.rs | 16 +-- poulpy-core/src/encryption/gglwe_tsk.rs | 25 +++-- poulpy-core/src/encryption/glwe_ct.rs | 14 +-- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 43 +++++--- poulpy-core/src/encryption/lwe_ct.rs | 4 +- poulpy-core/src/encryption/lwe_ksk.rs | 13 +-- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 36 +++--- poulpy-core/src/external_product/gglwe_atk.rs | 8 +- poulpy-core/src/external_product/gglwe_ksk.rs | 8 +- poulpy-core/src/glwe_packing.rs | 13 +-- poulpy-core/src/glwe_trace.rs | 12 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 36 +++--- poulpy-core/src/keyswitching/ggsw_ct.rs | 16 ++- poulpy-core/src/keyswitching/glwe_ct.rs | 34 +++--- poulpy-core/src/keyswitching/lwe_ct.rs | 14 +-- .../src/layouts/compressed/gglwe_atk.rs | 53 ++++++--- .../src/layouts/compressed/gglwe_ct.rs | 3 +- .../src/layouts/compressed/gglwe_ksk.rs | 96 ++++++++-------- .../src/layouts/compressed/gglwe_tsk.rs | 46 +++++--- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 4 +- poulpy-core/src/layouts/compressed/glwe_ct.rs | 1 - .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 42 +++---- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 41 +++---- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 42 +++---- poulpy-core/src/layouts/gglwe_atk.rs | 41 ++----- poulpy-core/src/layouts/gglwe_ksk.rs | 86 ++++++--------- poulpy-core/src/layouts/gglwe_tsk.rs | 23 ++-- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 40 +++---- poulpy-core/src/layouts/lwe_ksk.rs | 50 +++++---- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 40 +++---- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 59 ++++------ poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 78 ++++++------- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 39 +++---- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 50 ++++----- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 50 ++++----- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 50 ++++----- poulpy-core/src/lib.rs | 1 + poulpy-core/src/scratch.rs | 32 +++--- poulpy-core/src/tests/mod.rs | 20 ++-- .../tests/test_suite/encryption/gglwe_atk.rs | 13 ++- .../tests/test_suite/encryption/gglwe_ct.rs | 2 +- .../tests/test_suite/encryption/glwe_tsk.rs | 12 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 28 +++-- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 69 ++++++++---- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 15 ++- poulpy-hal/src/layouts/module.rs | 6 +- poulpy-hal/src/layouts/znx_base.rs | 4 +- 60 files changed, 918 insertions(+), 945 deletions(-) diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index db57e60..b768b81 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,10 +7,8 @@ use crate::{ ScratchTakeCore, automorphism::glwe_ct::GLWEAutomorphism, layouts::{ - AutomorphismKey, AutomorphismKeyToMut, AutomorphismKeyToRef, GGLWEInfos, GLWE, GLWEInfos, - prepared::{ - AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement, SetAutomorphismGaloisElement, - }, + AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE, + prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }, }; @@ -29,8 +27,8 @@ impl AutomorphismKey> { impl AutomorphismKey { pub fn automorphism(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where - A: AutomorphismKeyToRef + GetAutomorphismGaloisElement, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: AutomorphismKeyAutomorphism, { @@ -39,7 +37,7 @@ impl AutomorphismKey { pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: AutomorphismKeyAutomorphism, { @@ -67,35 +65,34 @@ where fn automorphism_key_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) where - R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, - A: AutomorphismKeyToRef + GetAutomorphismGaloisElement, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, + A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { + assert!( + res.dnum().as_u32() <= a.dnum().as_u32(), + "res dnum: {} > a dnum: {}", + res.dnum(), + a.dnum() + ); + + assert_eq!( + res.dsize(), + a.dsize(), + "res dnum: {} != a dnum: {}", + res.dsize(), + a.dsize() + ); + + let cols_out: usize = (key.rank_out() + 1).into(); + + let p: i64 = a.p(); + let p_inv: i64 = self.galois_element_inv(p); + { - let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); - let a: &AutomorphismKey<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref(); - - assert!( - res.dnum().as_u32() <= a.dnum().as_u32(), - "res dnum: {} > a dnum: {}", - res.dnum(), - a.dnum() - ); - - assert_eq!( - res.dsize(), - a.dsize(), - "res dnum: {} != a dnum: {}", - res.dsize(), - a.dsize() - ); - - let cols_out: usize = (key.rank_out() + 1).into(); - - let p: i64 = a.p(); - let p_inv: i64 = self.galois_element_inv(p); + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GGLWE<&[u8]> = &a.to_ref(); for row in 0..res.dnum().as_usize() { for col in 0..cols_out { @@ -104,11 +101,11 @@ where // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) for i in 0..cols_out { - self.vec_znx_automorphism(a.p(), res_tmp.data_mut(), i, &a_ct.data, i); + self.vec_znx_automorphism(p, res_tmp.data_mut(), i, &a_ct.data, i); } // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) - self.glwe_keyswitch_inplace(&mut res_tmp, &key.key, scratch); + self.glwe_keyswitch_inplace(&mut res_tmp, key, scratch); // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) (0..cols_out).for_each(|i| { @@ -118,32 +115,29 @@ where } } - res.set_p((a.p() * key.p()) % (self.cyclotomic_order() as i64)); + 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) where - R: AutomorphismKeyToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + R: GGLWEToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { + assert_eq!( + res.rank(), + key.rank(), + "key rank: {} != key rank: {}", + res.rank(), + key.rank() + ); + + let cols_out: usize = (key.rank_out() + 1).into(); + let p: i64 = res.p(); + let p_inv: i64 = self.galois_element_inv(p); + { - let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref(); - - assert_eq!( - res.rank(), - key.rank(), - "key rank: {} != key rank: {}", - res.rank(), - key.rank() - ); - - let cols_out: usize = (key.rank_out() + 1).into(); - - let p: i64 = res.p(); - let p_inv: i64 = self.galois_element_inv(p); - + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); for row in 0..res.dnum().as_usize() { for col in 0..cols_out { let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); @@ -154,7 +148,7 @@ where } // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) - self.glwe_keyswitch_inplace(&mut res_tmp, &key.key, scratch); + self.glwe_keyswitch_inplace(&mut res_tmp, key, scratch); // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) for i in 0..cols_out { diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index 8035063..58b8b53 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -7,8 +7,8 @@ use crate::{ GGSWExpandRows, ScratchTakeCore, automorphism::glwe_ct::GLWEAutomorphism, layouts::{ - GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, - prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, TensorKeyPrepared, TensorKeyPreparedToRef}, + GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GetAutomorphismGaloisElement, + prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, }, }; @@ -35,7 +35,7 @@ impl GGSW { pub fn automorphism(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where A: GGSWToRef, - K: AutomorphismKeyPreparedToRef, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, @@ -45,7 +45,7 @@ impl GGSW { pub fn automorphism_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where - K: AutomorphismKeyPreparedToRef, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, @@ -78,18 +78,16 @@ where where R: GGSWToMut, A: GGSWToRef, - K: AutomorphismKeyPreparedToRef, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let a: &GGSW<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); - assert_eq!(res.ggsw_layout(), a.ggsw_layout()); - assert_eq!(res.glwe_layout(), a.glwe_layout()); - assert_eq!(res.lwe_layout(), a.lwe_layout()); + assert_eq!(res.dsize(), a.dsize()); + assert!(res.dnum() <= a.dnum()); assert!(scratch.available() >= self.ggsw_automorphism_tmp_bytes(res, a, key, tsk)); // Keyswitch the j-th row of the col 0 @@ -105,12 +103,11 @@ where fn ggsw_automorphism_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, - K: AutomorphismKeyPreparedToRef, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); // Keyswitch the j-th row of the col 0 diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index d989891..5d973b6 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -9,8 +9,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, keyswitch_internal, layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement, }, }; @@ -31,7 +30,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism(self, a, key, scratch); @@ -41,7 +40,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_add(self, a, key, scratch); @@ -51,7 +50,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub(self, a, key, scratch); @@ -61,7 +60,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_negate(self, a, key, scratch); @@ -70,7 +69,7 @@ impl GLWE { pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_inplace(self, key, scratch); @@ -79,7 +78,7 @@ impl GLWE { pub fn automorphism_add_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_add_inplace(self, key, scratch); @@ -88,7 +87,7 @@ impl GLWE { pub fn automorphism_sub_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_inplace(self, key, scratch); @@ -97,7 +96,7 @@ impl GLWE { pub fn automorphism_sub_negate_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_negate_inplace(self, key, scratch); @@ -125,10 +124,10 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { - self.glwe_keyswitch(res, a, &key.to_ref().key, scratch); + self.glwe_keyswitch(res, a, key, scratch); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -140,10 +139,10 @@ where fn glwe_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { - self.glwe_keyswitch_inplace(res, &key.to_ref().key, scratch); + self.glwe_keyswitch_inplace(res, key, scratch); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -156,15 +155,14 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -184,14 +182,13 @@ where fn glwe_automorphism_add_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -212,15 +209,14 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -241,15 +237,14 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -269,14 +264,13 @@ where fn glwe_automorphism_sub_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -296,14 +290,13 @@ where fn glwe_automorphism_sub_negate_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs index 3447d61..7ad73e6 100644 --- a/poulpy-core/src/conversion/gglwe_to_ggsw.rs +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -119,10 +119,6 @@ where + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes + VecZnxDftApply + VecZnxDftCopy + VmpApplyDftToDft @@ -236,7 +232,7 @@ where // = // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2) for col_i in 1..cols { - let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j]) + let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).data; // Selects Enc(s[i]s[j]) // Extracts a[i] and multipies with Enc(s[i]s[j]) for di in 0..dsize { diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 17ca1bf..276010d 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -5,10 +5,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, - layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank, - prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank}, }; pub trait LWESampleExtract @@ -71,12 +68,11 @@ where where R: LWEToMut, A: GLWEToRef, - K: LWEToGLWESwitchingKeyPreparedToRef + GGLWEInfos, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let key: &LWEToGLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); assert_eq!(a.n(), self.n() as u32); assert_eq!(key.n(), self.n() as u32); @@ -90,7 +86,7 @@ where }; let (mut tmp_glwe, scratch_1) = scratch.take_glwe_ct(self, &glwe_layout); - self.glwe_keyswitch(&mut tmp_glwe, a, &key.0, scratch_1); + self.glwe_keyswitch(&mut tmp_glwe, a, key, scratch_1); self.lwe_sample_extract(res, &tmp_glwe); } } @@ -120,7 +116,7 @@ impl LWE { where R: LWEToMut, A: GLWEToRef, - K: LWEToGLWESwitchingKeyPreparedToRef + GGLWEInfos, + K: GGLWEPreparedToRef + GGLWEInfos, M: LWEFromGLWE, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index 22f38ed..7d72ef5 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -5,10 +5,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, - layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef, - prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef}, }; impl GLWEFromLWE for Module where Self: GLWEKeyswitch {} @@ -43,12 +40,11 @@ where where R: GLWEToMut, A: LWEToRef, - K: LWEToGLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let lwe: &LWE<&[u8]> = &lwe.to_ref(); - let ksk: &LWEToGLWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref(); assert_eq!(res.n(), self.n() as u32); assert_eq!(ksk.n(), self.n() as u32); @@ -108,7 +104,7 @@ where ); } - self.glwe_keyswitch(res, &glwe, &ksk.0, scratch_1); + self.glwe_keyswitch(res, &glwe, ksk, scratch_1); } } @@ -129,7 +125,7 @@ impl GLWE { where M: GLWEFromLWE, A: LWEToRef, - K: LWEToGLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_from_lwe(self, lwe, ksk, scratch); diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe_ct.rs index a847139..edd727b 100644 --- a/poulpy-core/src/decryption/lwe_ct.rs +++ b/poulpy-core/src/decryption/lwe_ct.rs @@ -1,33 +1,39 @@ use poulpy_hal::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnNormalizeInplace}, layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, ZnxView, ZnxViewMut}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToMut, LWESecret, LWESecretToRef, LWEToMut}; impl LWE { - pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: S) + pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S) where P: LWEPlaintextToMut, S: LWESecretToRef, M: LWEDecrypt, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { module.lwe_decrypt(self, pt, sk); } } -pub trait LWEDecrypt +pub trait LWEDecrypt { + fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: &S) + where + R: LWEToMut, + P: LWEPlaintextToMut, + S: LWESecretToRef; +} + +impl LWEDecrypt for Module where Self: Sized + ZnNormalizeInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { - fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: S) + fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: &S) where R: LWEToMut, P: LWEPlaintextToMut, S: LWESecretToRef, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut(); @@ -57,5 +63,3 @@ where pt.k = crate::layouts::TorusPrecision(res.k().0.min(pt.size() as u32 * res.base2k().0)); } } - -impl LWEDecrypt for Module where Self: Sized + ZnNormalizeInplace {} diff --git a/poulpy-core/src/decryption/mod.rs b/poulpy-core/src/decryption/mod.rs index 0af7985..03e5fd4 100644 --- a/poulpy-core/src/decryption/mod.rs +++ b/poulpy-core/src/decryption/mod.rs @@ -2,4 +2,4 @@ mod glwe_ct; mod lwe_ct; pub use glwe_ct::*; -// pub use lwe_ct::*; +pub use lwe_ct::*; diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 668a133..d6aa85f 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -5,11 +5,10 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, - encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, + GGLWECompressedEncryptSk, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut}, + GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared, + GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, compressed::AutomorphismKeyCompressed, }, }; @@ -34,7 +33,7 @@ impl AutomorphismKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - S: GLWESecretToRef, + S: GLWESecretToRef + GLWEInfos, M: AutomorphismKeyCompressedEncryptSk, { module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); @@ -55,20 +54,28 @@ pub trait AutomorphismKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: AutomorphismKeyCompressedToMut, - S: GLWESecretToRef; + R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos, + S: GLWESecretToRef + GLWEInfos; } impl AutomorphismKeyCompressedEncryptSk for Module where - Self: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk, + Self: ModuleN + + GaloisElement + + VecZnxAutomorphism + + GGLWECompressedEncryptSk + + GLWESecretPreparedAlloc + + GLWESecretPrepare, Scratch: ScratchTakeCore, { fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of(self.n().into(), infos.rank()) + assert_eq!(self.n() as u32, infos.n()); + self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) + .max(GLWESecret::bytes_of_from_infos(infos)) + + GLWESecretPrepared::bytes_of_from_infos(self, infos) } fn automorphism_key_compressed_encrypt_sk( @@ -80,12 +87,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: AutomorphismKeyCompressedToMut, - S: GLWESecretToRef, + R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos, + S: GLWESecretToRef + GLWEInfos, { - let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); - assert_eq!(res.n(), sk.n()); assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); @@ -96,9 +101,9 @@ where AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) ); - let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); - + let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); { + let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank()); for i in 0..res.rank_out().into() { self.vec_znx_automorphism( self.galois_element_inv(p), @@ -108,10 +113,18 @@ where i, ); } + sk_out_prepared.prepare(self, &sk_out); } - self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); + self.gglwe_compressed_encrypt_sk( + res, + &sk.data, + &sk_out_prepared, + seed_xa, + source_xe, + scratch_1, + ); - res.p = p; + res.set_p(p); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 7825e69..7067250 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -8,8 +8,9 @@ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, + GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, + GLWESwitchingKeyDegreesMut, LWEInfos, + compressed::GLWESwitchingKeyCompressed, prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; @@ -57,7 +58,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWESwitchingKeyCompressedToMut, + R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: GLWESecretToRef, S2: GLWESecretToRef; } @@ -86,11 +87,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWESwitchingKeyCompressedToMut, + R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: GLWESecretToRef, S2: GLWESecretToRef, { - let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); @@ -122,15 +122,9 @@ where } } - self.gglwe_compressed_encrypt_sk( - &mut res.key, - &sk_in_tmp, - &sk_out_tmp, - seed_xa, - source_xe, - scratch_2, - ); - res.sk_in_n = sk_in.n().into(); - res.sk_out_n = sk_out.n().into(); + self.gglwe_compressed_encrypt_sk(res, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2); + + *res.input_degree() = sk_in.n(); + *res.output_degree() = sk_out.n(); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 9a37d17..dbbbcb5 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -4,16 +4,15 @@ use poulpy_hal::{ VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, }, layouts::{Backend, DataMut, Module, Scratch}, - oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, source::Source, }; use crate::{ - GetDistribution, ScratchTakeCore, - encryption::{compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, gglwe_tsk::TensorKeyEncryptSk}, + GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore, + encryption::gglwe_tsk::TensorKeyEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, - compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, Rank, + TensorKeyCompressedAtMut, compressed::TensorKeyCompressed, }, }; @@ -23,7 +22,7 @@ impl TensorKeyCompressed> { A: GGLWEInfos, M: GGLWETensorKeyCompressedEncryptSk, { - module.gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(infos) + module.tensor_key_compressed_encrypt_sk_tmp_bytes(infos) } } @@ -39,16 +38,16 @@ impl TensorKeyCompressed { S: GLWESecretToRef + GetDistribution, M: GGLWETensorKeyCompressedEncryptSk, { - module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); + module.tensor_key_compressed_encrypt_sk(self, sk, seed_xa, source_xe, scratch); } } pub trait GGLWETensorKeyCompressedEncryptSk { - fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn gglwe_tensor_key_encrypt_sk( + fn tensor_key_compressed_encrypt_sk( &self, res: &mut R, sk: &S, @@ -56,36 +55,40 @@ pub trait GGLWETensorKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: TensorKeyCompressedToMut, + D: DataMut, + R: TensorKeyCompressedAtMut + GGLWEInfos, S: GLWESecretToRef + GetDistribution; } impl GGLWETensorKeyCompressedEncryptSk for Module where Self: ModuleN - + GLWESwitchingKeyCompressedEncryptSk + + GGLWECompressedEncryptSk + TensorKeyEncryptSk + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxBigNormalize + SvpPrepare - + SvpPPolAllocBytesImpl + SvpPPolBytesOf - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl + VecZnxDftBytesOf - + VecZnxBigBytesOf, + + VecZnxBigBytesOf + + GLWESecretPreparedAlloc, Scratch: ScratchTakeBasic + ScratchTakeCore, { - fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.tensor_key_encrypt_sk_tmp_bytes(infos) + GLWESecretPrepared::bytes_of(self, infos.rank_out()) + + self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + + self.bytes_of_vec_znx_big(1, 1) + + self.bytes_of_vec_znx_dft(1, 1) + + GLWESecret::bytes_of(self.n().into(), Rank(1)) + + self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) } - fn gglwe_tensor_key_encrypt_sk( + fn tensor_key_compressed_encrypt_sk( &self, res: &mut R, sk: &S, @@ -93,11 +96,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: TensorKeyCompressedToMut, + D: DataMut, + R: GGLWEInfos + TensorKeyCompressedAtMut, S: GLWESecretToRef + GetDistribution, { - let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out()); sk_dft_prep.prepare(self, sk); @@ -141,10 +143,10 @@ where let (seed_xa_tmp, _) = source_xa.branch(); - self.glwe_switching_key_compressed_encrypt_sk( + self.gglwe_compressed_encrypt_sk( res.at_mut(i, j), - &sk_ij, - sk, + &sk_ij.data, + &sk_dft_prep, seed_xa_tmp, source_xe, scratch_5, diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index 4526faf..9d242c8 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace}, - layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero}, source::Source, }; @@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCompressedToMut + GGSWCompressedSeedMut, + R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -83,30 +83,29 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCompressedToMut + GGSWCompressedSeedMut, + R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()]; + let base2k: usize = res.base2k().into(); + let rank: usize = res.rank().into(); + let cols: usize = rank + 1; + let dsize: usize = res.dsize().into(); + + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + + assert_eq!(res.rank(), sk.rank()); + assert_eq!(pt.n(), self.n()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + + let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.dnum().as_usize() * (res.rank().as_usize() + 1)]; { let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); - let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - } - - let base2k: usize = res.base2k().into(); - let rank: usize = res.rank().into(); - let cols: usize = rank + 1; - let dsize: usize = res.dsize().into(); + println!("res.seed: {:?}", res.seed); let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index ea6096a..2b9cc06 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -1,13 +1,15 @@ use poulpy_hal::{ - api::{ScratchAvailable, VecZnxAutomorphism}, + api::{ScratchAvailable, SvpPPolBytesOf, VecZnxAutomorphism}, layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, source::Source, }; use crate::{ - ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, - layouts::{AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos}, + GGLWEEncryptSk, ScratchTakeCore, + layouts::{ + AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared, + GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, + }, }; impl AutomorphismKey> { @@ -30,7 +32,7 @@ impl AutomorphismKey> { impl AutomorphismKey where - Self: AutomorphismKeyToMut, + Self: GGLWEToRef, { pub fn encrypt_sk( &mut self, @@ -62,13 +64,18 @@ pub trait AutomorphismKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: AutomorphismKeyToMut, + R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, S: GLWESecretToRef; } impl AutomorphismKeyEncryptSk for Module where - Self: GLWESwitchingKeyEncryptSk + VecZnxAutomorphism + GaloisElement, + Self: GGLWEEncryptSk + + VecZnxAutomorphism + + GaloisElement + + SvpPPolBytesOf + + GLWESecretPrepare + + GLWESecretPreparedAlloc, Scratch: ScratchTakeCore, { fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -80,7 +87,10 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(infos) + GLWESecretPrepared::bytes_of_from_infos(self, infos) + + self + .gglwe_encrypt_sk_tmp_bytes(infos) + .max(GLWESecret::bytes_of_from_infos(infos)) } fn automorphism_key_encrypt_sk( @@ -92,10 +102,9 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: AutomorphismKeyToMut, + R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, S: GLWESecretToRef, { - let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); assert_eq!(res.n(), sk.n()); @@ -108,9 +117,10 @@ where self.automorphism_key_encrypt_sk_tmp_bytes(res) ); - let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); + let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); { + let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank()); (0..res.rank_out().into()).for_each(|i| { self.vec_znx_automorphism( self.galois_element_inv(p), @@ -120,12 +130,19 @@ where i, ); }); + sk_out_prepared.prepare(self, &sk_out); } - res.key - .encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); + self.gglwe_encrypt_sk( + res, + &sk.data, + &sk_out_prepared, + source_xa, + source_xe, + scratch_1, + ); - res.p = p; + res.set_p(p); } } diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index e8c49f3..2699e4a 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -8,7 +8,7 @@ use crate::{ ScratchTakeCore, encryption::gglwe_ct::GGLWEEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, + GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, prepared::GLWESecretPreparedAlloc, }, }; @@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWESwitchingKeyToMut, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: GLWESecretToRef, S2: GLWESecretToRef; } @@ -93,11 +93,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWESwitchingKeyToMut, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: GLWESecretToRef, S2: GLWESecretToRef, { - let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); @@ -129,16 +128,17 @@ where } } - res.key.encrypt_sk( - self, + self.gglwe_encrypt_sk( + res, &sk_in_tmp, &sk_out_tmp, source_xa, source_xe, scratch_2, ); - res.sk_in_n = sk_in.n().into(); - res.sk_out_n = sk_out.n().into(); + + *res.input_degree() = sk_in.n(); + *res.output_degree() = sk_out.n(); } } diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 2a4792d..10f404a 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -8,10 +8,9 @@ use poulpy_hal::{ }; use crate::{ - GetDistribution, ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + GGLWEEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, TensorKeyToMut, prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; @@ -63,7 +62,7 @@ pub trait TensorKeyEncryptSk { impl TensorKeyEncryptSk for Module where Self: ModuleN - + GLWESwitchingKeyEncryptSk + + GGLWEEncryptSk + VecZnxDftBytesOf + VecZnxBigBytesOf + GLWESecretPreparedAlloc @@ -83,7 +82,7 @@ where + self.bytes_of_vec_znx_big(1, 1) + self.bytes_of_vec_znx_dft(1, 1) + GLWESecret::bytes_of(self.n().into(), Rank(1)) - + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + + GGLWE::encrypt_sk_tmp_bytes(self, infos) } fn tensor_key_encrypt_sk( @@ -102,8 +101,8 @@ where // let n: RingDegree = sk.n(); let rank: Rank = res.rank_out(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, rank); - sk_dft_prep.prepare(self, sk); + let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, rank); + sk_prepared.prepare(self, sk); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); @@ -122,7 +121,7 @@ where (0..rank.into()).for_each(|i| { (i..rank.into()).for_each(|j| { - self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); + self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i); self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); self.vec_znx_big_normalize( @@ -135,8 +134,14 @@ where scratch_5, ); - res.at_mut(i, j) - .encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5); + res.at_mut(i, j).encrypt_sk( + self, + &sk_ij.data, + &sk_prepared, + source_xa, + source_xe, + scratch_5, + ); }); }) } diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index a0328d3..85aa6e1 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -79,7 +79,7 @@ impl GLWE { source_xe: &mut Source, scratch: &mut Scratch, ) where - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef, M: GLWEEncryptPk, { @@ -245,7 +245,7 @@ pub trait GLWEEncryptPk { scratch: &mut Scratch, ) where R: GLWEToMut, - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef; fn glwe_encrypt_zero_pk( @@ -285,7 +285,7 @@ where scratch: &mut Scratch, ) where R: GLWEToMut, - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef, { self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); @@ -324,7 +324,7 @@ pub(crate) trait GLWEEncryptPkInternal { scratch: &mut Scratch, ) where R: GLWEToMut, - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef; } @@ -351,7 +351,7 @@ where scratch: &mut Scratch, ) where R: GLWEToMut, - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -361,8 +361,8 @@ where assert_eq!(res.n(), pk.n()); assert_eq!(res.rank(), pk.rank()); if let Some((pt, _)) = pt { - assert_eq!(pt.to_ref().base2k(), pk.base2k()); - assert_eq!(pt.to_ref().n(), pk.n()); + assert_eq!(pt.base2k(), pk.base2k()); + assert_eq!(pt.n(), pk.n()); } let base2k: usize = pk.base2k().into(); diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index bd7c271..71b96ff 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -1,14 +1,13 @@ use poulpy_hal::{ - api::{ModuleN, VecZnxAutomorphismInplace}, + api::{ModuleN, VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes}, layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, source::Source, }; use crate::{ - ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + GGLWEEncryptSk, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, + GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPrepare, GLWESecretToRef, GLWEToLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, @@ -59,12 +58,17 @@ pub trait GLWEToLWESwitchingKeyEncrypt { ) where S1: LWESecretToRef, S2: GLWESecretToRef, - R: GLWEToLWESwitchingKeyToMut; + R: GGLWEToMut; } impl GLWEToLWESwitchingKeyEncrypt for Module where - Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Self: ModuleN + + GGLWEEncryptSk + + GLWESecretPreparedAlloc + + VecZnxAutomorphismInplace + + VecZnxAutomorphismInplaceTmpBytes + + GLWESecretPrepare, Scratch: ScratchTakeCore, { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -72,7 +76,8 @@ where A: GGLWEInfos, { GLWESecretPrepared::bytes_of(self, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self.n().into(), infos.rank_in())) + + GGLWE::encrypt_sk_tmp_bytes(self, infos) + .max(GLWESecret::bytes_of(self.n().into(), infos.rank_in()) + self.vec_znx_automorphism_inplace_tmp_bytes()) } fn glwe_to_lwe_switching_key_encrypt_sk( @@ -86,23 +91,27 @@ where ) where S1: LWESecretToRef, S2: GLWESecretToRef, - R: GLWEToLWESwitchingKeyToMut, + R: GGLWEToMut, { - let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref(); assert!(sk_lwe.n().0 <= self.n() as u32); - let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); - sk_lwe_as_glwe.data.zero(); - sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); - self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); + let (mut sk_lwe_as_glwe_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, Rank(1)); - res.0.encrypt_sk( - self, - sk_glwe, - &sk_lwe_as_glwe, + { + let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1)); + sk_lwe_as_glwe.data.zero(); + sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); + self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_2); + sk_lwe_as_glwe_prep.prepare(self, &sk_lwe_as_glwe); + } + + self.gglwe_encrypt_sk( + res, + &sk_glwe.data, + &sk_lwe_as_glwe_prep, source_xa, source_xe, scratch_1, diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index 4a5d6ac..7651e8d 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -1,7 +1,6 @@ use poulpy_hal::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace}, layouts::{Backend, DataMut, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, source::Source, }; @@ -16,7 +15,6 @@ impl LWE { P: LWEPlaintextToRef, S: LWESecretToRef, M: LWEEncryptSk, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { module.lwe_encrypt_sk(self, pt, sk, source_xa, source_xe); } @@ -33,7 +31,7 @@ pub trait LWEEncryptSk { impl LWEEncryptSk for Module where Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 215efee..50e08a1 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -8,8 +8,8 @@ use crate::{ ScratchTakeCore, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, LWESwitchingKeyToMut, - Rank, + GGLWEInfos, GGLWEToMut, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, LWESecret, LWESecretToRef, + LWESwitchingKey, Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; @@ -56,7 +56,7 @@ pub trait LWESwitchingKeyEncrypt { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: LWESwitchingKeyToMut, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: LWESecretToRef, S2: LWESecretToRef; } @@ -100,13 +100,12 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: LWESwitchingKeyToMut, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: LWESecretToRef, S2: LWESecretToRef, { - let res: &mut LWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_lwe_in: &LWESecret<&[u8]> = &sk_lwe_in.to_ref(); - let sk_lwe_out = &sk_lwe_out.to_ref(); + let sk_lwe_out: &LWESecret<&[u8]> = &sk_lwe_out.to_ref(); assert!(sk_lwe_in.n().0 <= res.n().0); assert!(sk_lwe_out.n().0 <= res.n().0); @@ -124,7 +123,7 @@ where self.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2); self.glwe_switching_key_encrypt_sk( - &mut res.0, + res, &sk_in_glwe, &sk_out_glwe, source_xa, diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index a66c95d..a9856cc 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -1,15 +1,14 @@ use poulpy_hal::{ - api::{ModuleN, VecZnxAutomorphismInplace}, + api::{ModuleN, VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes}, layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; use crate::{ - ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + GGLWEEncryptSk, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey, - LWEToGLWESwitchingKeyToMut, Rank, + GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPreparedAlloc, GLWESecretPreparedToRef, LWEInfos, LWESecret, + LWESecretToRef, LWEToGLWESwitchingKey, Rank, }, }; @@ -34,7 +33,7 @@ impl LWEToGLWESwitchingKey { scratch: &mut Scratch, ) where S1: LWESecretToRef, - S2: GLWESecretToRef, + S2: GLWESecretPreparedToRef, M: LWEToGLWESwitchingKeyEncrypt, Scratch: ScratchTakeCore, { @@ -57,13 +56,17 @@ pub trait LWEToGLWESwitchingKeyEncrypt { scratch: &mut Scratch, ) where S1: LWESecretToRef, - S2: GLWESecretToRef, - R: LWEToGLWESwitchingKeyToMut; + S2: GLWESecretPreparedToRef, + R: GGLWEToMut; } impl LWEToGLWESwitchingKeyEncrypt for Module where - Self: ModuleN + GLWESwitchingKeyEncryptSk + VecZnxAutomorphismInplace, + Self: ModuleN + + GGLWEEncryptSk + + VecZnxAutomorphismInplace + + GLWESecretPreparedAlloc + + VecZnxAutomorphismInplaceTmpBytes, Scratch: ScratchTakeCore, { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -75,7 +78,8 @@ where Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self.n().into(), infos.rank_in()) + GLWESecret::bytes_of(self.n().into(), infos.rank_in()) + + GGLWE::encrypt_sk_tmp_bytes(self, infos).max(self.vec_znx_automorphism_inplace_tmp_bytes()) } fn lwe_to_glwe_switching_key_encrypt_sk( @@ -88,12 +92,10 @@ where scratch: &mut Scratch, ) where S1: LWESecretToRef, - S2: GLWESecretToRef, - R: LWEToGLWESwitchingKeyToMut, + S2: GLWESecretPreparedToRef, + R: GGLWEToMut, { - let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); - let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref(); assert!(sk_lwe.n().0 <= self.n() as u32); @@ -102,9 +104,9 @@ where sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); - res.0.encrypt_sk( - self, - &sk_lwe_as_glwe, + self.gglwe_encrypt_sk( + res, + &sk_lwe_as_glwe.data, sk_glwe, source_xa, source_xe, diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs index 871eab8..2943ce3 100644 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ b/poulpy-core/src/external_product/gglwe_atk.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Scratch}; use crate::{ ScratchTakeCore, external_product::gglwe_ksk::GGLWEExternalProduct, - layouts::{AutomorphismKey, AutomorphismKeyToRef, GGLWEInfos, GGSWInfos, prepared::GGSWPreparedToRef}, + layouts::{AutomorphismKey, GGLWEInfos, GGLWEToRef, GGSWInfos, prepared::GGSWPreparedToRef}, }; impl AutomorphismKey> { @@ -28,11 +28,11 @@ impl AutomorphismKey { pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where M: GGLWEExternalProduct, - A: AutomorphismKeyToRef, + A: GGLWEToRef, B: GGSWPreparedToRef, Scratch: ScratchTakeCore, { - module.gglwe_external_product(&mut self.key.key, &a.to_ref().key.key, b, scratch); + module.gglwe_external_product(self, a, b, scratch); } pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) @@ -41,6 +41,6 @@ impl AutomorphismKey { A: GGSWPreparedToRef, Scratch: ScratchTakeCore, { - module.gglwe_external_product_inplace(&mut self.key.key, a, scratch); + module.gglwe_external_product_inplace(self, a, scratch); } } diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index 5bb4557..c6ee4a6 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero}; use crate::{ GLWEExternalProduct, ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, + GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, prepared::{GGSWPrepared, GGSWPreparedToRef}, }, }; @@ -116,11 +116,11 @@ impl GLWESwitchingKey { pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where M: GGLWEExternalProduct, - A: GLWESwitchingKeyToRef, + A: GGLWEToRef, B: GGSWPreparedToRef, Scratch: ScratchTakeCore, { - module.gglwe_external_product(&mut self.key, &a.to_ref().key, b, scratch); + module.gglwe_external_product(self, a, b, scratch); } pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) @@ -129,6 +129,6 @@ impl GLWESwitchingKey { A: GGSWPreparedToRef, Scratch: ScratchTakeCore, { - module.gglwe_external_product_inplace(&mut self.key, a, scratch); + module.gglwe_external_product_inplace(self, a, scratch); } } diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index bcd9e45..548f4d4 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -9,8 +9,7 @@ use crate::{ GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, glwe_trace::GLWETrace, layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement, }, }; @@ -118,7 +117,7 @@ impl GLWEPacker { pub fn add(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap, scratch: &mut Scratch) where A: GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, M: GLWEPacking, Scratch: ScratchTakeCore, { @@ -191,7 +190,7 @@ where scratch: &mut Scratch, ) where R: GLWEToMut + GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] @@ -235,7 +234,7 @@ fn pack_core( scratch: &mut Scratch, ) where A: GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, M: ModuleLogN + GLWEAutomorphism + GaloisElement @@ -308,7 +307,7 @@ fn combine( B: GLWEToRef + GLWEInfos, M: GLWEAutomorphism + GaloisElement + GLWERotate + GLWESub + GLWEShift + GLWEAdd + GLWENormalize, B: GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { let log_n: usize = acc.data.n().log2(); @@ -401,7 +400,7 @@ fn pack_internal( M: GLWEAutomorphism + GLWERotate + GLWESub + GLWEShift + GLWEAdd + GLWENormalize, A: GLWEToMut + GLWEToRef + GLWEInfos, B: GLWEToMut + GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 48608d1..a1c2573 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -8,8 +8,8 @@ use poulpy_hal::{ use crate::{ GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore, layouts::{ - Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + Base2K, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos, + prepared::GetAutomorphismGaloisElement, }, }; @@ -43,7 +43,7 @@ impl GLWE { scratch: &mut Scratch, ) where A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: GLWETrace, { @@ -58,7 +58,7 @@ impl GLWE { keys: &HashMap, scratch: &mut Scratch, ) where - K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: GLWETrace, { @@ -110,7 +110,7 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { self.glwe_copy(res, a); @@ -120,7 +120,7 @@ where fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &HashMap, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 912d803..3b6495a 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -3,11 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; use crate::{ ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, - layouts::{ - AutomorphismKey, AutomorphismKeyToRef, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWESwitchingKey, - GLWESwitchingKeyToRef, - prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef}, - }, + layouts::{AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWESwitchingKey}, }; impl AutomorphismKey> { @@ -25,21 +21,21 @@ impl AutomorphismKey> { impl AutomorphismKey { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where - A: AutomorphismKeyToRef, - B: GLWESwitchingKeyPreparedToRef, + A: GGLWEToRef + GGLWEToRef, + B: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { - module.gglwe_keyswitch(&mut self.key.key, &a.to_ref().key.key, b, scratch); + module.gglwe_keyswitch(self, a, b, scratch); } pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { - module.gglwe_keyswitch_inplace(&mut self.key.key, a, scratch); + module.gglwe_keyswitch_inplace(self, a, scratch); } } @@ -58,21 +54,21 @@ impl GLWESwitchingKey> { impl GLWESwitchingKey { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where - A: GLWESwitchingKeyToRef, - B: GLWESwitchingKeyPreparedToRef, + A: GGLWEToRef, + B: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { - module.gglwe_keyswitch(&mut self.key, &a.to_ref().key, b, scratch); + module.gglwe_keyswitch(self, a, b, scratch); } pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { - module.gglwe_keyswitch_inplace(&mut self.key, a, scratch); + module.gglwe_keyswitch_inplace(self, a, scratch); } } @@ -92,7 +88,7 @@ impl GGLWE { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where A: GGLWEToRef, - B: GLWESwitchingKeyPreparedToRef, + B: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { @@ -101,7 +97,7 @@ impl GGLWE { pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { @@ -128,12 +124,11 @@ where where R: GGLWEToMut, A: GGLWEToRef, - B: GLWESwitchingKeyPreparedToRef, + B: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let a: &GGLWE<&[u8]> = &a.to_ref(); - let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref(); assert_eq!( res.rank_in(), @@ -180,11 +175,10 @@ where fn gglwe_keyswitch_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) where R: GGLWEToMut, - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); - let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref(); assert_eq!( res.rank_out(), diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index 5e49d62..7682315 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -3,10 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, VecZnx}; use crate::{ GGSWExpandRows, ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, - layouts::{ - GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, - prepared::{GLWESwitchingKeyPreparedToRef, TensorKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::TensorKeyPreparedToRef}, }; impl GGSW> { @@ -32,7 +29,7 @@ impl GGSW { pub fn keyswitch(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where A: GGSWToRef, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, @@ -42,7 +39,7 @@ impl GGSW { pub fn keyswitch_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, @@ -93,14 +90,15 @@ where where R: GGSWToMut, A: GGSWToRef, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let a: &GGSW<&[u8]> = &a.to_ref(); - assert_eq!(res.ggsw_layout(), a.ggsw_layout()); + assert!(res.dnum() <= a.dnum()); + assert_eq!(res.dsize(), a.dsize()); for row in 0..a.dnum().into() { // Key-switch column 0, i.e. @@ -114,7 +112,7 @@ where fn ggsw_keyswitch_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index f82a4d1..be81ebf 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -4,15 +4,12 @@ use poulpy_hal::{ VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, - layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, + layouts::{Backend, DataMut, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, }; use crate::{ ScratchTakeCore, - layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPrepared, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos}, }; impl GLWE> { @@ -31,7 +28,7 @@ impl GLWE { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where A: GLWEToRef, - B: GLWESwitchingKeyPreparedToRef, + B: GGLWEPreparedToRef, M: GLWEKeyswitch, Scratch: ScratchTakeCore, { @@ -40,7 +37,7 @@ impl GLWE { pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef, M: GLWEKeyswitch, Scratch: ScratchTakeCore, { @@ -129,12 +126,12 @@ where where R: GLWEToMut, A: GLWEToRef, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); + let b: &GGLWEPrepared<&[u8], BE> = &key.to_ref(); assert_eq!( a.rank(), @@ -184,11 +181,11 @@ where fn glwe_keyswitch_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); + let a: &GGLWEPrepared<&[u8], BE> = &key.to_ref(); assert_eq!( res.rank(), @@ -239,17 +236,17 @@ impl GLWE> {} impl GLWE {} -pub(crate) fn keyswitch_internal( +pub(crate) fn keyswitch_internal( module: &M, mut res: VecZnxDft, - a: &GLWE, - key: &GLWESwitchingKeyPrepared, + a: &A, + key: &K, scratch: &mut Scratch, ) -> VecZnxBig where DR: DataMut, - DA: DataRef, - DB: DataRef, + A: GLWEToRef, + K: GGLWEPreparedToRef, M: ModuleN + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes @@ -264,11 +261,14 @@ where + VecZnxNormalize, Scratch: ScratchTakeCore, { + let a: &GLWE<&[u8]> = &a.to_ref(); + let key: &GGLWEPrepared<&[u8], BE> = &key.to_ref(); + let base2k_in: usize = a.base2k().into(); let base2k_out: usize = key.base2k().into(); let cols: usize = (a.rank() + 1).into(); let a_size: usize = (a.size() * base2k_in).div_ceil(base2k_out); - let pmat: &VmpPMat = &key.key.data; + let pmat: &VmpPMat<&[u8], BE> = &key.data; if key.dsize() == 1 { let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a.size()); diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 8a529c7..e73747a 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -6,10 +6,7 @@ use poulpy_hal::{ use crate::{ LWESampleExtract, ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, - layouts::{ - GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, - prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision}, }; impl LWE> { @@ -28,7 +25,7 @@ impl LWE { pub fn keyswitch(&mut self, module: &M, a: &A, ksk: &K, scratch: &mut Scratch) where A: LWEToRef, - K: LWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: LWEKeySwitch, { @@ -36,7 +33,7 @@ impl LWE { } } -impl LWEKeySwitch for Module where Self: LWEKeySwitch {} +impl LWEKeySwitch for Module where Self: GLWEKeyswitch + LWESampleExtract {} pub trait LWEKeySwitch where @@ -75,12 +72,11 @@ where where R: LWEToMut, A: LWEToRef, - K: LWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let a: &LWE<&[u8]> = &a.to_ref(); - let ksk: &LWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref(); assert!(res.n().as_usize() <= self.n()); assert!(a.n().as_usize() <= self.n()); @@ -120,7 +116,7 @@ where glwe_in.data.at_mut(1, i)[..n_lwe].copy_from_slice(&data_lwe[1..]); } - self.glwe_keyswitch(&mut glwe_out, &glwe_in, &ksk.0, scratch_1); + self.glwe_keyswitch(&mut glwe_out, &glwe_in, ksk, scratch_1); self.lwe_sample_extract(res, &glwe_out); } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 5a40e5d..1f15bfc 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -4,10 +4,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut, + GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWECompressed, GLWECompressedToMut, GLWECompressedToRef, + GLWEDecompress, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -15,7 +14,7 @@ use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct AutomorphismKeyCompressed { - pub(crate) key: GLWESwitchingKeyCompressed, + pub(crate) key: GGLWECompressed, pub(crate) p: i64, } @@ -102,7 +101,7 @@ impl AutomorphismKeyCompressed> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { AutomorphismKeyCompressed { - key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), + key: GGLWECompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -122,7 +121,7 @@ impl AutomorphismKeyCompressed> { } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank, dnum, dsize) + GGLWECompressed::bytes_of(n, base2k, k, rank, dnum, dsize) } } @@ -142,19 +141,19 @@ impl WriterTo for AutomorphismKeyCompressed { pub trait AutomorphismKeyDecompress where - Self: GLWESwitchingKeyDecompress, + Self: GGLWEDecompress, { fn decompress_automorphism_key(&self, res: &mut R, other: &O) where - R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, - O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + R: GGLWEToMut + SetAutomorphismGaloisElement, + O: GGLWECompressedToRef + GetAutomorphismGaloisElement, { - self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key); + self.decompress_gglwe(res, other); res.set_p(other.p()); } } -impl AutomorphismKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} +impl AutomorphismKeyDecompress for Module where Self: GLWEDecompress {} impl AutomorphismKey where @@ -162,20 +161,32 @@ where { pub fn decompress(&mut self, module: &M, other: &O) where - O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + O: GGLWECompressedToRef + GetAutomorphismGaloisElement, M: AutomorphismKeyDecompress, { module.decompress_automorphism_key(self, other); } } +impl GGLWECompressedToRef for AutomorphismKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.key.to_ref() + } +} + +impl GGLWECompressedToMut for AutomorphismKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.key.to_mut() + } +} + pub trait AutomorphismKeyCompressedToRef { fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>; } impl AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, + GLWECompressed: GLWECompressedToRef, { fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> { AutomorphismKeyCompressed { @@ -191,7 +202,7 @@ pub trait AutomorphismKeyCompressedToMut { impl AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, + GLWECompressed: GLWECompressedToMut, { fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> { AutomorphismKeyCompressed { @@ -200,3 +211,15 @@ where } } } + +impl GGLWECompressedSeedMut for AutomorphismKeyCompressed { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { + &mut self.key.seed + } +} + +impl SetAutomorphismGaloisElement for AutomorphismKeyCompressed { + fn set_p(&mut self, p: i64) { + self.p = p + } +} diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index dcc9886..0fb0382 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -258,7 +258,8 @@ where let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let other: &GGLWECompressed<&[u8]> = &other.to_ref(); - assert_eq!(res.gglwe_layout(), other.gglwe_layout()); + assert_eq!(res.dsize(), other.dsize()); + assert!(res.dnum() <= other.dnum()); let rank_in: usize = res.rank_in().into(); let dnum: usize = res.dnum().into(); diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index 3b94141..82fb797 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, - LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWECompressedSeedMut, GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESwitchingKey, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -14,8 +14,34 @@ use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct GLWESwitchingKeyCompressed { pub(crate) key: GGLWECompressed, - pub(crate) sk_in_n: usize, // Degree of sk_in - pub(crate) sk_out_n: usize, // Degree of sk_out + pub(crate) input_degree: Degree, // Degree of sk_in + pub(crate) output_degree: Degree, // Degree of sk_out +} + +impl GGLWECompressedSeedMut for GLWESwitchingKeyCompressed { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { + &mut self.key.seed + } +} + +impl GLWESwitchingKeyDegrees for GLWESwitchingKeyCompressed { + fn output_degree(&self) -> &Degree { + &self.output_degree + } + + fn input_degree(&self) -> &Degree { + &self.input_degree + } +} + +impl GLWESwitchingKeyDegreesMut for GLWESwitchingKeyCompressed { + fn output_degree(&mut self) -> &mut Degree { + &mut self.output_degree + } + + fn input_degree(&mut self) -> &mut Degree { + &mut self.input_degree + } } impl LWEInfos for GLWESwitchingKeyCompressed { @@ -76,7 +102,7 @@ impl fmt::Display for GLWESwitchingKeyCompressed { write!( f, "(GLWESwitchingKeyCompressed: sk_in_n={} sk_out_n={}) {}", - self.sk_in_n, self.sk_out_n, self.key.data + self.input_degree, self.output_degree, self.key.data ) } } @@ -100,8 +126,8 @@ impl GLWESwitchingKeyCompressed> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { GLWESwitchingKeyCompressed { key: GGLWECompressed::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), } } @@ -120,16 +146,16 @@ where { impl ReaderFrom for GLWESwitchingKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { - self.sk_in_n = reader.read_u64::()? as usize; - self.sk_out_n = reader.read_u64::()? as usize; + self.input_degree = Degree(reader.read_u32::()? as u32); + self.output_degree = Degree(reader.read_u32::()? as u32); self.key.read_from(reader) } } impl WriterTo for GLWESwitchingKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { - writer.write_u64::(self.sk_in_n as u64)?; - writer.write_u64::(self.sk_out_n as u64)?; + writer.write_u32::(self.input_degree.into())?; + writer.write_u32::(self.output_degree.into())?; self.key.write_to(writer) } } @@ -140,13 +166,13 @@ where { fn decompress_glwe_switching_key(&self, res: &mut R, other: &O) where - R: GLWESwitchingKeyToMut + GLWESwitchingKeySetMetaData, - O: GLWESwitchingKeyCompressedToRef, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, { - let other: &GLWESwitchingKeyCompressed<&[u8]> = &other.to_ref(); - self.decompress_gglwe(&mut res.to_mut().key, &other.key); - res.set_sk_in_n(other.sk_in_n); - res.set_sk_out_n(other.sk_out_n); + self.decompress_gglwe(res, other); + + *res.input_degree() = *other.input_degree(); + *res.output_degree() = *other.output_degree(); } } @@ -155,43 +181,21 @@ impl GLWESwitchingKeyDecompress for Module where Self: GGLWEDecom impl GLWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: GLWESwitchingKeyCompressedToRef, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, M: GLWESwitchingKeyDecompress, { module.decompress_glwe_switching_key(self, other); } } -pub trait GLWESwitchingKeyCompressedToMut { - fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]>; -} - -impl GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed -where - GGLWECompressed: GGLWECompressedToMut, -{ - fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> { - GLWESwitchingKeyCompressed { - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - key: self.key.to_mut(), - } +impl GGLWECompressedToMut for GLWESwitchingKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.key.to_mut() } } -pub trait GLWESwitchingKeyCompressedToRef { - fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]>; -} - -impl GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed -where - GGLWECompressed: GGLWECompressedToRef, -{ - fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> { - GLWESwitchingKeyCompressed { - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - key: self.key.to_ref(), - } +impl GGLWECompressedToRef for GLWESwitchingKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.key.to_ref() } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 4ec8a42..1e9e312 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -4,17 +4,15 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, + GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct TensorKeyCompressed { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, } impl LWEInfos for TensorKeyCompressed { @@ -67,7 +65,7 @@ impl FillUniform for TensorKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GLWESwitchingKeyCompressed| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GGLWECompressed| key.fill_uniform(log_bound, source)) } } @@ -100,7 +98,7 @@ impl TensorKeyCompressed> { let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); TensorKeyCompressed { keys: (0..pairs) - .map(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| GGLWECompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } @@ -121,7 +119,7 @@ impl TensorKeyCompressed> { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize) + pairs * GGLWECompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize) } } @@ -151,8 +149,26 @@ impl WriterTo for TensorKeyCompressed { } } -impl TensorKeyCompressed { - pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed { +pub trait TensorKeyCompressedAtRef { + fn at(&self, i: usize, j: usize) -> &GGLWECompressed; +} + +impl TensorKeyCompressedAtRef for TensorKeyCompressed { + fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed { + if i > j { + std::mem::swap(&mut i, &mut j); + }; + let rank: usize = self.rank_out().into(); + &self.keys[i * rank + j - (i * (i + 1) / 2)] + } +} + +pub trait TensorKeyCompressedAtMut { + fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed; +} + +impl TensorKeyCompressedAtMut for TensorKeyCompressed { + fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWECompressed { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -163,7 +179,7 @@ impl TensorKeyCompressed { pub trait TensorKeyDecompress where - Self: GLWESwitchingKeyDecompress, + Self: GGLWEDecompress, { fn decompress_tensor_key(&self, res: &mut R, other: &O) where @@ -182,12 +198,12 @@ where ); for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { - self.decompress_glwe_switching_key(a, b); + self.decompress_gglwe(a, b); } } } -impl TensorKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} +impl TensorKeyDecompress for Module where Self: GGLWEDecompress {} impl TensorKey { pub fn decompress(&mut self, module: &M, other: &O) @@ -205,7 +221,7 @@ pub trait TensorKeyCompressedToMut { impl TensorKeyCompressedToMut for TensorKeyCompressed where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, + GGLWECompressed: GGLWECompressedToMut, { fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> { TensorKeyCompressed { @@ -220,7 +236,7 @@ pub trait TensorKeyCompressedToRef { impl TensorKeyCompressedToRef for TensorKeyCompressed where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, + GGLWECompressed: GGLWECompressedToRef, { fn to_ref(&self) -> TensorKeyCompressed<&[u8]> { TensorKeyCompressed { diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index ca4446f..14fdd7a 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -138,7 +138,7 @@ impl GGSWCompressed> { base2k, dsize, rank, - seed: Vec::new(), + seed: vec![[0u8; 32]; dnum.as_usize() * (rank.as_usize() + 1)], } } @@ -260,7 +260,7 @@ where } } -impl GGSWDecompress for Module where Self: GGSWDecompress {} +impl GGSWDecompress for Module where Self: GLWEDecompress {} impl GGSW { pub fn decompress(&mut self, module: &M, other: &O) diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 1b05575..fa61b81 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -158,7 +158,6 @@ where self.ring_degree() ); - assert_eq!(res.lwe_layout(), other.lwe_layout()); assert_eq!(res.glwe_layout(), other.glwe_layout()); let mut source: Source = Source::new(other.seed); diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 08508b1..6ac325c 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -6,11 +6,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, - TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress}, }; #[derive(PartialEq, Eq, Clone)] @@ -151,10 +149,10 @@ where { fn decompress_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O) where - R: GLWEToLWESwitchingKeyToMut, - O: GLWEToLWESwitchingKeyCompressedToRef, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, { - self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + self.decompress_glwe_switching_key(res, other); } } @@ -163,35 +161,21 @@ impl GLWEToLWESwitchingKeyDecompress for Module where Self: GLWES impl GLWEToLWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: GLWEToLWESwitchingKeyCompressedToRef, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, M: GLWEToLWESwitchingKeyDecompress, { module.decompress_glwe_to_lwe_switching_key(self, other); } } -pub trait GLWEToLWESwitchingKeyCompressedToRef { - fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]>; -} - -impl GLWEToLWESwitchingKeyCompressedToRef for GLWEToLWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, -{ - fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]> { - GLWEToLWESwitchingKeyCompressed(self.0.to_ref()) +impl GGLWECompressedToRef for GLWEToLWESwitchingKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.0.to_ref() } } -pub trait GLWEToLWESwitchingKeyCompressedToMut { - fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]>; -} - -impl GLWEToLWESwitchingKeyCompressedToMut for GLWEToLWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, -{ - fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]> { - GLWEToLWESwitchingKeyCompressed(self.0.to_mut()) +impl GGLWECompressedToMut for GLWEToLWESwitchingKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.0.to_mut() } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 3738b92..764d423 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -4,10 +4,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress}, }; use std::fmt; @@ -152,10 +151,10 @@ where { fn decompress_lwe_switching_key(&self, res: &mut R, other: &O) where - R: LWESwitchingKeyToMut, - O: LWESwitchingKeyCompressedToRef, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, { - self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + self.decompress_glwe_switching_key(res, other); } } @@ -164,35 +163,21 @@ impl LWESwitchingKeyDecompress for Module where Self: GLWESwitchi impl LWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: LWESwitchingKeyCompressedToRef, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, M: LWESwitchingKeyDecompress, { module.decompress_lwe_switching_key(self, other); } } -pub trait LWESwitchingKeyCompressedToRef { - fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>; -} - -impl LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, -{ - fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> { - LWESwitchingKeyCompressed(self.0.to_ref()) +impl GGLWECompressedToRef for LWESwitchingKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.0.to_ref() } } -pub trait LWESwitchingKeyCompressedToMut { - fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>; -} - -impl LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, -{ - fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> { - LWESwitchingKeyCompressed(self.0.to_mut()) +impl GGLWECompressedToMut for LWESwitchingKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.0.to_mut() } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index ea0ca05..7a724c9 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -4,11 +4,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, - TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress}, }; use std::fmt; @@ -149,10 +147,10 @@ where { fn decompress_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O) where - R: LWEToGLWESwitchingKeyToMut, - O: LWEToGLWESwitchingKeyCompressedToRef, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, { - self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + self.decompress_glwe_switching_key(res, other); } } @@ -161,35 +159,21 @@ impl LWEToGLWESwitchingKeyDecompress for Module where Self: GLWES impl LWEToGLWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: LWEToGLWESwitchingKeyCompressedToRef, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, M: LWEToGLWESwitchingKeyDecompress, { module.decompress_lwe_to_glwe_switching_key(self, other); } } -pub trait LWEToGLWESwitchingKeyCompressedToRef { - fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>; -} - -impl LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, -{ - fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> { - LWEToGLWESwitchingKeyCompressed(self.0.to_ref()) +impl GGLWECompressedToRef for LWEToGLWESwitchingKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.0.to_ref() } } -pub trait LWEToGLWESwitchingKeyCompressedToMut { - fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>; -} - -impl LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, -{ - fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> { - LWEToGLWESwitchingKeyCompressed(self.0.to_mut()) +impl GGLWECompressedToMut for LWEToGLWESwitchingKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.0.to_mut() } } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index 5d0f5f7..b705056 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -4,8 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, - LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -24,7 +23,7 @@ pub struct AutomorphismKeyLayout { #[derive(PartialEq, Eq, Clone)] pub struct AutomorphismKey { - pub(crate) key: GLWESwitchingKey, + pub(crate) key: GGLWE, pub(crate) p: i64, } @@ -161,7 +160,7 @@ impl AutomorphismKey> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { AutomorphismKey { - key: GLWESwitchingKey::alloc(n, base2k, k, rank, rank, dnum, dsize), + key: GGLWE::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -186,39 +185,19 @@ impl AutomorphismKey> { } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GLWESwitchingKey::bytes_of(n, base2k, k, rank, rank, dnum, dsize) + GGLWE::bytes_of(n, base2k, k, rank, rank, dnum, dsize) } } -pub trait AutomorphismKeyToMut { - fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]>; -} - -impl AutomorphismKeyToMut for AutomorphismKey -where - GLWESwitchingKey: GLWESwitchingKeyToMut, -{ - fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]> { - AutomorphismKey { - key: self.key.to_mut(), - p: self.p, - } +impl GGLWEToMut for AutomorphismKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.key.to_mut() } } -pub trait AutomorphismKeyToRef { - fn to_ref(&self) -> AutomorphismKey<&[u8]>; -} - -impl AutomorphismKeyToRef for AutomorphismKey -where - GLWESwitchingKey: GLWESwitchingKeyToRef, -{ - fn to_ref(&self) -> AutomorphismKey<&[u8]> { - AutomorphismKey { - p: self.p, - key: self.key.to_ref(), - } +impl GGLWEToRef for AutomorphismKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.key.to_ref() } } diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index b451951..1e67222 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -62,37 +62,37 @@ impl GGLWEInfos for GLWESwitchingKeyLayout { #[derive(PartialEq, Eq, Clone)] pub struct GLWESwitchingKey { pub(crate) key: GGLWE, - pub(crate) sk_in_n: usize, // Degree of sk_in - pub(crate) sk_out_n: usize, // Degree of sk_out + pub(crate) input_degree: Degree, // Degree of sk_in + pub(crate) output_degree: Degree, // Degree of sk_out } -pub trait GLWESwitchingKeySetMetaData { - fn set_sk_in_n(&mut self, sk_in_n: usize); - fn set_sk_out_n(&mut self, sk_out_n: usize); +pub trait GLWESwitchingKeyDegrees { + fn input_degree(&self) -> &Degree; + fn output_degree(&self) -> &Degree; } -impl GLWESwitchingKeySetMetaData for GLWESwitchingKey { - fn set_sk_in_n(&mut self, sk_in_n: usize) { - self.sk_in_n = sk_in_n +impl GLWESwitchingKeyDegrees for GLWESwitchingKey { + fn output_degree(&self) -> &Degree { + &self.output_degree } - fn set_sk_out_n(&mut self, sk_out_n: usize) { - self.sk_out_n = sk_out_n + fn input_degree(&self) -> &Degree { + &self.input_degree } } -pub trait GLWESwtichingKeyGetMetaData { - fn sk_in_n(&self) -> usize; - fn sk_out_n(&self) -> usize; +pub trait GLWESwitchingKeyDegreesMut { + fn input_degree(&mut self) -> &mut Degree; + fn output_degree(&mut self) -> &mut Degree; } -impl GLWESwtichingKeyGetMetaData for GLWESwitchingKey { - fn sk_in_n(&self) -> usize { - self.sk_in_n +impl GLWESwitchingKeyDegreesMut for GLWESwitchingKey { + fn output_degree(&mut self) -> &mut Degree { + &mut self.output_degree } - fn sk_out_n(&self) -> usize { - self.sk_out_n + fn input_degree(&mut self) -> &mut Degree { + &mut self.input_degree } } @@ -149,8 +149,8 @@ impl fmt::Display for GLWESwitchingKey { write!( f, "(GLWESwitchingKey: sk_in_n={} sk_out_n={}) {}", - self.sk_in_n, - self.sk_out_n, + self.input_degree, + self.output_degree, self.key.data() ) } @@ -181,8 +181,8 @@ impl GLWESwitchingKey> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { GLWESwitchingKey { key: GGLWE::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), } } @@ -214,37 +214,15 @@ impl GLWESwitchingKey> { } } -pub trait GLWESwitchingKeyToMut { - fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>; -} - -impl GLWESwitchingKeyToMut for GLWESwitchingKey -where - GGLWE: GGLWEToMut, -{ - fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { - GLWESwitchingKey { - key: self.key.to_mut(), - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - } +impl GGLWEToMut for GLWESwitchingKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.key.to_mut() } } -pub trait GLWESwitchingKeyToRef { - fn to_ref(&self) -> GLWESwitchingKey<&[u8]>; -} - -impl GLWESwitchingKeyToRef for GLWESwitchingKey -where - GGLWE: GGLWEToRef, -{ - fn to_ref(&self) -> GLWESwitchingKey<&[u8]> { - GLWESwitchingKey { - key: self.key.to_ref(), - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - } +impl GGLWEToRef for GLWESwitchingKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.key.to_ref() } } @@ -262,16 +240,16 @@ impl GLWESwitchingKey { impl ReaderFrom for GLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { - self.sk_in_n = reader.read_u64::()? as usize; - self.sk_out_n = reader.read_u64::()? as usize; + self.input_degree = Degree(reader.read_u32::()? as u32); + self.output_degree = Degree(reader.read_u32::()? as u32); self.key.read_from(reader) } } impl WriterTo for GLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { - writer.write_u64::(self.sk_in_n as u64)?; - writer.write_u64::(self.sk_out_n as u64)?; + writer.write_u32::(self.input_degree.into())?; + writer.write_u32::(self.output_degree.into())?; self.key.write_to(writer) } } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index 240b482..7f1bb4c 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -4,8 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -23,7 +22,7 @@ pub struct TensorKeyLayout { #[derive(PartialEq, Eq, Clone)] pub struct TensorKey { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, } impl LWEInfos for TensorKey { @@ -116,7 +115,7 @@ impl FillUniform for TensorKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GLWESwitchingKey| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GGLWE| key.fill_uniform(log_bound, source)) } } @@ -154,7 +153,7 @@ impl TensorKey> { let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); TensorKey { keys: (0..pairs) - .map(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| GGLWE::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } @@ -180,13 +179,13 @@ impl TensorKey> { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize) + pairs * GGLWE::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize) } } impl TensorKey { - // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey { + // 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 { std::mem::swap(&mut i, &mut j); }; @@ -196,8 +195,8 @@ impl TensorKey { } impl TensorKey { - // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey { + // Returns a reference to GGLWE_{s}(s[i] * s[j]) + pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -238,7 +237,7 @@ pub trait TensorKeyToRef { impl TensorKeyToRef for TensorKey where - GLWESwitchingKey: GLWESwitchingKeyToRef, + GGLWE: GGLWEToRef, { fn to_ref(&self) -> TensorKey<&[u8]> { TensorKey { @@ -253,7 +252,7 @@ pub trait TensorKeyToMut { impl TensorKeyToMut for TensorKey where - GLWESwitchingKey: GLWESwitchingKeyToMut, + GGLWE: GGLWEToMut, { fn to_mut(&mut self) -> TensorKey<&mut [u8]> { TensorKey { diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 45fa6df..1bb28ff 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, + GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; use std::fmt; @@ -196,28 +196,24 @@ impl GLWEToLWESwitchingKey> { } } -pub trait GLWEToLWESwitchingKeyToRef { - fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>; -} - -impl GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToRef, -{ - fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> { - GLWEToLWESwitchingKey(self.0.to_ref()) +impl GGLWEToRef for GLWEToLWESwitchingKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.0.to_ref() } } -pub trait GLWEToLWESwitchingKeyToMut { - fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>; -} - -impl GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToMut, -{ - fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> { - GLWEToLWESwitchingKey(self.0.to_mut()) +impl GGLWEToMut for GLWEToLWESwitchingKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKey { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 25ce059..2ae6032 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -6,8 +6,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees, + GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -193,28 +193,34 @@ impl WriterTo for LWESwitchingKey { } } -pub trait LWESwitchingKeyToRef { - fn to_ref(&self) -> LWESwitchingKey<&[u8]>; -} - -impl LWESwitchingKeyToRef for LWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToRef, -{ - fn to_ref(&self) -> LWESwitchingKey<&[u8]> { - LWESwitchingKey(self.0.to_ref()) +impl GGLWEToRef for LWESwitchingKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.0.to_ref() } } -pub trait LWESwitchingKeyToMut { - fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>; -} - -impl LWESwitchingKeyToMut for LWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToMut, -{ - fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> { - LWESwitchingKey(self.0.to_mut()) +impl GGLWEToMut for LWESwitchingKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for LWESwitchingKey { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree + } +} + +impl GLWESwitchingKeyDegrees for LWESwitchingKey { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree } } diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index 26fb7f9..dbe95dd 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -6,8 +6,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, + GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -196,28 +196,24 @@ impl LWEToGLWESwitchingKey> { } } -pub trait LWEToGLWESwitchingKeyToRef { - fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>; -} - -impl LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToRef, -{ - fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> { - LWEToGLWESwitchingKey(self.0.to_ref()) +impl GGLWEToRef for LWEToGLWESwitchingKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.0.to_ref() } } -pub trait LWEToGLWESwitchingKeyToMut { - fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>; -} - -impl LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToMut, -{ - fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> { - LWEToGLWESwitchingKey(self.0.to_mut()) +impl GGLWEToMut for LWEToGLWESwitchingKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKey { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index d09b2e6..6f3d4b9 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -1,16 +1,13 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, + GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] pub struct AutomorphismKeyPrepared { - pub(crate) key: GLWESwitchingKeyPrepared, + pub(crate) key: GGLWEPrepared, pub(crate) p: i64, } @@ -78,7 +75,7 @@ impl GGLWEInfos for AutomorphismKeyPrepared { pub trait AutomorphismKeyPreparedAlloc where - Self: GLWESwitchingKeyPreparedAlloc, + Self: GGLWEPreparedAlloc, { fn alloc_automorphism_key_prepared( &self, @@ -89,7 +86,7 @@ where dsize: Dsize, ) -> AutomorphismKeyPrepared, B> { AutomorphismKeyPrepared::, B> { - key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize), + key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -120,7 +117,7 @@ where dnum: Dnum, dsize: Dsize, ) -> usize { - self.bytes_of_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize) + self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize) } fn bytes_of_automorphism_key_prepared_from_infos(&self, infos: &A) -> usize @@ -130,7 +127,7 @@ where assert_eq!( infos.rank_in(), infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" + "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); self.bytes_of_automorphism_key_prepared( infos.base2k(), @@ -142,7 +139,7 @@ where } } -impl AutomorphismKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} +impl AutomorphismKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} impl AutomorphismKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -178,26 +175,26 @@ impl AutomorphismKeyPrepared, B> { pub trait PrepareAutomorphismKey where - Self: GLWESwitchingKeyPrepare, + Self: GGLWEPrepare, { fn prepare_automorphism_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.prepare_glwe_switching_key_tmp_bytes(infos) + self.prepare_gglwe_tmp_bytes(infos) } fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, - O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + R: GGLWEPreparedToMut + SetAutomorphismGaloisElement, + O: GGLWEToRef + GetAutomorphismGaloisElement, { - self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch); + self.prepare_gglwe(res, other, scratch); res.set_p(other.p()); } } -impl PrepareAutomorphismKey for Module where Module: GLWESwitchingKeyPrepare {} +impl PrepareAutomorphismKey for Module where Module: GGLWEPrepare {} impl AutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize @@ -211,35 +208,21 @@ impl AutomorphismKeyPrepared, B> { impl AutomorphismKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + O: GGLWEToRef + GetAutomorphismGaloisElement, M: PrepareAutomorphismKey, { module.prepare_automorphism_key(self, other, scratch); } } -pub trait AutomorphismKeyPreparedToMut { - fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; -} - -impl AutomorphismKeyPreparedToMut for AutomorphismKeyPrepared { - fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> { - AutomorphismKeyPrepared { - p: self.p, - key: self.key.to_mut(), - } +impl GGLWEPreparedToMut for AutomorphismKeyPrepared { + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + self.key.to_mut() } } -pub trait AutomorphismKeyPreparedToRef { - fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>; -} - -impl AutomorphismKeyPreparedToRef for AutomorphismKeyPrepared { - fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> { - AutomorphismKeyPrepared { - p: self.p, - key: self.key.to_ref(), - } +impl GGLWEPreparedToRef for AutomorphismKeyPrepared { + fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> { + self.key.to_ref() } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index d8ab0ca..930f3e5 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -1,35 +1,35 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, - GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, + LWEInfos, Rank, TorusPrecision, prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] pub struct GLWESwitchingKeyPrepared { pub(crate) key: GGLWEPrepared, - pub(crate) sk_in_n: usize, // Degree of sk_in - pub(crate) sk_out_n: usize, // Degree of sk_out + pub(crate) input_degree: Degree, // Degree of sk_in + pub(crate) output_degree: Degree, // Degree of sk_out } -impl GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared { - fn set_sk_in_n(&mut self, sk_in_n: usize) { - self.sk_in_n = sk_in_n +impl GLWESwitchingKeyDegrees for GLWESwitchingKeyPrepared { + fn output_degree(&self) -> &Degree { + &self.output_degree } - fn set_sk_out_n(&mut self, sk_out_n: usize) { - self.sk_out_n = sk_out_n + fn input_degree(&self) -> &Degree { + &self.input_degree } } -impl GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared { - fn sk_in_n(&self) -> usize { - self.sk_in_n +impl GLWESwitchingKeyDegreesMut for GLWESwitchingKeyPrepared { + fn output_degree(&mut self) -> &mut Degree { + &mut self.output_degree } - fn sk_out_n(&self) -> usize { - self.sk_out_n + fn input_degree(&mut self) -> &mut Degree { + &mut self.input_degree } } @@ -90,8 +90,8 @@ where ) -> GLWESwitchingKeyPrepared, B> { GLWESwitchingKeyPrepared::, B> { key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), } } @@ -199,12 +199,12 @@ where fn prepare_glwe_switching(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, - O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, { - self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch); - res.set_sk_in_n(other.sk_in_n()); - res.set_sk_out_n(other.sk_out_n()); + self.prepare_gglwe(res, other, scratch); + *res.input_degree() = *other.input_degree(); + *res.output_degree() = *other.output_degree(); } } @@ -213,7 +213,7 @@ impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepa impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, + O: GGLWEToRef + GLWESwitchingKeyDegrees, M: GLWESwitchingKeyPrepare, { module.prepare_glwe_switching(self, other, scratch); @@ -229,30 +229,20 @@ impl GLWESwitchingKeyPrepared, B> { } } -pub trait GLWESwitchingKeyPreparedToMut { - fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; -} - -impl GLWESwitchingKeyPreparedToMut for GLWESwitchingKeyPrepared { - fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> { - GLWESwitchingKeyPrepared { - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - key: self.key.to_mut(), - } +impl GGLWEPreparedToRef for GLWESwitchingKeyPrepared +where + GGLWEPrepared: GGLWEPreparedToRef, +{ + fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> { + self.key.to_ref() } } -pub trait GLWESwitchingKeyPreparedToRef { - fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>; -} - -impl GLWESwitchingKeyPreparedToRef for GLWESwitchingKeyPrepared { - fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> { - GLWESwitchingKeyPrepared { - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - key: self.key.to_ref(), - } +impl GGLWEPreparedToMut for GLWESwitchingKeyPrepared +where + GGLWEPrepared: GGLWEPreparedToMut, +{ + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], BE> { + self.key.to_mut() } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 29f6e28..7b07ce7 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,16 +1,13 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, + GGLWEPreparedToRef, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, }; #[derive(PartialEq, Eq)] pub struct TensorKeyPrepared { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, } impl LWEInfos for TensorKeyPrepared { @@ -57,7 +54,7 @@ impl GGLWEInfos for TensorKeyPrepared { pub trait TensorKeyPreparedAlloc where - Self: GLWESwitchingKeyPreparedAlloc, + Self: GGLWEPreparedAlloc, { fn alloc_tensor_key_prepared( &self, @@ -70,7 +67,7 @@ where let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); TensorKeyPrepared { keys: (0..pairs) - .map(|_| self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| self.alloc_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } @@ -82,7 +79,7 @@ where assert_eq!( infos.rank_in(), infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" + "rank_in != rank_out is not supported for TensorKeyPrepared" ); self.alloc_tensor_key_prepared( infos.base2k(), @@ -95,7 +92,7 @@ where fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize) + pairs * self.bytes_of_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize) } fn bytes_of_tensor_key_prepared_from_infos(&self, infos: &A) -> usize @@ -112,7 +109,7 @@ where } } -impl TensorKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} +impl TensorKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} impl TensorKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -147,8 +144,8 @@ impl TensorKeyPrepared, B> { } impl TensorKeyPrepared { - // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared { + // 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 { std::mem::swap(&mut i, &mut j); }; @@ -158,8 +155,8 @@ impl TensorKeyPrepared { } impl TensorKeyPrepared { - // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared { + // Returns a reference to GGLWE_{s}(s[i] * s[j]) + pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -170,13 +167,13 @@ impl TensorKeyPrepared { pub trait TensorKeyPrepare where - Self: GLWESwitchingKeyPrepare, + Self: GGLWEPrepare, { fn prepare_tensor_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.prepare_glwe_switching_key_tmp_bytes(infos) + self.prepare_gglwe_tmp_bytes(infos) } fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) @@ -190,12 +187,12 @@ where assert_eq!(res.keys.len(), other.keys.len()); for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { - self.prepare_glwe_switching(a, b, scratch); + self.prepare_gglwe(a, b, scratch); } } } -impl TensorKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} +impl TensorKeyPrepare for Module where Self: GGLWEPrepare {} impl TensorKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize @@ -223,7 +220,7 @@ pub trait TensorKeyPreparedToMut { impl TensorKeyPreparedToMut for TensorKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, + GGLWEPrepared: GGLWEPreparedToMut, { fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { TensorKeyPrepared { @@ -238,7 +235,7 @@ pub trait TensorKeyPreparedToRef { impl TensorKeyPreparedToRef for TensorKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, + GGLWEPrepared: GGLWEPreparedToRef, { fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { TensorKeyPrepared { diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index cf1d875..716eeff 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -1,11 +1,9 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, + prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; #[derive(PartialEq, Eq)] @@ -152,10 +150,10 @@ where fn prepare_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: GLWEToLWESwitchingKeyPreparedToMut, - O: GLWEToLWESwitchingKeyToRef, + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, { - self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(res, other, scratch); } } @@ -174,35 +172,37 @@ impl GLWEToLWESwitchingKeyPrepared, B> { impl GLWEToLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: GLWEToLWESwitchingKeyToRef, + O: GGLWEToRef + GLWESwitchingKeyDegrees, M: GLWEToLWESwitchingKeyPrepare, { module.prepare_glwe_to_lwe_switching_key(self, other, scratch); } } -pub trait GLWEToLWESwitchingKeyPreparedToRef { - fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>; -} - -impl GLWEToLWESwitchingKeyPreparedToRef for GLWEToLWESwitchingKeyPrepared +impl GGLWEPreparedToRef for GLWEToLWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, + GLWESwitchingKeyPrepared: GGLWEPreparedToRef, { - fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> { - GLWEToLWESwitchingKeyPrepared(self.0.to_ref()) + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + self.0.to_ref() } } -pub trait GLWEToLWESwitchingKeyPreparedToMut { - fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>; -} - -impl GLWEToLWESwitchingKeyPreparedToMut for GLWEToLWESwitchingKeyPrepared +impl GGLWEPreparedToMut for GLWEToLWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPrepared: GGLWEPreparedToRef, { - fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> { - GLWEToLWESwitchingKeyPrepared(self.0.to_mut()) + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKeyPrepared { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 83508d0..0319f77 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -1,11 +1,9 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, + prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; #[derive(PartialEq, Eq)] @@ -160,10 +158,10 @@ where } fn prepare_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: LWESwitchingKeyPreparedToMut, - O: LWESwitchingKeyToRef, + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, { - self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(res, other, scratch); } } @@ -182,35 +180,37 @@ impl LWESwitchingKeyPrepared, B> { impl LWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: LWESwitchingKeyToRef, + O: GGLWEToRef + GLWESwitchingKeyDegrees, M: LWESwitchingKeyPrepare, { module.prepare_lwe_switching_key(self, other, scratch); } } -pub trait LWESwitchingKeyPreparedToRef { - fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>; -} - -impl LWESwitchingKeyPreparedToRef for LWESwitchingKeyPrepared +impl GGLWEPreparedToRef for LWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, + GGLWEPrepared: GGLWEPreparedToRef, { - fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B> { - LWESwitchingKeyPrepared(self.0.to_ref()) + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + self.0.to_ref() } } -pub trait LWESwitchingKeyPreparedToMut { - fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B>; -} - -impl LWESwitchingKeyPreparedToMut for LWESwitchingKeyPrepared +impl GGLWEPreparedToMut for LWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, + GGLWEPrepared: GGLWEPreparedToMut, { - fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B> { - LWESwitchingKeyPrepared(self.0.to_mut()) + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for LWESwitchingKeyPrepared { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 43b006c..c863c3f 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -1,11 +1,9 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, + prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE]. @@ -159,10 +157,10 @@ where fn prepare_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: LWEToGLWESwitchingKeyPreparedToMut, - O: LWEToGLWESwitchingKeyToRef, + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, { - self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(res, other, scratch); } } @@ -181,35 +179,37 @@ impl LWEToGLWESwitchingKeyPrepared, B> { impl LWEToGLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: LWEToGLWESwitchingKeyToRef, + O: GGLWEToRef + GLWESwitchingKeyDegrees, M: LWEToGLWESwitchingKeyPrepare, { module.prepare_lwe_to_glwe_switching_key(self, other, scratch); } } -pub trait LWEToGLWESwitchingKeyPreparedToRef { - fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>; -} - -impl LWEToGLWESwitchingKeyPreparedToRef for LWEToGLWESwitchingKeyPrepared +impl GGLWEPreparedToRef for LWEToGLWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, + GLWESwitchingKeyPrepared: GGLWEPreparedToRef, { - fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B> { - LWEToGLWESwitchingKeyPrepared(self.0.to_ref()) + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + self.0.to_ref() } } -pub trait LWEToGLWESwitchingKeyPreparedToMut { - fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B>; -} - -impl LWEToGLWESwitchingKeyPreparedToMut for LWEToGLWESwitchingKeyPrepared +impl GGLWEPreparedToMut for LWEToGLWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPrepared: GGLWEPreparedToMut, { - fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B> { - LWEToGLWESwitchingKeyPrepared(self.0.to_mut()) + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKeyPrepared { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index dae6157..78fb717 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -16,6 +16,7 @@ pub use operations::*; pub mod layouts; pub use automorphism::*; pub use conversion::*; +pub use decryption::*; pub use dist::*; pub use encryption::*; pub use external_product::*; diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index d1e95f9..a479080 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,8 +6,8 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - AutomorphismKey, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, - GLWESwitchingKey, Rank, TensorKey, + AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, + GLWESecret, GLWESwitchingKey, Rank, TensorKey, prepared::{ AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared, @@ -258,8 +258,8 @@ where ( GLWESwitchingKey { key: data, - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), }, scratch, ) @@ -279,8 +279,8 @@ where ( GLWESwitchingKeyPrepared { key: data, - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), }, scratch, ) @@ -292,7 +292,7 @@ where M: ModuleN, { assert_eq!(module.n() as u32, infos.n()); - let (data, scratch) = self.take_glwe_switching_key(module, infos); + let (data, scratch) = self.take_gglwe(module, infos); (AutomorphismKey { key: data, p: 0 }, scratch) } @@ -306,7 +306,7 @@ where M: ModuleN + VmpPMatBytesOf, { assert_eq!(module.n() as u32, infos.n()); - let (data, scratch) = self.take_gglwe_switching_key_prepared(module, infos); + let (data, scratch) = self.take_gglwe_prepared(module, infos); (AutomorphismKeyPrepared { key: data, p: 0 }, scratch) } @@ -321,21 +321,21 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let mut scratch: &mut Self = self; - let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); + let mut ksk_infos: GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe(module, &ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe(module, &ksk_infos); scratch = s; keys.push(gglwe); } @@ -354,21 +354,21 @@ where "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let mut scratch: &mut Self = self; - let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); + let mut ksk_infos: GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe_prepared(module, &ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe_prepared(module, &ksk_infos); scratch = s; keys.push(gglwe); } diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 8bdf034..61fbcb1 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -18,8 +18,8 @@ backend_test_suite!( glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, // GLWE Keyswitch -// glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, -// glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, +glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, +glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, // GLWE Automorphism // glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, // glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, @@ -35,10 +35,10 @@ gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryptio gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk, gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk, gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, -// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, +gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // GGLWE Keyswitching -// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, -// gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, +gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, +gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, // GGLWE External Product // gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, // gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, @@ -46,11 +46,11 @@ gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_ // gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, // gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, // GGSW Encryption -// ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, -// ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, +ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, +ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, // GGSW Keyswitching -// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, -// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, +ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, +ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, // GGSW External Product // ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, // ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, @@ -58,7 +58,7 @@ gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_ // ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, // ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, // LWE -// lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, +lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, // glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, // lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, } 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 487f1f7..4f84616 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -5,7 +5,8 @@ use poulpy_hal::{ }; use crate::{ - GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, + AutomorphismKeyCompressedEncryptSk, AutomorphismKeyEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, + GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare, @@ -16,7 +17,7 @@ use crate::{ pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) where - Module: GGLWEEncryptSk + Module: AutomorphismKeyEncryptSk + GGLWEKeyswitch + GLWESecretPreparedAlloc + GLWESecretPrepare @@ -82,7 +83,6 @@ where sk_out_prepared.prepare(module, &sk_out); atk.key - .key .assert_noise(module, &sk_out_prepared, &sk.data, SIGMA); } } @@ -90,7 +90,7 @@ where pub fn test_gglwe_automorphism_key_compressed_encrypt_sk(module: &Module) where - Module: GGLWEEncryptSk + Module: AutomorphismKeyCompressedEncryptSk + GGLWEKeyswitch + GLWESecretPreparedAlloc + GLWESecretPrepare @@ -125,7 +125,9 @@ where let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: 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(AutomorphismKeyCompressed::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); @@ -153,7 +155,6 @@ where atk.decompress(module, &atk_compressed); atk.key - .key .assert_noise(module, &sk_out_prepared, &sk.data, SIGMA); } } diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs index 06167d1..60eaf0d 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -31,7 +31,7 @@ where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { - let n = module.n(); + let n: usize = module.n(); let base2k: usize = 12; let k_ksk: usize = 54; let dsize: usize = k_ksk / base2k; 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 647f68e..48573db 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxSwitchRing, }, - layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, + layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxBig, VecZnxDft}, source::Source, }; @@ -73,8 +73,8 @@ where let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); - let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); - let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); + let mut sk_ij_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(1, 1); + let mut sk_ij_big: VecZnxBig, BE> = module.vec_znx_big_alloc(1, 1); let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); @@ -126,8 +126,6 @@ where + VecZnxFillUniform + VecZnxCopy + VecZnxSwitchRing, - // + VecZnxNormalizeInplaceImpl - // + VecZnxNormalizeImpl, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -170,8 +168,8 @@ where let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); - let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); - let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); + let mut sk_ij_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(1, 1); + let mut sk_ij_big: VecZnxBig, BE> = module.vec_znx_big_alloc(1, 1); let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); 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 08d268c..9646909 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -9,7 +9,8 @@ use crate::{ encryption::SIGMA, layouts::{ GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout, - GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPreparedAlloc, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare, + TensorKeyPreparedAlloc, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, @@ -26,7 +27,9 @@ where + GLWESecretPrepare + TensorKeyPreparedAlloc + GLWESwitchingKeyPreparedAlloc - + GGSWNoise, + + GGSWNoise + + GLWESwitchingKeyPrepare + + TensorKeyPrepare, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -145,8 +148,12 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); - let tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut ksk_prepared: GLWESwitchingKeyPrepared, BE> = + 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); + tsk_prepared.prepare(module, &tsk, scratch.borrow()); ggsw_out.keyswitch( module, @@ -188,7 +195,9 @@ where + GLWESecretPrepare + TensorKeyPreparedAlloc + GLWESwitchingKeyPreparedAlloc - + GGSWNoise, + + GGSWNoise + + GLWESwitchingKeyPrepare + + TensorKeyPrepare, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -259,6 +268,7 @@ where let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); + let mut sk_in_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_in_prepared.prepare(module, &sk_in); @@ -295,8 +305,12 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); - let tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut ksk_prepared: GLWESwitchingKeyPrepared, BE> = + 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); + 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/keyswitch/glwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs index 6cbe7c3..146ea32 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -1,14 +1,15 @@ use poulpy_hal::{ - api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - ScratchTakeCore, + GLWEEncryptSk, GLWEKeyswitch, GLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, + GLWESwitchingKeyLayout, GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -17,7 +18,15 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_glwe_keyswitch(module: &Module) where - Module:, + Module: VecZnxFillUniform + + GLWESwitchingKeyEncryptSk + + GLWEEncryptSk + + GLWEKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyPrepare + + GLWESwitchingKeyPreparedAlloc + + GLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -48,7 +57,7 @@ where rank: rank_out.into(), }; - let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { + let ksk: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -58,7 +67,7 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk); let mut glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_infos); let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); @@ -70,18 +79,22 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) - | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &key_apply), + | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ksk), ); let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_prepared: GLWESecretPrepared, BE> = sk_in.prepare_alloc(module, scratch.borrow()); + + let mut sk_in_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_in.into()); + sk_in_prepared.prepare(module, &sk_in); let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, BE> = sk_out.prepare_alloc(module, scratch.borrow()); + + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); + sk_out_prepared.prepare(module, &sk_out); ksk.encrypt_sk( module, @@ -101,7 +114,9 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, BE> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: GLWESwitchingKeyPrepared, BE> = + GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow()); @@ -126,7 +141,15 @@ where pub fn test_glwe_keyswitch_inplace(module: &Module) where - Module:, + Module: VecZnxFillUniform + + GLWESwitchingKeyEncryptSk + + GLWEEncryptSk + + GLWEKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyPrepare + + GLWESwitchingKeyPreparedAlloc + + GLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -148,7 +171,7 @@ where rank: rank.into(), }; - let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { + let ksk_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -158,7 +181,7 @@ where rank_out: rank.into(), }; - let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_infos); let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); @@ -169,20 +192,24 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) - | GLWE::keyswitch_inplace_tmp_bytes(module, &glwe_out_infos, &key_apply_infos), + | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &ksk_infos), ); let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_prepared: GLWESecretPrepared, BE> = sk_in.prepare_alloc(module, scratch.borrow()); + + let mut sk_in_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_in_prepared.prepare(module, &sk_in); let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, BE> = sk_out.prepare_alloc(module, scratch.borrow()); - key_apply.encrypt_sk( + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_out_prepared.prepare(module, &sk_out); + + ksk.encrypt_sk( module, &sk_in, &sk_out, @@ -200,7 +227,9 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, BE> = key_apply.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: GLWESwitchingKeyPrepared, BE> = + GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs index 0722b98..a709747 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -5,15 +5,21 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, + LWEDecrypt, LWEEncryptSk, LWEKeySwitch, LWESwitchingKeyEncrypt, ScratchTakeCore, layouts::{ - LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::LWESwitchingKeyPrepared, + LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, LWESwitchingKeyPrepare, + LWESwitchingKeyPreparedAlloc, prepared::LWESwitchingKeyPrepared, }, }; pub fn test_lwe_keyswitch(module: &Module) where - Module:, + Module: LWEKeySwitch + + LWESwitchingKeyEncrypt + + LWEEncryptSk + + LWESwitchingKeyPreparedAlloc + + LWEDecrypt + + LWESwitchingKeyPrepare, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -89,7 +95,8 @@ where let mut lwe_ct_out: LWE> = LWE::alloc_from_infos(&lwe_out_infos); - let ksk_prepared: LWESwitchingKeyPrepared, BE> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: LWESwitchingKeyPrepared, BE> = LWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index 3382774..6653f90 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -1,4 +1,4 @@ -use std::{fmt::Display, marker::PhantomData, ptr::NonNull}; +use std::{fmt::{Debug, Display}, marker::PhantomData, ptr::NonNull}; use rand_distr::num_traits::Zero; @@ -9,8 +9,8 @@ use crate::{ #[allow(clippy::missing_safety_doc)] pub trait Backend: Sized { - type ScalarBig: Copy + Zero + Display; - type ScalarPrep: Copy + Zero + Display; + type ScalarBig: Copy + Zero + Display + Debug; + type ScalarPrep: Copy + Zero + Display + Debug; type Handle: 'static; fn layout_prep_word_count() -> usize; fn layout_big_word_count() -> usize; diff --git a/poulpy-hal/src/layouts/znx_base.rs b/poulpy-hal/src/layouts/znx_base.rs index 7ca75b2..45e30d4 100644 --- a/poulpy-hal/src/layouts/znx_base.rs +++ b/poulpy-hal/src/layouts/znx_base.rs @@ -1,3 +1,5 @@ +use std::fmt::{Debug, Display}; + use crate::{ layouts::{Backend, Data, DataMut, DataRef}, source::Source, @@ -48,7 +50,7 @@ pub trait DataViewMut: DataView { } pub trait ZnxView: ZnxInfos + DataView { - type Scalar: Copy + Zero; + type Scalar: Copy + Zero + Display + Debug; /// Returns a non-mutable pointer to the underlying coefficients array. fn as_ptr(&self) -> *const Self::Scalar {