diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index c6ee4a6..f79ff6e 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -95,13 +95,7 @@ where impl GGLWEExternalProduct for Module where Self: GLWEExternalProduct {} impl GLWESwitchingKey> { - pub fn external_product_tmp_bytes( - &self, - module: &M, - res_infos: &R, - a_infos: &A, - b_infos: &B, - ) -> usize + pub fn external_product_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GGLWEInfos, A: GGLWEInfos, diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index d3a59a6..f9659fe 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -97,13 +97,7 @@ where impl GGSWExternalProduct for Module where Self: GLWEExternalProduct {} impl GGSW> { - pub fn external_product_tmp_bytes( - &self, - module: &M, - res_infos: &R, - a_infos: &A, - b_infos: &B, - ) -> usize + pub fn external_product_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GGSWInfos, A: GGSWInfos, diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 61fbcb1..2281caf 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -24,8 +24,8 @@ glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswit // 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, +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, // GLWE Trace // glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, // glwe_packing => crate::tests::test_suite::test_glwe_packing, @@ -40,8 +40,8 @@ gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption:: 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, +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, @@ -52,8 +52,8 @@ ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_co 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, +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, diff --git a/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs b/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs index a369978..d5f13fb 100644 --- a/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs @@ -1,72 +1,34 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, - VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, Scratch, ScratchOwned, ZnxViewMut}, source::Source, }; use crate::{ + GGLWEExternalProduct, GGLWENoise, GGSWEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_switching_key_external_product(module: &Module) +pub fn test_gglwe_switching_key_external_product(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VecZnxRotateInplace - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGLWEExternalProduct + + GGSWEncryptSk + + GLWESwitchingKeyEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + VecZnxRotateInplace + + GGSWPrepare + + GGSWPreparedAlloc + + GGLWENoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -101,7 +63,7 @@ where rank_out: rank_out.into(), }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_ggsw.into(), @@ -120,14 +82,9 @@ 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( GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_in_infos) - | GLWESwitchingKey::external_product_tmp_bytes( - module, - &gglwe_out_infos, - &gglwe_in_infos, - &ggsw_infos, - ) + | GLWESwitchingKey::external_product_tmp_bytes(module, &gglwe_out_infos, &gglwe_in_infos, &ggsw_infos) | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos), ); @@ -142,7 +99,9 @@ where let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = 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); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe_in.encrypt_sk( @@ -163,7 +122,8 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let mut ct_rgsw_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ct_rgsw); + ct_rgsw_prepared.prepare(module, &ct_rgsw, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow()); @@ -207,48 +167,19 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_switching_key_external_product_inplace(module: &Module) +pub fn test_gglwe_switching_key_external_product_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VecZnxRotateInplace - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGLWEExternalProduct + + GGSWEncryptSk + + GLWESwitchingKeyEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + VecZnxRotateInplace + + GGSWPrepare + + GGSWPreparedAlloc + + GGLWENoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -273,7 +204,7 @@ where rank_out: rank_out.into(), }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_ggsw.into(), @@ -291,9 +222,9 @@ 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( GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_out_infos) - | GLWESwitchingKey::external_product_inplace_tmp_bytes(module, &gglwe_out_infos, &ggsw_infos) + | GLWESwitchingKey::external_product_tmp_bytes(module, &gglwe_out_infos, &gglwe_out_infos, &ggsw_infos) | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos), ); @@ -308,7 +239,9 @@ where let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = 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); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe.encrypt_sk( @@ -329,7 +262,8 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let mut ct_rgsw_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ct_rgsw); + ct_rgsw_prepared.prepare(module, &ct_rgsw, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs index 84a2f68..de080a3 100644 --- a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs @@ -1,74 +1,32 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, Scratch, ScratchOwned, ZnxViewMut}, source::Source, }; use crate::{ + GGSWEncryptSk, GGSWExternalProduct, GGSWNoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWESecret, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_external_product(module: &Module) +pub fn test_ggsw_external_product(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VecZnxRotateInplace - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGSWEncryptSk + + GGSWExternalProduct + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GGSWPrepare + + GGSWPreparedAlloc + + VecZnxRotateInplace + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -84,7 +42,7 @@ where let dnum_in: usize = k_in.div_euclid(base2k * di); let dsize_in: usize = 1; - let ggsw_in_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_in_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -93,7 +51,7 @@ where rank: rank.into(), }; - let ggsw_out_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -102,7 +60,7 @@ where rank: rank.into(), }; - let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_apply_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -127,7 +85,7 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) | GGSW::external_product_tmp_bytes(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), @@ -135,7 +93,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); 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(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module, @@ -155,7 +115,8 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_rhs_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply); + ct_rhs_prepared.prepare(module, &ggsw_apply, scratch.borrow()); ggsw_out.external_product(module, &ggsw_in, &ct_rhs_prepared, scratch.borrow()); @@ -190,50 +151,18 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_external_product_inplace(module: &Module) +pub fn test_ggsw_external_product_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VecZnxRotateInplace - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGSWEncryptSk + + GGSWExternalProduct + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GGSWPrepare + + GGSWPreparedAlloc + + VecZnxRotateInplace + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -247,7 +176,7 @@ where let dnum_in: usize = k_out.div_euclid(base2k * di); let dsize_in: usize = 1; - let ggsw_out_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -256,7 +185,7 @@ where rank: rank.into(), }; - let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_apply_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -281,15 +210,17 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) - | GGSW::external_product_inplace_tmp_bytes(module, &ggsw_out_infos, &ggsw_apply_infos), + | GGSW::external_product_tmp_bytes(module, &ggsw_out_infos, &ggsw_out_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); 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(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module, @@ -309,7 +240,8 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_rhs_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply); + ct_rhs_prepared.prepare(module, &ggsw_apply, scratch.borrow()); ggsw_out.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs b/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs index 60026b8..6c9de79 100644 --- a/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs @@ -1,66 +1,35 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, VecZnxRotateInplace}, + layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned, ZnxViewMut}, source::Source, }; use crate::{ + GGSWEncryptSk, GLWEEncryptSk, GLWEExternalProduct, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, + GLWESecretPreparedAlloc, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_glwe_external_product(module: &Module) +pub fn test_glwe_external_product(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume + Module: GGSWEncryptSk + + GGSWPrepare + + GGSWPreparedAlloc + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VecZnxRotateInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWEExternalProduct + + GLWEEncryptSk + + GLWENoise + + VecZnxRotateInplace + + GLWESecretPrepare + + GLWESecretPreparedAlloc, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 45; @@ -87,7 +56,7 @@ where rank: rank.into(), }; - let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_apply_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_ggsw.into(), @@ -115,7 +84,7 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) | GLWE::external_product_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), @@ -123,7 +92,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); 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(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module, @@ -143,7 +114,8 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_ggsw_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply); + ct_ggsw_prepared.prepare(module, &ggsw_apply, scratch.borrow()); glwe_out.external_product(module, &glwe_in, &ct_ggsw_prepared, scratch.borrow()); @@ -176,43 +148,20 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_glwe_external_product_inplace(module: &Module) +pub fn test_glwe_external_product_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume + Module: GGSWEncryptSk + + GGSWPrepare + + GGSWPreparedAlloc + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VecZnxRotateInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWEExternalProduct + + GLWEEncryptSk + + GLWENoise + + VecZnxRotateInplace + + GLWESecretPrepare + + GLWESecretPreparedAlloc, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -231,7 +180,7 @@ where rank: rank.into(), }; - let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_apply_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_ggsw.into(), @@ -258,15 +207,17 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) - | GLWE::external_product_inplace_tmp_bytes(module, &glwe_out_infos, &ggsw_apply_infos), + | GLWE::external_product_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); 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(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module, @@ -286,7 +237,8 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_ggsw_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply); + ct_ggsw_prepared.prepare(module, &ggsw_apply, scratch.borrow()); glwe_out.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index 9737a54..b0bfff4 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -1,6 +1,6 @@ // pub mod automorphism; pub mod encryption; -// pub mod external_product; +pub mod external_product; pub mod keyswitch; // mod conversion;