diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 276010d..072f3d5 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -112,14 +112,13 @@ impl LWE { module.lwe_sample_extract(self, a); } - pub fn from_glwe(&self, module: &M, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + pub fn from_glwe(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where - R: LWEToMut, A: GLWEToRef, K: GGLWEPreparedToRef + GGLWEInfos, M: LWEFromGLWE, Scratch: ScratchTakeCore, { - module.lwe_from_glwe(res, a, key, scratch); + module.lwe_from_glwe(self, a, key, scratch); } } diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 71b96ff..28a9778 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -17,7 +17,7 @@ impl GLWEToLWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyEncrypt, + M: GLWEToLWESwitchingKeyEncryptSk, { module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos) } @@ -33,7 +33,7 @@ impl GLWEToLWESwitchingKey { source_xe: &mut Source, scratch: &mut Scratch, ) where - M: GLWEToLWESwitchingKeyEncrypt, + M: GLWEToLWESwitchingKeyEncryptSk, S1: LWESecretToRef, S2: GLWESecretToRef, Scratch: ScratchTakeCore, @@ -42,7 +42,7 @@ impl GLWEToLWESwitchingKey { } } -pub trait GLWEToLWESwitchingKeyEncrypt { +pub trait GLWEToLWESwitchingKeyEncryptSk { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; @@ -61,7 +61,7 @@ pub trait GLWEToLWESwitchingKeyEncrypt { R: GGLWEToMut; } -impl GLWEToLWESwitchingKeyEncrypt for Module +impl GLWEToLWESwitchingKeyEncryptSk for Module where Self: ModuleN + GGLWEEncryptSk diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index a9856cc..ddf6264 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -16,7 +16,7 @@ impl LWEToGLWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyEncrypt, + M: LWEToGLWESwitchingKeyEncryptSk, { module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos) } @@ -34,14 +34,14 @@ impl LWEToGLWESwitchingKey { ) where S1: LWESecretToRef, S2: GLWESecretPreparedToRef, - M: LWEToGLWESwitchingKeyEncrypt, + M: LWEToGLWESwitchingKeyEncryptSk, Scratch: ScratchTakeCore, { module.lwe_to_glwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); } } -pub trait LWEToGLWESwitchingKeyEncrypt { +pub trait LWEToGLWESwitchingKeyEncryptSk { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; @@ -60,7 +60,7 @@ pub trait LWEToGLWESwitchingKeyEncrypt { R: GGLWEToMut; } -impl LWEToGLWESwitchingKeyEncrypt for Module +impl LWEToGLWESwitchingKeyEncryptSk for Module where Self: ModuleN + GGLWEEncryptSk diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 1bb28ff..bc3ee4b 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; @@ -217,3 +217,13 @@ impl GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKey { &mut self.0.output_degree } } + +impl GLWESwitchingKeyDegrees for GLWEToLWESwitchingKey { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +} diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index dbe95dd..caa676d 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; @@ -217,3 +217,13 @@ impl GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKey { &mut self.0.output_degree } } + +impl GLWESwitchingKeyDegrees for LWEToGLWESwitchingKey { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +} diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 716eeff..26d617f 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -206,3 +206,13 @@ impl GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKe &mut self.0.output_degree } } + +impl GLWESwitchingKeyDegrees for GLWEToLWESwitchingKeyPrepared { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +} diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index f24ff84..595c480 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -59,8 +59,8 @@ ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorph 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, -// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, +glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, +lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, } ); // #[cfg(test)] diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index 972e0c6..610b61e 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -1,68 +1,33 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, - }, - layouts::{Backend, Module, ScratchOwned, ZnxView}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, Scratch, ScratchOwned, ZnxView}, source::Source, }; -use crate::layouts::{ - Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, - LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, - prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, +use crate::{ + GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, + LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore, + layouts::{ + Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyPrepare, GLWEToLWESwitchingKeyPreparedAlloc, LWE, + LWELayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, LWEToGLWESwitchingKeyPrepare, + LWEToGLWESwitchingKeyPreparedAlloc, Rank, TorusPrecision, + prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared}, + }, }; -pub fn test_lwe_to_glwe(module: &Module) +pub fn test_lwe_to_glwe(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + ZnNormalizeInplace - + ZnFillUniform - + ZnAddNormal, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GLWEFromLWE + + LWEToGLWESwitchingKeyEncryptSk + + GLWEDecrypt + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + LWEEncryptSk + + LWEToGLWESwitchingKeyPreparedAlloc + + LWEToGLWESwitchingKeyPrepare, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let n_glwe: Degree = Degree(module.n() as u32); let n_lwe: Degree = Degree(22); @@ -89,13 +54,13 @@ where rank, }; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe, base2k: Base2K(17), k: TorusPrecision(34), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( LWEToGLWESwitchingKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos) | GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), @@ -104,7 +69,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe); + sk_glwe_prepared.prepare(module, &sk_glwe); let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs); @@ -122,7 +88,7 @@ where ksk.encrypt_sk( module, &sk_lwe, - &sk_glwe, + &sk_glwe_prepared, &mut source_xa, &mut source_xe, scratch.borrow(), @@ -130,7 +96,9 @@ where let mut glwe_ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); - let ksk_prepared: LWEToGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared, BE> = + LWEToGLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow()); @@ -140,46 +108,20 @@ where assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); } -pub fn test_glwe_to_lwe(module: &Module) +pub fn test_glwe_to_lwe(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + ZnNormalizeInplace, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GLWEFromLWE + + GLWEToLWESwitchingKeyEncryptSk + + GLWEEncryptSk + + LWEDecrypt + + GLWEDecrypt + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWEToLWESwitchingKeyEncryptSk + + GLWEToLWESwitchingKeyPrepare + + GLWEToLWESwitchingKeyPreparedAlloc, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let n_glwe: Degree = Degree(module.n() as u32); let n_lwe: Degree = Degree(22); @@ -202,7 +144,7 @@ where rank, }; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe, base2k: Base2K(17), k: TorusPrecision(34), @@ -212,7 +154,7 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWEToLWESwitchingKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos) | LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), @@ -221,7 +163,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe); + sk_glwe_prepared.prepare(module, &sk_glwe); let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs); @@ -230,7 +173,7 @@ where let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0); - let mut glwe_ct = GLWE::alloc_from_infos(&glwe_infos); + let mut glwe_ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); glwe_ct.encrypt_sk( module, &glwe_pt, @@ -240,7 +183,7 @@ where scratch.borrow(), ); - let mut ksk: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&glwe_to_lwe_infos); + let mut ksk: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc_from_infos(&glwe_to_lwe_infos); ksk.encrypt_sk( module, @@ -253,7 +196,9 @@ where let mut lwe_ct: LWE> = LWE::alloc_from_infos(&lwe_infos); - let ksk_prepared: GLWEToLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared, BE> = + GLWEToLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index 11af37a..e3b8e28 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -3,10 +3,10 @@ pub mod encryption; pub mod external_product; pub mod keyswitch; -// mod conversion; +mod conversion; // mod packing; // mod trace; -// pub use conversion::*; +pub use conversion::*; // pub use packing::*; // pub use trace::*;