automorphism tests
jump to
@@ -86,6 +86,7 @@ a.dsize()
); 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 @@ 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 { + 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 @@ key.rank()
); 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)
@@ -121,7 +121,7 @@ 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| { + 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 @@ i,
&sk.data.as_vec_znx(), i, ); - }); + } sk_out_prepared.prepare(self, &sk_out); }
@@ -156,7 +156,6 @@ // Example ksk rank 2 to rank 1
// // (-(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
@@ -173,7 +173,7 @@ module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
} } -pub trait PrepareAutomorphismKey<B: Backend> +pub trait AutomorphismKeyPrepare<B: Backend> where Self: GGLWEPrepare<B>, {@@ -194,12 +194,12 @@ res.set_p(other.p());
} } -impl<B: Backend> PrepareAutomorphismKey<B> for Module<B> where Module<B>: GGLWEPrepare<B> {} +impl<B: Backend> AutomorphismKeyPrepare<B> for Module<B> where Module<B>: GGLWEPrepare<B> {} impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> { pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize where - M: PrepareAutomorphismKey<B>, + M: AutomorphismKeyPrepare<B>, { module.prepare_automorphism_key_tmp_bytes(self) }@@ -209,7 +209,7 @@ impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>) where O: GGLWEToRef + GetAutomorphismGaloisElement, - M: PrepareAutomorphismKey<B>, + M: AutomorphismKeyPrepare<B>, { module.prepare_automorphism_key(self, other, scratch); }
@@ -21,8 +21,8 @@ // 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 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 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 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 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 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,
@@ -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<B>(module: &Module<B>) +pub fn test_gglwe_automorphism_key_automorphism<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> + Module<BE>: AutomorphismKeyEncryptSk<BE> + + AutomorphismKeyPreparedAlloc<BE> + + AutomorphismKeyPrepare<BE> + + AutomorphismKeyAutomorphism<BE> + VecZnxAutomorphism - + VecZnxAutomorphismInplace<B> - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + VmpPMatAlloc<B> - + VmpPrepare<B> - + SvpPrepare<B> - + SvpApplyDftToDftInplace<B> - + VecZnxAddScalarInplace - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + VecZnxSwitchRing - + SvpPPolAlloc<B> - + VecZnxBigAddInplace<B> - + VecZnxSubScalarInplace, - B: Backend - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxDftImpl<B> - + TakeVecZnxImpl<B> - + TakeSvpPPolImpl<B> - + TakeVecZnxBigImpl<B>, + + GaloisElement + + VecZnxSubScalarInplace + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + GLWEDecrypt<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_in: usize = 60;@@ -119,7 +83,7 @@ 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( 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 @@ &mut source_xe,
scratch.borrow(), ); - let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = + let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, 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 @@ i,
); } - let sk_auto_dft: GLWESecretPrepared<Vec<u8>, B> = sk_auto.prepare_alloc(module, scratch.borrow()); + let mut sk_auto_dft: GLWESecretPrepared<Vec<u8>, 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 @@ }
} #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_automorphism_key_automorphism_inplace<B>(module: &Module<B>) +pub fn test_gglwe_automorphism_key_automorphism_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> - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> + Module<BE>: AutomorphismKeyEncryptSk<BE> + + AutomorphismKeyPreparedAlloc<BE> + + AutomorphismKeyPrepare<BE> + + AutomorphismKeyAutomorphism<BE> + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxAutomorphismInplace<B> - + VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxNormalizeTmpBytes + + GaloisElement + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc<B> - + VmpPrepare<B>, - B: Backend - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxDftImpl<B> - + TakeVecZnxImpl<B> - + TakeSvpPPolImpl<B> - + TakeVecZnxBigImpl<B>, + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + GLWEDecrypt<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_in: usize = 60;@@ -318,10 +242,10 @@ 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( 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<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key);@@ -347,7 +271,7 @@ &mut source_xe,
scratch.borrow(), ); - let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = + let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, 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 @@ i,
); } - let sk_auto_dft: GLWESecretPrepared<Vec<u8>, B> = sk_auto.prepare_alloc(module, scratch.borrow()); + let mut sk_auto_dft: GLWESecretPrepared<Vec<u8>, 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| {
@@ -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<B>(module: &Module<B>) +pub fn test_ggsw_automorphism<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc<B> - + VecZnxDftAlloc<B> - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA<B> - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxAddScalarInplace - + VecZnxCopy - + VecZnxSubInplace - + VmpPMatAlloc<B> - + VmpPrepare<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftCopy<B> - + VecZnxDftAddInplace<B> - + VecZnxFillUniform - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpApplyDftToDft<B> - + VecZnxSwitchRing - + VecZnxAutomorphismInplace<B> - + VecZnxAutomorphism, - 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> + + AutomorphismKeyEncryptSk<BE> + + AutomorphismKeyPreparedAlloc<BE> + + AutomorphismKeyPrepare<BE> + + GGSWAutomorphism<BE> + + TensorKeyPrepare<BE> + + TensorKeyPreparedAlloc<BE> + + TensorKeyEncryptSk<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + VecZnxAutomorphismInplace<BE> + + GGSWNoise<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_in: usize = 54;@@ -92,7 +49,7 @@ let dnum_in: usize = k_in.div_euclid(base2k * di);
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 @@ dsize: dsize_in.into(),
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 @@ 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( 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 @@ let var_xs: f64 = 0.5;
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(var_xs, &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_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); auto_key.encrypt_sk( module,@@ -178,11 +137,12 @@ &mut source_xe,
scratch.borrow(), ); - let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = + let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = + TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism(@@ -217,56 +177,22 @@ }
} #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_automorphism_inplace<B>(module: &Module<B>) +pub fn test_ggsw_automorphism_inplace<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc<B> - + VecZnxDftAlloc<B> - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA<B> - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxAddScalarInplace - + VecZnxCopy - + VecZnxSubInplace - + VmpPMatAlloc<B> - + VmpPrepare<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxDftCopy<B> - + VecZnxDftAddInplace<B> - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + VecZnxFillUniform - + SvpApplyDftToDft<B> - + VecZnxSwitchRing - + VecZnxAutomorphismInplace<B> - + VecZnxAutomorphism, - 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> + + AutomorphismKeyEncryptSk<BE> + + AutomorphismKeyPreparedAlloc<BE> + + AutomorphismKeyPrepare<BE> + + GGSWAutomorphism<BE> + + TensorKeyPrepare<BE> + + TensorKeyPreparedAlloc<BE> + + TensorKeyEncryptSk<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + VecZnxAutomorphismInplace<BE> + + GGSWNoise<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_out: usize = 54;@@ -282,7 +208,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_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_layout: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(),@@ -318,18 +244,20 @@ 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( 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<Vec<u8>> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(var_xs, &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_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); auto_key.encrypt_sk( module,@@ -358,11 +286,12 @@ &mut source_xe,
scratch.borrow(), ); - let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = + let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, 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());
@@ -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<B>(module: &Module<B>) +pub fn test_glwe_automorphism<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes + Module<BE>: GLWEEncryptSk<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace<B> - + VmpPMatAlloc<B> - + VmpPrepare<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + TakeSvpPPolImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B>, + + GLWEDecrypt<BE> + + GLWEAutomorphism<BE> + + AutomorphismKeyEncryptSk<BE> + + AutomorphismKeyPrepare<BE> + + AutomorphismKeyPreparedAlloc<BE> + + GLWENoise<BE> + + VecZnxAutomorphismInplace<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_in: usize = 60;@@ -111,7 +77,7 @@ let mut source_xa: Source = Source::new([0u8; 32]);
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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 @@ );
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct_out); 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_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); autokey.encrypt_sk( module,@@ -140,7 +108,7 @@ &mut source_xe,
scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = + let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &autokey_infos); autokey_prepared.prepare(module, &autokey, scratch.borrow());@@ -167,46 +135,21 @@ }
} #[allow(clippy::too_many_arguments)] -pub fn test_glwe_automorphism_inplace<B>(module: &Module<B>) +pub fn test_glwe_automorphism_inplace<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes + Module<BE>: GLWEEncryptSk<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace<B> - + VmpPMatAlloc<B> - + VmpPrepare<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + TakeSvpPPolImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B>, + + GLWEDecrypt<BE> + + GLWEAutomorphism<BE> + + AutomorphismKeyEncryptSk<BE> + + AutomorphismKeyPrepare<BE> + + AutomorphismKeyPreparedAlloc<BE> + + GLWENoise<BE> + + VecZnxAutomorphismInplace<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_out: usize = 60;@@ -245,16 +188,18 @@ let mut source_xa: Source = Source::new([0u8; 32]);
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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<Vec<u8>> = GLWESecret::alloc_from_infos(&ct); 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_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); autokey.encrypt_sk( module,@@ -274,7 +219,7 @@ &mut source_xe,
scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = + let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &autokey); autokey_prepared.prepare(module, &autokey, scratch.borrow());
@@ -1,4 +1,4 @@
-// pub mod automorphism; +pub mod automorphism; pub mod encryption; pub mod external_product; pub mod keyswitch;
@@ -114,7 +114,7 @@ panic!("cannot invert 0")
} 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() } }