Removed traits on structs not dependent on Module API
jump to
@@ -61,9 +61,9 @@ k: k_ct_in,
rank, }; - let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&module, &ggsw_layout); - let mut ct_glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_in_layout); - let mut ct_glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_out_layout); + let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_layout); let pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(@@ -76,7 +76,7 @@ let mut source_xs = Source::new([0u8; 32]);
let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&module, &glwe_in_layout); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank);@@ -165,8 +165,8 @@ k: k_glwe,
rank, }; - let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&module, &ggsw_layout); - let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_layout); + let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_layout); let pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(@@ -179,7 +179,7 @@ let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&module, &glwe_layout); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank);
@@ -62,9 +62,9 @@ k: k_glwe_out,
rank, }; - let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&module, &gglwe_atk_layout); - let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_in_layout); - let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_out_layout); + let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_layout); let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout)@@ -81,7 +81,7 @@ let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&module, &glwe_in_layout); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let mut sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank);@@ -177,8 +177,8 @@ k: k_ct,
rank, }; - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&module, &gglwe_layout); - let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_layout); + let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_layout); let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout)@@ -190,13 +190,13 @@ let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&module, &glwe_layout); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let mut sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); sk_in_dft.prepare(&module, &sk_in); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&module, &glwe_layout); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk(
@@ -43,9 +43,9 @@
let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; // Allocates ciphertext & plaintexts - let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_ct_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&module, &glwe_pt_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&module, &glwe_pt_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_ct_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]);@@ -58,7 +58,7 @@ GLWE::encrypt_sk_tmp_bytes(&module, &glwe_ct_infos) | GLWE::decrypt_tmp_bytes(&module, &glwe_ct_infos),
); // Generate secret-key - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&module, &glwe_ct_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret
@@ -6,7 +6,7 @@
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 @@ }
} impl<BE: Backend> LWESampleExtract for Module<BE> where Self: ModuleN {} -impl<BE: Backend> LWEFromGLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> + GLWEAlloc + LWESampleExtract {} +impl<BE: Backend> LWEFromGLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> + LWESampleExtract {} pub trait LWEFromGLWE<BE: Backend> where - Self: GLWEKeyswitch<BE> + GLWEAlloc + LWESampleExtract, + Self: GLWEKeyswitch<BE> + LWESampleExtract, { fn lwe_from_glwe_tmp_bytes<R, A, K>(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize where@@ -59,8 +59,12 @@ k: lwe_infos.k(),
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<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>)
@@ -6,16 +6,16 @@
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<BE: Backend> GLWEFromLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> + GLWEAlloc {} +impl<BE: Backend> GLWEFromLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> {} pub trait GLWEFromLWE<BE: Backend> where - Self: GLWEKeyswitch<BE> + GLWEAlloc, + Self: GLWEKeyswitch<BE>, { fn glwe_from_lwe_tmp_bytes<R, A, K>(&self, glwe_infos: &R, lwe_infos: &A, key_infos: &K) -> usize where@@ -23,7 +23,8 @@ R: GLWEInfos,
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(),
@@ -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 @@ fn automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
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<R, S>(
@@ -11,7 +11,7 @@ SIGMA,
glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, }, layouts::{ - GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintextAlloc, GLWESecretPrepared, LWEInfos, + GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintext, GLWESecretPrepared, LWEInfos, compressed::{GGLWECompressed, GGLWECompressedToMut}, prepared::GLWESecretPreparedToRef, },@@ -82,7 +82,7 @@ A: GGLWEInfos,
{ 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<R, P, S>(
@@ -80,7 +80,7 @@ infos.rank_in(),
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<R, S>(
@@ -85,8 +85,7 @@ fn gglwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
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<R, P, S>(
@@ -82,7 +82,7 @@ GLWESecretPrepared::bytes_of(self, infos.rank_out())
+ 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) }
@@ -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 @@ A: GGSWInfos,
{ 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<R, P, S>(
@@ -55,7 +55,7 @@
// Its ok to allocate scratch space here since pk is usually generated only once. let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); - let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(self, res); + let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); }
@@ -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<R, S1, S2>(
@@ -85,7 +85,7 @@ infos.rank_out().0,
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) }
@@ -75,7 +75,7 @@ infos.rank_in(),
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<R, S1, S2>(
@@ -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 @@ /// * `module`: static backend FFT tables.
/// * `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<A, M, BE: Backend>(module: &M, infos: &A) -> Self + pub fn alloc<A>(infos: &A) -> Self where A: GLWEInfos, - M: GLWEPacking<BE>, { Self { - data: GLWE::alloc_from_infos(module, infos), + data: GLWE::alloc_from_infos(infos), value: false, control: false, }@@ -59,20 +58,18 @@ /// Instantiates a new [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<A, M, BE: Backend>(module: &M, infos: &A, log_batch: usize) -> Self + pub fn alloc<A>(infos: &A, log_batch: usize) -> Self where A: GLWEInfos, - M: GLWEPacking<BE>, { let mut accumulators: Vec<Accumulator> = Vec::<Accumulator>::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 @@ R: GLWEInfos,
K: GGLWEInfos, M: GLWEPacking<BE>, { - 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 @@ + GLWEShift<BE>
+ GLWEAdd + GLWENormalize<BE> + GLWECopy - + GLWEAlloc { }@@ -183,8 +179,7 @@ + GLWESub
+ GLWEShift<BE> + GLWEAdd + GLWENormalize<BE> - + 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)]
@@ -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<BE: Backend> LWEKeySwitch<BE> for Module<BE> where Self: LWEKeySwitch<BE> {}
pub trait LWEKeySwitch<BE: Backend> where - Self: GLWEKeyswitch<BE> + GLWEAlloc + LWESampleExtract, + Self: GLWEKeyswitch<BE> + LWESampleExtract, { fn lwe_keyswitch_tmp_bytes<R, A, K>(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where@@ -51,21 +51,21 @@ {
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
@@ -6,8 +6,7 @@
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,13 @@ write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key)
} } -impl<BE: Backend> AutomorphismKeyCompressedAlloc for Module<BE> where Self: GLWESwitchingKeyCompressedAlloc {} - -pub trait AutomorphismKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_automorphism_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> AutomorphismKeyCompressed<Vec<u8>> { - AutomorphismKeyCompressed { - key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), - p: 0, - } - } - - fn alloc_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> AutomorphismKeyCompressed<Vec<u8>> +impl AutomorphismKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, { - assert_eq!(infos.rank_in(), infos.rank_out()); - self.alloc_automorphism_key_compressed( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank(),@@ -120,23 +100,19 @@ infos.dsize(),
) } - 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) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { + AutomorphismKeyCompressed { + key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), + p: 0, + } } - fn bytes_of_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!(infos.rank_in(), infos.rank_out()); - self.bytes_of_automorphism_key_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(),@@ -144,37 +120,9 @@ infos.dnum(),
infos.dsize(), ) } -} -impl AutomorphismKeyCompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: AutomorphismKeyCompressedAlloc, - { - module.alloc_automorphism_key_compressed_from_infos(infos) - } - - pub fn alloc<M>(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<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: AutomorphismKeyCompressedAlloc, - { - module.bytes_of_automorphism_key_compressed_from_infos(infos) - } - - pub fn bytes_of<M>(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) } }
@@ -7,7 +7,7 @@ source::Source,
}; 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 @@ )
} } -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<Vec<u8>> { +impl GGLWECompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(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 @@ );
GGLWECompressed { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), 1,@@ -148,22 +152,21 @@ seed: vec![[0u8; 32]; (dnum.0 * rank_in.0) as usize],
} } - fn alloc_gglwe_compressed_from_infos<A>(&self, infos: &A) -> GGLWECompressed<Vec<u8>> + pub fn bytes_of_from_infos<A>(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,68 +182,12 @@ dsize.0,
); 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<A>(&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<B: Backend> GGLWECompressedAlloc for Module<B> where Self: GetDegree {} - -impl GGLWECompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GGLWECompressedAlloc, - { - module.alloc_gglwe_compressed_from_infos(infos) - } - - pub fn alloc<M>( - 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<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GGLWECompressedAlloc, - { - module.bytes_of_gglwe_compressed_from_infos(infos) - } - - pub fn byte_of<M>(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) } }
@@ -6,7 +6,7 @@
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<BE: Backend> GLWESwitchingKeyCompressedAlloc for Module<BE> 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<Vec<u8>> { - 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<A>(&self, infos: &A) -> GLWESwitchingKeyCompressed<Vec<u8>> +impl GLWESwitchingKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(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 @@ infos.dsize(),
) } - 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) - } - - fn bytes_of_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.bytes_of_gglwe_compressed_from_infos(infos) + 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, + } } -} -impl GLWESwitchingKeyCompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, - M: GLWESwitchingKeyCompressedAlloc, { - module.alloc_glwe_switching_key_compressed_from_infos(infos) + GGLWECompressed::bytes_of_from_infos(infos) } - pub fn alloc<M>( - 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<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_glwe_switching_key_compressed_from_infos(infos) - } - - pub fn bytes_of<M>(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) } }
@@ -6,8 +6,7 @@
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,13 @@ Ok(())
} } -impl<BE: Backend> TensorKeyCompressedAlloc for Module<BE> where Self: GLWESwitchingKeyCompressedAlloc {} - -pub trait TensorKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_tensor_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> TensorKeyCompressed<Vec<u8>> { - 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)) - .collect(), - } - } - - fn alloc_tensor_key_compressed_from_infos<A>(&self, infos: &A) -> TensorKeyCompressed<Vec<u8>> +impl TensorKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self 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::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank(),@@ -122,16 +96,21 @@ infos.dsize(),
) } - fn bytes_of_tensor_key_compressed(&self, 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) + 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(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } } - fn bytes_of_tensor_key_compressed_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, { - self.bytes_of_tensor_key_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(),@@ -139,37 +118,10 @@ infos.dnum(),
infos.dsize(), ) } -} -impl TensorKeyCompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: TensorKeyCompressedAlloc, - { - module.alloc_tensor_key_compressed_from_infos(infos) - } - - pub fn alloc<M>(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<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: TensorKeyCompressedAlloc, - { - module.bytes_of_tensor_key_compressed_from_infos(infos) - } - - pub fn bytes_of<M>(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) + 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 * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize) } }
@@ -6,7 +6,7 @@ source::Source,
}; 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 @@ self.data.fill_uniform(log_bound, source);
} } -impl<BE: Backend> GGSWCompressedAlloc for Module<BE> where Self: GetDegree {} +impl GGSWCompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(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<Vec<u8>> { + 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 @@ );
GGSWCompressed { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), 1,@@ -140,11 +142,12 @@ seed: Vec::new(),
} } - fn alloc_ggsw_compressed_from_infos<A>(&self, infos: &A) -> GGSWCompressed<Vec<u8>> + pub fn bytes_of_from_infos<A>(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 @@ infos.dsize(),
) } - 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,57 +172,12 @@ dsize.0,
); 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<A>(&self, infos: &A) -> usize - where - A: GGSWInfos, - { - self.bytes_of_ggsw_compressed( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGSWCompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGSWInfos, - M: GGSWCompressedAlloc, - { - module.alloc_ggsw_compressed_from_infos(infos) - } - - pub fn alloc<M>(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<A, M>(module: &M, infos: &A) -> usize - where - A: GGSWInfos, - M: GGSWCompressedAlloc, - { - module.bytes_of_ggsw_compressed_key_from_infos(infos) - } - - pub fn bytes_of<M>(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) } }
@@ -88,17 +88,17 @@ self.data.fill_uniform(log_bound, source);
} } -pub trait GLWECompressedAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed<Vec<u8>> { +impl GLWECompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(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 @@ seed: [0u8; 32],
} } - fn alloc_glwe_compressed_from_infos<A>(&self, infos: &A) -> GLWECompressed<Vec<u8>> + pub fn bytes_of_from_infos<A>(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<A>(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - assert_eq!(self.ring_degree(), infos.n()); - self.bytes_of_glwe_compressed(infos.base2k(), infos.k()) - } -} - -impl<B: Backend> GLWECompressedAlloc for Module<B> where Self: GetDegree {} - -impl GLWECompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWECompressedAlloc, - { - module.alloc_glwe_compressed_from_infos(infos) - } - - pub fn alloc<M>(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<A, M>(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWECompressedAlloc, - { - module.bytes_of_glwe_compressed_from_infos(infos) - } - - pub fn bytes_of<M>(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) } }
@@ -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,21 +87,8 @@ self.0.write_to(writer)
} } -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<Vec<u8>> { - 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<A>(&self, infos: &A) -> GLWEToLWESwitchingKeyCompressed<Vec<u8>> +impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, {@@ -116,20 +102,28 @@ 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()) + Self::alloc( + infos.n(), + 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)) + 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), + )) } - fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, {@@ -143,41 +137,11 @@ infos.dsize().0,
1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - self.bytes_of_glwe_switching_key_compressed_from_infos(infos) - } -} - -impl<B: Backend> GLWEToLWESwitchingKeyCompressedAlloc for Module<B> where Self: GLWESwitchingKeyCompressedAlloc {} - -impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GLWEToLWESwitchingKeyCompressedAlloc, - { - module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) - } - - pub fn alloc<M>(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) + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GLWEToLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) - } - - pub fn bytes_of<M>(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)) } }
@@ -62,8 +62,15 @@ self.data.fill_uniform(log_bound, source);
} } -pub trait LWECompressedAlloc { - fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed<Vec<u8>> { +impl LWECompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(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,56 +79,15 @@ seed: [0u8; 32],
} } - fn alloc_lwe_compressed_from_infos<A>(&self, infos: &A) -> LWECompressed<Vec<u8>> + pub fn bytes_of_from_infos<A>(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<A>(&self, infos: &A) -> usize - where - A: LWEInfos, - { - self.bytes_of_lwe_compressed(infos.base2k(), infos.k()) - } -} - -impl<B: Backend> LWECompressedAlloc for Module<B> {} - -impl LWECompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: LWEInfos, - M: LWECompressedAlloc, - { - module.alloc_lwe_compressed_from_infos(infos) - } - - pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWECompressedAlloc, - { - module.alloc_lwe_compressed(base2k, k) - } - - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize - where - A: LWEInfos, - M: LWECompressedAlloc, - { - module.bytes_of_lwe_compressed_from_infos(infos) - } - - pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> usize - where - M: LWECompressedAlloc, - { - module.bytes_of_lwe_compressed(base2k, k) } }
@@ -6,8 +6,7 @@
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,20 +84,8 @@ self.0.write_to(writer)
} } -pub trait LWESwitchingKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_lwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - dnum: Dnum, - ) -> LWESwitchingKeyCompressed<Vec<u8>> { - LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) - } - - fn alloc_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> LWESwitchingKeyCompressed<Vec<u8>> +impl LWESwitchingKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, {@@ -117,14 +104,22 @@ 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()) + Self::alloc(infos.n(), 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)) + 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), + )) } - fn bytes_of_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, {@@ -143,41 +138,11 @@ infos.rank_out().0,
1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - self.bytes_of_glwe_switching_key_compressed_from_infos(infos) + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } -} -impl<B: Backend> LWESwitchingKeyCompressedAlloc for Module<B> where Self: GLWESwitchingKeyCompressedAlloc {} - -impl LWESwitchingKeyCompressed<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: LWESwitchingKeyCompressedAlloc, - { - module.alloc_lwe_switching_key_compressed_from_infos(infos) - } - - pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self - where - M: LWESwitchingKeyCompressedAlloc, - { - module.alloc_lwe_switching_key_compressed(base2k, k, dnum) - } - - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: LWESwitchingKeyCompressedAlloc, - { - module.bytes_of_lwe_switching_key_compressed_from_infos(infos) - } - - pub fn bytes_of<M>(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)) } }
@@ -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,23 +85,8 @@ self.0.write_to(writer)
} } -impl<BE: Backend> LWEToGLWESwitchingKeyCompressedAlloc for Module<BE> 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<Vec<u8>> { - 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<A>(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed<Vec<u8>> +impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, {@@ -116,14 +100,28 @@ 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()) + Self::alloc( + infos.n(), + 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)) + 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), + )) } - fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, {@@ -137,39 +135,11 @@ 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<Vec<u8>> { - pub fn alloc<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: LWEToGLWESwitchingKeyCompressedAlloc, - { - module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } - pub fn alloc_with<M>(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 bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: LWEToGLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) - } - - pub fn bytes_of<M>(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)) } }
@@ -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 @@ write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key)
} } -impl<B: Backend> AutomorphismKeyAlloc for Module<B> 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<Vec<u8>> { - AutomorphismKey { - key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize), - p: 0, - } - } - - fn alloc_automorphism_key_from_infos<A>(&self, infos: &A) -> AutomorphismKey<Vec<u8>> +impl AutomorphismKey<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, { - self.alloc_automorphism_key( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank(),@@ -177,11 +159,14 @@ infos.dsize(),
) } - 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<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, {@@ -190,7 +175,8 @@ infos.rank_in(),
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 @@ infos.dnum(),
infos.dsize(), ) } -} -impl AutomorphismKey<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: AutomorphismKeyAlloc, - { - module.alloc_automorphism_key_from_infos(infos) - } - - pub fn alloc_with<M>(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<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: AutomorphismKeyAlloc, - { - module.bytes_of_automorphism_key_from_infos(infos) - } - - pub fn bytes_of<M>(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) } }
@@ -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 @@ }
} } -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<Vec<u8>> { +impl GGLWE<Vec<u8>> { + pub fn alloc_from_infos<A>(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 @@ );
GGLWE { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), (rank_out + 1).into(),@@ -232,11 +234,12 @@ dsize,
} } - fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GGLWE<Vec<u8>> + pub fn bytes_of_from_infos<A>(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 @@ infos.dsize(),
) } - fn bytes_of_gglwe( - &self, + pub fn bytes_of( + n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank,@@ -270,76 +273,12 @@ dsize.0,
); 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<A>(&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<B: Backend> GGLWEAlloc for Module<B> where Self: GetDegree {} - -impl GGLWE<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GGLWEAlloc, - { - module.alloc_glwe_from_infos(infos) - } - - pub fn alloc<M>( - 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<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GGLWEAlloc, - { - module.bytes_of_gglwe_from_infos(infos) - } - - pub fn bytes_of<M>( - 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) } }
@@ -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 @@ self.key.fill_uniform(log_bound, source);
} } -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<Vec<u8>> { - 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<A>(&self, infos: &A) -> GLWESwitchingKey<Vec<u8>> +impl GLWESwitchingKey<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, { - self.alloc_glwe_switching_key( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(),@@ -197,23 +178,20 @@ infos.dsize(),
) } - fn bytes_of_glwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize { - self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + 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, + } } - fn bytes_of_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, { - self.bytes_of_glwe_switching_key( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(),@@ -222,55 +200,17 @@ infos.dnum(),
infos.dsize(), ) } -} -impl<B: Backend> GLWESwitchingKeyAlloc for Module<B> where Self: GGLWEAlloc {} - -impl GLWESwitchingKey<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GLWESwitchingKeyAlloc, - { - module.alloc_glwe_switching_key_from_infos(infos) - } - - pub fn alloc<M>( - 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<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GLWESwitchingKeyAlloc, - { - module.bytes_of_glwe_switching_key_from_infos(infos) - } - - pub fn bytes_of<M>( - module: &M, + pub fn bytes_of( + n: Degree, 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) + ) -> usize { + GGLWE::bytes_of(n, base2k, k, rank_in, rank_out, dnum, dsize) } }
@@ -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,8 @@ Ok(())
} } -pub trait TensorKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey<Vec<u8>> { - 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)) - .collect(), - } - } - - fn alloc_tensor_key_from_infos<A>(&self, infos: &A) -> TensorKey<Vec<u8>> +impl TensorKey<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, {@@ -152,7 +140,8 @@ infos.rank_in(),
infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - self.alloc_tensor_key( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank(),@@ -161,12 +150,16 @@ infos.dsize(),
) } - fn bytes_of_tensor_key(&self, 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) + 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(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } } - fn bytes_of_tensor_key_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, {@@ -175,7 +168,8 @@ infos.rank_in(),
infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - self.bytes_of_tensor_key( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(),@@ -183,39 +177,10 @@ infos.dnum(),
infos.dsize(), ) } -} -impl<B: Backend> TensorKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} - -impl TensorKey<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: TensorKeyAlloc, - { - module.alloc_tensor_key_from_infos(infos) - } - - pub fn alloc<M>(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<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: TensorKeyAlloc, - { - module.bytes_of_tensor_key_from_infos(infos) - } - - pub fn bytes_of<M>(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) + 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 * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize) } }
@@ -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<B: Backend> GGSWAlloc for Module<B> where Self: GetDegree {} +impl GGSW<Vec<u8>> { + pub fn alloc_from_infos<A>(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<Vec<u8>> { + 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 @@ );
GGSW { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), (rank + 1).into(),@@ -187,11 +194,12 @@ dsize,
} } - fn alloc_ggsw_from_infos<A>(&self, infos: &A) -> GGSW<Vec<u8>> + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGSWInfos, { - self.alloc_ggsw( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(),@@ -200,7 +208,7 @@ infos.dsize(),
) } - 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,57 +224,12 @@ dsize.0,
); 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<A>(&self, infos: &A) -> usize - where - A: GGSWInfos, - { - self.bytes_of_ggsw( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGSW<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGSWInfos, - M: GGSWAlloc, - { - module.alloc_ggsw_from_infos(infos) - } - - pub fn alloc<M>(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<A, M>(module: &M, infos: &A) -> usize - where - A: GGSWInfos, - M: GGSWAlloc, - { - module.bytes_of_ggsw_from_infos(infos) - } - - pub fn bytes_of<M>(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) } }
@@ -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 @@ self.data.fill_uniform(log_bound, source);
} } -pub trait GLWEAlloc -where - Self: GetDegree, -{ - fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE<Vec<u8>> { - GLWE { - data: VecZnx::alloc( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ), - base2k, - k, - } - } - - fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GLWE<Vec<u8>> - where - A: GLWEInfos, - { - self.alloc_glwe(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<A>(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank()) - } -} - -impl<B: Backend> GLWEAlloc for Module<B> where Self: GetDegree {} - impl GLWE<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GLWEInfos, - M: GLWEAlloc, { - module.alloc_glwe_from_infos(infos) + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - M: GLWEAlloc, - { - module.alloc_glwe(base2k, k, rank) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { + GLWE { + data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), + base2k, + k, + } } - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GLWEInfos, - M: GLWEAlloc, { - module.bytes_of_glwe_from_infos(infos) + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank()) } - pub fn bytes_of<M>(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) } }
@@ -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 @@ self.rank
} } -pub trait GLWEPublicKeyAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey<Vec<u8>> { - GLWEPublicKey { - data: VecZnx::alloc( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ), - base2k, - k, - dist: Distribution::NONE, - } - } - - fn alloc_glwe_public_key_from_infos<A>(&self, infos: &A) -> GLWEPublicKey<Vec<u8>> - where - A: GLWEInfos, - { - self.alloc_glwe_public_key(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<A>(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) - } -} - -impl<B: Backend> GLWEPublicKeyAlloc for Module<B> where Self: GetDegree {} - impl GLWEPublicKey<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GLWEInfos, - M: GLWEPublicKeyAlloc, { - module.alloc_glwe_public_key_from_infos(infos) + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - M: GLWEPublicKeyAlloc, - { - module.alloc_glwe_public_key(base2k, k, rank) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { + GLWEPublicKey { + data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), + base2k, + k, + dist: Distribution::NONE, + } } - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GLWEInfos, - M: GLWEPublicKeyAlloc, { - module.bytes_of_glwe_public_key_from_infos(infos) + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank()) } - pub fn bytes_of<M>(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) } }
@@ -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 @@ )
} } -pub trait GLWEPlaintextAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext<Vec<u8>> { - GLWEPlaintext { - data: VecZnx::alloc( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ), - base2k, - k, - } - } - - fn alloc_glwe_plaintext_from_infos<A>(&self, infos: &A) -> GLWEPlaintext<Vec<u8>> - where - A: GLWEInfos, - { - self.alloc_glwe_plaintext(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<A>(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_plaintext(infos.base2k(), infos.k()) - } -} - -impl<B: Backend> GLWEPlaintextAlloc for Module<B> where Self: GetDegree {} - impl GLWEPlaintext<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GLWEInfos, - M: GLWEPlaintextAlloc, { - module.alloc_glwe_plaintext_from_infos(infos) + Self::alloc(infos.n(), infos.base2k(), infos.k()) } - pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: GLWEPlaintextAlloc, - { - module.alloc_glwe_plaintext(base2k, k) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { + GLWEPlaintext { + data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize), + base2k, + k, + } } - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GLWEInfos, - M: GLWEPlaintextAlloc, { - module.bytes_of_glwe_plaintext_from_infos(infos) + Self::bytes_of(infos.n(), infos.base2k(), infos.k()) } - pub fn bytes_of<M>(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) } }
@@ -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 @@ Rank(self.data.cols() as u32)
} } -pub trait GLWESecretAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret<Vec<u8>> { - GLWESecret { - data: ScalarZnx::alloc(self.ring_degree().into(), rank.into()), - dist: Distribution::NONE, - } - } - - fn alloc_glwe_secret_from_infos<A>(&self, infos: &A) -> GLWESecret<Vec<u8>> - where - A: GLWEInfos, - { - self.alloc_glwe_secret(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<A>(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_secret(infos.rank()) - } -} - -impl<B: Backend> GLWESecretAlloc for Module<B> where Self: GetDegree {} - impl GLWESecret<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GLWEInfos, - M: GLWESecretAlloc, { - module.alloc_glwe_secret_from_infos(infos) + Self::alloc(infos.n(), infos.rank()) } - pub fn alloc<M>(module: &M, rank: Rank) -> Self - where - M: GLWESecretAlloc, - { - module.alloc_glwe_secret(rank) + pub fn alloc(n: Degree, rank: Rank) -> Self { + GLWESecret { + data: ScalarZnx::alloc(n.into(), rank.into()), + dist: Distribution::NONE, + } } - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GLWEInfos, - M: GLWESecretAlloc, { - module.bytes_of_glwe_secret_from_infos(infos) + Self::bytes_of(infos.n(), infos.rank()) } - pub fn bytes_of<M>(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()) } }
@@ -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,21 +132,8 @@ self.0.write_to(writer)
} } -pub trait GLWEToLWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_glwe_to_lwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> GLWEToLWESwitchingKey<Vec<u8>> { - GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) - } - - fn alloc_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKey<Vec<u8>> +impl GLWEToLWESwitchingKey<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, {@@ -160,14 +147,28 @@ 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()) + Self::alloc( + infos.n(), + 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)) + 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), + )) } - fn bytes_of_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, {@@ -181,41 +182,17 @@ 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<B: Backend> GLWEToLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} - -impl GLWEToLWESwitchingKey<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GLWEToLWESwitchingKeyAlloc, - { - module.alloc_glwe_to_lwe_switching_key_from_infos(infos) - } - - pub fn alloc<M>(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 bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GLWEToLWESwitchingKeyAlloc, - { - module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) + Self::bytes_of( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + ) } - pub fn bytes_of<M>(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)) } }
@@ -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,65 +125,31 @@ self.data.fill_uniform(log_bound, source);
} } -pub trait LWEAlloc { - fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE<Vec<u8>> { - LWE { - data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), - k, - base2k, - } - } - - fn alloc_lwe_from_infos<A>(&self, infos: &A) -> LWE<Vec<u8>> - where - A: LWEInfos, - { - self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) - } - - fn bytes_of_lwe(&self, 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<A>(&self, infos: &A) -> usize - where - A: LWEInfos, - { - self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k()) - } -} - -impl<B: Backend> LWEAlloc for Module<B> {} - impl LWE<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: LWEInfos, - M: LWEAlloc, { - module.alloc_lwe_from_infos(infos) + Self::alloc(infos.n(), infos.base2k(), infos.k()) } - pub fn alloc<M>(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWEAlloc, - { - module.alloc_lwe(n, base2k, 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, + base2k, + } } - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: LWEInfos, - M: LWEAlloc, { - module.bytes_of_lwe_from_infos(infos) + Self::bytes_of(infos.n(), infos.base2k(), infos.k()) } - pub fn bytes_of<M>(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize - where - M: LWEAlloc, - { - module.bytes_of_lwe(n, base2k, k) + 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) } }
@@ -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,15 +101,8 @@ self.0.dnum()
} } -pub trait LWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey<Vec<u8>> { - LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) - } - - fn alloc_lwe_switching_key_from_infos<A>(&self, infos: &A) -> LWESwitchingKey<Vec<u8>> +impl LWESwitchingKey<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, {@@ -128,14 +121,22 @@ infos.rank_out().0,
1, "rank_out > 1 is not supported for LWESwitchingKey" ); - self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) + Self::alloc(infos.n(), 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)) + 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), + )) } - fn bytes_of_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, {@@ -154,41 +155,11 @@ 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()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.dnum()) } -} -impl<B: Backend> LWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} - -impl LWESwitchingKey<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: LWESwitchingKeyAlloc, - { - module.alloc_lwe_switching_key_from_infos(infos) - } - - pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self - where - M: LWESwitchingKeyAlloc, - { - module.alloc_lwe_switching_key(base2k, k, dnum) - } - - pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: LWESwitchingKeyAlloc, - { - module.bytes_of_glwe_switching_key_from_infos(infos) - } - - pub fn bytes_of<M>(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)) } }
@@ -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,39 +52,20 @@ self.data.size()
} } -pub trait LWEPlaintextAlloc { - fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext<Vec<u8>> { - LWEPlaintext { - data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), - k, - base2k, - } - } - - fn alloc_lwe_plaintext_from_infos<A>(&self, infos: &A) -> LWEPlaintext<Vec<u8>> - where - A: LWEInfos, - { - self.alloc_lwe_plaintext(infos.base2k(), infos.k()) - } -} - -impl<B: Backend> LWEPlaintextAlloc for Module<B> {} - impl LWEPlaintext<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: LWEInfos, - M: LWEPlaintextAlloc, { - module.alloc_lwe_plaintext_from_infos(infos) + Self::alloc(infos.base2k(), infos.k()) } - pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWEPlaintextAlloc, - { - module.alloc_lwe_plaintext(base2k, 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, + } } }
@@ -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,23 +13,12 @@ pub(crate) data: ScalarZnx<D>,
pub(crate) dist: Distribution, } -pub trait LWESecretAlloc { - fn alloc_lwe_secret(&self, n: Degree) -> LWESecret<Vec<u8>> { +impl LWESecret<Vec<u8>> { + pub fn alloc(n: Degree) -> Self { LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, } - } -} - -impl<B: Backend> LWESecretAlloc for Module<B> {} - -impl LWESecret<Vec<u8>> { - pub fn alloc<M>(module: &M, n: Degree) -> Self - where - M: LWESecretAlloc, - { - module.alloc_lwe_secret(n) } }
@@ -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,21 +131,8 @@ self.0.write_to(writer)
} } -pub trait LWEToGLWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_lwe_to_glwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_out: Rank, - dnum: Dnum, - ) -> LWEToGLWESwitchingKey<Vec<u8>> { - LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) - } - - fn alloc_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKey<Vec<u8>> +impl LWEToGLWESwitchingKey<Vec<u8>> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos, {@@ -160,14 +147,28 @@ 1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + Self::alloc( + infos.n(), + 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)) + 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), + )) } - fn bytes_of_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(infos: &A) -> usize where A: GGLWEInfos, {@@ -181,41 +182,17 @@ 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<B: Backend> LWEToGLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} - -impl LWEToGLWESwitchingKey<Vec<u8>> { - pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: LWEToGLWESwitchingKeyAlloc, - { - module.alloc_lwe_to_glwe_switching_key_from_infos(infos) - } - - pub fn alloc<M>(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 bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: LWEToGLWESwitchingKeyAlloc, - { - module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos) + Self::bytes_of( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_out(), + infos.dnum(), + ) } - pub fn bytes_of<M>(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)) } }
@@ -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<D: DataRef> GGLWE<D> { - pub fn assert_noise<M, DataSk, DataWant, BE: Backend>( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared<DataSk, BE>, - pt_want: &ScalarZnx<DataWant>, - max_noise: f64, - ) where - DataSk: DataRef, - DataWant: DataRef, + pub fn assert_noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + S: GLWESecretPreparedToRef<BE>, + P: ScalarZnxToRef, M: GGLWENoise<BE>, Scratch<BE>: ScratchTakeBasic, { module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - - } pub trait GGLWENoise<BE: Backend> {@@ -34,7 +24,7 @@ where
R: GGLWEToRef, S: GLWESecretPreparedToRef<BE>, P: ScalarZnxToRef, - Scratch<BE>: ScratchTakeBasic; + Scratch<BE>: ScratchTakeBasic; } impl<BE: Backend> GGLWENoise<BE> for Module<BE>@@ -49,7 +39,7 @@ R: GGLWEToRef,
S: GLWESecretPreparedToRef<BE>, P: ScalarZnxToRef, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, - Scratch<BE>: ScratchAvailable + ScratchTakeBasic, + Scratch<BE>: ScratchAvailable + ScratchTakeBasic, { let res: &GGLWE<&[u8]> = &res.to_ref();@@ -57,7 +47,7 @@ let dsize: usize = res.dsize().into();
let base2k: usize = res.base2k().into(); let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res); (0..res.rank_in().into()).for_each(|col_i| { (0..res.dnum().into()).for_each(|row_i| {
@@ -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<D: DataRef> GGSW<D> { - pub fn assert_noise<M, BE, DataSk, DataScalar, F>( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared<DataSk, BE>, - pt_want: &ScalarZnx<DataScalar>, - max_noise: F, - ) where - DataSk: DataRef, - DataScalar: DataRef, + pub fn assert_noise<M, BE: Backend, P, S, F>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: F) + where + S: GLWESecretPreparedToRef<BE>, + P: ScalarZnxToRef, M: GGSWNoise<BE>, - BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>, F: Fn(usize) -> f64, { module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise); } - pub fn print_noise<M, BE, DataSk, DataScalar>( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared<DataSk, BE>, - pt_want: &ScalarZnx<DataScalar>, - ) where - DataSk: DataRef, - DataScalar: DataRef, + pub fn print_noise<M, BE: Backend, P, S>(&self, module: &M, sk_prepared: &S, pt_want: &P) + where + S: GLWESecretPreparedToRef<BE>, + P: ScalarZnxToRef, M: GGSWNoise<BE>, - BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>, { module.ggsw_print_noise(self, sk_prepared, pt_want); }@@ -51,43 +37,31 @@ where
R: GGSWToRef, S: GLWESecretPreparedToRef<BE>, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>, F: Fn(usize) -> f64; fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P) where R: GGSWToRef, S: GLWESecretPreparedToRef<BE>, - P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>; + P: ScalarZnxToRef; } impl<BE: Backend> GGSWNoise<BE> for Module<BE> where - Module<BE>: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply<BE> - + SvpApplyDftToDftInplace<BE> - + VecZnxIdftApplyConsume<BE> - + VecZnxBigAddInplace<BE> - + VecZnxBigAddSmallInplace<BE> - + VecZnxBigNormalize<BE> - + VecZnxNormalizeTmpBytes + Module<BE>: GLWEDecrypt<BE> + + VecZnxDftAlloc<BE> + VecZnxBigAlloc<BE> - + VecZnxDftAlloc<BE> - + VecZnxBigNormalizeTmpBytes + + VecZnxAddScalarInplace + VecZnxIdftApplyTmpA<BE> - + VecZnxAddScalarInplace - + VecZnxSubInplace - + GLWEDecrypt<BE>, + + VecZnxSubInplace, Scratch<BE>: ScratchTakeBasic, + ScratchOwned<BE>: ScratchOwnedBorrow<BE> + ScratchOwnedAlloc<BE>, { fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) where R: GGSWToRef, S: GLWESecretPreparedToRef<BE>, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>, F: Fn(usize) -> f64, { let res: &GGSW<&[u8]> = &res.to_ref();@@ -96,8 +70,8 @@
let base2k: usize = res.base2k().into(); let dsize: usize = res.dsize().into(); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res); let mut pt_dft: VecZnxDft<Vec<u8>, BE> = self.vec_znx_dft_alloc(1, res.size()); let mut pt_big: VecZnxBig<Vec<u8>, BE> = self.vec_znx_big_alloc(1, res.size());@@ -147,7 +121,6 @@ where
R: GGSWToRef, S: GLWESecretPreparedToRef<BE>, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>, { let res: &GGSW<&[u8]> = &res.to_ref(); let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();@@ -155,16 +128,16 @@
let base2k: usize = res.base2k().into(); let dsize: usize = res.dsize().into(); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res); let mut pt_dft: VecZnxDft<Vec<u8>, BE> = self.vec_znx_dft_alloc(1, res.size()); let mut pt_big: VecZnxBig<Vec<u8>, BE> = self.vec_znx_big_alloc(1, res.size()); let mut scratch: ScratchOwned<BE> = 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 @@
let std_pt: f64 = pt_have.data.std(base2k, 0).log2(); println!("col: {col_j} row: {row_i}: {std_pt}"); pt.data.zero(); - }); - }); + } + } } }
@@ -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<D: DataRef> GLWE<D> {@@ -25,75 +18,15 @@ P: GLWEPlaintextToRef,
{ module.glwe_noise(self, sk_prepared, pt_want, scratch) } - // pub fn noise<B, DataSk, DataPt>( - // &self, - // module: &Module<B>, - // sk_prepared: &GLWESecretPrepared<DataSk, B>, - // pt_want: &GLWEPlaintext<DataPt>, - // scratch: &mut Scratch<B>, - // ) -> f64 - // where - // DataSk: DataRef, - // DataPt: DataRef, - // B: Backend, - // Module<B>: VecZnxDftApply<B> - // + VecZnxSubInplace - // + VecZnxNormalizeInplace<B> - // + SvpApplyDftToDftInplace<B> - // + VecZnxIdftApplyConsume<B> - // + VecZnxBigAddInplace<B> - // + VecZnxBigAddSmallInplace<B> - // + VecZnxBigNormalize<B>, - // Scratch<B>:, - // { - // let mut pt_have: GLWEPlaintext<Vec<u8>> = 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<M, BE, DataSk, DataPt>( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared<DataSk, BE>, - pt_want: &GLWEPlaintext<DataPt>, - max_noise: f64, - ) where - DataSk: DataRef, - DataPt: DataRef, + pub fn assert_noise<M, BE: Backend, S, P>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + S: GLWESecretPreparedToRef<BE>, + P: GLWEPlaintextToRef, M: GLWENoise<BE>, - BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>, { module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - - // pub fn assert_noise<B, DataSk, DataPt>( - // &self, - // module: &Module<B>, - // sk_prepared: &GLWESecretPrepared<DataSk, B>, - // pt_want: &GLWEPlaintext<DataPt>, - // max_noise: f64, - // ) where - // DataSk: DataRef, - // DataPt: DataRef, - // Module<B>: VecZnxDftBytesOf - // + VecZnxBigBytesOf - // + VecZnxDftApply<B> - // + SvpApplyDftToDftInplace<B> - // + VecZnxIdftApplyConsume<B> - // + VecZnxBigAddInplace<B> - // + VecZnxBigAddSmallInplace<B> - // + VecZnxBigNormalize<B> - // + VecZnxNormalizeTmpBytes - // + VecZnxSubInplace - // + VecZnxNormalizeInplace<B>, - // B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, - // { - // let mut scratch: ScratchOwned<B> = 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<BE: Backend> {@@ -107,29 +40,14 @@ fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
where R: GLWEToRef, S: GLWESecretPreparedToRef<BE>, - P: GLWEPlaintextToRef, - BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>; + P: GLWEPlaintextToRef; } impl<BE: Backend> GLWENoise<BE> for Module<BE> where - Module<BE>: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply<BE> - + SvpApplyDftToDftInplace<BE> - + VecZnxIdftApplyConsume<BE> - + VecZnxBigAddInplace<BE> - + VecZnxBigAddSmallInplace<BE> - + VecZnxBigNormalize<BE> - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxNormalizeInplace<BE> - + GLWEDecrypt<BE>, - Scratch<BE>: ScratchTakeBasic - + ScratchOwnedAllocImpl<BE> - + ScratchOwnedBorrowImpl<BE> - + ScratchOwnedBorrowImpl<BE> - + ScratchOwnedBorrow<BE>, + Module<BE>: GLWEDecrypt<BE> + VecZnxSubInplace + VecZnxNormalizeInplace<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64 where@@ -141,7 +59,7 @@ let res_ref: &GLWE<&[u8]> = &res.to_ref();
let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.to_ref(); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res_ref); + let mut pt_have: GLWEPlaintext<Vec<u8>> = 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<BE>, P: GLWEPlaintextToRef, - BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>, { let res: &GLWE<&[u8]> = &res.to_ref(); let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));
@@ -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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: GLWE<Vec<u8>> = GLWE::alloc(&module, BASE2K, K, RANK); + let original: GLWE<Vec<u8>> = 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: GLWECompressed<Vec<u8>> = GLWECompressed::alloc(&module, BASE2K, K, RANK); + let original: GLWECompressed<Vec<u8>> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK); test_reader_writer_interface(original); } #[test] fn lwe_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: LWE<Vec<u8>> = LWE::alloc(&module, N_LWE, BASE2K, K); + let original: LWE<Vec<u8>> = LWE::alloc(N_LWE, BASE2K, K); test_reader_writer_interface(original); } #[test] fn lwe_compressed_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: LWECompressed<Vec<u8>> = LWECompressed::alloc(&module, BASE2K, K); + let original: LWECompressed<Vec<u8>> = LWECompressed::alloc(BASE2K, K); test_reader_writer_interface(original); } #[test] fn test_gglwe_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: GGLWE<Vec<u8>> = GGLWE::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE<Vec<u8>> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_gglwe_compressed_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: GGLWECompressed<Vec<u8>> = GGLWECompressed::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWECompressed<Vec<u8>> = 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey<Vec<u8>> = 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: GLWESwitchingKeyCompressed<Vec<u8>> = - 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_with(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_compressed_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: TensorKey<Vec<u8>> = TensorKey::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey<Vec<u8>> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed<Vec<u8>> = 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey<Vec<u8>> = 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: GLWEToLWESwitchingKeyCompressed<Vec<u8>> = - 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM); + let original: LWEToGLWESwitchingKey<Vec<u8>> = 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: LWEToGLWESwitchingKeyCompressed<Vec<u8>> = - 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<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc(&module, BASE2K, K, DNUM); + let original: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_compressed_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: LWESwitchingKeyCompressed<Vec<u8>> = LWESwitchingKeyCompressed::alloc(&module, BASE2K, K, DNUM); + let original: LWESwitchingKeyCompressed<Vec<u8>> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn ggsw_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: GGSW<Vec<u8>> = GGSW::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW<Vec<u8>> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn ggsw_compressed_serialization() { - let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); - let original: GGSWCompressed<Vec<u8>> = GGSWCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSWCompressed<Vec<u8>> = GGSWCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); }
@@ -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<BE: Backend>(module: &Module<BE>)@@ -24,7 +20,6 @@ Module<BE>: GGLWEEncryptSk<BE>
+ GGLWEKeyswitch<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk<BE> + GLWESwitchingKeyCompressedEncryptSk<BE> + GLWESwitchingKeyDecompress@@ -32,7 +27,7 @@ + GGLWENoise<BE>
+ VecZnxFillUniform + VecZnxAutomorphism, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, - Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_ksk: usize = 60;@@ -51,17 +46,15 @@ dsize: di.into(),
rank: rank.into(), }; - let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(module, &atk_infos); + let mut atk: AutomorphismKey<Vec<u8>> = 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<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( - module, &atk_infos, - )); + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &atk_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5;@@ -101,7 +94,6 @@ Module<BE>: GGLWEEncryptSk<BE>
+ GGLWEKeyswitch<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk<BE> + GLWESwitchingKeyCompressedEncryptSk<BE> + AutomorphismKeyDecompress@@ -109,7 +101,7 @@ + VecZnxAutomorphism
+ VecZnxFillUniform + GGLWENoise<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, - Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, + Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { let base2k: usize = 12; let k_ksk: usize = 60;@@ -128,16 +120,14 @@ dsize: di.into(),
rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc_from_infos(module, &atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed<Vec<u8>> = 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<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( - module, &atk_infos, - )); + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &atk_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5;@@ -159,7 +149,7 @@ });
let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); sk_out_prepared.prepare(module, &sk_out); - let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(module, &atk_infos); + let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key
@@ -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<BE: Backend>(module: &Module<BE>)@@ -21,20 +25,19 @@ + GGLWEKeyswitch<BE>
+ GLWEDecrypt<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk<BE> + VecZnxFillUniform + GGLWENoise<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { + 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 @@ rank_in: rank_in.into(),
rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos); + let mut ksk: GLWESwitchingKey<Vec<u8>> = 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<BE> = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes( - module, - &gglwe_infos, - )); + let mut scratch: ScratchOwned<BE> = + ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_infos)); - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, rank_in.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, rank_out.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); sk_out_prepared.prepare(module, &sk_out);@@ -91,7 +92,6 @@ + GGLWEKeyswitch<BE>
+ GLWEDecrypt<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk<BE> + GLWESwitchingKeyCompressedEncryptSk<BE> + GLWESwitchingKeyDecompress@@ -100,13 +100,13 @@ + VecZnxFillUniform,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, { + 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 @@ rank_out: rank_out.into(),
}; let mut ksk_compressed: GLWESwitchingKeyCompressed<Vec<u8>> = - 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 @@ module,
&gglwe_infos, )); - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, rank_in.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(module, rank_out.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); sk_out_prepared.prepare(module, &sk_out);@@ -149,7 +149,7 @@ &mut source_xe,
scratch.borrow(), ); - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos); + let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key
@@ -9,8 +9,8 @@ GLWECompressedEncryptSk, GLWEEncryptPk, GLWEEncryptSk, GLWEPublicKeyGenerate, GLWESub, ScratchTakeCore,
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 @@ };
pub fn test_glwe_encrypt_sk<BE: Backend>(module: &Module<BE>) where - Module<BE>: GLWEAlloc - + GLWEEncryptSk<BE> - + GLWEDecrypt<BE> - + GLWESecretPreparedAlloc<BE> - + GLWESecretPrepare<BE> - + VecZnxFillUniform - + GLWESub, + Module<BE>: + GLWEEncryptSk<BE> + GLWEDecrypt<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + VecZnxFillUniform + GLWESub, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, {@@ -48,9 +43,9 @@ base2k: base2k.into(),
k: k_pt.into(), }; - let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(module, &glwe_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = 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 @@
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());@@ -89,8 +84,7 @@ }
pub fn test_glwe_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>) where - Module<BE>: GLWEAlloc - + GLWECompressedEncryptSk<BE> + Module<BE>: GLWECompressedEncryptSk<BE> + GLWEDecrypt<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE>@@ -120,10 +114,10 @@ base2k: base2k.into(),
k: k_pt.into(), }; - let mut ct_compressed: GLWECompressed<Vec<u8>> = GLWECompressed::alloc_from_infos(module, &glwe_infos); + let mut ct_compressed: GLWECompressed<Vec<u8>> = GLWECompressed::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = 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 @@ let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());@@ -152,7 +146,7 @@ &mut source_xe,
scratch.borrow(), ); - let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE<Vec<u8>> = 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 @@ }
pub fn test_glwe_encrypt_zero_sk<BE: Backend>(module: &Module<BE>) where - Module<BE>: GLWEAlloc - + GLWEEncryptSk<BE> - + GLWEDecrypt<BE> - + GLWESecretPreparedAlloc<BE> - + GLWESecretPrepare<BE> - + VecZnxFillUniform - + GLWESub, + Module<BE>: + GLWEEncryptSk<BE> + GLWEDecrypt<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + VecZnxFillUniform + GLWESub, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, {@@ -195,7 +184,7 @@ k: k_ct.into(),
rank: rank.into(), }; - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); + let mut pt: GLWEPlaintext<Vec<u8>> = 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 @@
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); - let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); ct.encrypt_zero_sk( module,@@ -227,8 +216,7 @@ }
pub fn test_glwe_encrypt_pk<BE: Backend>(module: &Module<BE>) where - Module<BE>: GLWEAlloc - + GLWEEncryptPk<BE> + Module<BE>: GLWEEncryptPk<BE> + GLWEPublicKeyPrepare<BE> + GLWEPublicKeyPreparedAlloc<BE> + GLWEPublicKeyGenerate<BE>@@ -253,9 +241,9 @@ k: k_ct.into(),
rank: rank.into(), }; - let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(module, &glwe_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = 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 @@
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); - let mut pk: GLWEPublicKey<Vec<u8>> = GLWEPublicKey::alloc_from_infos(module, &glwe_infos); + let mut pk: GLWEPublicKey<Vec<u8>> = 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);
@@ -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<BE: Backend>(module: &Module<BE>) where Module<BE>: TensorKeyEncryptSk<BE> - + TensorKeyAlloc + GLWESecretPrepare<BE> + GLWESecretPreparedAlloc<BE> + GLWEDecrypt<BE>@@ -43,18 +50,15 @@ dsize: Dsize(1),
rank: rank.into(), }; - let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key: TensorKey<Vec<u8>> = 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<BE> = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( - module, - &tensor_key_infos, - )); + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key_infos)); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk);@@ -67,11 +71,11 @@ &mut source_xe,
scratch.borrow(), ); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos); + let mut pt: GLWEPlaintext<Vec<u8>> = 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<Vec<u8>> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank {@@ -92,9 +96,7 @@ 0,
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 @@
pub fn test_gglwe_tensor_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>) where Module<BE>: TensorKeyEncryptSk<BE> - + TensorKeyAlloc - + GLWESecretPrepare<BE> - + GLWESecretPreparedAlloc<BE> - + GGLWETensorKeyCompressedEncryptSk<BE> - + GLWEDecrypt<BE> - + VecZnxDftAlloc<BE> - + VecZnxBigAlloc<BE> - + VecZnxDftApply<BE> - + SvpApplyDftToDft<BE> - + VecZnxIdftApplyTmpA<BE> - + VecZnxSubScalarInplace - + VecZnxFillUniform - + VecZnxCopy - + VecZnxSwitchRing, + + GLWESecretPrepare<BE> + + GLWESecretPreparedAlloc<BE> + + GGLWETensorKeyCompressedEncryptSk<BE> + + GLWEDecrypt<BE> + + VecZnxDftAlloc<BE> + + VecZnxBigAlloc<BE> + + VecZnxDftApply<BE> + + SvpApplyDftToDft<BE> + + VecZnxIdftApplyTmpA<BE> + + VecZnxSubScalarInplace + + VecZnxFillUniform + + VecZnxCopy + + VecZnxSwitchRing, // + VecZnxNormalizeInplaceImpl<BE> // + VecZnxNormalizeImpl<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,@@ -145,7 +146,7 @@ dsize: Dsize(1),
rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed<Vec<u8>> = 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 @@ module,
&tensor_key_infos, )); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk);@@ -164,14 +165,14 @@ let seed_xa: [u8; 32] = [1u8; 32];
tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos); + let mut pt: GLWEPlaintext<Vec<u8>> = 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<Vec<u8>> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank {