diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index b768b81..3c9c9f3 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -86,6 +86,7 @@ where ); let cols_out: usize = (key.rank_out() + 1).into(); + let cols_in: usize = key.rank_in().into(); let p: i64 = a.p(); let p_inv: i64 = self.galois_element_inv(p); @@ -95,7 +96,7 @@ where let a: &GGLWE<&[u8]> = &a.to_ref(); for row in 0..res.dnum().as_usize() { - for col in 0..cols_out { + for col in 0..cols_in { let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); let a_ct: GLWE<&[u8]> = a.at(row, col); @@ -133,18 +134,19 @@ where ); let cols_out: usize = (key.rank_out() + 1).into(); + let cols_in: usize = key.rank_in().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 { + for col in 0..cols_in { let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); // 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_inplace(p_inv, res_tmp.data_mut(), i, scratch); + self.vec_znx_automorphism_inplace(p, res_tmp.data_mut(), i, scratch); } // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 2b9cc06..b7469a2 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -121,7 +121,7 @@ where { let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank()); - (0..res.rank_out().into()).for_each(|i| { + for i in 0..res.rank().into() { self.vec_znx_automorphism( self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), @@ -129,7 +129,7 @@ where &sk.data.as_vec_znx(), i, ); - }); + } sk_out_prepared.prepare(self, &sk_out); } diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index ba4ae8e..fa80541 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -156,7 +156,6 @@ where // // (-(a*s) + s0, a) // (-(b*s) + s1, b) - for col_i in 0..rank_in { for row_i in 0..dnum { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 6f3d4b9..244ab0a 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -173,7 +173,7 @@ impl AutomorphismKeyPrepared, B> { } } -pub trait PrepareAutomorphismKey +pub trait AutomorphismKeyPrepare where Self: GGLWEPrepare, { @@ -194,12 +194,12 @@ where } } -impl PrepareAutomorphismKey for Module where Module: GGLWEPrepare {} +impl AutomorphismKeyPrepare for Module where Module: GGLWEPrepare {} impl AutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - M: PrepareAutomorphismKey, + M: AutomorphismKeyPrepare, { module.prepare_automorphism_key_tmp_bytes(self) } @@ -209,7 +209,7 @@ impl AutomorphismKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GetAutomorphismGaloisElement, - M: PrepareAutomorphismKey, + M: AutomorphismKeyPrepare, { module.prepare_automorphism_key(self, other, scratch); } diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 2281caf..f24ff84 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -21,8 +21,8 @@ backend_test_suite!( 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, +glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, +glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, // GLWE External Product glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, @@ -43,8 +43,8 @@ gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::te 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, // GGLWE Automorphism -// 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, +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, @@ -55,8 +55,8 @@ ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswit 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, // GGSW Automorphism -// ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, -// ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, +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, // glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, diff --git a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs index 37b5403..5a7bad6 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -1,71 +1,35 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphism, VecZnxSubScalarInplace}, + layouts::{Backend, GaloisElement, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + AutomorphismKeyAutomorphism, AutomorphismKeyEncryptSk, GLWEDecrypt, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GGLWEInfos, GLWEPlaintext, + GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_automorphism_key_automorphism(module: &Module) +pub fn test_gglwe_automorphism_key_automorphism(module: &Module) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize + Module: AutomorphismKeyEncryptSk + + AutomorphismKeyPreparedAlloc + + AutomorphismKeyPrepare + + AutomorphismKeyAutomorphism + VecZnxAutomorphism - + VecZnxAutomorphismInplace - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpApplyDftToDftInplace - + VecZnxAddScalarInplace - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxSwitchRing - + SvpPPolAlloc - + VecZnxBigAddInplace - + VecZnxSubScalarInplace, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl, + + GaloisElement + + VecZnxSubScalarInplace + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWEDecrypt, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -119,7 +83,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos) | AutomorphismKey::automorphism_tmp_bytes( @@ -153,7 +117,7 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_infos); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -180,7 +144,8 @@ where ); } - let sk_auto_dft: GLWESecretPrepared, B> = sk_auto.prepare_alloc(module, scratch.borrow()); + let mut sk_auto_dft: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_auto); + sk_auto_dft.prepare(module, &sk_auto); (0..auto_key_out.rank_in().into()).for_each(|col_i| { (0..auto_key_out.dnum().into()).for_each(|row_i| { @@ -222,61 +187,20 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) +pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize + Module: AutomorphismKeyEncryptSk + + AutomorphismKeyPreparedAlloc + + AutomorphismKeyPrepare + + AutomorphismKeyAutomorphism + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxAutomorphismInplace - + VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes + + GaloisElement + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl, + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWEDecrypt, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -318,10 +242,10 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply) - | AutomorphismKey::automorphism_inplace_tmp_bytes(module, &auto_key, &auto_key_apply), + | AutomorphismKey::automorphism_tmp_bytes(module, &auto_key, &auto_key, &auto_key_apply), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); @@ -347,7 +271,7 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_layout); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -370,7 +294,8 @@ where ); } - let sk_auto_dft: GLWESecretPrepared, B> = sk_auto.prepare_alloc(module, scratch.borrow()); + let mut sk_auto_dft: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_auto); + sk_auto_dft.prepare(module, &sk_auto); (0..auto_key.rank_in().into()).for_each(|col_i| { (0..auto_key.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs index 7c2a427..4a2b665 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -1,79 +1,36 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphismInplace}, + layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned}, source::Source, }; use crate::{ + AutomorphismKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, TensorKeyEncryptSk, encryption::SIGMA, layouts::{ - AutomorphismKey, GGSW, GGSWCiphertextLayout, GLWESecret, TensorKey, TensorKeyLayout, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc, TensorKeyPrepared}, + AutomorphismKey, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, + GLWESecretPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare, TensorKeyPreparedAlloc, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; -pub fn test_ggsw_automorphism(module: &Module) +pub fn test_ggsw_automorphism(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxAddScalarInplace - + VecZnxCopy - + VecZnxSubInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxFillUniform - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpApplyDftToDft - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + VecZnxAutomorphism, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGSWEncryptSk + + AutomorphismKeyEncryptSk + + AutomorphismKeyPreparedAlloc + + AutomorphismKeyPrepare + + GGSWAutomorphism + + TensorKeyPrepare + + TensorKeyPreparedAlloc + + TensorKeyEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + VecZnxAutomorphismInplace + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 54; @@ -92,7 +49,7 @@ where let dsize_in: usize = 1; - let ggsw_in_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_in_layout: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -101,7 +58,7 @@ where rank: rank.into(), }; - let ggsw_out_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_layout: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -138,7 +95,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ct_in) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) @@ -149,7 +106,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(var_xs, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); auto_key.encrypt_sk( module, @@ -178,11 +137,12 @@ where scratch.borrow(), ); - let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + let mut auto_key_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, BE> = + TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -217,56 +177,22 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_automorphism_inplace(module: &Module) +pub fn test_ggsw_automorphism_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxAddScalarInplace - + VecZnxCopy - + VecZnxSubInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigAddSmallInplace - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxFillUniform - + SvpApplyDftToDft - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + VecZnxAutomorphism, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGSWEncryptSk + + AutomorphismKeyEncryptSk + + AutomorphismKeyPreparedAlloc + + AutomorphismKeyPrepare + + GGSWAutomorphism + + TensorKeyPrepare + + TensorKeyPreparedAlloc + + TensorKeyEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + VecZnxAutomorphismInplace + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 54; @@ -282,7 +208,7 @@ where let dnum_in: usize = k_out.div_euclid(base2k * di); let dsize_in: usize = 1; - let ggsw_out_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_layout: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -318,18 +244,20 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ct) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) - | GGSW::automorphism_inplace_tmp_bytes(module, &ct, &auto_key, &tensor_key), + | GGSW::automorphism_tmp_bytes(module, &ct, &ct, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(var_xs, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); auto_key.encrypt_sk( module, @@ -358,11 +286,12 @@ where scratch.borrow(), ); - let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + let mut auto_key_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, BE> = + TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs index 02afcb8..22d913d 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -1,69 +1,35 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphismInplace, VecZnxFillUniform}, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + AutomorphismKeyEncryptSk, GLWEAutomorphism, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout, + GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, }; -pub fn test_glwe_automorphism(module: &Module) +pub fn test_glwe_automorphism(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes + Module: GLWEEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWEDecrypt + + GLWEAutomorphism + + AutomorphismKeyEncryptSk + + AutomorphismKeyPrepare + + AutomorphismKeyPreparedAlloc + + GLWENoise + + VecZnxAutomorphismInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -111,7 +77,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) | GLWE::decrypt_tmp_bytes(module, &ct_out) | GLWE::encrypt_sk_tmp_bytes(module, &ct_in) @@ -120,7 +86,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); autokey.encrypt_sk( module, @@ -140,7 +108,7 @@ where scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared, B> = + let mut autokey_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &autokey_infos); autokey_prepared.prepare(module, &autokey, scratch.borrow()); @@ -167,46 +135,21 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_glwe_automorphism_inplace(module: &Module) +pub fn test_glwe_automorphism_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes + Module: GLWEEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWEDecrypt + + GLWEAutomorphism + + AutomorphismKeyEncryptSk + + AutomorphismKeyPrepare + + AutomorphismKeyPreparedAlloc + + GLWENoise + + VecZnxAutomorphismInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -245,16 +188,18 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) | GLWE::decrypt_tmp_bytes(module, &ct) | GLWE::encrypt_sk_tmp_bytes(module, &ct) - | GLWE::automorphism_inplace_tmp_bytes(module, &ct, &autokey), + | GLWE::automorphism_tmp_bytes(module, &ct, &ct, &autokey), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); autokey.encrypt_sk( module, @@ -274,7 +219,7 @@ where scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared, B> = + let mut autokey_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &autokey); autokey_prepared.prepare(module, &autokey, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index b0bfff4..11af37a 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -1,4 +1,4 @@ -// pub mod automorphism; +pub mod automorphism; pub mod encryption; pub mod external_product; pub mod keyswitch; diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index 6653f90..10b04b9 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -114,7 +114,7 @@ where } let g_exp: u64 = - mod_exp_u64(GALOISGENERATOR, (self.cyclotomic_order() - 1) as usize) & (self.cyclotomic_order() - 1) as u64; + mod_exp_u64(gal_el.unsigned_abs(), (self.cyclotomic_order() - 1) as usize) & (self.cyclotomic_order() - 1) as u64; g_exp as i64 * gal_el.signum() } }