From 53bc78f421a95531c5239d588efda7ac6152152d Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 10:32:02 +0200 Subject: [PATCH] Removed traits on structs not dependent on Module API --- .../benches/external_product_glwe_fft64.rs | 14 +- poulpy-core/benches/keyswitch_glwe_fft64.rs | 16 +-- poulpy-core/examples/encryption.rs | 8 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 14 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 9 +- .../src/encryption/compressed/gglwe_atk.rs | 4 +- .../src/encryption/compressed/gglwe_ct.rs | 4 +- poulpy-core/src/encryption/gglwe_atk.rs | 2 +- poulpy-core/src/encryption/gglwe_ct.rs | 3 +- poulpy-core/src/encryption/gglwe_tsk.rs | 2 +- poulpy-core/src/encryption/ggsw_ct.rs | 4 +- poulpy-core/src/encryption/glwe_pk.rs | 2 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 2 +- poulpy-core/src/encryption/lwe_ksk.rs | 2 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 2 +- poulpy-core/src/glwe_packing.rs | 19 +-- poulpy-core/src/keyswitching/lwe_ct.rs | 12 +- .../src/layouts/compressed/gglwe_atk.rs | 96 +++---------- .../src/layouts/compressed/gglwe_ct.rs | 101 ++++---------- .../src/layouts/compressed/gglwe_ksk.rs | 90 +++--------- .../src/layouts/compressed/gglwe_tsk.rs | 92 +++---------- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 86 +++--------- poulpy-core/src/layouts/compressed/glwe_ct.rs | 75 ++-------- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 122 ++++++----------- poulpy-core/src/layouts/compressed/lwe_ct.rs | 58 ++------ poulpy-core/src/layouts/compressed/lwe_ksk.rs | 129 +++++++----------- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 116 ++++++---------- poulpy-core/src/layouts/gglwe_atk.rs | 76 +++-------- poulpy-core/src/layouts/gglwe_ct.rs | 113 ++++----------- poulpy-core/src/layouts/gglwe_ksk.rs | 124 +++++------------ poulpy-core/src/layouts/gglwe_tsk.rs | 95 ++++--------- poulpy-core/src/layouts/ggsw_ct.rs | 83 ++++------- poulpy-core/src/layouts/glwe_ct.rs | 78 +++-------- poulpy-core/src/layouts/glwe_pk.rs | 79 +++-------- poulpy-core/src/layouts/glwe_pt.rs | 79 +++-------- poulpy-core/src/layouts/glwe_sk.rs | 72 +++------- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 125 +++++++---------- poulpy-core/src/layouts/lwe_ct.rs | 60 ++------ poulpy-core/src/layouts/lwe_ksk.rs | 127 +++++++---------- poulpy-core/src/layouts/lwe_pt.rs | 39 ++---- poulpy-core/src/layouts/lwe_sk.rs | 17 +-- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 127 +++++++---------- poulpy-core/src/noise/gglwe_ct.rs | 28 ++-- poulpy-core/src/noise/ggsw_ct.rs | 77 ++++------- poulpy-core/src/noise/glwe_ct.rs | 107 ++------------- poulpy-core/src/tests/serialization.rs | 63 +++------ .../tests/test_suite/encryption/gglwe_atk.rs | 38 ++---- .../tests/test_suite/encryption/gglwe_ct.rs | 42 +++--- .../tests/test_suite/encryption/glwe_ct.rs | 62 ++++----- .../tests/test_suite/encryption/glwe_tsk.rs | 73 +++++----- 50 files changed, 858 insertions(+), 2010 deletions(-) diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 47b98ea..900eda1 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -61,9 +61,9 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { rank, }; - 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 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 pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( @@ -76,7 +76,7 @@ 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(&module, &glwe_in_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); @@ -165,8 +165,8 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - 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 mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( @@ -179,7 +179,7 @@ fn bench_external_product_glwe_inplace_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: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index d603b60..66fb932 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -62,9 +62,9 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { rank, }; - 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 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 scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout) @@ -81,7 +81,7 @@ 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(&module, &glwe_in_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let mut sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); @@ -177,8 +177,8 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&module, &gglwe_layout); - let mut ct: GLWE> = GLWE::alloc_from_infos(&module, &glwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout) @@ -190,13 +190,13 @@ fn bench_keyswitch_glwe_inplace_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(&module, &glwe_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); 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); + let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk( diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index 43a2b0e..5ee8c8e 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -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(&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); + 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); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]); @@ -58,7 +58,7 @@ fn main() { ); // Generate secret-key - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_ct_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 49744ba..17ca1bf 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank, + GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank, prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, }, }; @@ -40,11 +40,11 @@ where } impl LWESampleExtract for Module where Self: ModuleN {} -impl LWEFromGLWE for Module where Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract {} +impl LWEFromGLWE for Module where Self: GLWEKeyswitch + LWESampleExtract {} pub trait LWEFromGLWE where - Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract, + Self: GLWEKeyswitch + LWESampleExtract, { fn lwe_from_glwe_tmp_bytes(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize where @@ -59,8 +59,12 @@ where rank: Rank(1), }; - self.bytes_of_glwe(lwe_infos.base2k(), lwe_infos.k(), 1u32.into()) - + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos) + GLWE::bytes_of( + self.n().into(), + lwe_infos.base2k(), + lwe_infos.k(), + 1u32.into(), + ) + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos) } fn lwe_from_glwe(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index 7be6574..22f38ed 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -6,16 +6,16 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef, + GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef, prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, }, }; -impl GLWEFromLWE for Module where Self: GLWEKeyswitch + GLWEAlloc {} +impl GLWEFromLWE for Module where Self: GLWEKeyswitch {} pub trait GLWEFromLWE where - Self: GLWEKeyswitch + GLWEAlloc, + Self: GLWEKeyswitch, { fn glwe_from_lwe_tmp_bytes(&self, glwe_infos: &R, lwe_infos: &A, key_infos: &K) -> usize where @@ -23,7 +23,8 @@ where A: LWEInfos, K: GGLWEInfos, { - let ct: usize = self.bytes_of_glwe( + let ct: usize = GLWE::bytes_of( + self.n().into(), key_infos.base2k(), lwe_infos.k().max(glwe_infos.k()), 1u32.into(), diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index a3415cc..668a133 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -8,7 +8,7 @@ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretAlloc, GLWESecretToRef, LWEInfos, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut}, }, }; @@ -68,7 +68,7 @@ where where A: GGLWEInfos, { - self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + self.bytes_of_glwe_secret(infos.rank()) + self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of(self.n().into(), infos.rank()) } fn automorphism_key_compressed_encrypt_sk( diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index fbac0f6..1081525 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, }, layouts::{ - GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintextAlloc, GLWESecretPrepared, LWEInfos, + GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintext, GLWESecretPrepared, LWEInfos, compressed::{GGLWECompressed, GGLWECompressedToMut}, prepared::GLWESecretPreparedToRef, }, @@ -82,7 +82,7 @@ where { self.glwe_encrypt_sk_tmp_bytes(infos) .max(self.vec_znx_normalize_tmp_bytes()) - + self.bytes_of_glwe_plaintext_from_infos(infos) + + GLWEPlaintext::bytes_of_from_infos(infos) } fn gglwe_compressed_encrypt_sk( diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 8521df3..ea6096a 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -80,7 +80,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(self, infos) + self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(infos) } fn automorphism_key_encrypt_sk( diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index bb1a9c1..ba4ae8e 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -85,8 +85,7 @@ where where A: GGLWEInfos, { - self.glwe_encrypt_sk_tmp_bytes(infos) - + GLWEPlaintext::bytes_of_from_infos(self, infos).max(self.vec_znx_normalize_tmp_bytes()) + self.glwe_encrypt_sk_tmp_bytes(infos) + GLWEPlaintext::bytes_of_from_infos(infos).max(self.vec_znx_normalize_tmp_bytes()) } fn gglwe_encrypt_sk( diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 6867b55..2a4792d 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -82,7 +82,7 @@ where + self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + self.bytes_of_vec_znx_big(1, 1) + self.bytes_of_vec_znx_dft(1, 1) - + GLWESecret::bytes_of(self, Rank(1)) + + GLWESecret::bytes_of(self.n().into(), Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) } diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 8964122..bc67822 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ SIGMA, ScratchTakeCore, encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, layouts::{ - GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintextAlloc, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -79,7 +79,7 @@ where { self.glwe_encrypt_sk_tmp_bytes(infos) .max(self.vec_znx_normalize_tmp_bytes()) - + self.bytes_of_glwe_plaintext_from_infos(infos) + + GLWEPlaintext::bytes_of_from_infos(infos) } fn ggsw_encrypt_sk( diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index f4a8c35..58b5b8a 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -55,7 +55,7 @@ where // Its ok to allocate scratch space here since pk is usually generated only once. let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); - let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); + let mut tmp: GLWE> = GLWE::alloc_from_infos(res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); } diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 59be47c..bd7c271 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -72,7 +72,7 @@ where A: GGLWEInfos, { GLWESecretPrepared::bytes_of(self, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in())) + + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self.n().into(), infos.rank_in())) } fn glwe_to_lwe_switching_key_encrypt_sk( diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 348df2c..215efee 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -85,7 +85,7 @@ where 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESecret::bytes_of(self, Rank(1)) + GLWESecret::bytes_of(self.n().into(), Rank(1)) + GLWESecretPrepared::bytes_of(self, Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) } diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index d51faf7..a66c95d 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -75,7 +75,7 @@ where Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self, infos.rank_in()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self.n().into(), infos.rank_in()) } fn lwe_to_glwe_switching_key_encrypt_sk( diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 4b7d5b1..bcd9e45 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -9,7 +9,7 @@ use crate::{ GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, glwe_trace::GLWETrace, layouts::{ - GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, }, }; @@ -41,13 +41,12 @@ impl Accumulator { /// * `base2k`: base 2 logarithm of the GLWE ciphertext in memory digit representation. /// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus. /// * `rank`: rank of the GLWE ciphertext. - pub fn alloc(module: &M, infos: &A) -> Self + pub fn alloc(infos: &A) -> Self where A: GLWEInfos, - M: GLWEPacking, { Self { - data: GLWE::alloc_from_infos(module, infos), + data: GLWE::alloc_from_infos(infos), value: false, control: false, } @@ -59,20 +58,18 @@ impl GLWEPacker { /// /// # Arguments /// - /// * `module`: static backend FFT tables. /// * `log_batch`: packs coefficients which are multiples of X^{N/2^log_batch}. /// i.e. with `log_batch=0` only the constant coefficient is packed /// and N GLWE ciphertext can be packed. With `log_batch=2` all coefficients /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts /// can be packed. - pub fn alloc(module: &M, infos: &A, log_batch: usize) -> Self + pub fn alloc(infos: &A, log_batch: usize) -> Self where A: GLWEInfos, - M: GLWEPacking, { let mut accumulators: Vec = Vec::::new(); let log_n: usize = infos.n().log2(); - (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(module, infos))); + (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(infos))); GLWEPacker { accumulators, log_batch, @@ -96,7 +93,7 @@ impl GLWEPacker { K: GGLWEInfos, M: GLWEPacking, { - module.bytes_of_glwe_from_infos(res_infos) + GLWE::bytes_of_from_infos(res_infos) + module .glwe_rsh_tmp_byte() .max(module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos)) @@ -169,7 +166,6 @@ impl GLWEPacking for Module where + GLWEAdd + GLWENormalize + GLWECopy - + GLWEAlloc { } @@ -183,8 +179,7 @@ where + GLWEShift + GLWEAdd + GLWENormalize - + GLWECopy - + GLWEAlloc, + + GLWECopy, { /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 7aa5321..8a529c7 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -7,7 +7,7 @@ use crate::{ LWESampleExtract, ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, layouts::{ - GGLWEInfos, GLWE, GLWEAlloc, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, + GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef}, }, }; @@ -40,7 +40,7 @@ impl LWEKeySwitch for Module where Self: LWEKeySwitch { pub trait LWEKeySwitch where - Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract, + Self: GLWEKeyswitch + LWESampleExtract, { fn lwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where @@ -51,21 +51,21 @@ where let max_k: TorusPrecision = a_infos.k().max(res_infos.k()); let glwe_a_infos: GLWELayout = GLWELayout { - n: self.ring_degree(), + n: self.n().into(), base2k: a_infos.base2k(), k: max_k, rank: Rank(1), }; let glwe_res_infos: GLWELayout = GLWELayout { - n: self.ring_degree(), + n: self.n().into(), base2k: res_infos.base2k(), k: max_k, rank: Rank(1), }; - let glwe_in: usize = GLWE::bytes_of_from_infos(self, &glwe_a_infos); - let glwe_out: usize = GLWE::bytes_of_from_infos(self, &glwe_res_infos); + let glwe_in: usize = GLWE::bytes_of_from_infos(&glwe_a_infos); + let glwe_out: usize = GLWE::bytes_of_from_infos(&glwe_res_infos); let ks: usize = self.glwe_keyswitch_tmp_bytes(&glwe_res_infos, &glwe_a_infos, key_infos); glwe_in + glwe_out + ks diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 8c57529..5a40e5d 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -6,8 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; @@ -86,32 +85,34 @@ impl fmt::Display for AutomorphismKeyCompressed { } } -impl AutomorphismKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} +impl AutomorphismKeyCompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } -pub trait AutomorphismKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_automorphism_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> AutomorphismKeyCompressed> { + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { AutomorphismKeyCompressed { - key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), + key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } - fn alloc_automorphism_key_compressed_from_infos(&self, infos: &A) -> AutomorphismKeyCompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!(infos.rank_in(), infos.rank_out()); - self.alloc_automorphism_key_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -120,61 +121,8 @@ where ) } - fn bytes_of_automorphism_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize { - self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize) - } - - fn bytes_of_automorphism_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!(infos.rank_in(), infos.rank_out()); - self.bytes_of_automorphism_key_compressed( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl AutomorphismKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: AutomorphismKeyCompressedAlloc, - { - module.alloc_automorphism_key_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: AutomorphismKeyCompressedAlloc, - { - module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: AutomorphismKeyCompressedAlloc, - { - module.bytes_of_automorphism_key_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: AutomorphismKeyCompressedAlloc, - { - module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 13d13f9..dcc9886 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -105,19 +105,23 @@ impl fmt::Display for GGLWECompressed { } } -pub trait GGLWECompressedAlloc -where - Self: GetDegree, -{ - fn alloc_gglwe_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GGLWECompressed> { +impl GGLWECompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -134,7 +138,7 @@ where GGLWECompressed { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), 1, @@ -148,22 +152,21 @@ where } } - fn alloc_gglwe_compressed_from_infos(&self, infos: &A) -> GGLWECompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!(infos.n(), self.ring_degree()); - self.alloc_gglwe_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(), - infos.rank_out(), infos.dnum(), infos.dsize(), ) } - fn bytes_of_gglwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -179,69 +182,13 @@ where ); MatZnx::bytes_of( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), 1, k.0.div_ceil(base2k.0) as usize, ) } - - fn bytes_of_gglwe_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!(infos.n(), self.ring_degree()); - self.bytes_of_gglwe_compressed( - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGLWECompressedAlloc for Module where Self: GetDegree {} - -impl GGLWECompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GGLWECompressedAlloc, - { - module.alloc_gglwe_compressed_from_infos(infos) - } - - pub fn alloc( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self - where - M: GGLWECompressedAlloc, - { - module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GGLWECompressedAlloc, - { - module.bytes_of_gglwe_compressed_from_infos(infos) - } - - pub fn byte_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: GGLWECompressedAlloc, - { - module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) - } } impl GGLWECompressed { diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index d9574c4..3b94141 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, LWEInfos, Rank, TorusPrecision, - compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, + compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -81,33 +81,13 @@ impl fmt::Display for GLWESwitchingKeyCompressed { } } -impl GLWESwitchingKeyCompressedAlloc for Module where Self: GGLWECompressedAlloc {} - -pub trait GLWESwitchingKeyCompressedAlloc -where - Self: GGLWECompressedAlloc, -{ - fn alloc_glwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GLWESwitchingKeyCompressed> { - GLWESwitchingKeyCompressed { - key: self.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, - } - } - - fn alloc_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWESwitchingKeyCompressed> +impl GLWESwitchingKeyCompressed> { + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, { - self.alloc_glwe_switching_key_compressed( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(), @@ -117,62 +97,24 @@ where ) } - fn bytes_of_glwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize { - self.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { + GLWESwitchingKeyCompressed { + key: GGLWECompressed::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), + sk_in_n: 0, + sk_out_n: 0, + } } - fn bytes_of_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - self.bytes_of_gglwe_compressed_from_infos(infos) - } -} - -impl GLWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GLWESwitchingKeyCompressedAlloc, - { - module.alloc_glwe_switching_key_compressed_from_infos(infos) + GGLWECompressed::bytes_of_from_infos(infos) } - pub fn alloc( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self - where - M: GLWESwitchingKeyCompressedAlloc, - { - module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_glwe_switching_key_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: GLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize +where { + GGLWECompressed::bytes_of(n, base2k, k, rank_in, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index d0202cc..4ec8a42 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -6,8 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -82,38 +81,36 @@ impl fmt::Display for TensorKeyCompressed { } } -impl TensorKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} +impl TensorKeyCompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } -pub trait TensorKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_tensor_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> TensorKeyCompressed> { + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); TensorKeyCompressed { keys: (0..pairs) - .map(|_| self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } - fn alloc_tensor_key_compressed_from_infos(&self, infos: &A) -> TensorKeyCompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" - ); - self.alloc_tensor_key_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -122,54 +119,9 @@ where ) } - fn bytes_of_tensor_key_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, dsize) - } - - fn bytes_of_tensor_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.bytes_of_tensor_key_compressed( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl TensorKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: TensorKeyCompressedAlloc, - { - module.alloc_tensor_key_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: TensorKeyCompressedAlloc, - { - module.alloc_tensor_key_compressed(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: TensorKeyCompressedAlloc, - { - module.bytes_of_tensor_key_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: TensorKeyCompressedAlloc, - { - module.bytes_of_tensor_key_compressed(base2k, k, rank, dnum, dsize) + pairs * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize) } } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index d12a85b..ca4446f 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -96,20 +96,22 @@ impl FillUniform for GGSWCompressed { } } -impl GGSWCompressedAlloc for Module where Self: GetDegree {} +impl GGSWCompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGSWInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } -pub trait GGSWCompressedAlloc -where - Self: GetDegree, -{ - fn alloc_ggsw_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GGSWCompressed> { + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; assert!( size as u32 > dsize.0, @@ -126,7 +128,7 @@ where GGSWCompressed { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), 1, @@ -140,11 +142,12 @@ where } } - fn alloc_ggsw_compressed_from_infos(&self, infos: &A) -> GGSWCompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGSWInfos, { - self.alloc_ggsw_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -153,7 +156,7 @@ where ) } - fn bytes_of_ggsw_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; assert!( size as u32 > dsize.0, @@ -169,58 +172,13 @@ where ); MatZnx::bytes_of( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), 1, k.0.div_ceil(base2k.0) as usize, ) } - - fn bytes_of_ggsw_compressed_key_from_infos(&self, infos: &A) -> usize - where - A: GGSWInfos, - { - self.bytes_of_ggsw_compressed( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGSWCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGSWInfos, - M: GGSWCompressedAlloc, - { - module.alloc_ggsw_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: GGSWCompressedAlloc, - { - module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGSWInfos, - M: GGSWCompressedAlloc, - { - module.bytes_of_ggsw_compressed_key_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: GGSWCompressedAlloc, - { - module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize) - } } impl GGSWCompressed { diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 98115b6..1b05575 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -88,17 +88,17 @@ impl FillUniform for GLWECompressed { } } -pub trait GLWECompressedAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed> { +impl GLWECompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { GLWECompressed { - data: VecZnx::alloc( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ), + data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, rank, @@ -106,62 +106,15 @@ where } } - fn alloc_glwe_compressed_from_infos(&self, infos: &A) -> GLWECompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - assert_eq!(self.ring_degree(), infos.n()); - self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k()) } - fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::bytes_of( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ) - } - - fn bytes_of_glwe_compressed_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - assert_eq!(self.ring_degree(), infos.n()); - self.bytes_of_glwe_compressed(infos.base2k(), infos.k()) - } -} - -impl GLWECompressedAlloc for Module where Self: GetDegree {} - -impl GLWECompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWECompressedAlloc, - { - module.alloc_glwe_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - M: GLWECompressedAlloc, - { - module.alloc_glwe_compressed(base2k, k, rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWECompressedAlloc, - { - module.bytes_of_glwe_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize - where - M: GLWECompressedAlloc, - { - module.bytes_of_glwe_compressed(base2k, k) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize) } } diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index afdb493..08508b1 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -9,8 +9,7 @@ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; @@ -88,96 +87,61 @@ impl WriterTo for GLWEToLWESwitchingKeyCompressed { } } -pub trait GLWEToLWESwitchingKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_glwe_to_lwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> GLWEToLWESwitchingKeyCompressed> { - GLWEToLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) - } - - fn alloc_glwe_to_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyCompressed> - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" - ); - self.alloc_glwe_to_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) - } - - fn bytes_of_glwe_to_lwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> usize { - self.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, Dsize(1)) - } - - fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" - ); - self.bytes_of_glwe_switching_key_compressed_from_infos(infos) - } -} - -impl GLWEToLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} - impl GLWEToLWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyCompressedAlloc, { - module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" + ); + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + ) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self - where - M: GLWEToLWESwitchingKeyCompressedAlloc, - { - module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { + GLWEToLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc( + n, + base2k, + k, + rank_in, + Rank(1), + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyCompressedAlloc, { - module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" + ); + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize - where - M: GLWEToLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { + GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank_in, dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index c21a6dd..ce4c000 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ct.rs @@ -62,8 +62,15 @@ impl FillUniform for LWECompressed { } } -pub trait LWECompressedAlloc { - fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed> { +impl LWECompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: LWEInfos, + { + Self::alloc(infos.base2k(), infos.k()) + } + + pub fn alloc(base2k: Base2K, k: TorusPrecision) -> Self { LWECompressed { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, @@ -72,57 +79,16 @@ pub trait LWECompressedAlloc { } } - fn alloc_lwe_compressed_from_infos(&self, infos: &A) -> LWECompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: LWEInfos, { - self.alloc_lwe_compressed(infos.base2k(), infos.k()) + Self::bytes_of(infos.base2k(), infos.k()) } - fn bytes_of_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { + pub fn bytes_of(base2k: Base2K, k: TorusPrecision) -> usize { Zn::bytes_of(1, 1, k.0.div_ceil(base2k.0) as usize) } - - fn bytes_of_lwe_compressed_from_infos(&self, infos: &A) -> usize - where - A: LWEInfos, - { - self.bytes_of_lwe_compressed(infos.base2k(), infos.k()) - } -} - -impl LWECompressedAlloc for Module {} - -impl LWECompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: LWEInfos, - M: LWECompressedAlloc, - { - module.alloc_lwe_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWECompressedAlloc, - { - module.alloc_lwe_compressed(base2k, k) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: LWEInfos, - M: LWECompressedAlloc, - { - module.bytes_of_lwe_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize - where - M: LWECompressedAlloc, - { - module.bytes_of_lwe_compressed(base2k, k) - } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 7e1b0cf..3738b92 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -6,8 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; use std::fmt; @@ -85,99 +84,65 @@ impl WriterTo for LWESwitchingKeyCompressed { } } -pub trait LWESwitchingKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_lwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - dnum: Dnum, - ) -> LWESwitchingKeyCompressed> { - LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) - } - - fn alloc_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWESwitchingKeyCompressed> - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKeyCompressed" - ); - self.alloc_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) - } - - fn bytes_of_lwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) - } - - fn bytes_of_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKeyCompressed" - ); - self.bytes_of_glwe_switching_key_compressed_from_infos(infos) - } -} - -impl LWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} - impl LWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: LWESwitchingKeyCompressedAlloc, { - module.alloc_lwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKeyCompressed" + ); + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self - where - M: LWESwitchingKeyCompressedAlloc, - { - module.alloc_lwe_switching_key_compressed(base2k, k, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { + LWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc( + n, + base2k, + k, + Rank(1), + Rank(1), + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: LWESwitchingKeyCompressedAlloc, { - module.bytes_of_lwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKeyCompressed" + ); + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize - where - M: LWESwitchingKeyCompressedAlloc, - { - module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index 545f126..ea0ca05 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -7,8 +7,7 @@ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; use std::fmt; @@ -86,90 +85,61 @@ impl WriterTo for LWEToGLWESwitchingKeyCompressed { } } -impl LWEToGLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} - -pub trait LWEToGLWESwitchingKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_lwe_to_glwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_out: Rank, - dnum: Dnum, - ) -> LWEToGLWESwitchingKeyCompressed> { - LWEToGLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) - } - - fn alloc_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed> - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" - ); - self.alloc_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) - } - - fn bytes_of_lwe_to_glwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) - } - - fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" - ); - self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) - } -} - impl LWEToGLWESwitchingKeyCompressed> { - pub fn alloc(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyCompressedAlloc, { - module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_out(), + infos.dnum(), + ) } - pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self - where - M: LWEToGLWESwitchingKeyCompressedAlloc, - { - module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { + LWEToGLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc( + n, + base2k, + k, + Rank(1), + rank_out, + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyCompressedAlloc, { - module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize - where - M: LWEToGLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index b4b61c6..5d0f5f7 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, + LWEInfos, Rank, TorusPrecision, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -144,31 +144,13 @@ impl fmt::Display for AutomorphismKey { } } -impl AutomorphismKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - -pub trait AutomorphismKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_automorphism_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> AutomorphismKey> { - AutomorphismKey { - key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize), - p: 0, - } - } - - fn alloc_automorphism_key_from_infos(&self, infos: &A) -> AutomorphismKey> +impl AutomorphismKey> { + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, { - self.alloc_automorphism_key( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -177,11 +159,14 @@ where ) } - fn bytes_of_automorphism_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - self.bytes_of_glwe_switching_key(base2k, k, rank, rank, dnum, dsize) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { + AutomorphismKey { + key: GLWESwitchingKey::alloc(n, base2k, k, rank, rank, dnum, dsize), + p: 0, + } } - fn bytes_of_automorphism_key_from_infos(&self, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { @@ -190,7 +175,8 @@ where infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKey" ); - self.bytes_of_automorphism_key( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -198,37 +184,9 @@ where infos.dsize(), ) } -} -impl AutomorphismKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: AutomorphismKeyAlloc, - { - module.alloc_automorphism_key_from_infos(infos) - } - - pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: AutomorphismKeyAlloc, - { - module.alloc_automorphism_key(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: AutomorphismKeyAlloc, - { - module.bytes_of_automorphism_key_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: AutomorphismKeyAlloc, - { - module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + GLWESwitchingKey::bytes_of(n, base2k, k, rank, rank, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 666293a..a491186 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -1,11 +1,9 @@ use poulpy_hal::{ - layouts::{ - Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, - }, + layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -191,19 +189,23 @@ impl GGLWE { } } -pub trait GGLWEAlloc -where - Self: GetDegree, -{ - fn alloc_gglwe( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GGLWE> { +impl GGLWE> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -220,7 +222,7 @@ where GGLWE { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), @@ -232,11 +234,12 @@ where } } - fn alloc_glwe_from_infos(&self, infos: &A) -> GGLWE> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - self.alloc_gglwe( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(), @@ -246,8 +249,8 @@ where ) } - fn bytes_of_gglwe( - &self, + pub fn bytes_of( + n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -270,77 +273,13 @@ where ); MatZnx::bytes_of( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } - - fn bytes_of_gglwe_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.bytes_of_gglwe( - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGLWEAlloc for Module where Self: GetDegree {} - -impl GGLWE> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GGLWEAlloc, - { - module.alloc_glwe_from_infos(infos) - } - - pub fn alloc( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self - where - M: GGLWEAlloc, - { - module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GGLWEAlloc, - { - module.bytes_of_gglwe_from_infos(infos) - } - - pub fn bytes_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize - where - M: GGLWEAlloc, - { - module.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) - } } pub trait GGLWEToMut { diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 466f8ac..b451951 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -1,11 +1,10 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, - TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -163,31 +162,13 @@ impl FillUniform for GLWESwitchingKey { } } -pub trait GLWESwitchingKeyAlloc -where - Self: GGLWEAlloc, -{ - fn alloc_glwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GLWESwitchingKey> { - GLWESwitchingKey { - key: self.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, - } - } - - fn alloc_glwe_switching_key_from_infos(&self, infos: &A) -> GLWESwitchingKey> +impl GLWESwitchingKey> { + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, { - self.alloc_glwe_switching_key( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(), @@ -197,8 +178,31 @@ where ) } - fn bytes_of_glwe_switching_key( - &self, + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { + GLWESwitchingKey { + key: GGLWE::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), + sk_in_n: 0, + sk_out_n: 0, + } + } + + pub fn bytes_of_from_infos(infos: &A) -> usize + where + A: GGLWEInfos, + { + Self::bytes_of( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + pub fn bytes_of( + n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -206,71 +210,7 @@ where dnum: Dnum, dsize: Dsize, ) -> usize { - self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) - } - - fn bytes_of_glwe_switching_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.bytes_of_glwe_switching_key( - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GLWESwitchingKeyAlloc for Module where Self: GGLWEAlloc {} - -impl GLWESwitchingKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GLWESwitchingKeyAlloc, - { - module.alloc_glwe_switching_key_from_infos(infos) - } - - pub fn alloc( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self - where - M: GLWESwitchingKeyAlloc, - { - module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GLWESwitchingKeyAlloc, - { - module.bytes_of_glwe_switching_key_from_infos(infos) - } - - pub fn bytes_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize - where - M: GLWESwitchingKeyAlloc, - { - module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) + GGLWE::bytes_of(n, base2k, k, rank_in, rank_out, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index 10523b1..240b482 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -130,20 +130,36 @@ impl fmt::Display for TensorKey { } } -pub trait TensorKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey> { +impl TensorKey> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in(), + infos.rank_out(), + "rank_in != rank_out is not supported for GGLWETensorKey" + ); + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); TensorKey { keys: (0..pairs) - .map(|_| self.alloc_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } - fn alloc_tensor_key_from_infos(&self, infos: &A) -> TensorKey> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { @@ -152,7 +168,8 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - self.alloc_tensor_key( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -161,61 +178,9 @@ where ) } - fn bytes_of_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize) - } - - fn bytes_of_tensor_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKey" - ); - self.bytes_of_tensor_key( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl TensorKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - -impl TensorKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: TensorKeyAlloc, - { - module.alloc_tensor_key_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: TensorKeyAlloc, - { - module.alloc_tensor_key(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: TensorKeyAlloc, - { - module.bytes_of_tensor_key_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: TensorKeyAlloc, - { - module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize) + pairs * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index a4d091d..a4109d0 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -1,12 +1,10 @@ use poulpy_hal::{ - layouts::{ - Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, - }, + layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, source::Source, }; use std::fmt; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision}; pub trait GGSWInfos where @@ -152,13 +150,22 @@ impl GGSW { } } -impl GGSWAlloc for Module where Self: GetDegree {} +impl GGSW> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGSWInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } -pub trait GGSWAlloc -where - Self: GetDegree, -{ - fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW> { + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -175,7 +182,7 @@ where GGSW { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -187,11 +194,12 @@ where } } - fn alloc_ggsw_from_infos(&self, infos: &A) -> GGSW> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGSWInfos, { - self.alloc_ggsw( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -200,7 +208,7 @@ where ) } - fn bytes_of_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -216,58 +224,13 @@ where ); MatZnx::bytes_of( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } - - fn bytes_of_ggsw_from_infos(&self, infos: &A) -> usize - where - A: GGSWInfos, - { - self.bytes_of_ggsw( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGSW> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGSWInfos, - M: GGSWAlloc, - { - module.alloc_ggsw_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: GGSWAlloc, - { - module.alloc_ggsw(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGSWInfos, - M: GGSWAlloc, - { - module.bytes_of_ggsw_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: GGSWAlloc, - { - module.bytes_of_ggsw(base2k, k, rank, dnum, dsize) - } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index edf7b4c..d47f8cd 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -1,12 +1,11 @@ use poulpy_hal::{ layouts::{ - Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, - WriterTo, ZnxInfos, + Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, }, source::Source, }; -use crate::layouts::{Base2K, Degree, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -146,76 +145,31 @@ impl FillUniform for GLWE { } } -pub trait GLWEAlloc -where - Self: GetDegree, -{ - fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE> { +impl GLWE> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { GLWE { - data: VecZnx::alloc( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ), + data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k, k, } } - fn alloc_glwe_from_infos(&self, infos: &A) -> GLWE> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - self.alloc_glwe(infos.base2k(), infos.k(), infos.rank()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank()) } - fn bytes_of_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::bytes_of( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ) - } - - fn bytes_of_glwe_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank()) - } -} - -impl GLWEAlloc for Module where Self: GetDegree {} - -impl GLWE> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWEAlloc, - { - module.alloc_glwe_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - M: GLWEAlloc, - { - module.alloc_glwe(base2k, k, rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWEAlloc, - { - module.bytes_of_glwe_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize - where - M: GLWEAlloc, - { - module.bytes_of_glwe(base2k, k, rank) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } } diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index b601e82..2231389 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -1,11 +1,9 @@ -use poulpy_hal::layouts::{ - Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, -}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos}; use crate::{ GetDistribution, GetDistributionMut, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -85,77 +83,32 @@ impl GLWEInfos for GLWEPublicKeyLayout { } } -pub trait GLWEPublicKeyAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey> { +impl GLWEPublicKey> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { GLWEPublicKey { - data: VecZnx::alloc( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ), + data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k, k, dist: Distribution::NONE, } } - fn alloc_glwe_public_key_from_infos(&self, infos: &A) -> GLWEPublicKey> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank()) } - fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::bytes_of( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ) - } - - fn bytes_of_glwe_public_key_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) - } -} - -impl GLWEPublicKeyAlloc for Module where Self: GetDegree {} - -impl GLWEPublicKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWEPublicKeyAlloc, - { - module.alloc_glwe_public_key_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - M: GLWEPublicKeyAlloc, - { - module.alloc_glwe_public_key(base2k, k, rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWEPublicKeyAlloc, - { - module.bytes_of_glwe_public_key_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize - where - M: GLWEPublicKeyAlloc, - { - module.bytes_of_glwe_public_key(base2k, k, rank) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } } diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index e3d7290..3261d3d 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -1,10 +1,8 @@ use std::fmt; -use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; -use crate::layouts::{ - Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision, -}; +use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, Rank, SetGLWEInfos, TorusPrecision}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPlaintextLayout { @@ -85,76 +83,31 @@ impl fmt::Display for GLWEPlaintext { } } -pub trait GLWEPlaintextAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext> { +impl GLWEPlaintext> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { GLWEPlaintext { - data: VecZnx::alloc( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ), + data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, } } - fn alloc_glwe_plaintext_from_infos(&self, infos: &A) -> GLWEPlaintext> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - self.alloc_glwe_plaintext(infos.base2k(), infos.k()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k()) } - fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::bytes_of( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ) - } - - fn bytes_of_glwe_plaintext_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_plaintext(infos.base2k(), infos.k()) - } -} - -impl GLWEPlaintextAlloc for Module where Self: GetDegree {} - -impl GLWEPlaintext> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWEPlaintextAlloc, - { - module.alloc_glwe_plaintext_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: GLWEPlaintextAlloc, - { - module.alloc_glwe_plaintext(base2k, k) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWEPlaintextAlloc, - { - module.bytes_of_glwe_plaintext_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize - where - M: GLWEPlaintextAlloc, - { - module.bytes_of_glwe_plaintext(base2k, k) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize) } } diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 2d4ade1..b99a78e 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -1,15 +1,12 @@ use poulpy_hal::{ - layouts::{ - Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, - ZnxZero, - }, + layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ GetDistribution, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -77,67 +74,30 @@ impl GLWEInfos for GLWESecret { } } -pub trait GLWESecretAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret> { +impl GLWESecret> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.rank()) + } + + pub fn alloc(n: Degree, rank: Rank) -> Self { GLWESecret { - data: ScalarZnx::alloc(self.ring_degree().into(), rank.into()), + data: ScalarZnx::alloc(n.into(), rank.into()), dist: Distribution::NONE, } } - fn alloc_glwe_secret_from_infos(&self, infos: &A) -> GLWESecret> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - self.alloc_glwe_secret(infos.rank()) + Self::bytes_of(infos.n(), infos.rank()) } - fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { - ScalarZnx::bytes_of(self.ring_degree().into(), rank.into()) - } - - fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_secret(infos.rank()) - } -} - -impl GLWESecretAlloc for Module where Self: GetDegree {} - -impl GLWESecret> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWESecretAlloc, - { - module.alloc_glwe_secret_from_infos(infos) - } - - pub fn alloc(module: &M, rank: Rank) -> Self - where - M: GLWESecretAlloc, - { - module.alloc_glwe_secret(rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWESecretAlloc, - { - module.bytes_of_glwe_secret_from_infos(infos) - } - - pub fn bytes_of(module: &M, rank: Rank) -> usize - where - M: GLWESecretAlloc, - { - module.bytes_of_glwe_secret(rank) + pub fn bytes_of(n: Degree, rank: Rank) -> usize { + ScalarZnx::bytes_of(n.into(), rank.into()) } } diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 3aea241..45fa6df 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, }; use std::fmt; @@ -132,90 +132,67 @@ impl WriterTo for GLWEToLWESwitchingKey { } } -pub trait GLWEToLWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_glwe_to_lwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> GLWEToLWESwitchingKey> { - GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) - } - - fn alloc_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKey> - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKey" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKey" - ); - self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) - } - - fn bytes_of_glwe_to_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) - } - - fn bytes_of_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKey" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKey" - ); - self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) - } -} - -impl GLWEToLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - impl GLWEToLWESwitchingKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyAlloc, { - module.alloc_glwe_to_lwe_switching_key_from_infos(infos) + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKey" + ); + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + ) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self - where - M: GLWEToLWESwitchingKeyAlloc, - { - module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { + GLWEToLWESwitchingKey(GLWESwitchingKey::alloc( + n, + base2k, + k, + rank_in, + Rank(1), + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyAlloc, { - module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKey" + ); + Self::bytes_of( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + ) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize - where - M: GLWEToLWESwitchingKeyAlloc, - { - module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + GLWESwitchingKey::bytes_of(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index aed2807..0900d78 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -1,7 +1,7 @@ use std::fmt; use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, source::Source, }; @@ -125,8 +125,15 @@ where } } -pub trait LWEAlloc { - fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE> { +impl LWE> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: LWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { LWE { data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), k, @@ -134,57 +141,16 @@ pub trait LWEAlloc { } } - fn alloc_lwe_from_infos(&self, infos: &A) -> LWE> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: LWEInfos, { - self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k()) } - fn bytes_of_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) } - - fn bytes_of_lwe_from_infos(&self, infos: &A) -> usize - where - A: LWEInfos, - { - self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k()) - } -} - -impl LWEAlloc for Module {} - -impl LWE> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: LWEInfos, - M: LWEAlloc, - { - module.alloc_lwe_from_infos(infos) - } - - pub fn alloc(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWEAlloc, - { - module.alloc_lwe(n, base2k, k) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: LWEInfos, - M: LWEAlloc, - { - module.bytes_of_lwe_from_infos(infos) - } - - pub fn bytes_of(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize - where - M: LWEAlloc, - { - module.bytes_of_lwe(n, base2k, k) - } } pub trait LWEToRef { diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index e449da6..25ce059 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -1,13 +1,13 @@ use std::fmt; use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -101,94 +101,65 @@ impl GGLWEInfos for LWESwitchingKey { } } -pub trait LWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey> { - LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) - } - - fn alloc_lwe_switching_key_from_infos(&self, infos: &A) -> LWESwitchingKey> - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) - } - - fn bytes_of_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) - } - - fn bytes_of_lwe_switching_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) - } -} - -impl LWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - impl LWESwitchingKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: LWESwitchingKeyAlloc, { - module.alloc_lwe_switching_key_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self - where - M: LWESwitchingKeyAlloc, - { - module.alloc_lwe_switching_key(base2k, k, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { + LWESwitchingKey(GLWESwitchingKey::alloc( + n, + base2k, + k, + Rank(1), + Rank(1), + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: LWESwitchingKeyAlloc, { - module.bytes_of_glwe_switching_key_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.dnum()) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize - where - M: LWESwitchingKeyAlloc, - { - module.bytes_of_lwe_switching_key(base2k, k, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_pt.rs index 01ae19b..966ceb5 100644 --- a/poulpy-core/src/layouts/lwe_pt.rs +++ b/poulpy-core/src/layouts/lwe_pt.rs @@ -1,6 +1,6 @@ use std::fmt; -use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Zn, ZnToMut, ZnToRef, ZnxInfos}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, Zn, ZnToMut, ZnToRef, ZnxInfos}; use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision}; @@ -52,40 +52,21 @@ impl LWEInfos for LWEPlaintext { } } -pub trait LWEPlaintextAlloc { - fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext> { +impl LWEPlaintext> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: LWEInfos, + { + Self::alloc(infos.base2k(), infos.k()) + } + + pub fn alloc(base2k: Base2K, k: TorusPrecision) -> Self { LWEPlaintext { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, base2k, } } - - fn alloc_lwe_plaintext_from_infos(&self, infos: &A) -> LWEPlaintext> - where - A: LWEInfos, - { - self.alloc_lwe_plaintext(infos.base2k(), infos.k()) - } -} - -impl LWEPlaintextAlloc for Module {} - -impl LWEPlaintext> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: LWEInfos, - M: LWEPlaintextAlloc, - { - module.alloc_lwe_plaintext_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWEPlaintextAlloc, - { - module.alloc_lwe_plaintext(base2k, k) - } } impl fmt::Display for LWEPlaintext { diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index b5f019f..78ad9c7 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, + layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, source::Source, }; @@ -13,8 +13,8 @@ pub struct LWESecret { pub(crate) dist: Distribution, } -pub trait LWESecretAlloc { - fn alloc_lwe_secret(&self, n: Degree) -> LWESecret> { +impl LWESecret> { + pub fn alloc(n: Degree) -> Self { LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, @@ -22,17 +22,6 @@ pub trait LWESecretAlloc { } } -impl LWESecretAlloc for Module {} - -impl LWESecret> { - pub fn alloc(module: &M, n: Degree) -> Self - where - M: LWESecretAlloc, - { - module.alloc_lwe_secret(n) - } -} - impl LWESecret { pub fn raw(&self) -> &[i64] { self.data.at(0, 0) diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index 1427447..26fb7f9 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -1,13 +1,13 @@ use std::fmt; use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -131,91 +131,68 @@ impl WriterTo for LWEToGLWESwitchingKey { } } -pub trait LWEToGLWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_lwe_to_glwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_out: Rank, - dnum: Dnum, - ) -> LWEToGLWESwitchingKey> { - LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) - } - - fn alloc_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKey> - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - - self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) - } - - fn bytes_of_lwe_to_glwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) - } - - fn bytes_of_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) - } -} - -impl LWEToGLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - impl LWEToGLWESwitchingKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyAlloc, { - module.alloc_lwe_to_glwe_switching_key_from_infos(infos) + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_out(), + infos.dnum(), + ) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self - where - M: LWEToGLWESwitchingKeyAlloc, - { - module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { + LWEToGLWESwitchingKey(GLWESwitchingKey::alloc( + n, + base2k, + k, + Rank(1), + rank_out, + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyAlloc, { - module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos) + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + Self::bytes_of( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_out(), + infos.dnum(), + ) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize - where - M: LWEToGLWESwitchingKeyAlloc, - { - module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } } diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 687b657..dc32d57 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -1,31 +1,21 @@ use poulpy_hal::{ api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace}, - layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero}, + layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero}, }; use crate::decryption::GLWEDecrypt; -use crate::layouts::{ - GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, - prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, -}; +use crate::layouts::{GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::GLWESecretPreparedToRef}; impl GGLWE { - pub fn assert_noise( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared, - pt_want: &ScalarZnx, - max_noise: f64, - ) where - DataSk: DataRef, - DataWant: DataRef, + pub fn assert_noise(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, M: GGLWENoise, Scratch: ScratchTakeBasic, { module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - - } pub trait GGLWENoise { @@ -34,7 +24,7 @@ pub trait GGLWENoise { R: GGLWEToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - Scratch: ScratchTakeBasic; + Scratch: ScratchTakeBasic; } impl GGLWENoise for Module @@ -49,7 +39,7 @@ where S: GLWESecretPreparedToRef, P: ScalarZnxToRef, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeBasic, + Scratch: ScratchAvailable + ScratchTakeBasic, { let res: &GGLWE<&[u8]> = &res.to_ref(); @@ -57,7 +47,7 @@ where let base2k: usize = res.base2k().into(); let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); (0..res.rank_in().into()).for_each(|col_i| { (0..res.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 13531e3..7adeeca 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -1,12 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, - VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace, + ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAlloc, + VecZnxBigNormalize, VecZnxDftAlloc, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, - layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, + layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, }; use crate::decryption::GLWEDecrypt; @@ -14,32 +11,21 @@ use crate::layouts::prepared::GLWESecretPreparedToRef; use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl GGSW { - pub fn assert_noise( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared, - pt_want: &ScalarZnx, - max_noise: F, - ) where - DataSk: DataRef, - DataScalar: DataRef, + pub fn assert_noise(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: F) + where + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, M: GGSWNoise, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64, { module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise); } - pub fn print_noise( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared, - pt_want: &ScalarZnx, - ) where - DataSk: DataRef, - DataScalar: DataRef, + pub fn print_noise(&self, module: &M, sk_prepared: &S, pt_want: &P) + where + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, M: GGSWNoise, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { module.ggsw_print_noise(self, sk_prepared, pt_want); } @@ -51,43 +37,31 @@ pub trait GGSWNoise { R: GGSWToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64; fn ggsw_print_noise(&self, res: &R, sk_prepared: &S, pt_want: &P) where R: GGSWToRef, S: GLWESecretPreparedToRef, - P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow; + P: ScalarZnxToRef; } impl GGSWNoise for Module where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc + Module: GLWEDecrypt + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA + + VecZnxBigAlloc + VecZnxAddScalarInplace - + VecZnxSubInplace - + GLWEDecrypt, + + VecZnxIdftApplyTmpA + + VecZnxSubInplace, Scratch: ScratchTakeBasic, + ScratchOwned: ScratchOwnedBorrow + ScratchOwnedAlloc, { fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) where R: GGSWToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64, { let res: &GGSW<&[u8]> = &res.to_ref(); @@ -96,8 +70,8 @@ where let base2k: usize = res.base2k().into(); let dsize: usize = res.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); let mut pt_dft: VecZnxDft, BE> = self.vec_znx_dft_alloc(1, res.size()); let mut pt_big: VecZnxBig, BE> = self.vec_znx_big_alloc(1, res.size()); @@ -147,7 +121,6 @@ where R: GGSWToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { let res: &GGSW<&[u8]> = &res.to_ref(); let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref(); @@ -155,16 +128,16 @@ where let base2k: usize = res.base2k().into(); let dsize: usize = res.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); let mut pt_dft: VecZnxDft, BE> = self.vec_znx_dft_alloc(1, res.size()); let mut pt_big: VecZnxBig, BE> = self.vec_znx_big_alloc(1, res.size()); let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes()); - (0..(res.rank() + 1).into()).for_each(|col_j| { - (0..res.dnum().into()).for_each(|row_i| { + for col_j in 0..(res.rank() + 1).into() { + for row_i in 0..res.dnum().into() { self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0); // mul with sk[col_j-1] @@ -194,7 +167,7 @@ where let std_pt: f64 = pt_have.data.std(base2k, 0).log2(); println!("col: {col_j} row: {row_i}: {std_pt}"); pt.data.zero(); - }); - }); + } + } } } diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index b89bdc6..7c6979e 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -1,19 +1,12 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, - }, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubInplace}, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; use crate::{ + ScratchTakeCore, decryption::GLWEDecrypt, - layouts::{ - GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, - prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, - }, + layouts::{GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef}, }; impl GLWE { @@ -25,75 +18,15 @@ impl GLWE { { module.glwe_noise(self, sk_prepared, pt_want, scratch) } - // pub fn noise( - // &self, - // module: &Module, - // sk_prepared: &GLWESecretPrepared, - // pt_want: &GLWEPlaintext, - // scratch: &mut Scratch, - // ) -> f64 - // where - // DataSk: DataRef, - // DataPt: DataRef, - // B: Backend, - // Module: VecZnxDftApply - // + VecZnxSubInplace - // + VecZnxNormalizeInplace - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxBigAddInplace - // + VecZnxBigAddSmallInplace - // + VecZnxBigNormalize, - // Scratch:, - // { - // let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - // self.decrypt(module, &mut pt_have, sk_prepared, scratch); - // module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); - // module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch); - // pt_have.data.std(self.base2k().into(), 0).log2() - // } - pub fn assert_noise( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared, - pt_want: &GLWEPlaintext, - max_noise: f64, - ) where - DataSk: DataRef, - DataPt: DataRef, + pub fn assert_noise(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + S: GLWESecretPreparedToRef, + P: GLWEPlaintextToRef, M: GLWENoise, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - - // pub fn assert_noise( - // &self, - // module: &Module, - // sk_prepared: &GLWESecretPrepared, - // pt_want: &GLWEPlaintext, - // max_noise: f64, - // ) where - // DataSk: DataRef, - // DataPt: DataRef, - // Module: VecZnxDftBytesOf - // + VecZnxBigBytesOf - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxBigAddInplace - // + VecZnxBigAddSmallInplace - // + VecZnxBigNormalize - // + VecZnxNormalizeTmpBytes - // + VecZnxSubInplace - // + VecZnxNormalizeInplace, - // B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - // { - // let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); - // let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); - // assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); - // } } pub trait GLWENoise { @@ -107,29 +40,14 @@ pub trait GLWENoise { where R: GLWEToRef, S: GLWESecretPreparedToRef, - P: GLWEPlaintextToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow; + P: GLWEPlaintextToRef; } impl GLWENoise for Module where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxNormalizeInplace - + GLWEDecrypt, - Scratch: ScratchTakeBasic - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchOwnedBorrowImpl - + ScratchOwnedBorrow, + Module: GLWEDecrypt + VecZnxSubInplace + VecZnxNormalizeInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { fn glwe_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 where @@ -141,7 +59,7 @@ where let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.to_ref(); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res_ref); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res_ref); self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch); self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); self.vec_znx_normalize_inplace(res_ref.base2k().into(), &mut pt_have.data, 0, scratch); @@ -153,7 +71,6 @@ where R: GLWEToRef, S: GLWESecretPreparedToRef, P: GLWEPlaintextToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { let res: &GLWE<&[u8]> = &res.to_ref(); let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index c770311..ddaaf9c 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,5 +1,4 @@ -use poulpy_backend::FFT64Ref; -use poulpy_hal::{api::ModuleNew, layouts::Module, test_suite::serialization::test_reader_writer_interface}; +use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, @@ -21,143 +20,123 @@ const DSIZE: Dsize = Dsize(1); #[test] fn glwe_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GLWE> = GLWE::alloc(&module, BASE2K, K, RANK); + let original: GLWE> = GLWE::alloc(N_GLWE, BASE2K, K, RANK); poulpy_hal::test_suite::serialization::test_reader_writer_interface(original); } #[test] fn glwe_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GLWECompressed> = GLWECompressed::alloc(&module, BASE2K, K, RANK); + let original: GLWECompressed> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK); test_reader_writer_interface(original); } #[test] fn lwe_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWE> = LWE::alloc(&module, N_LWE, BASE2K, K); + let original: LWE> = LWE::alloc(N_LWE, BASE2K, K); test_reader_writer_interface(original); } #[test] fn lwe_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWECompressed> = LWECompressed::alloc(&module, BASE2K, K); + let original: LWECompressed> = LWECompressed::alloc(BASE2K, K); test_reader_writer_interface(original); } #[test] fn test_gglwe_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GGLWE> = GGLWE::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_gglwe_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GGLWECompressed> = GGLWECompressed::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWECompressed> = GGLWECompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GLWESwitchingKey> = GLWESwitchingKey::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey> = GLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: GLWESwitchingKeyCompressed> = - GLWESwitchingKeyCompressed::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + GLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: AutomorphismKey> = AutomorphismKey::alloc_with(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKey> = AutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: TensorKey> = TensorKey::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: GLWEToLWESwitchingKeyCompressed> = - GLWEToLWESwitchingKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM); + GLWEToLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM); + let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: LWEToGLWESwitchingKeyCompressed> = - LWEToGLWESwitchingKeyCompressed::alloc_with(&module, BASE2K, K, RANK, DNUM); + LWEToGLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWESwitchingKey> = LWESwitchingKey::alloc(&module, BASE2K, K, DNUM); + let original: LWESwitchingKey> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(&module, BASE2K, K, DNUM); + let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn ggsw_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GGSW> = GGSW::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn ggsw_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GGSWCompressed> = GGSWCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSWCompressed> = GGSWCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs index 81b727f..487f1f7 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -1,21 +1,17 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, - VecZnxAutomorphism,VecZnxFillUniform, - }, - layouts::{Backend, Module, Scratch, ScratchOwned, GaloisElement}, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphism, VecZnxFillUniform}, + layouts::{Backend, GaloisElement, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared, - AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, - GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKeyAlloc, GLWESwitchingKeyDecompress + AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare, + GLWESecretPreparedAlloc, GLWESwitchingKeyDecompress, compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared, }, noise::GGLWENoise, - GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore }; pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) @@ -24,7 +20,6 @@ where + GGLWEKeyswitch + GLWESecretPreparedAlloc + GLWESecretPrepare - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + GLWESwitchingKeyDecompress @@ -32,7 +27,7 @@ where + VecZnxFillUniform + VecZnxAutomorphism, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 60; @@ -51,17 +46,15 @@ where rank: rank.into(), }; - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(module, &atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); 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 = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( - module, &atk_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -101,7 +94,6 @@ where + GGLWEKeyswitch + GLWESecretPreparedAlloc + GLWESecretPrepare - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + AutomorphismKeyDecompress @@ -109,7 +101,7 @@ where + VecZnxFillUniform + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 60; @@ -128,16 +120,14 @@ where rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(module, &atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( - module, &atk_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -159,7 +149,7 @@ where let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); sk_out_prepared.prepare(module, &sk_out); - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(module, &atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs index ba19219..06167d1 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -1,15 +1,19 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - prepared::{GGLWEPrepare, GGLWEPreparedAlloc, GLWESecretPrepared}, GGLWELayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress - }, noise::GGLWENoise, GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore + GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, + decryption::GLWEDecrypt, + encryption::SIGMA, + layouts::{ + GGLWELayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyCompressed, + GLWESwitchingKeyDecompress, + prepared::{GGLWEPrepare, GGLWEPreparedAlloc, GLWESecretPrepared}, + }, + noise::GGLWENoise, }; pub fn test_gglwe_switching_key_encrypt_sk(module: &Module) @@ -21,20 +25,19 @@ where + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk + VecZnxFillUniform + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { + let n = module.n(); let base2k: usize = 12; let k_ksk: usize = 54; let dsize: usize = k_ksk / base2k; for rank_in in 1_usize..3 { for rank_out in 1_usize..3 { for di in 1_usize..dsize + 1 { - let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); let gglwe_infos: GGLWELayout = GGLWELayout { @@ -47,21 +50,19 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); 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 = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes( - module, - &gglwe_infos, - )); + let mut scratch: ScratchOwned = + ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_infos)); - let mut sk_in: GLWESecret> = GLWESecret::alloc(module, rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc(module, rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); sk_out_prepared.prepare(module, &sk_out); @@ -91,7 +92,6 @@ where + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + GLWESwitchingKeyDecompress @@ -100,13 +100,13 @@ where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { + let n: usize = module.n(); let base2k: usize = 12; let k_ksk: usize = 54; let dsize: usize = k_ksk / base2k; for rank_in in 1_usize..3 { for rank_out in 1_usize..3 { for di in 1_usize..dsize + 1 { - let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); let gglwe_infos: GGLWELayout = GGLWELayout { @@ -120,7 +120,7 @@ where }; let mut ksk_compressed: GLWESwitchingKeyCompressed> = - GLWESwitchingKeyCompressed::alloc_from_infos(module, &gglwe_infos); + GLWESwitchingKeyCompressed::alloc_from_infos(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -130,10 +130,10 @@ where &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc(module, rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc(module, rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); sk_out_prepared.prepare(module, &sk_out); @@ -149,7 +149,7 @@ where scratch.borrow(), ); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs index 1d9d701..0f07c32 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -9,8 +9,8 @@ use crate::{ decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - GLWE, GLWEAlloc, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPrepare, - GLWEPublicKeyPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, + GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPrepare, GLWEPublicKeyPreparedAlloc, + GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, compressed::GLWECompressed, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, }, @@ -18,13 +18,8 @@ use crate::{ pub fn test_glwe_encrypt_sk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptSk - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare - + VecZnxFillUniform - + GLWESub, + Module: + GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -48,9 +43,9 @@ where k: k_pt.into(), }; - let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -59,7 +54,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); @@ -89,8 +84,7 @@ where pub fn test_glwe_compressed_encrypt_sk(module: &Module) where - Module: GLWEAlloc - + GLWECompressedEncryptSk + Module: GLWECompressedEncryptSk + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare @@ -120,10 +114,10 @@ where k: k_pt.into(), }; - let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(module, &glwe_infos); + let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -133,7 +127,7 @@ where GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); @@ -152,7 +146,7 @@ where scratch.borrow(), ); - let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.decompress(module, &ct_compressed); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); @@ -172,13 +166,8 @@ where pub fn test_glwe_encrypt_zero_sk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptSk - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare - + VecZnxFillUniform - + GLWESub, + Module: + GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -195,7 +184,7 @@ where rank: rank.into(), }; - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]); @@ -204,13 +193,13 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); - let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.encrypt_zero_sk( module, @@ -227,8 +216,7 @@ where pub fn test_glwe_encrypt_pk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptPk + Module: GLWEEncryptPk + GLWEPublicKeyPrepare + GLWEPublicKeyPreparedAlloc + GLWEPublicKeyGenerate @@ -253,9 +241,9 @@ where rank: rank.into(), }; - let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -265,13 +253,13 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); - let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(module, &glwe_infos); + let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(&glwe_infos); pk.generate(module, &sk_prepared, &mut source_xa, &mut source_xe); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs index c7f5e2e..647f68e 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -1,19 +1,26 @@ use poulpy_hal::{ api::{ - ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxSwitchRing - }, layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, oep::{VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl}, source::Source + ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize, + VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, + VecZnxSwitchRing, + }, + layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, + source::Source, }; use crate::{ - decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - prepared::GLWESecretPrepared, Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyAlloc, TensorKeyCompressed, TensorKeyLayout - }, GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk + GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, + decryption::GLWEDecrypt, + encryption::SIGMA, + layouts::{ + Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyCompressed, + TensorKeyLayout, prepared::GLWESecretPrepared, + }, }; pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where Module: TensorKeyEncryptSk - + TensorKeyAlloc + GLWESecretPrepare + GLWESecretPreparedAlloc + GLWEDecrypt @@ -43,18 +50,15 @@ where rank: rank.into(), }; - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); 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 = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( - module, - &tensor_key_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); @@ -67,11 +71,11 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { @@ -92,9 +96,7 @@ where scratch.borrow(), ); for row_i in 0..dnum { - let ct = tensor_key - .at(i, j) - .at(row_i, 0); + let ct = tensor_key.at(i, j).at(row_i, 0); ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow()); @@ -111,20 +113,19 @@ where pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where Module: TensorKeyEncryptSk - + TensorKeyAlloc - + GLWESecretPrepare - + GLWESecretPreparedAlloc - + GGLWETensorKeyCompressedEncryptSk - + GLWEDecrypt - + VecZnxDftAlloc - + VecZnxBigAlloc - + VecZnxDftApply - + SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxSubScalarInplace - + VecZnxFillUniform - + VecZnxCopy - + VecZnxSwitchRing, + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GGLWETensorKeyCompressedEncryptSk + + GLWEDecrypt + + VecZnxDftAlloc + + VecZnxBigAlloc + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxSubScalarInplace + + VecZnxFillUniform + + VecZnxCopy + + VecZnxSwitchRing, // + VecZnxNormalizeInplaceImpl // + VecZnxNormalizeImpl, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -145,7 +146,7 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -155,7 +156,7 @@ where &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); @@ -164,14 +165,14 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank {