diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 2849e3d..db78656 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,6 +1,6 @@ use poulpy_core::layouts::{ Base2K, Dnum, Dsize, GGSW, GGSWLayout, GLWE, GLWELayout, GLWESecret, Rank, RingDegree, TorusPrecision, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + prepared::{GGSWPrepared, GLWESecretPrepared}, }; use std::hint::black_box; @@ -61,9 +61,9 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); - let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); - let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&module, &ggsw_layout); + let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&module, &glwe_in_layout); + let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&module, &glwe_out_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( @@ -76,9 +76,11 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); + + let mut sk_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_dft.prepare(&module, &sk); ct_ggsw.encrypt_sk( &module, @@ -97,7 +99,8 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); + let mut ggsw_prepared: GGSWPrepared, FFT64Spqlios> = GGSWPrepared::alloc_from_infos(&module, &ct_ggsw); + ggsw_prepared.prepare(&module, &ct_ggsw, scratch.borrow()); move || { ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow()); @@ -162,23 +165,25 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); - let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&module, &ggsw_layout); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&module, &glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(&module, &ggsw_layout) | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_layout) - | GLWE::external_product_inplace_tmp_bytes(&module, &glwe_layout, &ggsw_layout), + | GLWE::external_product_tmp_bytes(&module, &glwe_layout, &glwe_layout, &ggsw_layout), ); 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 sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); + + let mut sk_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_dft.prepare(&module, &sk); ct_ggsw.encrypt_sk( &module, @@ -197,8 +202,8 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); - + let mut ggsw_prepared: GGSWPrepared, FFT64Spqlios> = GGSWPrepared::alloc_from_infos(&module, &ct_ggsw); + ggsw_prepared.prepare(&module, &ct_ggsw, scratch.borrow()); move || { let scratch_borrow = scratch.borrow(); ct_glwe.external_product_inplace(&module, &ggsw_prepared, scratch_borrow); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index fc1dc69..5806b16 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -1,7 +1,7 @@ use poulpy_core::layouts::{ AutomorphismKey, AutomorphismKeyLayout, Base2K, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, - GLWESwitchingKeyLayout, Rank, RingDegree, TorusPrecision, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, + GLWESwitchingKeyLayout, GLWESwitchingKeyPrepared, Rank, RingDegree, TorusPrecision, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }; use std::{hint::black_box, time::Duration}; @@ -62,9 +62,9 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); - let mut ct_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); - let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); + let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&module, &gglwe_atk_layout); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&module, &glwe_in_layout); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&module, &glwe_out_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout) @@ -81,9 +81,11 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); + + let mut sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_in_dft.prepare(&module, &sk_in); ksk.encrypt_sk( &module, @@ -102,7 +104,8 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: AutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); + let mut ksk_prepared: AutomorphismKeyPrepared, _> = AutomorphismKeyPrepared::alloc_from_infos(&module, &ksk); + ksk_prepared.prepare(&module, &ksk, scratch.borrow()); move || { ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()); @@ -174,24 +177,26 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); - let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&module, &gglwe_layout); + let mut ct: GLWE> = GLWE::alloc_from_infos(&module, &glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout) | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_layout) - | GLWE::keyswitch_inplace_tmp_bytes(&module, &glwe_layout, &gglwe_layout), + | GLWE::keyswitch_tmp_bytes(&module, &glwe_layout, &glwe_layout, &gglwe_layout), ); 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 sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); + let mut sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_in_dft.prepare(&module, &sk_in); + + let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk( @@ -211,7 +216,8 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); + let mut ksk_prepared: GLWESwitchingKeyPrepared, _> = GLWESwitchingKeyPrepared::alloc_from_infos(&module, &ksk); + ksk_prepared.prepare(&module, &ksk, scratch.borrow()); move || { ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index efd838e..aae1204 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -1,9 +1,9 @@ use poulpy_backend::cpu_spqlios::FFT64Spqlios; use poulpy_core::{ - GLWEOperations, SIGMA, + GLWESub, SIGMA, layouts::{ Base2K, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, RingDegree, TorusPrecision, - prepared::{GLWESecretPrepared, PrepareAlloc}, + prepared::GLWESecretPrepared, }, }; use poulpy_hal::{ @@ -43,9 +43,9 @@ fn main() { let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; // Allocates ciphertext & plaintexts - let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_ct_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&module, &glwe_ct_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&module, &glwe_pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&module, &glwe_pt_infos); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]); @@ -58,11 +58,12 @@ fn main() { ); // Generate secret-key - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_ct_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret - let sk_prepared: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); + let mut sk_prepared: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_prepared.prepare(&module, &sk); // Uniform plaintext module.vec_znx_fill_uniform(base2k.into(), &mut pt_want.data, 0, &mut source_xa); @@ -81,7 +82,7 @@ fn main() { ct.decrypt(&module, &mut pt_have, &sk_prepared, scratch.borrow()); // Diff between pt - Dec(Enc(pt)) - pt_want.sub_inplace_ab(&module, &pt_have); + module.glwe_sub_inplace(&mut pt_want, &pt_have); // Ideal vs. actual noise let noise_have: f64 = pt_want.data.std(base2k.into(), 0) * (ct.k().as_u32() as f64).exp2(); diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs index 24d02bd..3447d61 100644 --- a/poulpy-core/src/conversion/gglwe_to_ggsw.rs +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -38,9 +38,7 @@ impl GGSW { } } -impl GGSWFromGGLWE for Module where Self: GGSWExpandRows + GLWECopy {} - -pub trait GGSWFromGGLWE +impl GGSWFromGGLWE for Module where Self: GGSWExpandRows + GLWECopy, { @@ -77,6 +75,20 @@ where } } +pub trait GGSWFromGGLWE { + fn ggsw_from_gglwe_tmp_bytes(&self, res_infos: &R, tsk_infos: &A) -> usize + where + R: GGSWInfos, + A: GGLWEInfos; + + fn ggsw_from_gglwe(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + A: GGLWEToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore; +} + impl GGSWExpandRows for Module where Self: Sized + ModuleN @@ -99,7 +111,7 @@ impl GGSWExpandRows for Module where { } -pub(crate) trait GGSWExpandRows +pub trait GGSWExpandRows where Self: Sized + ModuleN diff --git a/poulpy-core/src/decryption/mod.rs b/poulpy-core/src/decryption/mod.rs index 03e5fd4..0af7985 100644 --- a/poulpy-core/src/decryption/mod.rs +++ b/poulpy-core/src/decryption/mod.rs @@ -2,4 +2,4 @@ mod glwe_ct; mod lwe_ct; pub use glwe_ct::*; -pub use lwe_ct::*; +// pub use lwe_ct::*; diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index ab5fb27..a3415cc 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -61,7 +61,7 @@ pub trait AutomorphismKeyCompressedEncryptSk { impl AutomorphismKeyCompressedEncryptSk for Module where - Module: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk, + Self: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk, Scratch: ScratchTakeCore, { fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -99,7 +99,7 @@ where let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); { - for i in 0..res.rank_out().into(){ + for i in 0..res.rank_out().into() { self.vec_znx_automorphism( self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), @@ -107,7 +107,7 @@ where &sk.data.as_vec_znx(), i, ); - }; + } } self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 98cadfb..638c566 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -67,7 +67,7 @@ pub trait GGLWECompressedEncryptSk { impl GGLWECompressedEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWEEncryptSkInternal + GLWEEncryptSk + VecZnxDftBytesOf diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 4ba0bcb..7825e69 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk { impl GLWESwitchingKeyCompressedEncryptSk for Module where - Module: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxSwitchRing, + Self: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxSwitchRing, Scratch: ScratchTakeCore, { fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 5f88e42..1184c03 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, Module, Scratch}, oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, source::Source, }; @@ -13,7 +13,7 @@ use crate::{ encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, encryption::gglwe_tsk::TensorKeyEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; @@ -29,23 +29,22 @@ impl TensorKeyCompressed> { } impl TensorKeyCompressed { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - sk: &GLWESecret, + module: &M, + sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where - GLWESecret: GetDist, - Module: GGLWETensorKeyCompressedEncryptSk, + S: GLWESecretToRef + GetDist, + M: GGLWETensorKeyCompressedEncryptSk, { module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); } } pub trait GGLWETensorKeyCompressedEncryptSk { - fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; @@ -64,7 +63,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk { impl GGLWETensorKeyCompressedEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWESwitchingKeyCompressedEncryptSk + TensorKeyEncryptSk + VecZnxDftApply @@ -82,7 +81,7 @@ where { fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where - A: GGLWEInfos + A: GGLWEInfos, { self.tensor_key_encrypt_sk_tmp_bytes(infos) } @@ -154,4 +153,4 @@ where } } } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index ef122d8..ec45398 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace}, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; @@ -26,22 +26,23 @@ impl GGSWCompressed> { impl GGSWCompressed { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, + module: &M, + pt: &P, + sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: GGSWCompressedEncryptSk, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + M: GGSWCompressedEncryptSk, { module.ggsw_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } - pub trait GGSWCompressedEncryptSk { fn ggsw_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where @@ -63,11 +64,12 @@ pub trait GGSWCompressedEncryptSk { impl GGSWCompressedEncryptSk for Module where - Module: ModuleN + GLWEEncryptSkInternal + GGSWEncryptSk + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Self: ModuleN + GLWEEncryptSkInternal + GGSWEncryptSk + VecZnxAddScalarInplace + VecZnxNormalizeInplace, Scratch: ScratchTakeCore, { fn ggsw_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize - where A: GGSWInfos, + where + A: GGSWInfos, { self.ggsw_encrypt_sk_tmp_bytes(infos) } @@ -139,4 +141,4 @@ where } } } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 0e9fca5..a30001c 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -1,15 +1,17 @@ use poulpy_hal::{ - api::{VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; use crate::{ - encryption::{SIGMA, glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}}, + encryption::{ + SIGMA, + glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, + }, layouts::{ - GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + GLWEInfos, GLWEPlaintextToRef, LWEInfos, compressed::{GLWECompressed, GLWECompressedToMut}, - prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + prepared::GLWESecretPreparedToRef, }, }; @@ -25,22 +27,23 @@ impl GLWECompressed> { impl GLWECompressed { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - pt: &GLWEPlaintext, - sk: &GLWESecretPrepared, + module: &M, + pt: &P, + sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: GLWECompressedEncryptSk, + M: GLWECompressedEncryptSk, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, { module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } - pub trait GLWECompressedEncryptSk { fn glwe_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where @@ -62,9 +65,8 @@ pub trait GLWECompressedEncryptSk { impl GLWECompressedEncryptSk for Module where - Module: GLWEEncryptSkInternal + GLWEEncryptSk, + Self: GLWEEncryptSkInternal + GLWEEncryptSk, { - fn glwe_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos, @@ -105,4 +107,4 @@ where res.seed = seed_xa; } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index dc760de..8521df3 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -68,7 +68,7 @@ pub trait AutomorphismKeyEncryptSk { impl AutomorphismKeyEncryptSk for Module where - Module: GLWESwitchingKeyEncryptSk + VecZnxAutomorphism + GaloisElement, + Self: GLWESwitchingKeyEncryptSk + VecZnxAutomorphism + GaloisElement, Scratch: ScratchTakeCore, { fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -137,7 +137,7 @@ pub trait GGLWEAutomorphismKeyEncryptPk { impl GGLWEAutomorphismKeyEncryptPk for Module where - Module:, + Self:, Scratch: ScratchTakeCore, { fn automorphism_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index 67d517d..bb1a9c1 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -73,7 +73,7 @@ pub trait GGLWEEncryptSk { impl GGLWEEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index b55e5df..e8c49f3 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -66,8 +66,7 @@ pub trait GLWESwitchingKeyEncryptSk { ) where R: GLWESwitchingKeyToMut, S1: GLWESecretToRef, - S2: GLWESecretToRef, - Scratch: ScratchTakeCore; + S2: GLWESecretToRef; } impl GLWESwitchingKeyEncryptSk for Module diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index f67d730..d10af38 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxIdftApplyTmpA, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxIdftApplyTmpA, }, layouts::{Backend, DataMut, Module, Scratch}, source::Source, @@ -37,7 +37,7 @@ impl TensorKey { ) where M: TensorKeyEncryptSk, S: GLWESecretToRef + GetDist, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchTakeCore, { module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); } @@ -62,7 +62,7 @@ pub trait TensorKeyEncryptSk { impl TensorKeyEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWESwitchingKeyEncryptSk + VecZnxDftBytesOf + VecZnxBigBytesOf diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index a758384..8964122 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -64,7 +64,7 @@ pub trait GGSWEncryptSk { impl GGSWEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWEEncryptSkInternal + GLWEEncryptSk + VecZnxDftBytesOf diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index 082e53f..5877f37 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -38,7 +38,7 @@ impl GLWE> { } impl GLWE { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, pt: &P, @@ -133,7 +133,7 @@ pub trait GLWEEncryptSk { impl GLWEEncryptSk for Module where - Module: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal, + Self: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal, Scratch: ScratchAvailable, { fn glwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -262,7 +262,7 @@ pub trait GLWEEncryptPk { impl GLWEEncryptPk for Module where - Module: GLWEEncryptPkInternal + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, + Self: GLWEEncryptPkInternal + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, { fn glwe_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where @@ -330,7 +330,7 @@ pub(crate) trait GLWEEncryptPkInternal { impl GLWEEncryptPkInternal for Module where - Module: SvpPrepare + Self: SvpPrepare + SvpApplyDftToDft + VecZnxIdftApplyConsume + VecZnxBigAddNormal @@ -445,7 +445,7 @@ pub(crate) trait GLWEEncryptSkInternal { impl GLWEEncryptSkInternal for Module where - Module: ModuleN + Self: ModuleN + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply @@ -459,7 +459,7 @@ where + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: ScratchAvailable + ScratchTakeBasic, + Scratch: ScratchTakeBasic, { fn glwe_encrypt_sk_internal( &self, diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index f35457f..d30fe62 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow}, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ScratchOwned}, + layouts::{Backend, DataMut, Module, Scratch, ScratchOwned}, source::Source, }; @@ -14,14 +14,10 @@ use crate::{ }; impl GLWEPublicKey { - pub fn generate( - &mut self, - module: &Module, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - ) where - Module: GLWEPublicKeyGenerate, + pub fn generate(&mut self, module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source) + where + S: GLWESecretPreparedToRef, + M: GLWEPublicKeyGenerate, { module.glwe_public_key_generate(self, sk, source_xa, source_xe); } @@ -36,7 +32,7 @@ pub trait GLWEPublicKeyGenerate { impl GLWEPublicKeyGenerate for Module where - Module: GLWEEncryptSk, + Self: GLWEEncryptSk, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 94d0c62..59be47c 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -64,7 +64,7 @@ pub trait GLWEToLWESwitchingKeyEncrypt { impl GLWEToLWESwitchingKeyEncrypt for Module where - Module: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, Scratch: ScratchTakeCore, { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index ddb4de7..e18648b 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -67,7 +67,7 @@ pub struct GLWESwitchingKey { pub(crate) sk_out_n: usize, // Degree of sk_out } -pub(crate) trait GLWESwitchingKeySetMetaData { +pub trait GLWESwitchingKeySetMetaData { fn set_sk_in_n(&mut self, sk_in_n: usize); fn set_sk_out_n(&mut self, sk_out_n: usize); } @@ -82,7 +82,7 @@ impl GLWESwitchingKeySetMetaData for GLWESwitchingKey { } } -pub(crate) trait GLWESwtichingKeyGetMetaData { +pub trait GLWESwtichingKeyGetMetaData { fn sk_in_n(&self) -> usize; fn sk_out_n(&self) -> usize; } diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 9166388..72ecd95 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -8,7 +8,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, + layouts::{Base2K, GLWEInfos, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -64,6 +64,12 @@ impl LWEInfos for GLWESecret { } } +impl GetDist for GLWESecret { + fn get_dist(&self) -> Distribution { + self.dist + } +} + impl GLWEInfos for GLWESecret { fn rank(&self) -> Rank { Rank(self.data.cols() as u32) diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 09064f3..0d53307 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -17,6 +17,7 @@ mod lwe_to_glwe_ksk; pub mod compressed; pub mod prepared; +pub use compressed::*; pub use gglwe_atk::*; pub use gglwe_ct::*; pub use gglwe_ksk::*; @@ -32,6 +33,7 @@ pub use lwe_ksk::*; pub use lwe_pt::*; pub use lwe_sk::*; pub use lwe_to_glwe_ksk::*; +pub use prepared::*; use poulpy_hal::layouts::{Backend, Module}; diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 2d054c1..1ff5759 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -198,7 +198,7 @@ where impl TensorKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} impl TensorKeyPrepared, B> { - fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize + pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, M: TensorKeyPrepare, @@ -208,7 +208,7 @@ impl TensorKeyPrepared, B> { } impl TensorKeyPrepared { - fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: TensorKeyToRef, M: TensorKeyPrepare, diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index bca1826..62165c8 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -18,7 +18,7 @@ pub struct GLWEPublicKeyPrepared { pub(crate) dist: Distribution, } -pub(crate) trait SetDist { +pub trait SetDist { fn set_dist(&mut self, dist: Distribution); } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index dd7876c..f50f2dc 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -63,7 +63,7 @@ where self.alloc_glwe_secret_prepared(infos.rank()) } - fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + fn bytes_of_glwe_secret_prepared(&self, rank: Rank) -> usize { self.bytes_of_svp_ppol(rank.into()) } fn bytes_of_glwe_secret_prepared_from_infos(&self, infos: &A) -> usize @@ -71,7 +71,7 @@ where A: GLWEInfos, { assert_eq!(self.ring_degree(), infos.n()); - self.bytes_of_glwe_secret(infos.rank()) + self.bytes_of_glwe_secret_prepared(infos.rank()) } } @@ -105,7 +105,7 @@ impl GLWESecretPrepared, B> { where M: GLWESecretPreparedAlloc, { - module.bytes_of_glwe_secret(rank) + module.bytes_of_glwe_secret_prepared(rank) } } 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 55a7bf9..4c7f65e 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -172,7 +172,7 @@ impl GLWEToLWESwitchingKeyPrepared, B> { } impl GLWEToLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GLWEToLWESwitchingKeyToRef, M: GLWEToLWESwitchingKeyPrepare, diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index a857bf9..7e0b1da 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -180,7 +180,7 @@ impl LWESwitchingKeyPrepared, B> { } impl LWESwitchingKeyPrepared { - fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: LWESwitchingKeyToRef, M: LWESwitchingKeyPrepare, diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 5df692e..6afb32a 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -179,7 +179,7 @@ impl LWEToGLWESwitchingKeyPrepared, B> { } impl LWEToGLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: LWEToGLWESwitchingKeyToRef, M: LWEToGLWESwitchingKeyPrepare, diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index e1cefbf..189f471 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -1,19 +1,16 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace, - }, - layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ScalarZnx, ScalarZnxToRef, ZnxZero}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace}, + layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl}, }; +use crate::decryption::GLWEDecryption; use crate::layouts::{ - GGLWE, GGLWEToRef, GGLWEInfos, GLWEPlaintext, LWEInfos, + GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }; -use crate::decryption::GLWEDecryption; impl GGLWE { - pub fn assert_noise( &self, module: &M, @@ -24,12 +21,15 @@ impl GGLWE { DataSk: DataRef, DataWant: DataRef, M: GGLWENoise, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl, + BE: Backend + + ScratchOwnedAllocImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrow + + VecZnxSubScalarInplaceImpl, { module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - // pub fn assert_noise( // &self, // module: &Module, @@ -79,7 +79,6 @@ impl GGLWE { // } } - pub trait GGLWENoise { fn gglwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) where @@ -89,10 +88,14 @@ pub trait GGLWENoise { BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl; } -impl GGLWENoise for Module +impl GGLWENoise for Module where Module: GLWEDecryption, - Scratch: ScratchTakeBasic + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, + Scratch: ScratchTakeBasic + + ScratchOwnedAllocImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrow, { fn gglwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) where @@ -101,9 +104,8 @@ where P: ScalarZnxToRef, BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl, { - let res: &GGLWE<&[u8]> = &res.to_ref(); - + let dsize: usize = res.dsize().into(); let base2k: usize = res.base2k().into(); @@ -112,7 +114,12 @@ where (0..res.rank_in().into()).for_each(|col_i| { (0..res.dnum().into()).for_each(|row_i| { - self.glwe_decrypt(&res.at(row_i, col_i), &mut pt, sk_prepared, scratch.borrow()); + self.glwe_decrypt( + &res.at(row_i, col_i), + &mut pt, + sk_prepared, + scratch.borrow(), + ); self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i); @@ -129,4 +136,4 @@ where }); }); } -} \ No newline at end of file +} diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 1a00f4d..aa270a5 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -1,18 +1,17 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, - VecZnxSubInplace, - ScratchTakeBasic, + ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, + VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{GGSW, GGSWInfos, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, GGSWToRef}; -use crate::layouts::prepared::GLWESecretPreparedToRef; use crate::decryption::GLWEDecryption; +use crate::layouts::prepared::GLWESecretPreparedToRef; +use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl GGSW { pub fn assert_noise( @@ -20,7 +19,7 @@ impl GGSW { module: &M, sk_prepared: &GLWESecretPrepared, pt_want: &ScalarZnx, - max_noise: F + max_noise: F, ) where DataSk: DataRef, DataScalar: DataRef, @@ -91,7 +90,6 @@ where BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64, { - let res: &GGSW<&[u8]> = &res.to_ref(); let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref(); @@ -126,7 +124,12 @@ where ); } - self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow()); + self.glwe_decrypt( + &res.at(row_i, col_j), + &mut pt_have, + sk_prepared, + scratch.borrow(), + ); self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0); @@ -136,7 +139,7 @@ where pt.data.zero(); }); - }); + }); } fn ggsw_print_noise(&self, res: &R, sk_prepared: &S, pt_want: &P) @@ -180,7 +183,12 @@ where ); } - self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow()); + self.glwe_decrypt( + &res.at(row_i, col_j), + &mut pt_have, + sk_prepared, + scratch.borrow(), + ); self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0); let std_pt: f64 = pt_have.data.std(base2k, 0).log2(); @@ -188,5 +196,5 @@ where pt.data.zero(); }); }); - } -} \ No newline at end of file + } +} diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index 16986b5..d21ae60 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSubInplace, ScratchTakeBasic, + ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, + VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, @@ -96,7 +96,6 @@ impl GLWE { // } } - pub trait GLWENoise { fn glwe_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 where @@ -126,7 +125,11 @@ where + VecZnxSubInplace + VecZnxNormalizeInplace + GLWEDecryption, - Scratch: ScratchTakeBasic + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, + Scratch: ScratchTakeBasic + + ScratchOwnedAllocImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrow, { fn glwe_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 where @@ -157,4 +160,4 @@ where let noise_have: f64 = self.glwe_noise(res, sk_prepared, pt_want, scratch.borrow()); assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); } -} \ No newline at end of file +} diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index 27021e7..48e67ca 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -84,6 +84,11 @@ where impl GLWEAdd for Module where Self: ModuleN + VecZnxAdd + VecZnxCopy + VecZnxAddInplace {} +impl GLWESub for Module where + Self: ModuleN + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxSubInplace + VecZnxSubNegateInplace +{ +} + pub trait GLWESub where Self: ModuleN + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxSubInplace + VecZnxSubNegateInplace, diff --git a/poulpy-hal/src/api/scratch.rs b/poulpy-hal/src/api/scratch.rs index ee4a080..fb17266 100644 --- a/poulpy-hal/src/api/scratch.rs +++ b/poulpy-hal/src/api/scratch.rs @@ -28,6 +28,8 @@ pub trait TakeSlice { fn take_slice(&mut self, len: usize) -> (&mut [T], &mut Self); } +impl ScratchTakeBasic for Scratch where Self: TakeSlice {} + pub trait ScratchTakeBasic where Self: TakeSlice, diff --git a/poulpy-hal/src/delegates/module.rs b/poulpy-hal/src/delegates/module.rs index 0e3a455..fa01c2f 100644 --- a/poulpy-hal/src/delegates/module.rs +++ b/poulpy-hal/src/delegates/module.rs @@ -1,5 +1,5 @@ use crate::{ - api::ModuleNew, + api::{ModuleN, ModuleNew}, layouts::{Backend, Module}, oep::ModuleNewImpl, }; @@ -12,3 +12,12 @@ where B::new_impl(n) } } + +impl ModuleN for Module +where + B: Backend, +{ + fn n(&self) -> usize { + self.n() + } +}