external product tests
jump to
@@ -95,13 +95,7 @@
impl<BE: Backend> GGLWEExternalProduct<BE> for Module<BE> where Self: GLWEExternalProduct<BE> {} impl GLWESwitchingKey<Vec<u8>> { - pub fn external_product_tmp_bytes<R, A, B, M, BE: Backend>( - &self, - module: &M, - res_infos: &R, - a_infos: &A, - b_infos: &B, - ) -> usize + pub fn external_product_tmp_bytes<R, A, B, M, BE: Backend>(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GGLWEInfos, A: GGLWEInfos,
@@ -97,13 +97,7 @@
impl<BE: Backend> GGSWExternalProduct<BE> for Module<BE> where Self: GLWEExternalProduct<BE> {} impl GGSW<Vec<u8>> { - pub fn external_product_tmp_bytes<R, A, B, M, BE: Backend>( - &self, - module: &M, - res_infos: &R, - a_infos: &A, - b_infos: &B, - ) -> usize + pub fn external_product_tmp_bytes<R, A, B, M, BE: Backend>(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GGSWInfos, A: GGSWInfos,
@@ -24,8 +24,8 @@ // 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 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 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 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 Keyswitching
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,
@@ -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<B>(module: &Module<B>) +pub fn test_gglwe_switching_key_external_product<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc<B> - + VecZnxRotateInplace<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VmpPrepare<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B> - + VecZnxDftAllocBytesImpl<B> - + VecZnxBigAllocBytesImpl<B> - + TakeSvpPPolImpl<B>, + Module<BE>: GGLWEExternalProduct<BE> + + GGSWEncryptSk<BE> + + GLWESwitchingKeyEncryptSk<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + VecZnxRotateInplace<BE> + + GGSWPrepare<BE> + + GGSWPreparedAlloc<BE> + + GGLWENoise<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_in: usize = 60;@@ -101,7 +63,7 @@ rank_in: rank_in.into(),
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 @@ let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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 @@ sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow()); + + let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, 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 @@ &mut source_xe,
scratch.borrow(), ); - let ct_rgsw_prepared: GGSWPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let mut ct_rgsw_prepared: GGSWPrepared<Vec<u8>, 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 @@ }
} #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_switching_key_external_product_inplace<B>(module: &Module<B>) +pub fn test_gglwe_switching_key_external_product_inplace<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc<B> - + VecZnxRotateInplace<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VmpPrepare<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B> - + VecZnxDftAllocBytesImpl<B> - + VecZnxBigAllocBytesImpl<B> - + TakeSvpPPolImpl<B>, + Module<BE>: GGLWEExternalProduct<BE> + + GGSWEncryptSk<BE> + + GLWESwitchingKeyEncryptSk<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + VecZnxRotateInplace<BE> + + GGSWPrepare<BE> + + GGSWPreparedAlloc<BE> + + GGLWENoise<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_out: usize = 60;@@ -273,7 +204,7 @@ rank_in: rank_in.into(),
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 @@ let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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 @@ sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow()); + + let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, 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 @@ &mut source_xe,
scratch.borrow(), ); - let ct_rgsw_prepared: GGSWPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let mut ct_rgsw_prepared: GGSWPrepared<Vec<u8>, 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());
@@ -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<B>(module: &Module<B>) +pub fn test_ggsw_external_product<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxAddScalarInplace - + VecZnxCopy - + VmpPMatAlloc<B> - + VecZnxRotateInplace<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VmpPrepare<B> - + VecZnxBigAlloc<B> - + VecZnxDftAlloc<B> - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B> - + VecZnxDftAllocBytesImpl<B> - + VecZnxBigAllocBytesImpl<B> - + TakeSvpPPolImpl<B>, + Module<BE>: GGSWEncryptSk<BE> + + GGSWExternalProduct<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + GGSWPrepare<BE> + + GGSWPreparedAlloc<BE> + + VecZnxRotateInplace<BE> + + GGSWNoise<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_in: usize = 60;@@ -84,7 +42,7 @@ let dnum: usize = k_in.div_ceil(base2k * di);
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 @@ dsize: dsize_in.into(),
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 @@ dsize: dsize_in.into(),
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 @@ let k: usize = 1;
pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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 @@ );
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module,@@ -155,7 +115,8 @@ &mut source_xe,
scratch.borrow(), ); - let ct_rhs_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_rhs_prepared: GGSWPrepared<Vec<u8>, 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 @@ }
} #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_external_product_inplace<B>(module: &Module<B>) +pub fn test_ggsw_external_product_inplace<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxAddScalarInplace - + VecZnxCopy - + VmpPMatAlloc<B> - + VecZnxRotateInplace<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VmpPrepare<B> - + VecZnxBigAlloc<B> - + VecZnxDftAlloc<B> - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B> - + VecZnxDftAllocBytesImpl<B> - + VecZnxBigAllocBytesImpl<B> - + TakeSvpPPolImpl<B>, + Module<BE>: GGSWEncryptSk<BE> + + GGSWExternalProduct<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + GGSWPrepare<BE> + + GGSWPreparedAlloc<BE> + + VecZnxRotateInplace<BE> + + GGSWNoise<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_out: usize = 60;@@ -247,7 +176,7 @@ let dnum: usize = k_out.div_ceil(di * base2k);
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 @@ dsize: dsize_in.into(),
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 @@ let k: usize = 1;
pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module,@@ -309,7 +240,8 @@ &mut source_xe,
scratch.borrow(), ); - let ct_rhs_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_rhs_prepared: GGSWPrepared<Vec<u8>, 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());
@@ -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<B>(module: &Module<B>) +pub fn test_glwe_external_product<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> + Module<BE>: GGSWEncryptSk<BE> + + GGSWPrepare<BE> + + GGSWPreparedAlloc<BE> + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VecZnxRotateInplace<B> - + VmpPMatAlloc<B> - + VmpPrepare<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + TakeSvpPPolImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B>, + + GLWEExternalProduct<BE> + + GLWEEncryptSk<BE> + + GLWENoise<BE> + + VecZnxRotateInplace<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_in: usize = 45;@@ -87,7 +56,7 @@ k: k_out.into(),
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 @@ let k: usize = 1;
pt_ggsw.raw_mut()[k] = 1; // X^{k} - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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 @@ );
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module,@@ -143,7 +114,8 @@ &mut source_xe,
scratch.borrow(), ); - let ct_ggsw_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_ggsw_prepared: GGSWPrepared<Vec<u8>, 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 @@ }
} #[allow(clippy::too_many_arguments)] -pub fn test_glwe_external_product_inplace<B>(module: &Module<B>) +pub fn test_glwe_external_product_inplace<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> + Module<BE>: GGSWEncryptSk<BE> + + GGSWPrepare<BE> + + GGSWPreparedAlloc<BE> + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VecZnxRotateInplace<B> - + VmpPMatAlloc<B> - + VmpPrepare<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + TakeSvpPPolImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B>, + + GLWEExternalProduct<BE> + + GLWEEncryptSk<BE> + + GLWENoise<BE> + + VecZnxRotateInplace<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_out: usize = 60;@@ -231,7 +180,7 @@ k: k_out.into(),
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 @@ let k: usize = 1;
pt_ggsw.raw_mut()[k] = 1; // X^{k} - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module,@@ -286,7 +237,8 @@ &mut source_xe,
scratch.borrow(), ); - let ct_ggsw_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_ggsw_prepared: GGSWPrepared<Vec<u8>, 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());
@@ -1,6 +1,6 @@
// pub mod automorphism; pub mod encryption; -// pub mod external_product; +pub mod external_product; pub mod keyswitch; // mod conversion;