Removed traits on structs not dependent on Module API

This commit is contained in:
Pro7ech
2025-10-20 10:32:02 +02:00
parent 60fbd3e625
commit 53bc78f421
50 changed files with 858 additions and 2010 deletions

View File

@@ -61,9 +61,9 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
rank, rank,
}; };
let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&module, &ggsw_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(&module, &glwe_in_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(&module, &glwe_out_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 pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n.into(), 1);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
@@ -76,7 +76,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
let mut source_xe = Source::new([0u8; 32]); let mut source_xe = Source::new([0u8; 32]);
let mut source_xa = 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); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); let mut sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank);
@@ -165,8 +165,8 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
rank, rank,
}; };
let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&module, &ggsw_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(&module, &glwe_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 pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n.into(), 1);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
@@ -179,7 +179,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: 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); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); let mut sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank);

View File

@@ -62,9 +62,9 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
rank, rank,
}; };
let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&module, &gglwe_atk_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(&module, &glwe_in_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(&module, &glwe_out_layout); let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_layout);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout) GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout)
@@ -81,7 +81,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: 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); sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); let mut sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank);
@@ -177,8 +177,8 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
rank, rank,
}; };
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&module, &gglwe_layout); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout);
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_layout); let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_layout);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout) GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout)
@@ -190,13 +190,13 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: 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); sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); let mut sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank);
sk_in_dft.prepare(&module, &sk_in); 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); sk_out.fill_ternary_prob(0.5, &mut source_xs);
ksk.encrypt_sk( ksk.encrypt_sk(

View File

@@ -43,9 +43,9 @@ fn main() {
let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt };
// Allocates ciphertext & plaintexts // Allocates ciphertext & plaintexts
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&module, &glwe_ct_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(&module, &glwe_pt_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(&module, &glwe_pt_infos); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos);
// CPRNG // CPRNG
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -58,7 +58,7 @@ fn main() {
); );
// Generate secret-key // 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); sk.fill_ternary_prob(0.5, &mut source_xs);
// Backend-prepared secret // Backend-prepared secret

View File

@@ -6,7 +6,7 @@ use poulpy_hal::{
use crate::{ use crate::{
GLWEKeyswitch, ScratchTakeCore, GLWEKeyswitch, ScratchTakeCore,
layouts::{ layouts::{
GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank,
prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef},
}, },
}; };
@@ -40,11 +40,11 @@ where
} }
impl<BE: Backend> LWESampleExtract for Module<BE> where Self: ModuleN {} 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> pub trait LWEFromGLWE<BE: Backend>
where 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 fn lwe_from_glwe_tmp_bytes<R, A, K>(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize
where where
@@ -59,8 +59,12 @@ where
rank: Rank(1), rank: Rank(1),
}; };
self.bytes_of_glwe(lwe_infos.base2k(), lwe_infos.k(), 1u32.into()) GLWE::bytes_of(
+ self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos) 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>) fn lwe_from_glwe<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>)

View File

@@ -6,16 +6,16 @@ use poulpy_hal::{
use crate::{ use crate::{
GLWEKeyswitch, ScratchTakeCore, GLWEKeyswitch, ScratchTakeCore,
layouts::{ layouts::{
GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef,
prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, 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> pub trait GLWEFromLWE<BE: Backend>
where 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 fn glwe_from_lwe_tmp_bytes<R, A, K>(&self, glwe_infos: &R, lwe_infos: &A, key_infos: &K) -> usize
where where
@@ -23,7 +23,8 @@ where
A: LWEInfos, A: LWEInfos,
K: GGLWEInfos, K: GGLWEInfos,
{ {
let ct: usize = self.bytes_of_glwe( let ct: usize = GLWE::bytes_of(
self.n().into(),
key_infos.base2k(), key_infos.base2k(),
lwe_infos.k().max(glwe_infos.k()), lwe_infos.k().max(glwe_infos.k()),
1u32.into(), 1u32.into(),

View File

@@ -8,7 +8,7 @@ use crate::{
ScratchTakeCore, ScratchTakeCore,
encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretAlloc, GLWESecretToRef, LWEInfos, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut}, compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut},
}, },
}; };
@@ -68,7 +68,7 @@ where
where where
A: GGLWEInfos, 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>( fn automorphism_key_compressed_encrypt_sk<R, S>(

View File

@@ -11,7 +11,7 @@ use crate::{
glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
}, },
layouts::{ layouts::{
GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintextAlloc, GLWESecretPrepared, LWEInfos, GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintext, GLWESecretPrepared, LWEInfos,
compressed::{GGLWECompressed, GGLWECompressedToMut}, compressed::{GGLWECompressed, GGLWECompressedToMut},
prepared::GLWESecretPreparedToRef, prepared::GLWESecretPreparedToRef,
}, },
@@ -82,7 +82,7 @@ where
{ {
self.glwe_encrypt_sk_tmp_bytes(infos) self.glwe_encrypt_sk_tmp_bytes(infos)
.max(self.vec_znx_normalize_tmp_bytes()) .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>( fn gglwe_compressed_encrypt_sk<R, P, S>(

View File

@@ -80,7 +80,7 @@ where
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKey" "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>( fn automorphism_key_encrypt_sk<R, S>(

View File

@@ -85,8 +85,7 @@ where
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.glwe_encrypt_sk_tmp_bytes(infos) self.glwe_encrypt_sk_tmp_bytes(infos) + GLWEPlaintext::bytes_of_from_infos(infos).max(self.vec_znx_normalize_tmp_bytes())
+ GLWEPlaintext::bytes_of_from_infos(self, infos).max(self.vec_znx_normalize_tmp_bytes())
} }
fn gglwe_encrypt_sk<R, P, S>( fn gglwe_encrypt_sk<R, P, S>(

View File

@@ -82,7 +82,7 @@ where
+ self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1)
+ self.bytes_of_vec_znx_big(1, 1) + self.bytes_of_vec_znx_big(1, 1)
+ self.bytes_of_vec_znx_dft(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) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
} }

View File

@@ -8,7 +8,7 @@ use crate::{
SIGMA, ScratchTakeCore, SIGMA, ScratchTakeCore,
encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
layouts::{ layouts::{
GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintextAlloc, LWEInfos, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
}, },
}; };
@@ -79,7 +79,7 @@ where
{ {
self.glwe_encrypt_sk_tmp_bytes(infos) self.glwe_encrypt_sk_tmp_bytes(infos)
.max(self.vec_znx_normalize_tmp_bytes()) .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>( fn ggsw_encrypt_sk<R, P, S>(

View File

@@ -55,7 +55,7 @@ where
// Its ok to allocate scratch space here since pk is usually generated only once. // 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 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()); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow());
} }

View File

@@ -72,7 +72,7 @@ where
A: GGLWEInfos, A: GGLWEInfos,
{ {
GLWESecretPrepared::bytes_of(self, infos.rank_in()) 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>( fn glwe_to_lwe_switching_key_encrypt_sk<R, S1, S2>(

View File

@@ -85,7 +85,7 @@ where
1, 1,
"rank_out > 1 is not supported for LWESwitchingKey" "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)) + GLWESecretPrepared::bytes_of(self, Rank(1))
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
} }

View File

@@ -75,7 +75,7 @@ where
Rank(1), Rank(1),
"rank_in != 1 is not supported for LWEToGLWESwitchingKey" "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>( fn lwe_to_glwe_switching_key_encrypt_sk<R, S1, S2>(

View File

@@ -9,7 +9,7 @@ use crate::{
GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore,
glwe_trace::GLWETrace, glwe_trace::GLWETrace,
layouts::{ layouts::{
GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos,
prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement},
}, },
}; };
@@ -41,13 +41,12 @@ impl Accumulator {
/// * `base2k`: base 2 logarithm of the GLWE ciphertext in memory digit representation. /// * `base2k`: base 2 logarithm of the GLWE ciphertext in memory digit representation.
/// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus. /// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus.
/// * `rank`: rank of the GLWE ciphertext. /// * `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 where
A: GLWEInfos, A: GLWEInfos,
M: GLWEPacking<BE>,
{ {
Self { Self {
data: GLWE::alloc_from_infos(module, infos), data: GLWE::alloc_from_infos(infos),
value: false, value: false,
control: false, control: false,
} }
@@ -59,20 +58,18 @@ impl GLWEPacker {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `module`: static backend FFT tables.
/// * `log_batch`: packs coefficients which are multiples of X^{N/2^log_batch}. /// * `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 /// 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 /// 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 /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts
/// can be packed. /// 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 where
A: GLWEInfos, A: GLWEInfos,
M: GLWEPacking<BE>,
{ {
let mut accumulators: Vec<Accumulator> = Vec::<Accumulator>::new(); let mut accumulators: Vec<Accumulator> = Vec::<Accumulator>::new();
let log_n: usize = infos.n().log2(); 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 { GLWEPacker {
accumulators, accumulators,
log_batch, log_batch,
@@ -96,7 +93,7 @@ impl GLWEPacker {
K: GGLWEInfos, K: GGLWEInfos,
M: GLWEPacking<BE>, M: GLWEPacking<BE>,
{ {
module.bytes_of_glwe_from_infos(res_infos) GLWE::bytes_of_from_infos(res_infos)
+ module + module
.glwe_rsh_tmp_byte() .glwe_rsh_tmp_byte()
.max(module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos)) .max(module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos))
@@ -169,7 +166,6 @@ impl<BE: Backend> GLWEPacking<BE> for Module<BE> where
+ GLWEAdd + GLWEAdd
+ GLWENormalize<BE> + GLWENormalize<BE>
+ GLWECopy + GLWECopy
+ GLWEAlloc
{ {
} }
@@ -183,8 +179,7 @@ where
+ GLWEShift<BE> + GLWEShift<BE>
+ GLWEAdd + GLWEAdd
+ GLWENormalize<BE> + GLWENormalize<BE>
+ GLWECopy + GLWECopy,
+ GLWEAlloc,
{ {
/// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] /// 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)] /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)]

View File

@@ -7,7 +7,7 @@ use crate::{
LWESampleExtract, ScratchTakeCore, LWESampleExtract, ScratchTakeCore,
keyswitching::glwe_ct::GLWEKeyswitch, keyswitching::glwe_ct::GLWEKeyswitch,
layouts::{ layouts::{
GGLWEInfos, GLWE, GLWEAlloc, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision,
prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef}, prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef},
}, },
}; };
@@ -40,7 +40,7 @@ impl<BE: Backend> LWEKeySwitch<BE> for Module<BE> where Self: LWEKeySwitch<BE> {
pub trait LWEKeySwitch<BE: Backend> pub trait LWEKeySwitch<BE: Backend>
where 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 fn lwe_keyswitch_tmp_bytes<R, A, K>(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize
where where
@@ -51,21 +51,21 @@ where
let max_k: TorusPrecision = a_infos.k().max(res_infos.k()); let max_k: TorusPrecision = a_infos.k().max(res_infos.k());
let glwe_a_infos: GLWELayout = GLWELayout { let glwe_a_infos: GLWELayout = GLWELayout {
n: self.ring_degree(), n: self.n().into(),
base2k: a_infos.base2k(), base2k: a_infos.base2k(),
k: max_k, k: max_k,
rank: Rank(1), rank: Rank(1),
}; };
let glwe_res_infos: GLWELayout = GLWELayout { let glwe_res_infos: GLWELayout = GLWELayout {
n: self.ring_degree(), n: self.n().into(),
base2k: res_infos.base2k(), base2k: res_infos.base2k(),
k: max_k, k: max_k,
rank: Rank(1), rank: Rank(1),
}; };
let glwe_in: usize = GLWE::bytes_of_from_infos(self, &glwe_a_infos); let glwe_in: usize = GLWE::bytes_of_from_infos(&glwe_a_infos);
let glwe_out: usize = GLWE::bytes_of_from_infos(self, &glwe_res_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); let ks: usize = self.glwe_keyswitch_tmp_bytes(&glwe_res_infos, &glwe_a_infos, key_infos);
glwe_in + glwe_out + ks glwe_in + glwe_out + ks

View File

@@ -6,8 +6,7 @@ use poulpy_hal::{
use crate::layouts::{ use crate::layouts::{
AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{ compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
}, },
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
}; };
@@ -86,32 +85,34 @@ impl<D: DataRef> fmt::Display for AutomorphismKeyCompressed<D> {
} }
} }
impl<BE: Backend> AutomorphismKeyCompressedAlloc for Module<BE> where Self: GLWESwitchingKeyCompressedAlloc {} impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub trait AutomorphismKeyCompressedAlloc pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
where
Self: GLWESwitchingKeyCompressedAlloc,
{
fn alloc_automorphism_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> AutomorphismKeyCompressed<Vec<u8>> {
AutomorphismKeyCompressed { AutomorphismKeyCompressed {
key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize),
p: 0, p: 0,
} }
} }
fn alloc_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> AutomorphismKeyCompressed<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
assert_eq!(infos.rank_in(), infos.rank_out()); Self::bytes_of(
self.alloc_automorphism_key_compressed( infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank(), infos.rank(),
@@ -120,61 +121,8 @@ where
) )
} }
fn bytes_of_automorphism_key_compressed( pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
&self, GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank, dnum, dsize)
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize)
}
fn bytes_of_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(infos.rank_in(), infos.rank_out());
self.bytes_of_automorphism_key_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl AutomorphismKeyCompressed<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)
} }
} }

View File

@@ -7,7 +7,7 @@ use poulpy_hal::{
}; };
use crate::layouts::{ 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}, compressed::{GLWECompressed, GLWEDecompress},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -105,19 +105,23 @@ impl<D: DataRef> fmt::Display for GGLWECompressed<D> {
} }
} }
pub trait GGLWECompressedAlloc impl GGLWECompressed<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
Self: GetDegree, where
{ A: GGLWEInfos,
fn alloc_gglwe_compressed( {
&self, Self::alloc(
base2k: Base2K, infos.n(),
k: TorusPrecision, infos.base2k(),
rank_in: Rank, infos.k(),
rank_out: Rank, infos.rank_in(),
dnum: Dnum, infos.rank_out(),
dsize: Dsize, infos.dnum(),
) -> GGLWECompressed<Vec<u8>> { 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; let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!( debug_assert!(
size as u32 > dsize.0, size as u32 > dsize.0,
@@ -134,7 +138,7 @@ where
GGLWECompressed { GGLWECompressed {
data: MatZnx::alloc( data: MatZnx::alloc(
self.ring_degree().into(), n.into(),
dnum.into(), dnum.into(),
rank_in.into(), rank_in.into(),
1, 1,
@@ -148,22 +152,21 @@ where
} }
} }
fn alloc_gglwe_compressed_from_infos<A>(&self, infos: &A) -> GGLWECompressed<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
assert_eq!(infos.n(), self.ring_degree()); Self::bytes_of(
self.alloc_gglwe_compressed( infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank_in(), infos.rank_in(),
infos.rank_out(),
infos.dnum(), infos.dnum(),
infos.dsize(), 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; let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!( debug_assert!(
size as u32 > dsize.0, size as u32 > dsize.0,
@@ -179,69 +182,13 @@ where
); );
MatZnx::bytes_of( MatZnx::bytes_of(
self.ring_degree().into(), n.into(),
dnum.into(), dnum.into(),
rank_in.into(), rank_in.into(),
1, 1,
k.0.div_ceil(base2k.0) as usize, 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)
}
} }
impl<D: DataRef> GGLWECompressed<D> { impl<D: DataRef> GGLWECompressed<D> {

View File

@@ -6,7 +6,7 @@ use poulpy_hal::{
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut,
LWEInfos, Rank, TorusPrecision, LWEInfos, Rank, TorusPrecision,
compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
@@ -81,33 +81,13 @@ impl<D: DataRef> fmt::Display for GLWESwitchingKeyCompressed<D> {
} }
} }
impl<BE: Backend> GLWESwitchingKeyCompressedAlloc for Module<BE> where Self: GGLWECompressedAlloc {} impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
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>>
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.alloc_glwe_switching_key_compressed( Self::alloc(
infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank_in(), infos.rank_in(),
@@ -117,62 +97,24 @@ where
) )
} }
fn bytes_of_glwe_switching_key_compressed( pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self {
&self, GLWESwitchingKeyCompressed {
base2k: Base2K, key: GGLWECompressed::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize),
k: TorusPrecision, sk_in_n: 0,
rank_in: Rank, sk_out_n: 0,
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 pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.bytes_of_gglwe_compressed_from_infos(infos) GGLWECompressed::bytes_of_from_infos(infos)
}
}
impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: GLWESwitchingKeyCompressedAlloc,
{
module.alloc_glwe_switching_key_compressed_from_infos(infos)
} }
pub fn alloc<M>( pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize
module: &M, where {
base2k: Base2K, GGLWECompressed::bytes_of(n, base2k, k, rank_in, dnum, dsize)
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)
} }
} }

View File

@@ -6,8 +6,7 @@ use poulpy_hal::{
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision,
compressed::{ compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
}, },
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -82,38 +81,36 @@ impl<D: DataRef> fmt::Display for TensorKeyCompressed<D> {
} }
} }
impl<BE: Backend> TensorKeyCompressedAlloc for Module<BE> where Self: GLWESwitchingKeyCompressedAlloc {} impl TensorKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub trait TensorKeyCompressedAlloc pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
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); let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
TensorKeyCompressed { TensorKeyCompressed {
keys: (0..pairs) keys: (0..pairs)
.map(|_| self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank, dnum, dsize)) .map(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.collect(), .collect(),
} }
} }
fn alloc_tensor_key_compressed_from_infos<A>(&self, infos: &A) -> TensorKeyCompressed<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
assert_eq!( Self::bytes_of(
infos.rank_in(), infos.n(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKeyCompressed"
);
self.alloc_tensor_key_compressed(
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank(), infos.rank(),
@@ -122,54 +119,9 @@ where
) )
} }
fn bytes_of_tensor_key_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; 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) pairs * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize)
}
fn bytes_of_tensor_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_tensor_key_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl TensorKeyCompressed<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)
} }
} }

View File

@@ -6,7 +6,7 @@ use poulpy_hal::{
}; };
use crate::layouts::{ 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}, compressed::{GLWECompressed, GLWEDecompress},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -96,20 +96,22 @@ impl<D: DataMut> FillUniform for GGSWCompressed<D> {
} }
} }
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 pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
where
Self: GetDegree,
{
fn alloc_ggsw_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GGSWCompressed<Vec<u8>> {
let size: usize = k.0.div_ceil(base2k.0) as usize; let size: usize = k.0.div_ceil(base2k.0) as usize;
assert!( assert!(
size as u32 > dsize.0, size as u32 > dsize.0,
@@ -126,7 +128,7 @@ where
GGSWCompressed { GGSWCompressed {
data: MatZnx::alloc( data: MatZnx::alloc(
self.ring_degree().into(), n.into(),
dnum.into(), dnum.into(),
(rank + 1).into(), (rank + 1).into(),
1, 1,
@@ -140,11 +142,12 @@ where
} }
} }
fn alloc_ggsw_compressed_from_infos<A>(&self, infos: &A) -> GGSWCompressed<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGSWInfos, A: GGSWInfos,
{ {
self.alloc_ggsw_compressed( Self::bytes_of(
infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank(), infos.rank(),
@@ -153,7 +156,7 @@ where
) )
} }
fn bytes_of_ggsw_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let size: usize = k.0.div_ceil(base2k.0) as usize; let size: usize = k.0.div_ceil(base2k.0) as usize;
assert!( assert!(
size as u32 > dsize.0, size as u32 > dsize.0,
@@ -169,58 +172,13 @@ where
); );
MatZnx::bytes_of( MatZnx::bytes_of(
self.ring_degree().into(), n.into(),
dnum.into(), dnum.into(),
(rank + 1).into(), (rank + 1).into(),
1, 1,
k.0.div_ceil(base2k.0) as usize, 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)
}
} }
impl<D: DataRef> GGSWCompressed<D> { impl<D: DataRef> GGSWCompressed<D> {

View File

@@ -88,17 +88,17 @@ impl<D: DataMut> FillUniform for GLWECompressed<D> {
} }
} }
pub trait GLWECompressedAlloc impl GLWECompressed<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
Self: GetDegree, where
{ A: GLWEInfos,
fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed<Vec<u8>> { {
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
GLWECompressed { GLWECompressed {
data: VecZnx::alloc( data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize),
self.ring_degree().into(),
1,
k.0.div_ceil(base2k.0) as usize,
),
base2k, base2k,
k, k,
rank, rank,
@@ -106,62 +106,15 @@ where
} }
} }
fn alloc_glwe_compressed_from_infos<A>(&self, infos: &A) -> GLWECompressed<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GLWEInfos, A: GLWEInfos,
{ {
assert_eq!(self.ring_degree(), infos.n()); Self::bytes_of(infos.n(), infos.base2k(), infos.k())
self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank())
} }
fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
VecZnx::bytes_of( VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize)
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)
} }
} }

View File

@@ -9,8 +9,7 @@ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank,
TorusPrecision, TorusPrecision,
compressed::{ compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
}, },
}; };
@@ -88,96 +87,61 @@ impl<D: DataRef> WriterTo for GLWEToLWESwitchingKeyCompressed<D> {
} }
} }
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>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
self.alloc_glwe_to_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
}
fn bytes_of_glwe_to_lwe_switching_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, Dsize(1))
}
fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
self.bytes_of_glwe_switching_key_compressed_from_infos(infos)
}
}
impl<B: Backend> GLWEToLWESwitchingKeyCompressedAlloc for Module<B> where Self: GLWESwitchingKeyCompressedAlloc {}
impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> { impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
M: GLWEToLWESwitchingKeyCompressedAlloc,
{ {
module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.dnum(),
)
} }
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
where GLWEToLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
M: GLWEToLWESwitchingKeyCompressedAlloc, n,
{ base2k,
module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) k,
rank_in,
Rank(1),
dnum,
Dsize(1),
))
} }
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
M: GLWEToLWESwitchingKeyCompressedAlloc,
{ {
module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
} }
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize {
where GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank_in, dnum, Dsize(1))
M: GLWEToLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum)
} }
} }

View File

@@ -62,8 +62,15 @@ impl<D: DataMut> FillUniform for LWECompressed<D> {
} }
} }
pub trait LWECompressedAlloc { impl LWECompressed<Vec<u8>> {
fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> 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 { LWECompressed {
data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize),
k, k,
@@ -72,57 +79,16 @@ pub trait LWECompressedAlloc {
} }
} }
fn alloc_lwe_compressed_from_infos<A>(&self, infos: &A) -> LWECompressed<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: LWEInfos, 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) 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)
}
} }
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

View File

@@ -6,8 +6,7 @@ use poulpy_hal::{
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision,
compressed::{ compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
}, },
}; };
use std::fmt; use std::fmt;
@@ -85,99 +84,65 @@ impl<D: DataRef> WriterTo for LWESwitchingKeyCompressed<D> {
} }
} }
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>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
self.alloc_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum())
}
fn bytes_of_lwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1))
}
fn bytes_of_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
self.bytes_of_glwe_switching_key_compressed_from_infos(infos)
}
}
impl<B: Backend> LWESwitchingKeyCompressedAlloc for Module<B> where Self: GLWESwitchingKeyCompressedAlloc {}
impl LWESwitchingKeyCompressed<Vec<u8>> { impl LWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
M: LWESwitchingKeyCompressedAlloc,
{ {
module.alloc_lwe_switching_key_compressed_from_infos(infos) assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum())
} }
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
where LWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
M: LWESwitchingKeyCompressedAlloc, n,
{ base2k,
module.alloc_lwe_switching_key_compressed(base2k, k, dnum) k,
Rank(1),
Rank(1),
dnum,
Dsize(1),
))
} }
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
M: LWESwitchingKeyCompressedAlloc,
{ {
module.bytes_of_lwe_switching_key_compressed_from_infos(infos) assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
} }
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
where GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1))
M: LWESwitchingKeyCompressedAlloc,
{
module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum)
} }
} }

View File

@@ -7,8 +7,7 @@ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank,
TorusPrecision, TorusPrecision,
compressed::{ compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
}, },
}; };
use std::fmt; use std::fmt;
@@ -86,90 +85,61 @@ impl<D: DataRef> WriterTo for LWEToGLWESwitchingKeyCompressed<D> {
} }
} }
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>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
self.alloc_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
}
fn bytes_of_lwe_to_glwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1))
}
fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum())
}
}
impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> { impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc<A, M>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
M: LWEToGLWESwitchingKeyCompressedAlloc,
{ {
module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_out(),
infos.dnum(),
)
} }
pub fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
where LWEToGLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
M: LWEToGLWESwitchingKeyCompressedAlloc, n,
{ base2k,
module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum) k,
Rank(1),
rank_out,
dnum,
Dsize(1),
))
} }
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
M: LWEToGLWESwitchingKeyCompressedAlloc,
{ {
module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
} }
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
where GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1))
M: LWEToGLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum)
} }
} }

View File

@@ -1,11 +1,11 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source, source::Source,
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, LWEInfos, Rank, TorusPrecision,
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -144,31 +144,13 @@ impl<D: DataRef> fmt::Display for AutomorphismKey<D> {
} }
} }
impl<B: Backend> AutomorphismKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} impl AutomorphismKey<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
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>>
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.alloc_automorphism_key( Self::alloc(
infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank(), infos.rank(),
@@ -177,11 +159,14 @@ where
) )
} }
fn bytes_of_automorphism_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
self.bytes_of_glwe_switching_key(base2k, k, rank, rank, dnum, dsize) 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 where
A: GGLWEInfos, A: GGLWEInfos,
{ {
@@ -190,7 +175,8 @@ where
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for AutomorphismKey" "rank_in != rank_out is not supported for AutomorphismKey"
); );
self.bytes_of_automorphism_key( Self::bytes_of(
infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank(), infos.rank(),
@@ -198,37 +184,9 @@ where
infos.dsize(), infos.dsize(),
) )
} }
}
impl AutomorphismKey<Vec<u8>> { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self GLWESwitchingKey::bytes_of(n, base2k, k, rank, rank, dnum, dsize)
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)
} }
} }

View File

@@ -1,11 +1,9 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{ layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos},
Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos,
},
source::Source, 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 byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
@@ -191,19 +189,23 @@ impl<D: DataMut> GGLWE<D> {
} }
} }
pub trait GGLWEAlloc impl GGLWE<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
Self: GetDegree, where
{ A: GGLWEInfos,
fn alloc_gglwe( {
&self, Self::alloc(
base2k: Base2K, infos.n(),
k: TorusPrecision, infos.base2k(),
rank_in: Rank, infos.k(),
rank_out: Rank, infos.rank_in(),
dnum: Dnum, infos.rank_out(),
dsize: Dsize, infos.dnum(),
) -> GGLWE<Vec<u8>> { 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; let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!( debug_assert!(
size as u32 > dsize.0, size as u32 > dsize.0,
@@ -220,7 +222,7 @@ where
GGLWE { GGLWE {
data: MatZnx::alloc( data: MatZnx::alloc(
self.ring_degree().into(), n.into(),
dnum.into(), dnum.into(),
rank_in.into(), rank_in.into(),
(rank_out + 1).into(), (rank_out + 1).into(),
@@ -232,11 +234,12 @@ where
} }
} }
fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GGLWE<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.alloc_gglwe( Self::bytes_of(
infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank_in(), infos.rank_in(),
@@ -246,8 +249,8 @@ where
) )
} }
fn bytes_of_gglwe( pub fn bytes_of(
&self, n: Degree,
base2k: Base2K, base2k: Base2K,
k: TorusPrecision, k: TorusPrecision,
rank_in: Rank, rank_in: Rank,
@@ -270,77 +273,13 @@ where
); );
MatZnx::bytes_of( MatZnx::bytes_of(
self.ring_degree().into(), n.into(),
dnum.into(), dnum.into(),
rank_in.into(), rank_in.into(),
(rank_out + 1).into(), (rank_out + 1).into(),
k.0.div_ceil(base2k.0) as usize, 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)
}
} }
pub trait GGLWEToMut { pub trait GGLWEToMut {

View File

@@ -1,11 +1,10 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source, source::Source,
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision,
TorusPrecision,
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -163,31 +162,13 @@ impl<D: DataMut> FillUniform for GLWESwitchingKey<D> {
} }
} }
pub trait GLWESwitchingKeyAlloc impl GLWESwitchingKey<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
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>>
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
self.alloc_glwe_switching_key( Self::alloc(
infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank_in(), infos.rank_in(),
@@ -197,8 +178,31 @@ where
) )
} }
fn bytes_of_glwe_switching_key( pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self {
&self, GLWESwitchingKey {
key: GGLWE::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0,
sk_out_n: 0,
}
}
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
pub fn bytes_of(
n: Degree,
base2k: Base2K, base2k: Base2K,
k: TorusPrecision, k: TorusPrecision,
rank_in: Rank, rank_in: Rank,
@@ -206,71 +210,7 @@ where
dnum: Dnum, dnum: Dnum,
dsize: Dsize, dsize: Dsize,
) -> usize { ) -> usize {
self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) GGLWE::bytes_of(n, base2k, k, rank_in, rank_out, dnum, dsize)
}
fn bytes_of_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_glwe_switching_key(
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<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,
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)
} }
} }

View File

@@ -1,11 +1,11 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source, source::Source,
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, Rank, TorusPrecision,
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -130,20 +130,36 @@ impl<D: DataRef> fmt::Display for TensorKey<D> {
} }
} }
pub trait TensorKeyAlloc impl TensorKey<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
Self: GLWESwitchingKeyAlloc, where
{ A: GGLWEInfos,
fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey<Vec<u8>> { {
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKey"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
TensorKey { TensorKey {
keys: (0..pairs) keys: (0..pairs)
.map(|_| self.alloc_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize)) .map(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.collect(), .collect(),
} }
} }
fn alloc_tensor_key_from_infos<A>(&self, infos: &A) -> TensorKey<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
@@ -152,7 +168,8 @@ where
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKey" "rank_in != rank_out is not supported for GGLWETensorKey"
); );
self.alloc_tensor_key( Self::bytes_of(
infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank(), infos.rank(),
@@ -161,61 +178,9 @@ where
) )
} }
fn bytes_of_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; 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) pairs * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize)
}
fn bytes_of_tensor_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKey"
);
self.bytes_of_tensor_key(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<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)
} }
} }

View File

@@ -1,12 +1,10 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{ layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos},
Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos,
},
source::Source, source::Source,
}; };
use std::fmt; 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 pub trait GGSWInfos
where where
@@ -152,13 +150,22 @@ impl<D: DataMut> GGSW<D> {
} }
} }
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 pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
where
Self: GetDegree,
{
fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW<Vec<u8>> {
let size: usize = k.0.div_ceil(base2k.0) as usize; let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!( debug_assert!(
size as u32 > dsize.0, size as u32 > dsize.0,
@@ -175,7 +182,7 @@ where
GGSW { GGSW {
data: MatZnx::alloc( data: MatZnx::alloc(
self.ring_degree().into(), n.into(),
dnum.into(), dnum.into(),
(rank + 1).into(), (rank + 1).into(),
(rank + 1).into(), (rank + 1).into(),
@@ -187,11 +194,12 @@ where
} }
} }
fn alloc_ggsw_from_infos<A>(&self, infos: &A) -> GGSW<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGSWInfos, A: GGSWInfos,
{ {
self.alloc_ggsw( Self::bytes_of(
infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
infos.rank(), infos.rank(),
@@ -200,7 +208,7 @@ where
) )
} }
fn bytes_of_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let size: usize = k.0.div_ceil(base2k.0) as usize; let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!( debug_assert!(
size as u32 > dsize.0, size as u32 > dsize.0,
@@ -216,58 +224,13 @@ where
); );
MatZnx::bytes_of( MatZnx::bytes_of(
self.ring_degree().into(), n.into(),
dnum.into(), dnum.into(),
(rank + 1).into(), (rank + 1).into(),
(rank + 1).into(), (rank + 1).into(),
k.0.div_ceil(base2k.0) as usize, 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)
}
} }
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

View File

@@ -1,12 +1,11 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{ layouts::{
Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos,
WriterTo, ZnxInfos,
}, },
source::Source, 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 byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
@@ -146,76 +145,31 @@ impl<D: DataMut> FillUniform for GLWE<D> {
} }
} }
pub trait GLWEAlloc impl GLWE<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
Self: GetDegree, where
{ A: GLWEInfos,
fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE<Vec<u8>> { {
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
GLWE { GLWE {
data: VecZnx::alloc( data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
self.ring_degree().into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
),
base2k, base2k,
k, k,
} }
} }
fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GLWE<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GLWEInfos, A: GLWEInfos,
{ {
self.alloc_glwe(infos.base2k(), infos.k(), infos.rank()) Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank())
} }
fn bytes_of_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
VecZnx::bytes_of( VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
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
where
A: GLWEInfos,
M: GLWEAlloc,
{
module.alloc_glwe_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self
where
M: GLWEAlloc,
{
module.alloc_glwe(base2k, k, rank)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWEAlloc,
{
module.bytes_of_glwe_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize
where
M: GLWEAlloc,
{
module.bytes_of_glwe(base2k, k, rank)
} }
} }

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{ use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos};
Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos,
};
use crate::{ use crate::{
GetDistribution, GetDistributionMut, GetDistribution, GetDistributionMut,
dist::Distribution, dist::Distribution,
layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -85,77 +83,32 @@ impl GLWEInfos for GLWEPublicKeyLayout {
} }
} }
pub trait GLWEPublicKeyAlloc impl GLWEPublicKey<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
Self: GetDegree, where
{ A: GLWEInfos,
fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey<Vec<u8>> { {
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
GLWEPublicKey { GLWEPublicKey {
data: VecZnx::alloc( data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
self.ring_degree().into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
),
base2k, base2k,
k, k,
dist: Distribution::NONE, dist: Distribution::NONE,
} }
} }
fn alloc_glwe_public_key_from_infos<A>(&self, infos: &A) -> GLWEPublicKey<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GLWEInfos, A: GLWEInfos,
{ {
self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank())
} }
fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
VecZnx::bytes_of( VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
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
where
A: GLWEInfos,
M: GLWEPublicKeyAlloc,
{
module.alloc_glwe_public_key_from_infos(infos)
}
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 bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWEPublicKeyAlloc,
{
module.bytes_of_glwe_public_key_from_infos(infos)
}
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)
} }
} }

View File

@@ -1,10 +1,8 @@
use std::fmt; 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::{ use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, Rank, SetGLWEInfos, TorusPrecision};
Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct GLWEPlaintextLayout { pub struct GLWEPlaintextLayout {
@@ -85,76 +83,31 @@ impl<D: DataRef> fmt::Display for GLWEPlaintext<D> {
} }
} }
pub trait GLWEPlaintextAlloc impl GLWEPlaintext<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
Self: GetDegree, where
{ A: GLWEInfos,
fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext<Vec<u8>> { {
Self::alloc(infos.n(), infos.base2k(), infos.k())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self {
GLWEPlaintext { GLWEPlaintext {
data: VecZnx::alloc( data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize),
self.ring_degree().into(),
1,
k.0.div_ceil(base2k.0) as usize,
),
base2k, base2k,
k, k,
} }
} }
fn alloc_glwe_plaintext_from_infos<A>(&self, infos: &A) -> GLWEPlaintext<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GLWEInfos, A: GLWEInfos,
{ {
self.alloc_glwe_plaintext(infos.base2k(), infos.k()) Self::bytes_of(infos.n(), infos.base2k(), infos.k())
} }
fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
VecZnx::bytes_of( VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize)
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
where
A: GLWEInfos,
M: GLWEPlaintextAlloc,
{
module.alloc_glwe_plaintext_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self
where
M: GLWEPlaintextAlloc,
{
module.alloc_glwe_plaintext(base2k, k)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWEPlaintextAlloc,
{
module.bytes_of_glwe_plaintext_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> usize
where
M: GLWEPlaintextAlloc,
{
module.bytes_of_glwe_plaintext(base2k, k)
} }
} }

View File

@@ -1,15 +1,12 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{ layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, ZnxZero},
Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos,
ZnxZero,
},
source::Source, source::Source,
}; };
use crate::{ use crate::{
GetDistribution, GetDistribution,
dist::Distribution, dist::Distribution,
layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision},
}; };
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -77,67 +74,30 @@ impl<D: Data> GLWEInfos for GLWESecret<D> {
} }
} }
pub trait GLWESecretAlloc impl GLWESecret<Vec<u8>> {
where pub fn alloc_from_infos<A>(infos: &A) -> Self
Self: GetDegree, where
{ A: GLWEInfos,
fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret<Vec<u8>> { {
Self::alloc(infos.n(), infos.rank())
}
pub fn alloc(n: Degree, rank: Rank) -> Self {
GLWESecret { GLWESecret {
data: ScalarZnx::alloc(self.ring_degree().into(), rank.into()), data: ScalarZnx::alloc(n.into(), rank.into()),
dist: Distribution::NONE, dist: Distribution::NONE,
} }
} }
fn alloc_glwe_secret_from_infos<A>(&self, infos: &A) -> GLWESecret<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GLWEInfos, A: GLWEInfos,
{ {
self.alloc_glwe_secret(infos.rank()) Self::bytes_of(infos.n(), infos.rank())
} }
fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { pub fn bytes_of(n: Degree, rank: Rank) -> usize {
ScalarZnx::bytes_of(self.ring_degree().into(), rank.into()) ScalarZnx::bytes_of(n.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
where
A: GLWEInfos,
M: GLWESecretAlloc,
{
module.alloc_glwe_secret_from_infos(infos)
}
pub fn alloc<M>(module: &M, rank: Rank) -> Self
where
M: GLWESecretAlloc,
{
module.alloc_glwe_secret(rank)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWESecretAlloc,
{
module.bytes_of_glwe_secret_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, rank: Rank) -> usize
where
M: GLWESecretAlloc,
{
module.bytes_of_glwe_secret(rank)
} }
} }

View File

@@ -1,11 +1,11 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source, source::Source,
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, Rank, TorusPrecision,
}; };
use std::fmt; use std::fmt;
@@ -132,90 +132,67 @@ impl<D: DataRef> WriterTo for GLWEToLWESwitchingKey<D> {
} }
} }
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>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey"
);
self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
}
fn bytes_of_glwe_to_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))
}
fn bytes_of_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey"
);
self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
}
}
impl<B: Backend> GLWEToLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl GLWEToLWESwitchingKey<Vec<u8>> { impl GLWEToLWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
M: GLWEToLWESwitchingKeyAlloc,
{ {
module.alloc_glwe_to_lwe_switching_key_from_infos(infos) assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.dnum(),
)
} }
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
where GLWEToLWESwitchingKey(GLWESwitchingKey::alloc(
M: GLWEToLWESwitchingKeyAlloc, n,
{ base2k,
module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) k,
rank_in,
Rank(1),
dnum,
Dsize(1),
))
} }
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
M: GLWEToLWESwitchingKeyAlloc,
{ {
module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey"
);
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.dnum(),
)
} }
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
where GLWESwitchingKey::bytes_of(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1))
M: GLWEToLWESwitchingKeyAlloc,
{
module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum)
} }
} }

View File

@@ -1,7 +1,7 @@
use std::fmt; use std::fmt;
use poulpy_hal::{ 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, source::Source,
}; };
@@ -125,8 +125,15 @@ where
} }
} }
pub trait LWEAlloc { impl LWE<Vec<u8>> {
fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE<Vec<u8>> { pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: LWEInfos,
{
Self::alloc(infos.n(), infos.base2k(), infos.k())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self {
LWE { LWE {
data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize),
k, k,
@@ -134,57 +141,16 @@ pub trait LWEAlloc {
} }
} }
fn alloc_lwe_from_infos<A>(&self, infos: &A) -> LWE<Vec<u8>> pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: LWEInfos, A: LWEInfos,
{ {
self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) Self::bytes_of(infos.n(), infos.base2k(), infos.k())
} }
fn bytes_of_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) 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
where
A: LWEInfos,
M: LWEAlloc,
{
module.alloc_lwe_from_infos(infos)
}
pub fn alloc<M>(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self
where
M: LWEAlloc,
{
module.alloc_lwe(n, base2k, k)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: LWEInfos,
M: LWEAlloc,
{
module.bytes_of_lwe_from_infos(infos)
}
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 trait LWEToRef { pub trait LWEToRef {

View File

@@ -1,13 +1,13 @@
use std::fmt; use std::fmt;
use poulpy_hal::{ use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source, source::Source,
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, Rank, TorusPrecision,
}; };
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -101,94 +101,65 @@ impl<D: Data> GGLWEInfos for LWESwitchingKey<D> {
} }
} }
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>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum())
}
fn bytes_of_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
}
fn bytes_of_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum())
}
}
impl<B: Backend> LWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl LWESwitchingKey<Vec<u8>> { impl LWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
M: LWESwitchingKeyAlloc,
{ {
module.alloc_lwe_switching_key_from_infos(infos) assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum())
} }
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
where LWESwitchingKey(GLWESwitchingKey::alloc(
M: LWESwitchingKeyAlloc, n,
{ base2k,
module.alloc_lwe_switching_key(base2k, k, dnum) k,
Rank(1),
Rank(1),
dnum,
Dsize(1),
))
} }
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
M: LWESwitchingKeyAlloc,
{ {
module.bytes_of_glwe_switching_key_from_infos(infos) assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.dnum())
} }
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
where GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
M: LWESwitchingKeyAlloc,
{
module.bytes_of_lwe_switching_key(base2k, k, dnum)
} }
} }

View File

@@ -1,6 +1,6 @@
use std::fmt; 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}; use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision};
@@ -52,40 +52,21 @@ impl<D: Data> LWEInfos for LWEPlaintext<D> {
} }
} }
pub trait LWEPlaintextAlloc { impl LWEPlaintext<Vec<u8>> {
fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext<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 {
LWEPlaintext { LWEPlaintext {
data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize),
k, k,
base2k, 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
where
A: LWEInfos,
M: LWEPlaintextAlloc,
{
module.alloc_lwe_plaintext_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self
where
M: LWEPlaintextAlloc,
{
module.alloc_lwe_plaintext(base2k, k)
}
} }
impl<D: DataRef> fmt::Display for LWEPlaintext<D> { impl<D: DataRef> fmt::Display for LWEPlaintext<D> {

View File

@@ -1,5 +1,5 @@
use poulpy_hal::{ 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, source::Source,
}; };
@@ -13,8 +13,8 @@ pub struct LWESecret<D: Data> {
pub(crate) dist: Distribution, pub(crate) dist: Distribution,
} }
pub trait LWESecretAlloc { impl LWESecret<Vec<u8>> {
fn alloc_lwe_secret(&self, n: Degree) -> LWESecret<Vec<u8>> { pub fn alloc(n: Degree) -> Self {
LWESecret { LWESecret {
data: ScalarZnx::alloc(n.into(), 1), data: ScalarZnx::alloc(n.into(), 1),
dist: Distribution::NONE, dist: Distribution::NONE,
@@ -22,17 +22,6 @@ pub trait LWESecretAlloc {
} }
} }
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)
}
}
impl<D: DataRef> LWESecret<D> { impl<D: DataRef> LWESecret<D> {
pub fn raw(&self) -> &[i64] { pub fn raw(&self) -> &[i64] {
self.data.at(0, 0) self.data.at(0, 0)

View File

@@ -1,13 +1,13 @@
use std::fmt; use std::fmt;
use poulpy_hal::{ use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source, source::Source,
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, Rank, TorusPrecision,
}; };
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -131,91 +131,68 @@ impl<D: DataRef> WriterTo for LWEToGLWESwitchingKey<D> {
} }
} }
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>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
}
fn bytes_of_lwe_to_glwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))
}
fn bytes_of_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
}
}
impl<B: Backend> LWEToGLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl LWEToGLWESwitchingKey<Vec<u8>> { impl LWEToGLWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self pub fn alloc_from_infos<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
M: LWEToGLWESwitchingKeyAlloc,
{ {
module.alloc_lwe_to_glwe_switching_key_from_infos(infos) assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_out(),
infos.dnum(),
)
} }
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
where LWEToGLWESwitchingKey(GLWESwitchingKey::alloc(
M: LWEToGLWESwitchingKeyAlloc, n,
{ base2k,
module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) k,
Rank(1),
rank_out,
dnum,
Dsize(1),
))
} }
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
M: LWEToGLWESwitchingKeyAlloc,
{ {
module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos) assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_out(),
infos.dnum(),
)
} }
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
where GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1))
M: LWEToGLWESwitchingKeyAlloc,
{
module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum)
} }
} }

View File

@@ -1,31 +1,21 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace}, 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::decryption::GLWEDecrypt;
use crate::layouts::{ use crate::layouts::{GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::GLWESecretPreparedToRef};
GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
};
impl<D: DataRef> GGLWE<D> { impl<D: DataRef> GGLWE<D> {
pub fn assert_noise<M, DataSk, DataWant, BE: Backend>( pub fn assert_noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64)
&self, where
module: &M, S: GLWESecretPreparedToRef<BE>,
sk_prepared: &GLWESecretPrepared<DataSk, BE>, P: ScalarZnxToRef,
pt_want: &ScalarZnx<DataWant>,
max_noise: f64,
) where
DataSk: DataRef,
DataWant: DataRef,
M: GGLWENoise<BE>, M: GGLWENoise<BE>,
Scratch<BE>: ScratchTakeBasic, Scratch<BE>: ScratchTakeBasic,
{ {
module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise); module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
} }
} }
pub trait GGLWENoise<BE: Backend> { pub trait GGLWENoise<BE: Backend> {
@@ -34,7 +24,7 @@ pub trait GGLWENoise<BE: Backend> {
R: GGLWEToRef, R: GGLWEToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef, P: ScalarZnxToRef,
Scratch<BE>: ScratchTakeBasic; Scratch<BE>: ScratchTakeBasic;
} }
impl<BE: Backend> GGLWENoise<BE> for Module<BE> impl<BE: Backend> GGLWENoise<BE> for Module<BE>
@@ -49,7 +39,7 @@ where
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef, P: ScalarZnxToRef,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeBasic, Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
{ {
let res: &GGLWE<&[u8]> = &res.to_ref(); let res: &GGLWE<&[u8]> = &res.to_ref();
@@ -57,7 +47,7 @@ where
let base2k: usize = res.base2k().into(); let base2k: usize = res.base2k().into();
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); 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.rank_in().into()).for_each(|col_i| {
(0..res.dnum().into()).for_each(|row_i| { (0..res.dnum().into()).for_each(|row_i| {

View File

@@ -1,12 +1,9 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAlloc,
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalize, VecZnxDftAlloc, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
}, },
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
}; };
use crate::decryption::GLWEDecrypt; use crate::decryption::GLWEDecrypt;
@@ -14,32 +11,21 @@ use crate::layouts::prepared::GLWESecretPreparedToRef;
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
impl<D: DataRef> GGSW<D> { impl<D: DataRef> GGSW<D> {
pub fn assert_noise<M, BE, DataSk, DataScalar, F>( pub fn assert_noise<M, BE: Backend, P, S, F>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: F)
&self, where
module: &M, S: GLWESecretPreparedToRef<BE>,
sk_prepared: &GLWESecretPrepared<DataSk, BE>, P: ScalarZnxToRef,
pt_want: &ScalarZnx<DataScalar>,
max_noise: F,
) where
DataSk: DataRef,
DataScalar: DataRef,
M: GGSWNoise<BE>, M: GGSWNoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64, F: Fn(usize) -> f64,
{ {
module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise); module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise);
} }
pub fn print_noise<M, BE, DataSk, DataScalar>( pub fn print_noise<M, BE: Backend, P, S>(&self, module: &M, sk_prepared: &S, pt_want: &P)
&self, where
module: &M, S: GLWESecretPreparedToRef<BE>,
sk_prepared: &GLWESecretPrepared<DataSk, BE>, P: ScalarZnxToRef,
pt_want: &ScalarZnx<DataScalar>,
) where
DataSk: DataRef,
DataScalar: DataRef,
M: GGSWNoise<BE>, M: GGSWNoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{ {
module.ggsw_print_noise(self, sk_prepared, pt_want); module.ggsw_print_noise(self, sk_prepared, pt_want);
} }
@@ -51,43 +37,31 @@ pub trait GGSWNoise<BE: Backend> {
R: GGSWToRef, R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef, P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64; F: Fn(usize) -> f64;
fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P) fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P)
where where
R: GGSWToRef, R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef, P: ScalarZnxToRef;
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>;
} }
impl<BE: Backend> GGSWNoise<BE> for Module<BE> impl<BE: Backend> GGSWNoise<BE> for Module<BE>
where where
Module<BE>: VecZnxDftBytesOf Module<BE>: GLWEDecrypt<BE>
+ VecZnxBigBytesOf
+ VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddInplace<BE>
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxBigAlloc<BE>
+ VecZnxDftAlloc<BE> + VecZnxDftAlloc<BE>
+ VecZnxBigNormalizeTmpBytes + VecZnxBigAlloc<BE>
+ VecZnxIdftApplyTmpA<BE>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxSubInplace + VecZnxIdftApplyTmpA<BE>
+ GLWEDecrypt<BE>, + VecZnxSubInplace,
Scratch<BE>: ScratchTakeBasic, 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) fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F)
where where
R: GGSWToRef, R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef, P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64, F: Fn(usize) -> f64,
{ {
let res: &GGSW<&[u8]> = &res.to_ref(); let res: &GGSW<&[u8]> = &res.to_ref();
@@ -96,8 +70,8 @@ where
let base2k: usize = res.base2k().into(); let base2k: usize = res.base2k().into();
let dsize: usize = res.dsize().into(); let dsize: usize = res.dsize().into();
let mut pt: 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(self, 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_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 pt_big: VecZnxBig<Vec<u8>, BE> = self.vec_znx_big_alloc(1, res.size());
@@ -147,7 +121,6 @@ where
R: GGSWToRef, R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef, P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{ {
let res: &GGSW<&[u8]> = &res.to_ref(); let res: &GGSW<&[u8]> = &res.to_ref();
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref(); let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
@@ -155,16 +128,16 @@ where
let base2k: usize = res.base2k().into(); let base2k: usize = res.base2k().into();
let dsize: usize = res.dsize().into(); let dsize: usize = res.dsize().into();
let mut pt: 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(self, 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_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 pt_big: VecZnxBig<Vec<u8>, BE> = self.vec_znx_big_alloc(1, res.size());
let mut scratch: ScratchOwned<BE> = let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes()); ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes());
(0..(res.rank() + 1).into()).for_each(|col_j| { for col_j in 0..(res.rank() + 1).into() {
(0..res.dnum().into()).for_each(|row_i| { 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); self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0);
// mul with sk[col_j-1] // mul with sk[col_j-1]
@@ -194,7 +167,7 @@ where
let std_pt: f64 = pt_have.data.std(base2k, 0).log2(); let std_pt: f64 = pt_have.data.std(base2k, 0).log2();
println!("col: {col_j} row: {row_i}: {std_pt}"); println!("col: {col_j} row: {row_i}: {std_pt}");
pt.data.zero(); pt.data.zero();
}); }
}); }
} }
} }

View File

@@ -1,19 +1,12 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubInplace},
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace,
VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
},
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
}; };
use crate::{ use crate::{
ScratchTakeCore,
decryption::GLWEDecrypt, decryption::GLWEDecrypt,
layouts::{ layouts::{GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef},
GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
},
}; };
impl<D: DataRef> GLWE<D> { impl<D: DataRef> GLWE<D> {
@@ -25,75 +18,15 @@ impl<D: DataRef> GLWE<D> {
{ {
module.glwe_noise(self, sk_prepared, pt_want, scratch) 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>( pub fn assert_noise<M, BE: Backend, S, P>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64)
&self, where
module: &M, S: GLWESecretPreparedToRef<BE>,
sk_prepared: &GLWESecretPrepared<DataSk, BE>, P: GLWEPlaintextToRef,
pt_want: &GLWEPlaintext<DataPt>,
max_noise: f64,
) where
DataSk: DataRef,
DataPt: DataRef,
M: GLWENoise<BE>, M: GLWENoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{ {
module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise); 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> { pub trait GLWENoise<BE: Backend> {
@@ -107,29 +40,14 @@ pub trait GLWENoise<BE: Backend> {
where where
R: GLWEToRef, R: GLWEToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef;
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>;
} }
impl<BE: Backend> GLWENoise<BE> for Module<BE> impl<BE: Backend> GLWENoise<BE> for Module<BE>
where where
Module<BE>: VecZnxDftBytesOf Module<BE>: GLWEDecrypt<BE> + VecZnxSubInplace + VecZnxNormalizeInplace<BE>,
+ VecZnxBigBytesOf ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
+ VecZnxDftApply<BE> Scratch<BE>: ScratchTakeCore<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>,
{ {
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64 fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
where where
@@ -141,7 +59,7 @@ where
let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.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.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_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); self.vec_znx_normalize_inplace(res_ref.base2k().into(), &mut pt_have.data, 0, scratch);
@@ -153,7 +71,6 @@ where
R: GLWEToRef, R: GLWEToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{ {
let res: &GLWE<&[u8]> = &res.to_ref(); let res: &GLWE<&[u8]> = &res.to_ref();
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));

View File

@@ -1,5 +1,4 @@
use poulpy_backend::FFT64Ref; use poulpy_hal::test_suite::serialization::test_reader_writer_interface;
use poulpy_hal::{api::ModuleNew, layouts::Module, test_suite::serialization::test_reader_writer_interface};
use crate::layouts::{ use crate::layouts::{
AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE,
@@ -21,143 +20,123 @@ const DSIZE: Dsize = Dsize(1);
#[test] #[test]
fn glwe_serialization() { fn glwe_serialization() {
let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: GLWE<Vec<u8>> = GLWE::alloc(N_GLWE, BASE2K, K, RANK);
let original: GLWE<Vec<u8>> = GLWE::alloc(&module, BASE2K, K, RANK);
poulpy_hal::test_suite::serialization::test_reader_writer_interface(original); poulpy_hal::test_suite::serialization::test_reader_writer_interface(original);
} }
#[test] #[test]
fn glwe_compressed_serialization() { fn glwe_compressed_serialization() {
let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: GLWECompressed<Vec<u8>> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK);
let original: GLWECompressed<Vec<u8>> = GLWECompressed::alloc(&module, BASE2K, K, RANK);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn lwe_serialization() { fn lwe_serialization() {
let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: LWE<Vec<u8>> = LWE::alloc(N_LWE, BASE2K, K);
let original: LWE<Vec<u8>> = LWE::alloc(&module, N_LWE, BASE2K, K);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn lwe_compressed_serialization() { fn lwe_compressed_serialization() {
let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: LWECompressed<Vec<u8>> = LWECompressed::alloc(BASE2K, K);
let original: LWECompressed<Vec<u8>> = LWECompressed::alloc(&module, BASE2K, K);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn test_gglwe_serialization() { fn test_gglwe_serialization() {
let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: GGLWE<Vec<u8>> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
let original: GGLWE<Vec<u8>> = GGLWE::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn test_gglwe_compressed_serialization() { 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(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
let original: GGLWECompressed<Vec<u8>> = GGLWECompressed::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn test_glwe_switching_key_serialization() { 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(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
let original: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn test_glwe_switching_key_compressed_serialization() { 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>> = 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_reader_writer_interface(original);
} }
#[test] #[test]
fn test_automorphism_key_serialization() { 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(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_with(&module, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn test_automorphism_key_compressed_serialization() { 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(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn test_tensor_key_serialization() { 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(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: TensorKey<Vec<u8>> = TensorKey::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn test_tensor_key_compressed_serialization() { 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(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn glwe_to_lwe_switching_key_serialization() { 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(N_GLWE, BASE2K, K, RANK, DNUM);
let original: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn glwe_to_lwe_switching_key_compressed_serialization() { 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>> = 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_reader_writer_interface(original);
} }
#[test] #[test]
fn lwe_to_glwe_switching_key_serialization() { 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(N_GLWE, BASE2K, K, RANK, DNUM);
let original: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn lwe_to_glwe_switching_key_compressed_serialization() { 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>> = 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_reader_writer_interface(original);
} }
#[test] #[test]
fn lwe_switching_key_serialization() { 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(N_GLWE, BASE2K, K, DNUM);
let original: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc(&module, BASE2K, K, DNUM);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn lwe_switching_key_compressed_serialization() { 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(N_GLWE, BASE2K, K, DNUM);
let original: LWESwitchingKeyCompressed<Vec<u8>> = LWESwitchingKeyCompressed::alloc(&module, BASE2K, K, DNUM);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn ggsw_serialization() { fn ggsw_serialization() {
let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: GGSW<Vec<u8>> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: GGSW<Vec<u8>> = GGSW::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
#[test] #[test]
fn ggsw_compressed_serialization() { fn ggsw_compressed_serialization() {
let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(N_GLWE.as_u32() as u64); let original: GGSWCompressed<Vec<u8>> = GGSWCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: GGSWCompressed<Vec<u8>> = GGSWCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }

View File

@@ -1,21 +1,17 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphism, VecZnxFillUniform},
ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, layouts::{Backend, GaloisElement, Module, Scratch, ScratchOwned},
VecZnxAutomorphism,VecZnxFillUniform,
},
layouts::{Backend, Module, Scratch, ScratchOwned, GaloisElement},
source::Source, source::Source,
}; };
use crate::{ use crate::{
GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared, AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare,
AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecretPreparedAlloc, GLWESwitchingKeyDecompress, compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared,
GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKeyAlloc, GLWESwitchingKeyDecompress
}, },
noise::GGLWENoise, noise::GGLWENoise,
GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore
}; };
pub fn test_gglwe_automorphism_key_encrypt_sk<BE: Backend>(module: &Module<BE>) pub fn test_gglwe_automorphism_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
@@ -24,7 +20,6 @@ where
+ GGLWEKeyswitch<BE> + GGLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
+ GLWESwitchingKeyAlloc
+ GLWESwitchingKeyEncryptSk<BE> + GLWESwitchingKeyEncryptSk<BE>
+ GLWESwitchingKeyCompressedEncryptSk<BE> + GLWESwitchingKeyCompressedEncryptSk<BE>
+ GLWESwitchingKeyDecompress + GLWESwitchingKeyDecompress
@@ -32,7 +27,7 @@ where
+ VecZnxFillUniform + VecZnxFillUniform
+ VecZnxAutomorphism, + VecZnxAutomorphism,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
let base2k: usize = 12; let base2k: usize = 12;
let k_ksk: usize = 60; let k_ksk: usize = 60;
@@ -51,17 +46,15 @@ where
rank: rank.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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: 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 source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos));
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); sk.fill_ternary_prob(0.5, &mut source_xs);
let p = -5; let p = -5;
@@ -101,7 +94,6 @@ where
+ GGLWEKeyswitch<BE> + GGLWEKeyswitch<BE>
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
+ GLWESwitchingKeyAlloc
+ GLWESwitchingKeyEncryptSk<BE> + GLWESwitchingKeyEncryptSk<BE>
+ GLWESwitchingKeyCompressedEncryptSk<BE> + GLWESwitchingKeyCompressedEncryptSk<BE>
+ AutomorphismKeyDecompress + AutomorphismKeyDecompress
@@ -109,7 +101,7 @@ where
+ VecZnxFillUniform + VecZnxFillUniform
+ GGLWENoise<BE>, + GGLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
let base2k: usize = 12; let base2k: usize = 12;
let k_ksk: usize = 60; let k_ksk: usize = 60;
@@ -128,16 +120,14 @@ where
rank: rank.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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: 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( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos));
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); sk.fill_ternary_prob(0.5, &mut source_xs);
let p = -5; let p = -5;
@@ -159,7 +149,7 @@ where
let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into());
sk_out_prepared.prepare(module, &sk_out); 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.decompress(module, &atk_compressed);
atk.key atk.key

View File

@@ -1,15 +1,19 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform},
ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform,
},
layouts::{Backend, Module, Scratch, ScratchOwned}, layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source, source::Source,
}; };
use crate::{ use crate::{
decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
prepared::{GGLWEPrepare, GGLWEPreparedAlloc, GLWESecretPrepared}, GGLWELayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress decryption::GLWEDecrypt,
}, noise::GGLWENoise, GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore 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>) pub fn test_gglwe_switching_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
@@ -21,20 +25,19 @@ where
+ GLWEDecrypt<BE> + GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
+ GLWESwitchingKeyAlloc
+ GLWESwitchingKeyEncryptSk<BE> + GLWESwitchingKeyEncryptSk<BE>
+ VecZnxFillUniform + VecZnxFillUniform
+ GGLWENoise<BE>, + GGLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
let n = module.n();
let base2k: usize = 12; let base2k: usize = 12;
let k_ksk: usize = 54; let k_ksk: usize = 54;
let dsize: usize = k_ksk / base2k; let dsize: usize = k_ksk / base2k;
for rank_in in 1_usize..3 { for rank_in in 1_usize..3 {
for rank_out in 1_usize..3 { for rank_out in 1_usize..3 {
for di in 1_usize..dsize + 1 { for di in 1_usize..dsize + 1 {
let n: usize = module.n();
let dnum: usize = (k_ksk - di * base2k) / (di * base2k); let dnum: usize = (k_ksk - di * base2k) / (di * base2k);
let gglwe_infos: GGLWELayout = GGLWELayout { let gglwe_infos: GGLWELayout = GGLWELayout {
@@ -47,21 +50,19 @@ where
rank_out: rank_out.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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: 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 source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes( let mut scratch: ScratchOwned<BE> =
module, ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_infos));
&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); 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); 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()); let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_out.into());
sk_out_prepared.prepare(module, &sk_out); sk_out_prepared.prepare(module, &sk_out);
@@ -91,7 +92,6 @@ where
+ GLWEDecrypt<BE> + GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
+ GLWESwitchingKeyAlloc
+ GLWESwitchingKeyEncryptSk<BE> + GLWESwitchingKeyEncryptSk<BE>
+ GLWESwitchingKeyCompressedEncryptSk<BE> + GLWESwitchingKeyCompressedEncryptSk<BE>
+ GLWESwitchingKeyDecompress + GLWESwitchingKeyDecompress
@@ -100,13 +100,13 @@ where
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
let n: usize = module.n();
let base2k: usize = 12; let base2k: usize = 12;
let k_ksk: usize = 54; let k_ksk: usize = 54;
let dsize: usize = k_ksk / base2k; let dsize: usize = k_ksk / base2k;
for rank_in in 1_usize..3 { for rank_in in 1_usize..3 {
for rank_out in 1_usize..3 { for rank_out in 1_usize..3 {
for di in 1_usize..dsize + 1 { for di in 1_usize..dsize + 1 {
let n: usize = module.n();
let dnum: usize = (k_ksk - di * base2k) / (di * base2k); let dnum: usize = (k_ksk - di * base2k) / (di * base2k);
let gglwe_infos: GGLWELayout = GGLWELayout { let gglwe_infos: GGLWELayout = GGLWELayout {
@@ -120,7 +120,7 @@ where
}; };
let mut ksk_compressed: GLWESwitchingKeyCompressed<Vec<u8>> = 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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
@@ -130,10 +130,10 @@ where
&gglwe_infos, &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); 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); 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()); let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank_out.into());
sk_out_prepared.prepare(module, &sk_out); sk_out_prepared.prepare(module, &sk_out);
@@ -149,7 +149,7 @@ where
scratch.borrow(), 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.decompress(module, &ksk_compressed);
ksk.key ksk.key

View File

@@ -9,8 +9,8 @@ use crate::{
decryption::GLWEDecrypt, decryption::GLWEDecrypt,
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GLWE, GLWEAlloc, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPrepare, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPrepare, GLWEPublicKeyPreparedAlloc,
GLWEPublicKeyPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos,
compressed::GLWECompressed, compressed::GLWECompressed,
prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared},
}, },
@@ -18,13 +18,8 @@ use crate::{
pub fn test_glwe_encrypt_sk<BE: Backend>(module: &Module<BE>) pub fn test_glwe_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<BE>: GLWEAlloc Module<BE>:
+ GLWEEncryptSk<BE> GLWEEncryptSk<BE> + GLWEDecrypt<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + VecZnxFillUniform + GLWESub,
+ GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ VecZnxFillUniform
+ GLWESub,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
@@ -48,9 +43,9 @@ where
k: k_pt.into(), k: k_pt.into(),
}; };
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);
let mut pt_want: 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(module, &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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
@@ -59,7 +54,7 @@ where
let mut scratch: ScratchOwned<BE> = let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos)); 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); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
@@ -89,8 +84,7 @@ where
pub fn test_glwe_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>) pub fn test_glwe_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<BE>: GLWEAlloc Module<BE>: GLWECompressedEncryptSk<BE>
+ GLWECompressedEncryptSk<BE>
+ GLWEDecrypt<BE> + GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
@@ -120,10 +114,10 @@ where
k: k_pt.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_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
@@ -133,7 +127,7 @@ where
GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), 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); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
@@ -152,7 +146,7 @@ where
scratch.borrow(), 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.decompress(module, &ct_compressed);
ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
@@ -172,13 +166,8 @@ where
pub fn test_glwe_encrypt_zero_sk<BE: Backend>(module: &Module<BE>) pub fn test_glwe_encrypt_zero_sk<BE: Backend>(module: &Module<BE>)
where where
Module<BE>: GLWEAlloc Module<BE>:
+ GLWEEncryptSk<BE> GLWEEncryptSk<BE> + GLWEDecrypt<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + VecZnxFillUniform + GLWESub,
+ GLWEDecrypt<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>
+ VecZnxFillUniform
+ GLWESub,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>, Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{ {
@@ -195,7 +184,7 @@ where
rank: rank.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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]);
@@ -204,13 +193,13 @@ where
let mut scratch: ScratchOwned<BE> = let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos)); 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); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk); 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( ct.encrypt_zero_sk(
module, module,
@@ -227,8 +216,7 @@ where
pub fn test_glwe_encrypt_pk<BE: Backend>(module: &Module<BE>) pub fn test_glwe_encrypt_pk<BE: Backend>(module: &Module<BE>)
where where
Module<BE>: GLWEAlloc Module<BE>: GLWEEncryptPk<BE>
+ GLWEEncryptPk<BE>
+ GLWEPublicKeyPrepare<BE> + GLWEPublicKeyPrepare<BE>
+ GLWEPublicKeyPreparedAlloc<BE> + GLWEPublicKeyPreparedAlloc<BE>
+ GLWEPublicKeyGenerate<BE> + GLWEPublicKeyGenerate<BE>
@@ -253,9 +241,9 @@ where
rank: rank.into(), rank: rank.into(),
}; };
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);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &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(module, &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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
@@ -265,13 +253,13 @@ where
let mut scratch: ScratchOwned<BE> = let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos)); 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); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk); 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); 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); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);

View File

@@ -1,19 +1,26 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{
ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxSwitchRing ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize,
}, layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, oep::{VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl}, source::Source VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace,
VecZnxSwitchRing,
},
layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft},
source::Source,
}; };
use crate::{ use crate::{
decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk,
prepared::GLWESecretPrepared, Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyAlloc, TensorKeyCompressed, TensorKeyLayout decryption::GLWEDecrypt,
}, GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk 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>) pub fn test_gglwe_tensor_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<BE>: TensorKeyEncryptSk<BE> Module<BE>: TensorKeyEncryptSk<BE>
+ TensorKeyAlloc
+ GLWESecretPrepare<BE> + GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE> + GLWESecretPreparedAlloc<BE>
+ GLWEDecrypt<BE> + GLWEDecrypt<BE>
@@ -43,18 +50,15 @@ where
rank: rank.into(), 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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: 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 source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key_infos));
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); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk); sk_prepared.prepare(module, &sk);
@@ -67,11 +71,11 @@ where
scratch.borrow(), 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_dft = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big = module.vec_znx_big_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); let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
for i in 0..rank { for i in 0..rank {
@@ -92,9 +96,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
for row_i in 0..dnum { for row_i in 0..dnum {
let ct = tensor_key let ct = tensor_key.at(i, j).at(row_i, 0);
.at(i, j)
.at(row_i, 0);
ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow()); ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
@@ -111,20 +113,19 @@ where
pub fn test_gglwe_tensor_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>) pub fn test_gglwe_tensor_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<BE>: TensorKeyEncryptSk<BE> Module<BE>: TensorKeyEncryptSk<BE>
+ TensorKeyAlloc + GLWESecretPrepare<BE>
+ GLWESecretPrepare<BE> + GLWESecretPreparedAlloc<BE>
+ GLWESecretPreparedAlloc<BE> + GGLWETensorKeyCompressedEncryptSk<BE>
+ GGLWETensorKeyCompressedEncryptSk<BE> + GLWEDecrypt<BE>
+ GLWEDecrypt<BE> + VecZnxDftAlloc<BE>
+ VecZnxDftAlloc<BE> + VecZnxBigAlloc<BE>
+ VecZnxBigAlloc<BE> + VecZnxDftApply<BE>
+ VecZnxDftApply<BE> + SvpApplyDftToDft<BE>
+ SvpApplyDftToDft<BE> + VecZnxIdftApplyTmpA<BE>
+ VecZnxIdftApplyTmpA<BE> + VecZnxSubScalarInplace
+ VecZnxSubScalarInplace + VecZnxFillUniform
+ VecZnxFillUniform + VecZnxCopy
+ VecZnxCopy + VecZnxSwitchRing,
+ VecZnxSwitchRing,
// + VecZnxNormalizeInplaceImpl<BE> // + VecZnxNormalizeInplaceImpl<BE>
// + VecZnxNormalizeImpl<BE>, // + VecZnxNormalizeImpl<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
@@ -145,7 +146,7 @@ where
rank: rank.into(), 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_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
@@ -155,7 +156,7 @@ where
&tensor_key_infos, &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); sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk); sk_prepared.prepare(module, &sk);
@@ -164,14 +165,14 @@ where
tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); 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); 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_dft = module.vec_znx_dft_alloc(1, 1);
let mut sk_ij_big = module.vec_znx_big_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); let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
for i in 0..rank { for i in 0..rank {