conversions tests
jump to
@@ -112,14 +112,13 @@ {
module.lwe_sample_extract(self, a); } - pub fn from_glwe<R, A, K, M, BE: Backend>(&self, module: &M, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>) + pub fn from_glwe<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>) where - R: LWEToMut, A: GLWEToRef, K: GGLWEPreparedToRef<BE> + GGLWEInfos, M: LWEFromGLWE<BE>, Scratch<BE>: ScratchTakeCore<BE>, { - module.lwe_from_glwe(res, a, key, scratch); + module.lwe_from_glwe(self, a, key, scratch); } }
@@ -17,7 +17,7 @@ impl GLWEToLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyEncrypt<BE>, + M: GLWEToLWESwitchingKeyEncryptSk<BE>, { module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos) }@@ -33,7 +33,7 @@ source_xa: &mut Source,
source_xe: &mut Source, scratch: &mut Scratch<BE>, ) where - M: GLWEToLWESwitchingKeyEncrypt<BE>, + M: GLWEToLWESwitchingKeyEncryptSk<BE>, S1: LWESecretToRef, S2: GLWESecretToRef, Scratch<BE>: ScratchTakeCore<BE>,@@ -42,7 +42,7 @@ module.glwe_to_lwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch);
} } -pub trait GLWEToLWESwitchingKeyEncrypt<BE: Backend> { +pub trait GLWEToLWESwitchingKeyEncryptSk<BE: Backend> { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos;@@ -61,7 +61,7 @@ S2: GLWESecretToRef,
R: GGLWEToMut; } -impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE> +impl<BE: Backend> GLWEToLWESwitchingKeyEncryptSk<BE> for Module<BE> where Self: ModuleN + GGLWEEncryptSk<BE>
@@ -16,7 +16,7 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyEncrypt<BE>, + M: LWEToGLWESwitchingKeyEncryptSk<BE>, { module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos) }@@ -34,14 +34,14 @@ scratch: &mut Scratch<BE>,
) where S1: LWESecretToRef, S2: GLWESecretPreparedToRef<BE>, - M: LWEToGLWESwitchingKeyEncrypt<BE>, + M: LWEToGLWESwitchingKeyEncryptSk<BE>, Scratch<BE>: ScratchTakeCore<BE>, { module.lwe_to_glwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); } } -pub trait LWEToGLWESwitchingKeyEncrypt<BE: Backend> { +pub trait LWEToGLWESwitchingKeyEncryptSk<BE: Backend> { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos;@@ -60,7 +60,7 @@ S2: GLWESecretPreparedToRef<BE>,
R: GGLWEToMut; } -impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE> +impl<BE: Backend> LWEToGLWESwitchingKeyEncryptSk<BE> for Module<BE> where Self: ModuleN + GGLWEEncryptSk<BE>
@@ -4,7 +4,7 @@ source::Source,
}; 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 @@ fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree } } + +impl<D: DataRef> GLWESwitchingKeyDegrees for GLWEToLWESwitchingKey<D> { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +}
@@ -6,7 +6,7 @@ source::Source,
}; 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 @@ fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree } } + +impl<D: DataRef> GLWESwitchingKeyDegrees for LWEToGLWESwitchingKey<D> { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +}
@@ -206,3 +206,13 @@ fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree } } + +impl<D: DataRef, B: Backend> GLWESwitchingKeyDegrees for GLWEToLWESwitchingKeyPrepared<D, B> { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +}
@@ -59,8 +59,8 @@ 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, -// 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)]
@@ -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<B>(module: &Module<B>) +pub fn test_lwe_to_glwe<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc<B> - + VmpPrepare<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace<B> - + ZnNormalizeInplace<B> - + ZnFillUniform - + ZnAddNormal, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + TakeSvpPPolImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B>, + Module<BE>: GLWEFromLWE<BE> + + LWEToGLWESwitchingKeyEncryptSk<BE> + + GLWEDecrypt<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + LWEEncryptSk<BE> + + LWEToGLWESwitchingKeyPreparedAlloc<BE> + + LWEToGLWESwitchingKeyPrepare<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let n_glwe: Degree = Degree(module.n() as u32); let n_lwe: Degree = Degree(22);@@ -89,13 +54,13 @@ k: TorusPrecision(34),
rank, }; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe, base2k: Base2K(17), k: TorusPrecision(34), }; - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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 @@
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe); + sk_glwe_prepared.prepare(module, &sk_glwe); let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs);@@ -122,7 +88,7 @@
ksk.encrypt_sk( module, &sk_lwe, - &sk_glwe, + &sk_glwe_prepared, &mut source_xa, &mut source_xe, scratch.borrow(),@@ -130,7 +96,9 @@ );
let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); - let ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, 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 @@
assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); } -pub fn test_glwe_to_lwe<B>(module: &Module<B>) +pub fn test_glwe_to_lwe<BE: Backend>(module: &Module<BE>) where - Module<B>: VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxBigBytesOf - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc<B> - + VmpPrepare<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace<B> - + ZnNormalizeInplace<B>, - B: Backend - + TakeVecZnxDftImpl<B> - + TakeVecZnxBigImpl<B> - + TakeSvpPPolImpl<B> - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + ScratchAvailableImpl<B> - + TakeScalarZnxImpl<B> - + TakeVecZnxImpl<B>, + Module<BE>: GLWEFromLWE<BE> + + GLWEToLWESwitchingKeyEncryptSk<BE> + + GLWEEncryptSk<BE> + + LWEDecrypt<BE> + + GLWEDecrypt<BE> + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + GLWEToLWESwitchingKeyEncryptSk<BE> + + GLWEToLWESwitchingKeyPrepare<BE> + + GLWEToLWESwitchingKeyPreparedAlloc<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let n_glwe: Degree = Degree(module.n() as u32); let n_lwe: Degree = Degree(22);@@ -202,7 +144,7 @@ k: TorusPrecision(34),
rank, }; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe, base2k: Base2K(17), k: TorusPrecision(34),@@ -212,7 +154,7 @@ let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( + let mut scratch: ScratchOwned<BE> = 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 @@
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe); + sk_glwe_prepared.prepare(module, &sk_glwe); let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs);@@ -230,7 +173,7 @@ let data: i64 = 17;
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = 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<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); glwe_ct.encrypt_sk( module, &glwe_pt,@@ -240,7 +183,7 @@ &mut source_xe,
scratch.borrow(), ); - let mut ksk: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(&glwe_to_lwe_infos); + let mut ksk: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc_from_infos(&glwe_to_lwe_infos); ksk.encrypt_sk( module,@@ -253,7 +196,9 @@ );
let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos); - let ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, 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());
@@ -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::*;