This commit is contained in:
Pro7ech
2025-10-13 12:14:11 +02:00
parent 662e533eac
commit cf377ff243
94 changed files with 1892 additions and 1235 deletions

View File

@@ -1,7 +1,7 @@
use poulpy_core::layouts::{ use poulpy_core::layouts::{
Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, Base2K, Degree, Dnum, Dsize, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank,
TorusPrecision, TorusPrecision,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
}; };
use std::hint::black_box; use std::hint::black_box;
@@ -62,13 +62,13 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
rank, rank,
}; };
let mut ct_ggsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_layout); let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_layout);
let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_layout); let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_layout);
let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_layout); let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&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(
GGSWCiphertext::encrypt_sk_scratch_space(&module, &ggsw_layout) GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout)
| GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout)
| GLWECiphertext::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), | GLWECiphertext::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout),
); );
@@ -98,7 +98,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) {
scratch.borrow(), scratch.borrow(),
); );
let ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); let ggsw_prepared: GGSWPrepared<Vec<u8>, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow());
move || { move || {
ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow()); ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow());
@@ -163,12 +163,12 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
rank, rank,
}; };
let mut ct_ggsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_layout); let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_layout);
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_layout); let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&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(
GGSWCiphertext::encrypt_sk_scratch_space(&module, &ggsw_layout) GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout)
| GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout)
| GLWECiphertext::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout), | GLWECiphertext::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout),
); );
@@ -198,7 +198,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) {
scratch.borrow(), scratch.borrow(),
); );
let ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); let ggsw_prepared: GGSWPrepared<Vec<u8>, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow());
move || { move || {
let scratch_borrow = scratch.borrow(); let scratch_borrow = scratch.borrow();

View File

@@ -1,7 +1,7 @@
use poulpy_core::layouts::{ use poulpy_core::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWESwitchingKey, GGLWESwitchingKeyLayout, AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWECiphertextLayout, GLWESecret,
GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, TorusPrecision, GLWESwitchingKey, GLWESwitchingKeyLayout, Rank, TorusPrecision,
prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc},
}; };
use std::{hint::black_box, time::Duration}; use std::{hint::black_box, time::Duration};
@@ -39,7 +39,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
let dnum: Dnum = p.k_ct_in.div_ceil(p.base2k.0 * dsize.0).into(); let dnum: Dnum = p.k_ct_in.div_ceil(p.base2k.0 * dsize.0).into();
let gglwe_atk_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let gglwe_atk_layout: AutomorphismKeyLayout = AutomorphismKeyLayout {
n, n,
base2k, base2k,
k: k_gglwe, k: k_gglwe,
@@ -62,12 +62,12 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
rank, rank,
}; };
let mut ksk: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&gglwe_atk_layout); let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&gglwe_atk_layout);
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_layout); let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_layout);
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_layout); let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_layout);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GGLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout)
| GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout)
| GLWECiphertext::keyswitch_scratch_space( | GLWECiphertext::keyswitch_scratch_space(
&module, &module,
@@ -102,7 +102,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) {
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, _> = ksk.prepare_alloc(&module, scratch.borrow()); let ksk_prepared: AutomorphismKeyPrepared<Vec<u8>, _> = ksk.prepare_alloc(&module, scratch.borrow());
move || { move || {
ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()); ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow());
@@ -157,7 +157,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
let dnum: Dnum = p.k_ct.div_ceil(p.base2k).into(); let dnum: Dnum = p.k_ct.div_ceil(p.base2k).into();
let gglwe_layout: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_layout: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n, n,
base2k, base2k,
k: k_ksk, k: k_ksk,
@@ -174,11 +174,11 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
rank, rank,
}; };
let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_layout); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_layout);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_layout); let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_layout);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GGLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout) GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout)
| GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout)
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout), | GLWECiphertext::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout),
); );
@@ -211,7 +211,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) {
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow());
move || { move || {
ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()); ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow());

View File

@@ -7,9 +7,9 @@ use poulpy_hal::{
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero},
}; };
use crate::layouts::{GGLWEAutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::AutomorphismKeyPrepared};
impl GGLWEAutomorphismKey<Vec<u8>> { impl AutomorphismKey<Vec<u8>> {
pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY>( pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY>(
module: &Module<B>, module: &Module<B>,
out_infos: &OUT, out_infos: &OUT,
@@ -36,16 +36,16 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
KEY: GGLWEInfos, KEY: GGLWEInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,
{ {
GGLWEAutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos) AutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos)
} }
} }
impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> { impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
pub fn automorphism<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( pub fn automorphism<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>, lhs: &AutomorphismKey<DataLhs>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -133,7 +133,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes

View File

@@ -9,11 +9,11 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
GGLWEInfos, GGSWCiphertext, GGSWInfos, GLWECiphertext, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext,
prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared}, prepared::{AutomorphismKeyPrepared, TensorKeyPrepared},
}; };
impl GGSWCiphertext<Vec<u8>> { impl GGSW<Vec<u8>> {
pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY, TSK>( pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY, TSK>(
module: &Module<B>, module: &Module<B>,
out_infos: &OUT, out_infos: &OUT,
@@ -40,7 +40,7 @@ impl GGSWCiphertext<Vec<u8>> {
&in_infos.glwe_layout(), &in_infos.glwe_layout(),
key_infos, key_infos,
); );
let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, out_infos, tsk_infos); let expand: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos);
ci_dft + (ks_internal | expand) ci_dft + (ks_internal | expand)
} }
@@ -60,17 +60,17 @@ impl GGSWCiphertext<Vec<u8>> {
+ VecZnxNormalizeTmpBytes + VecZnxNormalizeTmpBytes
+ VecZnxBigNormalizeTmpBytes, + VecZnxBigNormalizeTmpBytes,
{ {
GGSWCiphertext::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos) GGSW::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos)
} }
} }
impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> { impl<DataSelf: DataMut> GGSW<DataSelf> {
pub fn automorphism<DataLhs: DataRef, DataAk: DataRef, DataTsk: DataRef, B: Backend>( pub fn automorphism<DataLhs: DataRef, DataAk: DataRef, DataTsk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>, lhs: &GGSW<DataLhs>,
auto_key: &GGLWEAutomorphismKeyPrepared<DataAk, B>, auto_key: &AutomorphismKeyPrepared<DataAk, B>,
tensor_key: &GGLWETensorKeyPrepared<DataTsk, B>, tensor_key: &TensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -121,7 +121,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
self.rank(), self.rank(),
tensor_key.rank_out() tensor_key.rank_out()
); );
assert!(scratch.available() >= GGSWCiphertext::automorphism_scratch_space(module, self, lhs, auto_key, tensor_key)) assert!(scratch.available() >= GGSW::automorphism_scratch_space(module, self, lhs, auto_key, tensor_key))
}; };
// Keyswitch the j-th row of the col 0 // Keyswitch the j-th row of the col 0
@@ -137,8 +137,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn automorphism_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>( pub fn automorphism_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
auto_key: &GGLWEAutomorphismKeyPrepared<DataKsk, B>, auto_key: &AutomorphismKeyPrepared<DataKsk, B>,
tensor_key: &GGLWETensorKeyPrepared<DataTsk, B>, tensor_key: &TensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes

View File

@@ -8,7 +8,7 @@ use poulpy_hal::{
layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig},
}; };
use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared}; use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared};
impl GLWECiphertext<Vec<u8>> { impl GLWECiphertext<Vec<u8>> {
pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY>( pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY>(
@@ -41,7 +41,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -67,7 +67,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -94,7 +94,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -135,7 +135,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_add_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_add_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -177,7 +177,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -219,7 +219,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_sub_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_sub_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -262,7 +262,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -304,7 +304,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_sub_negate_inplace<DataRhs: DataRef, B: Backend>( pub fn automorphism_sub_negate_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes

View File

@@ -9,18 +9,19 @@ use crate::{
encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{GGLWEAutomorphismKeyCompressed, GGLWEAutomorphismKeyCompressedToMut, GGLWEKeyCompressed}, compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut, GLWESwitchingKeyCompressed},
}, },
}; };
impl GGLWEAutomorphismKeyCompressed<Vec<u8>> { impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes,
{ {
assert_eq!(module.n() as u32, infos.n()); assert_eq!(module.n() as u32, infos.n());
GGLWEKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos)
+ GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out())
} }
} }
@@ -34,7 +35,7 @@ pub trait GGLWEAutomorphismKeyCompressedEncryptSk<B: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
R: GGLWEAutomorphismKeyCompressedToMut, R: AutomorphismKeyCompressedToMut,
S: GLWESecretToRef; S: GLWESecretToRef;
} }
@@ -53,10 +54,10 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
R: GGLWEAutomorphismKeyCompressedToMut, R: AutomorphismKeyCompressedToMut,
S: GLWESecretToRef, S: GLWESecretToRef,
{ {
let res: &mut GGLWEAutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref(); let sk: &GLWESecret<&[u8]> = &sk.to_ref();
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@@ -65,10 +66,10 @@ where
assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(res.rank_out(), res.rank_in());
assert_eq!(sk.rank(), res.rank_out()); assert_eq!(sk.rank(), res.rank_out());
assert!( assert!(
scratch.available() >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res), scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res),
"scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}", "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}",
scratch.available(), scratch.available(),
GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res) AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res)
) )
} }
@@ -92,7 +93,7 @@ where
} }
} }
impl<DataSelf: DataMut> GGLWEAutomorphismKeyCompressed<DataSelf> { impl<DataSelf: DataMut> AutomorphismKeyCompressed<DataSelf> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataSk: DataRef, B: Backend>( pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
&mut self, &mut self,

View File

@@ -11,7 +11,7 @@ use crate::{
TakeGLWEPt, TakeGLWEPt,
encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal},
layouts::{ layouts::{
GGLWECiphertext, GGLWEInfos, LWEInfos, GGLWE, GGLWEInfos, LWEInfos,
compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut}, compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
}, },
@@ -40,7 +40,7 @@ impl GGLWECiphertextCompressed<Vec<u8>> {
A: GGLWEInfos, A: GGLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
{ {
GGLWECiphertext::encrypt_sk_scratch_space(module, infos) GGLWE::encrypt_sk_scratch_space(module, infos)
} }
} }

View File

@@ -11,25 +11,25 @@ use crate::{
TakeGLWESecretPrepared, TakeGLWESecretPrepared,
encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
layouts::{ layouts::{
Degree, GGLWECiphertext, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{GGLWEKeyCompressed, GGLWEKeyCompressedToMut}, compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut},
prepared::GLWESecretPrepared, prepared::GLWESecretPrepared,
}, },
}; };
impl GGLWEKeyCompressed<Vec<u8>> { impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes,
{ {
(GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1))
+ ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into())
+ GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) + GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out())
} }
} }
impl<DataSelf: DataMut> GGLWEKeyCompressed<DataSelf> { impl<DataSelf: DataMut> GLWESwitchingKeyCompressed<DataSelf> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>( pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
&mut self, &mut self,
@@ -56,7 +56,7 @@ pub trait GGLWEKeyCompressedEncryptSk<B: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
R: GGLWEKeyCompressedToMut, R: GLWESwitchingKeyCompressedToMut,
SI: GLWESecretToRef, SI: GLWESecretToRef,
SO: GLWESecretToRef; SO: GLWESecretToRef;
} }
@@ -80,25 +80,25 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
R: GGLWEKeyCompressedToMut, R: GLWESwitchingKeyCompressedToMut,
SI: GLWESecretToRef, SI: GLWESecretToRef,
SO: GLWESecretToRef, SO: GLWESecretToRef,
{ {
let res: &mut GGLWEKeyCompressed<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
use crate::layouts::GGLWESwitchingKey; use crate::layouts::GLWESwitchingKey;
assert!(sk_in.n().0 <= self.n() as u32); assert!(sk_in.n().0 <= self.n() as u32);
assert!(sk_out.n().0 <= self.n() as u32); assert!(sk_out.n().0 <= self.n() as u32);
assert!( assert!(
scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(self, res), scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(self, res),
"scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
scratch.available(), scratch.available(),
GGLWESwitchingKey::encrypt_sk_scratch_space(self, res) GLWESwitchingKey::encrypt_sk_scratch_space(self, res)
) )
} }

View File

@@ -11,20 +11,19 @@ use crate::{
TakeGLWESecret, TakeGLWESecretPrepared, TakeGLWESecret, TakeGLWESecretPrepared,
encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk,
layouts::{ layouts::{
GGLWEInfos, GGLWETensorKey, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey,
compressed::{GGLWETensorKeyCompressed, GGLWETensorKeyCompressedToMut}, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
prepared::Prepare,
}, },
}; };
impl GGLWETensorKeyCompressed<Vec<u8>> { impl TensorKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
Module<B>: Module<B>:
SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes,
{ {
GGLWETensorKey::encrypt_sk_scratch_space(module, infos) TensorKey::encrypt_sk_scratch_space(module, infos)
} }
} }
@@ -37,7 +36,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk<B: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
R: GGLWETensorKeyCompressedToMut, R: TensorKeyCompressedToMut,
S: GLWESecretToRef; S: GLWESecretToRef;
} }
@@ -59,10 +58,10 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
R: GGLWETensorKeyCompressedToMut, R: TensorKeyCompressedToMut,
S: GLWESecretToRef, S: GLWESecretToRef,
{ {
let res: &mut GGLWETensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref(); let sk: &GLWESecret<&[u8]> = &sk.to_ref();
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@@ -119,7 +118,7 @@ where
} }
} }
impl<DataSelf: DataMut> GGLWETensorKeyCompressed<DataSelf> { impl<DataSelf: DataMut> TensorKeyCompressed<DataSelf> {
pub fn encrypt_sk<DataSk: DataRef, B: Backend>( pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,

View File

@@ -8,7 +8,7 @@ use crate::{
TakeGLWEPt, TakeGLWEPt,
encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal},
layouts::{ layouts::{
GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, GGSW, GGSWInfos, GLWEInfos, LWEInfos,
compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut}, compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
}, },
@@ -20,7 +20,7 @@ impl GGSWCiphertextCompressed<Vec<u8>> {
A: GGSWInfos, A: GGSWInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
{ {
GGSWCiphertext::encrypt_sk_scratch_space(module, infos) GGSW::encrypt_sk_scratch_space(module, infos)
} }
} }

View File

@@ -1,17 +1,15 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes},
ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal,
VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
},
layouts::{Backend, DataMut, DataRef, Module, Scratch}, layouts::{Backend, DataMut, DataRef, Module, Scratch},
source::Source, source::Source,
}; };
use crate::{ use crate::{
encryption::{SIGMA, glwe_ct::glwe_encrypt_sk_internal}, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal},
layouts::{ layouts::{
GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, compressed::GLWECiphertextCompressed, prepared::GLWESecretPrepared, GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos,
compressed::{GLWECiphertextCompressed, GLWECiphertextCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
}, },
}; };
@@ -25,6 +23,60 @@ impl GLWECiphertextCompressed<Vec<u8>> {
} }
} }
pub trait GLWECompressedEncryptSk<B: Backend> {
fn glwe_compressed_encrypt_sk<R, P, S>(
&self,
res: &mut R,
pt: &P,
sk: &S,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextCompressedToMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<B>;
}
impl<B: Backend> GLWECompressedEncryptSk<B> for Module<B>
where
Module<B>: GLWEEncryptSkInternal<B>,
{
fn glwe_compressed_encrypt_sk<R, P, S>(
&self,
res: &mut R,
pt: &P,
sk: &S,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextCompressedToMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<B>,
{
let res: &mut GLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut();
let mut source_xa: Source = Source::new(seed_xa);
let cols: usize = (res.rank() + 1).into();
self.glwe_encrypt_sk_internal(
res.base2k().into(),
res.k().into(),
&mut res.data,
cols,
true,
Some((pt, 0)),
sk,
&mut source_xa,
source_xe,
SIGMA,
scratch,
);
res.seed = seed_xa;
}
}
impl<D: DataMut> GLWECiphertextCompressed<D> { impl<D: DataMut> GLWECiphertextCompressed<D> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>( pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
@@ -36,65 +88,8 @@ impl<D: DataMut> GLWECiphertextCompressed<D> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: GLWECompressedEncryptSk<B>,
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub,
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
{ {
self.encrypt_sk_internal(module, Some((pt, 0)), sk, seed_xa, source_xe, scratch); module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch);
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn encrypt_sk_internal<DataPt: DataRef, DataSk: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk: &GLWESecretPrepared<DataSk, B>,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
Module<B>: VecZnxDftAllocBytes
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub,
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
{
let mut source_xa = Source::new(seed_xa);
let cols: usize = (self.rank() + 1).into();
glwe_encrypt_sk_internal(
module,
self.base2k().into(),
self.k().into(),
&mut self.data,
cols,
true,
pt,
sk,
&mut source_xa,
source_xe,
SIGMA,
scratch,
);
self.seed = seed_xa;
} }
} }

View File

@@ -12,12 +12,11 @@ use poulpy_hal::{
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretPrepared, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWEAutomorphismKeyToMut, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, GLWESecretToRef, AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos,
LWEInfos,
}, },
}; };
impl GGLWEAutomorphismKey<Vec<u8>> { impl AutomorphismKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<BE: Backend, A>(module: &Module<BE>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<BE: Backend, A>(module: &Module<BE>, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -28,7 +27,7 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKey" "rank_in != rank_out is not supported for GGLWEAutomorphismKey"
); );
GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout())
} }
pub fn encrypt_pk_scratch_space<BE: Backend, A>(module: &Module<BE>, _infos: &A) -> usize pub fn encrypt_pk_scratch_space<BE: Backend, A>(module: &Module<BE>, _infos: &A) -> usize
@@ -40,7 +39,7 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
_infos.rank_out(), _infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKey" "rank_in != rank_out is not supported for GGLWEAutomorphismKey"
); );
GGLWESwitchingKey::encrypt_pk_scratch_space(module, _infos) GLWESwitchingKey::encrypt_pk_scratch_space(module, _infos)
} }
} }
@@ -54,13 +53,13 @@ pub trait GGLWEAutomorphismKeyEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
A: GGLWEAutomorphismKeyToMut, A: AutomorphismKeyToMut,
B: GLWESecretToRef; B: GLWESecretToRef;
} }
impl<DM: DataMut> GGLWEAutomorphismKey<DM> impl<DM: DataMut> AutomorphismKey<DM>
where where
Self: GGLWEAutomorphismKeyToMut, Self: AutomorphismKeyToMut,
{ {
pub fn encrypt_sk<S, BE: Backend>( pub fn encrypt_sk<S, BE: Backend>(
&mut self, &mut self,
@@ -109,10 +108,10 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
A: GGLWEAutomorphismKeyToMut, A: AutomorphismKeyToMut,
B: GLWESecretToRef, B: GLWESecretToRef,
{ {
let res: &mut GGLWEAutomorphismKey<&mut [u8]> = &mut res.to_mut(); let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref(); let sk: &GLWESecret<&[u8]> = &sk.to_ref();
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@@ -123,10 +122,10 @@ where
assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(res.rank_out(), res.rank_in());
assert_eq!(sk.rank(), res.rank_out()); assert_eq!(sk.rank(), res.rank_out());
assert!( assert!(
scratch.available() >= GGLWEAutomorphismKey::encrypt_sk_scratch_space(self, res), scratch.available() >= AutomorphismKey::encrypt_sk_scratch_space(self, res),
"scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {:?}", "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {:?}",
scratch.available(), scratch.available(),
GGLWEAutomorphismKey::encrypt_sk_scratch_space(self, res) AutomorphismKey::encrypt_sk_scratch_space(self, res)
) )
} }

View File

@@ -11,12 +11,12 @@ use crate::{
TakeGLWEPt, TakeGLWEPt,
encryption::glwe_ct::GLWEEncryptSk, encryption::glwe_ct::GLWEEncryptSk,
layouts::{ layouts::{
GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, GGLWE, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
}, },
}; };
impl GGLWECiphertext<Vec<u8>> { impl GGLWE<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -68,7 +68,7 @@ where
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>, S: GLWESecretPreparedToRef<B>,
{ {
let res: &mut GGLWECiphertext<&mut [u8]> = &mut res.to_mut(); let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@@ -93,12 +93,12 @@ where
assert_eq!(res.n(), sk.n()); assert_eq!(res.n(), sk.n());
assert_eq!(pt.n() as u32, sk.n()); assert_eq!(pt.n() as u32, sk.n());
assert!( assert!(
scratch.available() >= GGLWECiphertext::encrypt_sk_scratch_space(self, res), scratch.available() >= GGLWE::encrypt_sk_scratch_space(self, res),
"scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(self, res.rank()={}, res.size()={}): {}", "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(self, res.rank()={}, res.size()={}): {}",
scratch.available(), scratch.available(),
res.rank_out(), res.rank_out(),
res.size(), res.size(),
GGLWECiphertext::encrypt_sk_scratch_space(self, res) GGLWE::encrypt_sk_scratch_space(self, res)
); );
assert!( assert!(
res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0,
@@ -148,7 +148,7 @@ where
} }
} }
impl<DataSelf: DataMut> GGLWECiphertext<DataSelf> { impl<DataSelf: DataMut> GGLWE<DataSelf> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>( pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
&mut self, &mut self,

View File

@@ -11,18 +11,16 @@ use poulpy_hal::{
use crate::{ use crate::{
TakeGLWESecretPrepared, TakeGLWESecretPrepared,
layouts::{ layouts::{Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, prepared::GLWESecretPrepared},
Degree, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, LWEInfos, prepared::GLWESecretPrepared,
},
}; };
impl GGLWESwitchingKey<Vec<u8>> { impl GLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
{ {
(GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1))
+ ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into())
+ GLWESecretPrepared::alloc_bytes(module, &infos.glwe_layout()) + GLWESecretPrepared::alloc_bytes(module, &infos.glwe_layout())
} }
@@ -31,11 +29,11 @@ impl GGLWESwitchingKey<Vec<u8>> {
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
GGLWECiphertext::encrypt_pk_scratch_space(module, _infos) GGLWE::encrypt_pk_scratch_space(module, _infos)
} }
} }
impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> { impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>( pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
&mut self, &mut self,
@@ -70,10 +68,10 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
assert!(sk_in.n().0 <= module.n() as u32); assert!(sk_in.n().0 <= module.n() as u32);
assert!(sk_out.n().0 <= module.n() as u32); assert!(sk_out.n().0 <= module.n() as u32);
assert!( assert!(
scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(module, self), scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(module, self),
"scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
scratch.available(), scratch.available(),
GGLWESwitchingKey::encrypt_sk_scratch_space(module, self) GLWESwitchingKey::encrypt_sk_scratch_space(module, self)
) )
} }

View File

@@ -12,12 +12,11 @@ use poulpy_hal::{
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretPrepared, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{ layouts::{
Degree, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GLWEInfos, GLWESecret, LWEInfos, Rank, Degree, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared,
prepared::{GLWESecretPrepared, Prepare},
}, },
}; };
impl GGLWETensorKey<Vec<u8>> { impl TensorKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -29,11 +28,11 @@ impl GGLWETensorKey<Vec<u8>> {
+ module.vec_znx_big_alloc_bytes(1, 1) + module.vec_znx_big_alloc_bytes(1, 1)
+ module.vec_znx_dft_alloc_bytes(1, 1) + module.vec_znx_dft_alloc_bytes(1, 1)
+ GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1))
+ GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos)
} }
} }
impl<DataSelf: DataMut> GGLWETensorKey<DataSelf> { impl<DataSelf: DataMut> TensorKey<DataSelf> {
pub fn encrypt_sk<DataSk: DataRef, B: Backend>( pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,

View File

@@ -8,12 +8,12 @@ use crate::{
SIGMA, TakeGLWEPt, SIGMA, TakeGLWEPt,
encryption::glwe_ct::GLWEEncryptSkInternal, encryption::glwe_ct::GLWEEncryptSkInternal,
layouts::{ layouts::{
GGSWCiphertext, GGSWCiphertextToMut, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, GGSW, GGSWInfos, GGSWToMut, GLWECiphertext, GLWEInfos, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
}, },
}; };
impl GGSWCiphertext<Vec<u8>> { impl GGSW<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where where
A: GGSWInfos, A: GGSWInfos,
@@ -37,7 +37,7 @@ pub trait GGSWEncryptSk<B: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
R: GGSWCiphertextToMut, R: GGSWToMut,
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>; S: GLWESecretPreparedToRef<B>;
} }
@@ -56,11 +56,11 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
R: GGSWCiphertextToMut, R: GGSWToMut,
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>, S: GLWESecretPreparedToRef<B>,
{ {
let res: &mut GGSWCiphertext<&mut [u8]> = &mut res.to_mut(); let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref();
@@ -106,7 +106,7 @@ where
} }
} }
impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> { impl<DataSelf: DataMut> GGSW<DataSelf> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>( pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
&mut self, &mut self,

View File

@@ -11,22 +11,24 @@ use poulpy_hal::{
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretPrepared, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWEInfos, GGLWESwitchingKey, GLWESecret, GLWEToLWEKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared}, layouts::{
GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared,
},
}; };
impl GLWEToLWEKey<Vec<u8>> { impl GLWEToLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where where
A: GGLWEInfos, A: GGLWEInfos,
Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
{ {
GLWESecretPrepared::alloc_bytes_with(module, infos.rank_in()) GLWESecretPrepared::alloc_bytes_with(module, infos.rank_in())
+ (GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos)
| GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in())) | GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in()))
} }
} }
impl<D: DataMut> GLWEToLWEKey<D> { impl<D: DataMut> GLWEToLWESwitchingKey<D> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DLwe, DGlwe, B: Backend>( pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
&mut self, &mut self,

View File

@@ -12,7 +12,7 @@ use poulpy_hal::{
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretPrepared, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{ layouts::{
Degree, GGLWEInfos, GGLWESwitchingKey, GLWESecret, LWEInfos, LWESecret, LWESwitchingKey, Rank, Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank,
prepared::GLWESecretPrepared, prepared::GLWESecretPrepared,
}, },
}; };
@@ -40,7 +40,7 @@ impl LWESwitchingKey<Vec<u8>> {
); );
GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1))
+ GLWESecretPrepared::alloc_bytes_with(module, Rank(1)) + GLWESecretPrepared::alloc_bytes_with(module, Rank(1))
+ GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos)
} }
} }

View File

@@ -11,7 +11,7 @@ use poulpy_hal::{
use crate::{ use crate::{
TakeGLWESecret, TakeGLWESecretPrepared, TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{Degree, GGLWEInfos, GGLWESwitchingKey, GLWESecret, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, layouts::{Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank},
}; };
impl LWEToGLWESwitchingKey<Vec<u8>> { impl LWEToGLWESwitchingKey<Vec<u8>> {
@@ -25,7 +25,7 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
Rank(1), Rank(1),
"rank_in != 1 is not supported for LWEToGLWESwitchingKey" "rank_in != 1 is not supported for LWEToGLWESwitchingKey"
); );
GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) GLWESwitchingKey::encrypt_sk_scratch_space(module, infos)
+ GLWESecret::alloc_bytes_with(Degree(module.n() as u32), infos.rank_in()) + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), infos.rank_in())
} }
} }

View File

@@ -7,9 +7,9 @@ use poulpy_hal::{
layouts::{Backend, DataMut, DataRef, Module, Scratch}, layouts::{Backend, DataMut, DataRef, Module, Scratch},
}; };
use crate::layouts::{GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GGSWInfos, prepared::GGSWCiphertextPrepared}; use crate::layouts::{AutomorphismKey, GGLWEInfos, GGSWInfos, GLWESwitchingKey, prepared::GGSWPrepared};
impl GGLWEAutomorphismKey<Vec<u8>> { impl AutomorphismKey<Vec<u8>> {
pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>( pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>(
module: &Module<B>, module: &Module<B>,
out_infos: &OUT, out_infos: &OUT,
@@ -22,7 +22,7 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
GGSW: GGSWInfos, GGSW: GGSWInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes,
{ {
GGLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos) GLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos)
} }
pub fn external_product_inplace_scratch_space<B: Backend, OUT, GGSW>( pub fn external_product_inplace_scratch_space<B: Backend, OUT, GGSW>(
@@ -35,16 +35,16 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
GGSW: GGSWInfos, GGSW: GGSWInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes,
{ {
GGLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos) GLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos)
} }
} }
impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> { impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
pub fn external_product<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( pub fn external_product<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>, lhs: &AutomorphismKey<DataLhs>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>, rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -64,7 +64,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>( pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>, rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes

View File

@@ -7,9 +7,9 @@ use poulpy_hal::{
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero},
}; };
use crate::layouts::{GGLWEInfos, GGLWESwitchingKey, GGSWInfos, GLWECiphertext, prepared::GGSWCiphertextPrepared}; use crate::layouts::{GGLWEInfos, GGSWInfos, GLWECiphertext, GLWESwitchingKey, prepared::GGSWPrepared};
impl GGLWESwitchingKey<Vec<u8>> { impl GLWESwitchingKey<Vec<u8>> {
pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>( pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>(
module: &Module<B>, module: &Module<B>,
out_infos: &OUT, out_infos: &OUT,
@@ -44,12 +44,12 @@ impl GGLWESwitchingKey<Vec<u8>> {
} }
} }
impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> { impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
pub fn external_product<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( pub fn external_product<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWESwitchingKey<DataLhs>, lhs: &GLWESwitchingKey<DataLhs>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>, rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -107,7 +107,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>( pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>, rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes

View File

@@ -7,9 +7,9 @@ use poulpy_hal::{
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero},
}; };
use crate::layouts::{GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWCiphertextPrepared}; use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWPrepared};
impl GGSWCiphertext<Vec<u8>> { impl GGSW<Vec<u8>> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>( pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>(
module: &Module<B>, module: &Module<B>,
@@ -45,12 +45,12 @@ impl GGSWCiphertext<Vec<u8>> {
} }
} }
impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> { impl<DataSelf: DataMut> GGSW<DataSelf> {
pub fn external_product<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( pub fn external_product<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>, lhs: &GGSW<DataLhs>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>, rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -86,7 +86,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
rhs.rank() rhs.rank()
); );
assert!(scratch.available() >= GGSWCiphertext::external_product_scratch_space(module, self, lhs, rhs)) assert!(scratch.available() >= GGSW::external_product_scratch_space(module, self, lhs, rhs))
} }
let min_dnum: usize = self.dnum().min(lhs.dnum()).into(); let min_dnum: usize = self.dnum().min(lhs.dnum()).into();
@@ -105,7 +105,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>( pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>, rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes

View File

@@ -11,7 +11,7 @@ use crate::{
GLWEExternalProduct, GLWEExternalProductInplace, GLWEExternalProduct, GLWEExternalProductInplace,
layouts::{ layouts::{
GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, LWEInfos, GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, LWEInfos,
prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef}, prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared},
}, },
}; };
@@ -74,7 +74,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>, rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProduct<B>, Module<B>: GLWEExternalProduct<B>,
@@ -85,7 +85,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>( pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>, rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: GLWEExternalProductInplace<B>, Module<B>: GLWEExternalProductInplace<B>,
@@ -113,7 +113,7 @@ where
D: GGSWCiphertextPreparedToRef<BE>, D: GGSWCiphertextPreparedToRef<BE>,
{ {
let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut();
let rhs: &GGSWCiphertextPrepared<&[u8], BE> = &ggsw.to_ref(); let rhs: &GGSWPrepared<&[u8], BE> = &ggsw.to_ref();
let basek_in: usize = res.base2k().into(); let basek_in: usize = res.base2k().into();
let basek_ggsw: usize = rhs.base2k().into(); let basek_ggsw: usize = rhs.base2k().into();
@@ -237,7 +237,7 @@ where
let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut();
let lhs: &GLWECiphertext<&[u8]> = &lhs.to_ref(); let lhs: &GLWECiphertext<&[u8]> = &lhs.to_ref();
let rhs: &GGSWCiphertextPrepared<&[u8], BE> = &rhs.to_ref(); let rhs: &GGSWPrepared<&[u8], BE> = &rhs.to_ref();
let basek_in: usize = lhs.base2k().into(); let basek_in: usize = lhs.base2k().into();
let basek_ggsw: usize = rhs.base2k().into(); let basek_ggsw: usize = rhs.base2k().into();

View File

@@ -13,7 +13,7 @@ use poulpy_hal::{
use crate::{ use crate::{
GLWEOperations, TakeGLWECt, GLWEOperations, TakeGLWECt,
layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared}, layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared},
}; };
/// [GLWEPacker] enables only the fly GLWE packing /// [GLWEPacker] enables only the fly GLWE packing
@@ -116,7 +116,7 @@ impl GLWEPacker {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
a: Option<&GLWECiphertext<DataA>>, a: Option<&GLWECiphertext<DataA>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -191,7 +191,7 @@ fn pack_core<D: DataRef, DataAK: DataRef, B: Backend>(
a: Option<&GLWECiphertext<D>>, a: Option<&GLWECiphertext<D>>,
accumulators: &mut [Accumulator], accumulators: &mut [Accumulator],
i: usize, i: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -285,7 +285,7 @@ fn combine<D: DataRef, DataAK: DataRef, B: Backend>(
acc: &mut Accumulator, acc: &mut Accumulator,
b: Option<&GLWECiphertext<D>>, b: Option<&GLWECiphertext<D>>,
i: usize, i: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -397,7 +397,7 @@ pub fn glwe_packing<D: DataMut, ATK, B: Backend>(
module: &Module<B>, module: &Module<B>,
cts: &mut HashMap<usize, &mut GLWECiphertext<D>>, cts: &mut HashMap<usize, &mut GLWECiphertext<D>>,
log_gap_out: usize, log_gap_out: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<ATK, B>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<ATK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
ATK: DataRef, ATK: DataRef,
@@ -439,7 +439,7 @@ pub fn glwe_packing<D: DataMut, ATK, B: Backend>(
(0..log_n - log_gap_out).for_each(|i| { (0..log_n - log_gap_out).for_each(|i| {
let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); let t: usize = (1 << log_n).min(1 << (log_n - 1 - i));
let auto_key: &GGLWEAutomorphismKeyPrepared<ATK, B> = if i == 0 { let auto_key: &AutomorphismKeyPrepared<ATK, B> = if i == 0 {
auto_keys.get(&-1).unwrap() auto_keys.get(&-1).unwrap()
} else { } else {
auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap() auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap()
@@ -466,7 +466,7 @@ fn pack_internal<A: DataMut, D: DataMut, DataAK: DataRef, B: Backend>(
a: &mut Option<&mut GLWECiphertext<A>>, a: &mut Option<&mut GLWECiphertext<A>>,
b: &mut Option<&mut GLWECiphertext<D>>, b: &mut Option<&mut GLWECiphertext<D>>,
i: usize, i: usize,
auto_key: &GGLWEAutomorphismKeyPrepared<DataAK, B>, auto_key: &AutomorphismKeyPrepared<DataAK, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxRotateInplace<B> Module<B>: VecZnxRotateInplace<B>

View File

@@ -11,9 +11,7 @@ use poulpy_hal::{
use crate::{ use crate::{
TakeGLWECt, TakeGLWECt,
layouts::{ layouts::{Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared},
Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared,
},
operations::GLWEOperations, operations::GLWEOperations,
}; };
@@ -72,7 +70,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
start: usize, start: usize,
end: usize, end: usize,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -100,7 +98,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
module: &Module<B>, module: &Module<B>,
start: usize, start: usize,
end: usize, end: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes

View File

@@ -8,11 +8,11 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GLWECiphertext, GLWEInfos, AutomorphismKey, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey,
prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared}, prepared::{AutomorphismKeyPrepared, GLWESwitchingKeyPrepared},
}; };
impl GGLWEAutomorphismKey<Vec<u8>> { impl AutomorphismKey<Vec<u8>> {
pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>( pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>(
module: &Module<B>, module: &Module<B>,
out_infos: &OUT, out_infos: &OUT,
@@ -25,7 +25,7 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
KEY: GGLWEInfos, KEY: GGLWEInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,
{ {
GGLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) GLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos)
} }
pub fn keyswitch_inplace_scratch_space<B: Backend, OUT, KEY>(module: &Module<B>, out_infos: &OUT, key_infos: &KEY) -> usize pub fn keyswitch_inplace_scratch_space<B: Backend, OUT, KEY>(module: &Module<B>, out_infos: &OUT, key_infos: &KEY) -> usize
@@ -34,16 +34,16 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
KEY: GGLWEInfos, KEY: GGLWEInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,
{ {
GGLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos) GLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos)
} }
} }
impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> { impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
pub fn keyswitch<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( pub fn keyswitch<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>, lhs: &AutomorphismKey<DataLhs>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -65,7 +65,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>( pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>, rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -85,7 +85,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
} }
} }
impl GGLWESwitchingKey<Vec<u8>> { impl GLWESwitchingKey<Vec<u8>> {
pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>( pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>(
module: &Module<B>, module: &Module<B>,
out_infos: &OUT, out_infos: &OUT,
@@ -111,12 +111,12 @@ impl GGLWESwitchingKey<Vec<u8>> {
} }
} }
impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> { impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
pub fn keyswitch<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( pub fn keyswitch<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGLWESwitchingKey<DataLhs>, lhs: &GLWESwitchingKey<DataLhs>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -187,7 +187,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>( pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes

View File

@@ -10,13 +10,13 @@ use poulpy_hal::{
use crate::{ use crate::{
layouts::{ layouts::{
GGLWECiphertext, GGLWEInfos, GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos,
prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared}, prepared::{GLWESwitchingKeyPrepared, TensorKeyPrepared},
}, },
operations::GLWEOperations, operations::GLWEOperations,
}; };
impl GGSWCiphertext<Vec<u8>> { impl GGSW<Vec<u8>> {
pub(crate) fn expand_row_scratch_space<B: Backend, OUT, TSK>(module: &Module<B>, out_infos: &OUT, tsk_infos: &TSK) -> usize pub(crate) fn expand_row_scratch_space<B: Backend, OUT, TSK>(module: &Module<B>, out_infos: &OUT, tsk_infos: &TSK) -> usize
where where
OUT: GGSWInfos, OUT: GGSWInfos,
@@ -77,7 +77,7 @@ impl GGSWCiphertext<Vec<u8>> {
let res_znx: usize = VecZnx::alloc_bytes(module.n(), rank + 1, size_out); let res_znx: usize = VecZnx::alloc_bytes(module.n(), rank + 1, size_out);
let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out);
let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos); let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos);
let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, out_infos, tsk_infos); let expand_rows: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos);
let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out);
if in_infos.base2k() == tsk_infos.base2k() { if in_infos.base2k() == tsk_infos.base2k() {
@@ -109,16 +109,16 @@ impl GGSWCiphertext<Vec<u8>> {
+ VecZnxNormalizeTmpBytes + VecZnxNormalizeTmpBytes
+ VecZnxBigNormalizeTmpBytes, + VecZnxBigNormalizeTmpBytes,
{ {
GGSWCiphertext::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos) GGSW::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos)
} }
} }
impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> { impl<DataSelf: DataMut> GGSW<DataSelf> {
pub fn from_gglwe<DataA, DataTsk, B: Backend>( pub fn from_gglwe<DataA, DataTsk, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
a: &GGLWECiphertext<DataA>, a: &GGLWE<DataA>,
tsk: &GGLWETensorKeyPrepared<DataTsk, B>, tsk: &TensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
DataA: DataRef, DataA: DataRef,
@@ -157,9 +157,9 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn keyswitch<DataLhs: DataRef, DataKsk: DataRef, DataTsk: DataRef, B: Backend>( pub fn keyswitch<DataLhs: DataRef, DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>, lhs: &GGSW<DataLhs>,
ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>, ksk: &GLWESwitchingKeyPrepared<DataKsk, B>,
tsk: &GGLWETensorKeyPrepared<DataTsk, B>, tsk: &TensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -192,8 +192,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn keyswitch_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>( pub fn keyswitch_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>, ksk: &GLWESwitchingKeyPrepared<DataKsk, B>,
tsk: &GGLWETensorKeyPrepared<DataTsk, B>, tsk: &TensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -226,7 +226,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn expand_row<DataTsk: DataRef, B: Backend>( pub fn expand_row<DataTsk: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
tsk: &GGLWETensorKeyPrepared<DataTsk, B>, tsk: &TensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -246,7 +246,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
let basek_in: usize = self.base2k().into(); let basek_in: usize = self.base2k().into();
let basek_tsk: usize = tsk.base2k().into(); let basek_tsk: usize = tsk.base2k().into();
assert!(scratch.available() >= GGSWCiphertext::expand_row_scratch_space(module, self, tsk)); assert!(scratch.available() >= GGSW::expand_row_scratch_space(module, self, tsk));
let n: usize = self.n().into(); let n: usize = self.n().into();
let rank: usize = self.rank().into(); let rank: usize = self.rank().into();

View File

@@ -7,7 +7,7 @@ use poulpy_hal::{
layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos},
}; };
use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWESwitchingKeyPrepared}; use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GLWESwitchingKeyPrepared};
impl GLWECiphertext<Vec<u8>> { impl GLWECiphertext<Vec<u8>> {
pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>( pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>(
@@ -68,7 +68,7 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
&self, &self,
module: &Module<B>, module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>, lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &Scratch<B>, scratch: &Scratch<B>,
) where ) where
DataLhs: DataRef, DataLhs: DataRef,
@@ -117,7 +117,7 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
pub(crate) fn assert_keyswitch_inplace<B: Backend, DataRhs>( pub(crate) fn assert_keyswitch_inplace<B: Backend, DataRhs>(
&self, &self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &Scratch<B>, scratch: &Scratch<B>,
) where ) where
DataRhs: DataRef, DataRhs: DataRef,
@@ -149,7 +149,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
glwe_in: &GLWECiphertext<DataLhs>, glwe_in: &GLWECiphertext<DataLhs>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -191,7 +191,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>( pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self, &mut self,
module: &Module<B>, module: &Module<B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
Module<B>: VecZnxDftAllocBytes Module<B>: VecZnxDftAllocBytes
@@ -237,7 +237,7 @@ impl<D: DataRef> GLWECiphertext<D> {
&self, &self,
module: &Module<B>, module: &Module<B>,
res_dft: VecZnxDft<DataRes, B>, res_dft: VecZnxDft<DataRes, B>,
rhs: &GGLWESwitchingKeyPrepared<DataKey, B>, rhs: &GLWESwitchingKeyPrepared<DataKey, B>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) -> VecZnxBig<DataRes, B> ) -> VecZnxBig<DataRes, B>
where where

View File

@@ -5,19 +5,19 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef}, compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GGLWEAutomorphismKeyCompressed<D: Data> { pub struct AutomorphismKeyCompressed<D: Data> {
pub(crate) key: GGLWEKeyCompressed<D>, pub(crate) key: GLWESwitchingKeyCompressed<D>,
pub(crate) p: i64, pub(crate) p: i64,
} }
impl<D: Data> LWEInfos for GGLWEAutomorphismKeyCompressed<D> { impl<D: Data> LWEInfos for AutomorphismKeyCompressed<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.key.n() self.key.n()
} }
@@ -34,13 +34,13 @@ impl<D: Data> LWEInfos for GGLWEAutomorphismKeyCompressed<D> {
self.key.size() self.key.size()
} }
} }
impl<D: Data> GLWEInfos for GGLWEAutomorphismKeyCompressed<D> { impl<D: Data> GLWEInfos for AutomorphismKeyCompressed<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data> GGLWEInfos for GGLWEAutomorphismKeyCompressed<D> { impl<D: Data> GGLWEInfos for AutomorphismKeyCompressed<D> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.key.rank_in() self.key.rank_in()
} }
@@ -58,39 +58,39 @@ impl<D: Data> GGLWEInfos for GGLWEAutomorphismKeyCompressed<D> {
} }
} }
impl<D: DataRef> fmt::Debug for GGLWEAutomorphismKeyCompressed<D> { impl<D: DataRef> fmt::Debug for AutomorphismKeyCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}") write!(f, "{self}")
} }
} }
impl<D: DataMut> FillUniform for GGLWEAutomorphismKeyCompressed<D> { impl<D: DataMut> FillUniform for AutomorphismKeyCompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.key.fill_uniform(log_bound, source); self.key.fill_uniform(log_bound, source);
} }
} }
impl<D: DataRef> fmt::Display for GGLWEAutomorphismKeyCompressed<D> { impl<D: DataRef> fmt::Display for AutomorphismKeyCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key) write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key)
} }
} }
impl GGLWEAutomorphismKeyCompressed<Vec<u8>> { impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
debug_assert_eq!(infos.rank_in(), infos.rank_out()); debug_assert_eq!(infos.rank_in(), infos.rank_out());
Self { Self {
key: GGLWEKeyCompressed::alloc(infos), key: GLWESwitchingKeyCompressed::alloc(infos),
p: 0, p: 0,
} }
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
Self { Self {
key: GGLWEKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), key: GLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize),
p: 0, p: 0,
} }
} }
@@ -100,64 +100,64 @@ impl GGLWEAutomorphismKeyCompressed<Vec<u8>> {
A: GGLWEInfos, A: GGLWEInfos,
{ {
debug_assert_eq!(infos.rank_in(), infos.rank_out()); debug_assert_eq!(infos.rank_in(), infos.rank_out());
GGLWEKeyCompressed::alloc_bytes(infos) GLWESwitchingKeyCompressed::alloc_bytes(infos)
} }
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize)
} }
} }
impl<D: DataMut> ReaderFrom for GGLWEAutomorphismKeyCompressed<D> { impl<D: DataMut> ReaderFrom for AutomorphismKeyCompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.p = reader.read_u64::<LittleEndian>()? as i64; self.p = reader.read_u64::<LittleEndian>()? as i64;
self.key.read_from(reader) self.key.read_from(reader)
} }
} }
impl<D: DataRef> WriterTo for GGLWEAutomorphismKeyCompressed<D> { impl<D: DataRef> WriterTo for AutomorphismKeyCompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.p as u64)?; writer.write_u64::<LittleEndian>(self.p as u64)?;
self.key.write_to(writer) self.key.write_to(writer)
} }
} }
impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWEAutomorphismKeyCompressed<DR>> for GGLWEAutomorphismKey<D> impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, AutomorphismKeyCompressed<DR>> for AutomorphismKey<D>
where where
Module<B>: VecZnxFillUniform + VecZnxCopy, Module<B>: VecZnxFillUniform + VecZnxCopy,
{ {
fn decompress(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKeyCompressed<DR>) { fn decompress(&mut self, module: &Module<B>, other: &AutomorphismKeyCompressed<DR>) {
self.key.decompress(module, &other.key); self.key.decompress(module, &other.key);
self.p = other.p; self.p = other.p;
} }
} }
pub trait GGLWEAutomorphismKeyCompressedToRef { pub trait AutomorphismKeyCompressedToRef {
fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]>; fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>;
} }
impl<D: DataRef> GGLWEAutomorphismKeyCompressedToRef for GGLWEAutomorphismKeyCompressed<D> impl<D: DataRef> AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed<D>
where where
GGLWEKeyCompressed<D>: GGLWEKeyCompressedToRef, GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
{ {
fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]> { fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> {
GGLWEAutomorphismKeyCompressed { AutomorphismKeyCompressed {
key: self.key.to_ref(), key: self.key.to_ref(),
p: self.p, p: self.p,
} }
} }
} }
pub trait GGLWEAutomorphismKeyCompressedToMut { pub trait AutomorphismKeyCompressedToMut {
fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]>; fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]>;
} }
impl<D: DataRef> GGLWEAutomorphismKeyCompressedToMut for GGLWEAutomorphismKeyCompressed<D> impl<D: DataMut> AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed<D>
where where
GGLWEKeyCompressed<D>: GGLWEKeyCompressedToMut, GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
{ {
fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]> { fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> {
GGLWEAutomorphismKeyCompressed { AutomorphismKeyCompressed {
p: self.p, p: self.p,
key: self.key.to_mut(), key: self.key.to_mut(),
} }

View File

@@ -7,7 +7,7 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{Decompress, GLWECiphertextCompressed}, compressed::{Decompress, GLWECiphertextCompressed},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -235,7 +235,7 @@ impl<D: DataRef> WriterTo for GGLWECiphertextCompressed<D> {
} }
} }
impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGLWECiphertextCompressed<DR>> for GGLWECiphertext<D> impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGLWECiphertextCompressed<DR>> for GGLWE<D>
where where
Module<B>: VecZnxFillUniform + VecZnxCopy, Module<B>: VecZnxFillUniform + VecZnxCopy,
{ {
@@ -313,7 +313,7 @@ pub trait GGLWECiphertextCompressedToRef {
fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>; fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>;
} }
impl<D: DataMut> GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed<D> { impl<D: DataRef> GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed<D> {
fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> { fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> {
GGLWECiphertextCompressed { GGLWECiphertextCompressed {
k: self.k(), k: self.k(),

View File

@@ -5,20 +5,20 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, LWEInfos, Rank, TorusPrecision,
compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef}, compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GGLWEKeyCompressed<D: Data> { pub struct GLWESwitchingKeyCompressed<D: Data> {
pub(crate) key: GGLWECiphertextCompressed<D>, pub(crate) key: GGLWECiphertextCompressed<D>,
pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out pub(crate) sk_out_n: usize, // Degree of sk_out
} }
impl<D: Data> LWEInfos for GGLWEKeyCompressed<D> { impl<D: Data> LWEInfos for GLWESwitchingKeyCompressed<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.key.n() self.key.n()
} }
@@ -35,13 +35,13 @@ impl<D: Data> LWEInfos for GGLWEKeyCompressed<D> {
self.key.size() self.key.size()
} }
} }
impl<D: Data> GLWEInfos for GGLWEKeyCompressed<D> { impl<D: Data> GLWEInfos for GLWESwitchingKeyCompressed<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data> GGLWEInfos for GGLWEKeyCompressed<D> { impl<D: Data> GGLWEInfos for GLWESwitchingKeyCompressed<D> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.key.rank_in() self.key.rank_in()
} }
@@ -59,19 +59,19 @@ impl<D: Data> GGLWEInfos for GGLWEKeyCompressed<D> {
} }
} }
impl<D: DataRef> fmt::Debug for GGLWEKeyCompressed<D> { impl<D: DataRef> fmt::Debug for GLWESwitchingKeyCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}") write!(f, "{self}")
} }
} }
impl<D: DataMut> FillUniform for GGLWEKeyCompressed<D> { impl<D: DataMut> FillUniform for GLWESwitchingKeyCompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.key.fill_uniform(log_bound, source); self.key.fill_uniform(log_bound, source);
} }
} }
impl<D: DataRef> fmt::Display for GGLWEKeyCompressed<D> { impl<D: DataRef> fmt::Display for GLWESwitchingKeyCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
@@ -81,12 +81,12 @@ impl<D: DataRef> fmt::Display for GGLWEKeyCompressed<D> {
} }
} }
impl GGLWEKeyCompressed<Vec<u8>> { impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
GGLWEKeyCompressed { GLWESwitchingKeyCompressed {
key: GGLWECiphertextCompressed::alloc(infos), key: GGLWECiphertextCompressed::alloc(infos),
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
@@ -102,7 +102,7 @@ impl GGLWEKeyCompressed<Vec<u8>> {
dnum: Dnum, dnum: Dnum,
dsize: Dsize, dsize: Dsize,
) -> Self { ) -> Self {
GGLWEKeyCompressed { GLWESwitchingKeyCompressed {
key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
@@ -121,7 +121,7 @@ impl GGLWEKeyCompressed<Vec<u8>> {
} }
} }
impl<D: DataMut> ReaderFrom for GGLWEKeyCompressed<D> { impl<D: DataMut> ReaderFrom for GLWESwitchingKeyCompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize; self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize;
self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize; self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize;
@@ -129,7 +129,7 @@ impl<D: DataMut> ReaderFrom for GGLWEKeyCompressed<D> {
} }
} }
impl<D: DataRef> WriterTo for GGLWEKeyCompressed<D> { impl<D: DataRef> WriterTo for GLWESwitchingKeyCompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?; writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?;
writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?; writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?;
@@ -137,27 +137,27 @@ impl<D: DataRef> WriterTo for GGLWEKeyCompressed<D> {
} }
} }
impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWEKeyCompressed<DR>> for GGLWESwitchingKey<D> impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GLWESwitchingKeyCompressed<DR>> for GLWESwitchingKey<D>
where where
Module<B>: VecZnxFillUniform + VecZnxCopy, Module<B>: VecZnxFillUniform + VecZnxCopy,
{ {
fn decompress(&mut self, module: &Module<B>, other: &GGLWEKeyCompressed<DR>) { fn decompress(&mut self, module: &Module<B>, other: &GLWESwitchingKeyCompressed<DR>) {
self.key.decompress(module, &other.key); self.key.decompress(module, &other.key);
self.sk_in_n = other.sk_in_n; self.sk_in_n = other.sk_in_n;
self.sk_out_n = other.sk_out_n; self.sk_out_n = other.sk_out_n;
} }
} }
pub trait GGLWEKeyCompressedToMut { pub trait GLWESwitchingKeyCompressedToMut {
fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]>; fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]>;
} }
impl<D: DataMut> GGLWEKeyCompressedToMut for GGLWEKeyCompressed<D> impl<D: DataMut> GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed<D>
where where
GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToMut, GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToMut,
{ {
fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]> { fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> {
GGLWEKeyCompressed { GLWESwitchingKeyCompressed {
sk_in_n: self.sk_in_n, sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n, sk_out_n: self.sk_out_n,
key: self.key.to_mut(), key: self.key.to_mut(),
@@ -165,16 +165,16 @@ where
} }
} }
pub trait GGLWEKeyCompressedToRef { pub trait GLWESwitchingKeyCompressedToRef {
fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]>; fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]>;
} }
impl<D: DataMut> GGLWEKeyCompressedToRef for GGLWEKeyCompressed<D> impl<D: DataRef> GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed<D>
where where
GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToRef, GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToRef,
{ {
fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]> { fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> {
GGLWEKeyCompressed { GLWESwitchingKeyCompressed {
sk_in_n: self.sk_in_n, sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n, sk_out_n: self.sk_out_n,
key: self.key.to_ref(), key: self.key.to_ref(),

View File

@@ -5,18 +5,18 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TorusPrecision,
compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef}, compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GGLWETensorKeyCompressed<D: Data> { pub struct TensorKeyCompressed<D: Data> {
pub(crate) keys: Vec<GGLWEKeyCompressed<D>>, pub(crate) keys: Vec<GLWESwitchingKeyCompressed<D>>,
} }
impl<D: Data> LWEInfos for GGLWETensorKeyCompressed<D> { impl<D: Data> LWEInfos for TensorKeyCompressed<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.keys[0].n() self.keys[0].n()
} }
@@ -32,13 +32,13 @@ impl<D: Data> LWEInfos for GGLWETensorKeyCompressed<D> {
self.keys[0].size() self.keys[0].size()
} }
} }
impl<D: Data> GLWEInfos for GGLWETensorKeyCompressed<D> { impl<D: Data> GLWEInfos for TensorKeyCompressed<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data> GGLWEInfos for GGLWETensorKeyCompressed<D> { impl<D: Data> GGLWEInfos for TensorKeyCompressed<D> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.rank_out() self.rank_out()
} }
@@ -56,21 +56,21 @@ impl<D: Data> GGLWEInfos for GGLWETensorKeyCompressed<D> {
} }
} }
impl<D: DataRef> fmt::Debug for GGLWETensorKeyCompressed<D> { impl<D: DataRef> fmt::Debug for TensorKeyCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}") write!(f, "{self}")
} }
} }
impl<D: DataMut> FillUniform for GGLWETensorKeyCompressed<D> { impl<D: DataMut> FillUniform for TensorKeyCompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.keys self.keys
.iter_mut() .iter_mut()
.for_each(|key: &mut GGLWEKeyCompressed<D>| key.fill_uniform(log_bound, source)) .for_each(|key: &mut GLWESwitchingKeyCompressed<D>| key.fill_uniform(log_bound, source))
} }
} }
impl<D: DataRef> fmt::Display for GGLWETensorKeyCompressed<D> { impl<D: DataRef> fmt::Display for TensorKeyCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "(GLWETensorKeyCompressed)",)?; writeln!(f, "(GLWETensorKeyCompressed)",)?;
for (i, key) in self.keys.iter().enumerate() { for (i, key) in self.keys.iter().enumerate() {
@@ -80,7 +80,7 @@ impl<D: DataRef> fmt::Display for GGLWETensorKeyCompressed<D> {
} }
} }
impl GGLWETensorKeyCompressed<Vec<u8>> { impl TensorKeyCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -101,10 +101,10 @@ impl GGLWETensorKeyCompressed<Vec<u8>> {
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let mut keys: Vec<GGLWEKeyCompressed<Vec<u8>>> = Vec::new(); let mut keys: Vec<GLWESwitchingKeyCompressed<Vec<u8>>> = Vec::new();
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
(0..pairs).for_each(|_| { (0..pairs).for_each(|_| {
keys.push(GGLWEKeyCompressed::alloc_with( keys.push(GLWESwitchingKeyCompressed::alloc_with(
n, n,
base2k, base2k,
k, k,
@@ -129,7 +129,7 @@ impl GGLWETensorKeyCompressed<Vec<u8>> {
let rank_out: usize = infos.rank_out().into(); let rank_out: usize = infos.rank_out().into();
let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1);
pairs pairs
* GGLWEKeyCompressed::alloc_bytes_with( * GLWESwitchingKeyCompressed::alloc_bytes_with(
infos.n(), infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
@@ -141,11 +141,11 @@ impl GGLWETensorKeyCompressed<Vec<u8>> {
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn alloc_bytes_with(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 * GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) pairs * GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize)
} }
} }
impl<D: DataMut> ReaderFrom for GGLWETensorKeyCompressed<D> { impl<D: DataMut> ReaderFrom for TensorKeyCompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
let len: usize = reader.read_u64::<LittleEndian>()? as usize; let len: usize = reader.read_u64::<LittleEndian>()? as usize;
if self.keys.len() != len { if self.keys.len() != len {
@@ -161,7 +161,7 @@ impl<D: DataMut> ReaderFrom for GGLWETensorKeyCompressed<D> {
} }
} }
impl<D: DataRef> WriterTo for GGLWETensorKeyCompressed<D> { impl<D: DataRef> WriterTo for TensorKeyCompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.keys.len() as u64)?; writer.write_u64::<LittleEndian>(self.keys.len() as u64)?;
for key in &self.keys { for key in &self.keys {
@@ -171,8 +171,8 @@ impl<D: DataRef> WriterTo for GGLWETensorKeyCompressed<D> {
} }
} }
impl<D: DataMut> GGLWETensorKeyCompressed<D> { impl<D: DataMut> TensorKeyCompressed<D> {
pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEKeyCompressed<D> { pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed<D> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -181,11 +181,11 @@ impl<D: DataMut> GGLWETensorKeyCompressed<D> {
} }
} }
impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWETensorKeyCompressed<DR>> for GGLWETensorKey<D> impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, TensorKeyCompressed<DR>> for TensorKey<D>
where where
Module<B>: VecZnxFillUniform + VecZnxCopy, Module<B>: VecZnxFillUniform + VecZnxCopy,
{ {
fn decompress(&mut self, module: &Module<B>, other: &GGLWETensorKeyCompressed<DR>) { fn decompress(&mut self, module: &Module<B>, other: &TensorKeyCompressed<DR>) {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
assert_eq!( assert_eq!(
@@ -206,31 +206,31 @@ where
} }
} }
pub trait GGLWETensorKeyCompressedToMut { pub trait TensorKeyCompressedToMut {
fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]>; fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]>;
} }
impl<D: DataMut> GGLWETensorKeyCompressedToMut for GGLWETensorKeyCompressed<D> impl<D: DataMut> TensorKeyCompressedToMut for TensorKeyCompressed<D>
where where
GGLWEKeyCompressed<D>: GGLWEKeyCompressedToMut, GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
{ {
fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]> { fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> {
GGLWETensorKeyCompressed { TensorKeyCompressed {
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
} }
} }
} }
pub trait GGLWETensorKeyCompressedToRef { pub trait TensorKeyCompressedToRef {
fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]>; fn to_ref(&self) -> TensorKeyCompressed<&[u8]>;
} }
impl<D: DataMut> GGLWETensorKeyCompressedToRef for GGLWETensorKeyCompressed<D> impl<D: DataRef> TensorKeyCompressedToRef for TensorKeyCompressed<D>
where where
GGLWEKeyCompressed<D>: GGLWEKeyCompressedToRef, GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
{ {
fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]> { fn to_ref(&self) -> TensorKeyCompressed<&[u8]> {
GGLWETensorKeyCompressed { TensorKeyCompressed {
keys: self.keys.iter().map(|c| c.to_ref()).collect(), keys: self.keys.iter().map(|c| c.to_ref()).collect(),
} }
} }

View File

@@ -7,7 +7,7 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{Decompress, GLWECiphertextCompressed}, compressed::{Decompress, GLWECiphertextCompressed},
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -217,7 +217,7 @@ impl<D: DataRef> WriterTo for GGSWCiphertextCompressed<D> {
} }
} }
impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGSWCiphertextCompressed<DR>> for GGSWCiphertext<D> impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGSWCiphertextCompressed<DR>> for GGSW<D>
where where
Module<B>: VecZnxFillUniform + VecZnxCopy, Module<B>: VecZnxFillUniform + VecZnxCopy,
{ {

View File

@@ -1,10 +1,14 @@
use poulpy_hal::{ use poulpy_hal::{
api::{VecZnxCopy, VecZnxFillUniform}, api::{VecZnxCopy, VecZnxFillUniform},
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, WriterTo, ZnxInfos}, layouts::{
Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos,
},
source::Source, source::Source,
}; };
use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress}; use crate::layouts::{
Base2K, BuildError, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
@@ -96,6 +100,101 @@ impl GLWECiphertextCompressed<Vec<u8>> {
} }
} }
pub struct GLWECiphertextCompressedBuilder<D: Data> {
data: Option<VecZnx<D>>,
base2k: Option<Base2K>,
k: Option<TorusPrecision>,
rank: Option<Rank>,
}
impl<D: Data> GLWECiphertextCompressed<D> {
#[inline]
pub fn builder() -> GLWECiphertextCompressedBuilder<D> {
GLWECiphertextCompressedBuilder {
data: None,
base2k: None,
k: None,
rank: None,
}
}
}
impl GLWECiphertextCompressedBuilder<Vec<u8>> {
#[inline]
pub fn layout<A>(mut self, layout: &A) -> Self
where
A: GLWEInfos,
{
self.data = Some(VecZnx::alloc(layout.n().into(), 1, layout.size()));
self.base2k = Some(layout.base2k());
self.k = Some(layout.k());
self.rank = Some(layout.rank());
self
}
}
impl<D: Data> GLWECiphertextCompressedBuilder<D> {
#[inline]
pub fn data(mut self, data: VecZnx<D>) -> Self {
self.data = Some(data);
self
}
#[inline]
pub fn base2k(mut self, base2k: Base2K) -> Self {
self.base2k = Some(base2k);
self
}
#[inline]
pub fn k(mut self, k: TorusPrecision) -> Self {
self.k = Some(k);
self
}
#[inline]
pub fn rank(mut self, rank: Rank) -> Self {
self.rank = Some(rank);
self
}
pub fn build(self) -> Result<GLWECiphertextCompressed<D>, BuildError> {
let data: VecZnx<D> = self.data.ok_or(BuildError::MissingData)?;
let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?;
let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?;
let rank: Rank = self.rank.ok_or(BuildError::MissingK)?;
if base2k == 0_u32 {
return Err(BuildError::ZeroBase2K);
}
if k == 0_u32 {
return Err(BuildError::ZeroTorusPrecision);
}
if rank == 0_u32 {
return Err(BuildError::ZeroRank);
}
if data.n() == 0 {
return Err(BuildError::ZeroDegree);
}
if data.cols() != 1 {
return Err(BuildError::VecZnxColsNotOne);
}
if data.size() == 0 {
return Err(BuildError::ZeroLimbs);
}
Ok(GLWECiphertextCompressed {
data,
base2k,
k,
rank,
seed: [0u8; 32],
})
}
}
impl<D: DataMut> ReaderFrom for GLWECiphertextCompressed<D> { impl<D: DataMut> ReaderFrom for GLWECiphertextCompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
@@ -176,3 +275,35 @@ impl<D: DataMut> GLWECiphertext<D> {
self.k = other.k; self.k = other.k;
} }
} }
pub trait GLWECiphertextCompressedToRef {
fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]>;
}
impl<D: DataRef> GLWECiphertextCompressedToRef for GLWECiphertextCompressed<D> {
fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> {
GLWECiphertextCompressed::builder()
.k(self.k())
.base2k(self.base2k())
.rank(self.rank())
.data(self.data.to_ref())
.build()
.unwrap()
}
}
pub trait GLWECiphertextCompressedToMut {
fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]>;
}
impl<D: DataMut> GLWECiphertextCompressedToMut for GLWECiphertextCompressed<D> {
fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> {
GLWECiphertextCompressed::builder()
.k(self.k())
.base2k(self.base2k())
.rank(self.rank())
.data(self.data.to_mut())
.build()
.unwrap()
}
}

View File

@@ -6,11 +6,11 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GGLWEKeyCompressed, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GLWESwitchingKeyCompressed,
}; };
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWEKeyCompressed<D>); pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
impl<D: Data> LWEInfos for GLWEToLWESwitchingKeyCompressed<D> { impl<D: Data> LWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
@@ -98,11 +98,11 @@ impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
1, 1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
); );
Self(GGLWEKeyCompressed::alloc(infos)) Self(GLWESwitchingKeyCompressed::alloc(infos))
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
Self(GGLWEKeyCompressed::alloc_with( Self(GLWESwitchingKeyCompressed::alloc_with(
n, n,
base2k, base2k,
k, k,
@@ -127,10 +127,10 @@ impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
1, 1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
); );
GGLWEKeyCompressed::alloc_bytes(infos) GLWESwitchingKeyCompressed::alloc_bytes(infos)
} }
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize {
GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1))
} }
} }

View File

@@ -6,12 +6,12 @@ use poulpy_hal::{
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision,
compressed::{Decompress, GGLWEKeyCompressed}, compressed::{Decompress, GLWESwitchingKeyCompressed},
}; };
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWEKeyCompressed<D>); pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
impl<D: Data> LWEInfos for LWESwitchingKeyCompressed<D> { impl<D: Data> LWEInfos for LWESwitchingKeyCompressed<D> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
@@ -103,11 +103,11 @@ impl LWESwitchingKeyCompressed<Vec<u8>> {
1, 1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed" "rank_out > 1 is not supported for LWESwitchingKeyCompressed"
); );
Self(GGLWEKeyCompressed::alloc(infos)) Self(GLWESwitchingKeyCompressed::alloc(infos))
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
Self(GGLWEKeyCompressed::alloc_with( Self(GLWESwitchingKeyCompressed::alloc_with(
n, n,
base2k, base2k,
k, k,
@@ -137,11 +137,11 @@ impl LWESwitchingKeyCompressed<Vec<u8>> {
1, 1,
"rank_out > 1 is not supported for LWESwitchingKey" "rank_out > 1 is not supported for LWESwitchingKey"
); );
GGLWEKeyCompressed::alloc_bytes(infos) GLWESwitchingKeyCompressed::alloc_bytes(infos)
} }
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1))
} }
} }

View File

@@ -6,12 +6,12 @@ use poulpy_hal::{
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision,
compressed::{Decompress, GGLWEKeyCompressed}, compressed::{Decompress, GLWESwitchingKeyCompressed},
}; };
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWEKeyCompressed<D>); pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
impl<D: Data> LWEInfos for LWEToGLWESwitchingKeyCompressed<D> { impl<D: Data> LWEInfos for LWEToGLWESwitchingKeyCompressed<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
@@ -98,11 +98,11 @@ impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
1, 1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
); );
Self(GGLWEKeyCompressed::alloc(infos)) Self(GLWESwitchingKeyCompressed::alloc(infos))
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
Self(GGLWEKeyCompressed::alloc_with( Self(GLWESwitchingKeyCompressed::alloc_with(
n, n,
base2k, base2k,
k, k,
@@ -127,11 +127,11 @@ impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
1, 1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey" "dsize > 1 is not supported for LWEToGLWESwitchingKey"
); );
GGLWEKeyCompressed::alloc_bytes(infos) GLWESwitchingKeyCompressed::alloc_bytes(infos)
} }
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1))
} }
} }

View File

@@ -4,15 +4,15 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GGLWESwitchingKeyToMut, GLWECiphertext, GLWEInfos, LWEInfos, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut,
Rank, TorusPrecision, GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct GGLWEAutomorphismKeyLayout { pub struct AutomorphismKeyLayout {
pub n: Degree, pub n: Degree,
pub base2k: Base2K, pub base2k: Base2K,
pub k: TorusPrecision, pub k: TorusPrecision,
@@ -22,18 +22,18 @@ pub struct GGLWEAutomorphismKeyLayout {
} }
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GGLWEAutomorphismKey<D: Data> { pub struct AutomorphismKey<D: Data> {
pub(crate) key: GGLWESwitchingKey<D>, pub(crate) key: GLWESwitchingKey<D>,
pub(crate) p: i64, pub(crate) p: i64,
} }
impl<D: Data> GGLWEAutomorphismKey<D> { impl<D: Data> AutomorphismKey<D> {
pub fn p(&self) -> i64 { pub fn p(&self) -> i64 {
self.p self.p
} }
} }
impl<D: Data> LWEInfos for GGLWEAutomorphismKey<D> { impl<D: Data> LWEInfos for AutomorphismKey<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.key.n() self.key.n()
} }
@@ -51,13 +51,13 @@ impl<D: Data> LWEInfos for GGLWEAutomorphismKey<D> {
} }
} }
impl<D: Data> GLWEInfos for GGLWEAutomorphismKey<D> { impl<D: Data> GLWEInfos for AutomorphismKey<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data> GGLWEInfos for GGLWEAutomorphismKey<D> { impl<D: Data> GGLWEInfos for AutomorphismKey<D> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.key.rank_in() self.key.rank_in()
} }
@@ -75,7 +75,7 @@ impl<D: Data> GGLWEInfos for GGLWEAutomorphismKey<D> {
} }
} }
impl LWEInfos for GGLWEAutomorphismKeyLayout { impl LWEInfos for AutomorphismKeyLayout {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
self.base2k self.base2k
} }
@@ -89,13 +89,13 @@ impl LWEInfos for GGLWEAutomorphismKeyLayout {
} }
} }
impl GLWEInfos for GGLWEAutomorphismKeyLayout { impl GLWEInfos for AutomorphismKeyLayout {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank self.rank
} }
} }
impl GGLWEInfos for GGLWEAutomorphismKeyLayout { impl GGLWEInfos for AutomorphismKeyLayout {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.rank self.rank
} }
@@ -113,25 +113,25 @@ impl GGLWEInfos for GGLWEAutomorphismKeyLayout {
} }
} }
impl<D: DataRef> fmt::Debug for GGLWEAutomorphismKey<D> { impl<D: DataRef> fmt::Debug for AutomorphismKey<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}") write!(f, "{self}")
} }
} }
impl<D: DataMut> FillUniform for GGLWEAutomorphismKey<D> { impl<D: DataMut> FillUniform for AutomorphismKey<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.key.fill_uniform(log_bound, source); self.key.fill_uniform(log_bound, source);
} }
} }
impl<D: DataRef> fmt::Display for GGLWEAutomorphismKey<D> { impl<D: DataRef> fmt::Display for AutomorphismKey<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key) write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key)
} }
} }
impl GGLWEAutomorphismKey<Vec<u8>> { impl AutomorphismKey<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -141,15 +141,15 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKey" "rank_in != rank_out is not supported for GGLWEAutomorphismKey"
); );
GGLWEAutomorphismKey { AutomorphismKey {
key: GGLWESwitchingKey::alloc(infos), key: GLWESwitchingKey::alloc(infos),
p: 0, p: 0,
} }
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
GGLWEAutomorphismKey { AutomorphismKey {
key: GGLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize), key: GLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize),
p: 0, p: 0,
} }
} }
@@ -163,54 +163,66 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKey" "rank_in != rank_out is not supported for GGLWEAutomorphismKey"
); );
GGLWESwitchingKey::alloc_bytes(infos) GLWESwitchingKey::alloc_bytes(infos)
} }
pub fn bytes_of(n: Degree, 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 {
GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize) GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize)
} }
} }
pub trait GGLWEAutomorphismKeyToMut { pub trait AutomorphismKeyToMut {
fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]>; fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]>;
} }
impl<D: DataMut> GGLWEAutomorphismKeyToMut for GGLWEAutomorphismKey<D> impl<D: DataMut> AutomorphismKeyToMut for AutomorphismKey<D>
where where
GGLWESwitchingKey<D>: GGLWESwitchingKeyToMut, GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{ {
fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]> { fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]> {
GGLWEAutomorphismKey { AutomorphismKey {
key: self.key.to_mut(), key: self.key.to_mut(),
p: self.p, p: self.p,
} }
} }
} }
pub trait GGLWEAutomorphismKeyToRef { pub trait AutomorphismKeyToRef {
fn to_ref(&self) -> GGLWEAutomorphismKey<&[u8]>; fn to_ref(&self) -> AutomorphismKey<&[u8]>;
} }
impl<D: DataRef> GGLWEAutomorphismKey<D> { impl<D: DataRef> AutomorphismKeyToRef for AutomorphismKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> AutomorphismKey<&[u8]> {
AutomorphismKey {
p: self.p,
key: self.key.to_ref(),
}
}
}
impl<D: DataRef> AutomorphismKey<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
self.key.at(row, col) self.key.at(row, col)
} }
} }
impl<D: DataMut> GGLWEAutomorphismKey<D> { impl<D: DataMut> AutomorphismKey<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> {
self.key.at_mut(row, col) self.key.at_mut(row, col)
} }
} }
impl<D: DataMut> ReaderFrom for GGLWEAutomorphismKey<D> { impl<D: DataMut> ReaderFrom for AutomorphismKey<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.p = reader.read_u64::<LittleEndian>()? as i64; self.p = reader.read_u64::<LittleEndian>()? as i64;
self.key.read_from(reader) self.key.read_from(reader)
} }
} }
impl<D: DataRef> WriterTo for GGLWEAutomorphismKey<D> { impl<D: DataRef> WriterTo for AutomorphismKey<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.p as u64)?; writer.write_u64::<LittleEndian>(self.p as u64)?;
self.key.write_to(writer) self.key.write_to(writer)

View File

@@ -29,6 +29,10 @@ where
} }
} }
pub trait SetGGLWEInfos {
fn set_dsize(&mut self, dsize: usize);
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct GGLWECiphertextLayout { pub struct GGLWECiphertextLayout {
pub n: Degree, pub n: Degree,
@@ -79,14 +83,14 @@ impl GGLWEInfos for GGLWECiphertextLayout {
} }
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GGLWECiphertext<D: Data> { pub struct GGLWE<D: Data> {
pub(crate) data: MatZnx<D>, pub(crate) data: MatZnx<D>,
pub(crate) k: TorusPrecision, pub(crate) k: TorusPrecision,
pub(crate) base2k: Base2K, pub(crate) base2k: Base2K,
pub(crate) dsize: Dsize, pub(crate) dsize: Dsize,
} }
impl<D: Data> LWEInfos for GGLWECiphertext<D> { impl<D: Data> LWEInfos for GGLWE<D> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
self.base2k self.base2k
} }
@@ -104,13 +108,13 @@ impl<D: Data> LWEInfos for GGLWECiphertext<D> {
} }
} }
impl<D: Data> GLWEInfos for GGLWECiphertext<D> { impl<D: Data> GLWEInfos for GGLWE<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data> GGLWEInfos for GGLWECiphertext<D> { impl<D: Data> GGLWEInfos for GGLWE<D> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
Rank(self.data.cols_in() as u32) Rank(self.data.cols_in() as u32)
} }
@@ -135,7 +139,7 @@ pub struct GGLWECiphertextBuilder<D: Data> {
dsize: Option<Dsize>, dsize: Option<Dsize>,
} }
impl<D: Data> GGLWECiphertext<D> { impl<D: Data> GGLWE<D> {
#[inline] #[inline]
pub fn builder() -> GGLWECiphertextBuilder<D> { pub fn builder() -> GGLWECiphertextBuilder<D> {
GGLWECiphertextBuilder { GGLWECiphertextBuilder {
@@ -190,7 +194,7 @@ impl<D: Data> GGLWECiphertextBuilder<D> {
self self
} }
pub fn build(self) -> Result<GGLWECiphertext<D>, BuildError> { pub fn build(self) -> Result<GGLWE<D>, BuildError> {
let data: MatZnx<D> = self.data.ok_or(BuildError::MissingData)?; let data: MatZnx<D> = self.data.ok_or(BuildError::MissingData)?;
let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?;
let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?;
@@ -220,7 +224,7 @@ impl<D: Data> GGLWECiphertextBuilder<D> {
return Err(BuildError::ZeroLimbs); return Err(BuildError::ZeroLimbs);
} }
Ok(GGLWECiphertext { Ok(GGLWE {
data, data,
base2k, base2k,
k, k,
@@ -229,31 +233,31 @@ impl<D: Data> GGLWECiphertextBuilder<D> {
} }
} }
impl<D: DataRef> GGLWECiphertext<D> { impl<D: DataRef> GGLWE<D> {
pub fn data(&self) -> &MatZnx<D> { pub fn data(&self) -> &MatZnx<D> {
&self.data &self.data
} }
} }
impl<D: DataMut> GGLWECiphertext<D> { impl<D: DataMut> GGLWE<D> {
pub fn data_mut(&mut self) -> &mut MatZnx<D> { pub fn data_mut(&mut self) -> &mut MatZnx<D> {
&mut self.data &mut self.data
} }
} }
impl<D: DataRef> fmt::Debug for GGLWECiphertext<D> { impl<D: DataRef> fmt::Debug for GGLWE<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}") write!(f, "{self}")
} }
} }
impl<D: DataMut> FillUniform for GGLWECiphertext<D> { impl<D: DataMut> FillUniform for GGLWE<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.data.fill_uniform(log_bound, source); self.data.fill_uniform(log_bound, source);
} }
} }
impl<D: DataRef> fmt::Display for GGLWECiphertext<D> { impl<D: DataRef> fmt::Display for GGLWE<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
@@ -266,7 +270,7 @@ impl<D: DataRef> fmt::Display for GGLWECiphertext<D> {
} }
} }
impl<D: DataRef> GGLWECiphertext<D> { impl<D: DataRef> GGLWE<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
GLWECiphertext::builder() GLWECiphertext::builder()
.data(self.data.at(row, col)) .data(self.data.at(row, col))
@@ -277,7 +281,7 @@ impl<D: DataRef> GGLWECiphertext<D> {
} }
} }
impl<D: DataMut> GGLWECiphertext<D> { impl<D: DataMut> GGLWE<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> {
GLWECiphertext::builder() GLWECiphertext::builder()
.base2k(self.base2k()) .base2k(self.base2k())
@@ -288,7 +292,7 @@ impl<D: DataMut> GGLWECiphertext<D> {
} }
} }
impl GGLWECiphertext<Vec<u8>> { impl GGLWE<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -390,12 +394,12 @@ impl GGLWECiphertext<Vec<u8>> {
} }
pub trait GGLWECiphertextToMut { pub trait GGLWECiphertextToMut {
fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]>; fn to_mut(&mut self) -> GGLWE<&mut [u8]>;
} }
impl<D: DataMut> GGLWECiphertextToMut for GGLWECiphertext<D> { impl<D: DataMut> GGLWECiphertextToMut for GGLWE<D> {
fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]> { fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
GGLWECiphertext { GGLWE {
k: self.k(), k: self.k(),
base2k: self.base2k(), base2k: self.base2k(),
dsize: self.dsize(), dsize: self.dsize(),
@@ -404,13 +408,13 @@ impl<D: DataMut> GGLWECiphertextToMut for GGLWECiphertext<D> {
} }
} }
pub trait GGLWECiphertextToRef { pub trait GGLWEToRef {
fn to_ref(&self) -> GGLWECiphertext<&[u8]>; fn to_ref(&self) -> GGLWE<&[u8]>;
} }
impl<D: DataMut> GGLWECiphertextToRef for GGLWECiphertext<D> { impl<D: DataRef> GGLWEToRef for GGLWE<D> {
fn to_ref(&self) -> GGLWECiphertext<&[u8]> { fn to_ref(&self) -> GGLWE<&[u8]> {
GGLWECiphertext { GGLWE {
k: self.k(), k: self.k(),
base2k: self.base2k(), base2k: self.base2k(),
dsize: self.dsize(), dsize: self.dsize(),
@@ -419,7 +423,7 @@ impl<D: DataMut> GGLWECiphertextToRef for GGLWECiphertext<D> {
} }
} }
impl<D: DataMut> ReaderFrom for GGLWECiphertext<D> { impl<D: DataMut> ReaderFrom for GGLWE<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?); self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
@@ -428,7 +432,7 @@ impl<D: DataMut> ReaderFrom for GGLWECiphertext<D> {
} }
} }
impl<D: DataRef> WriterTo for GGLWECiphertext<D> { impl<D: DataRef> WriterTo for GGLWE<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u32::<LittleEndian>(self.k.0)?; writer.write_u32::<LittleEndian>(self.k.0)?;
writer.write_u32::<LittleEndian>(self.base2k.0)?; writer.write_u32::<LittleEndian>(self.base2k.0)?;

View File

@@ -4,7 +4,7 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, Rank, Base2K, Degree, Dnum, Dsize, GGLWE, GGLWECiphertextToMut, GGLWEInfos, GGLWEToRef, GLWECiphertext, GLWEInfos, LWEInfos, Rank,
TorusPrecision, TorusPrecision,
}; };
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -12,7 +12,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct GGLWESwitchingKeyLayout { pub struct GLWESwitchingKeyLayout {
pub n: Degree, pub n: Degree,
pub base2k: Base2K, pub base2k: Base2K,
pub k: TorusPrecision, pub k: TorusPrecision,
@@ -22,7 +22,7 @@ pub struct GGLWESwitchingKeyLayout {
pub dsize: Dsize, pub dsize: Dsize,
} }
impl LWEInfos for GGLWESwitchingKeyLayout { impl LWEInfos for GLWESwitchingKeyLayout {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.n self.n
} }
@@ -36,13 +36,13 @@ impl LWEInfos for GGLWESwitchingKeyLayout {
} }
} }
impl GLWEInfos for GGLWESwitchingKeyLayout { impl GLWEInfos for GLWESwitchingKeyLayout {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl GGLWEInfos for GGLWESwitchingKeyLayout { impl GGLWEInfos for GLWESwitchingKeyLayout {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.rank_in self.rank_in
} }
@@ -61,13 +61,13 @@ impl GGLWEInfos for GGLWESwitchingKeyLayout {
} }
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GGLWESwitchingKey<D: Data> { pub struct GLWESwitchingKey<D: Data> {
pub(crate) key: GGLWECiphertext<D>, pub(crate) key: GGLWE<D>,
pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out pub(crate) sk_out_n: usize, // Degree of sk_out
} }
impl<D: Data> LWEInfos for GGLWESwitchingKey<D> { impl<D: Data> LWEInfos for GLWESwitchingKey<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.key.n() self.key.n()
} }
@@ -85,13 +85,13 @@ impl<D: Data> LWEInfos for GGLWESwitchingKey<D> {
} }
} }
impl<D: Data> GLWEInfos for GGLWESwitchingKey<D> { impl<D: Data> GLWEInfos for GLWESwitchingKey<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data> GGLWEInfos for GGLWESwitchingKey<D> { impl<D: Data> GGLWEInfos for GLWESwitchingKey<D> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.key.rank_in() self.key.rank_in()
} }
@@ -109,13 +109,13 @@ impl<D: Data> GGLWEInfos for GGLWESwitchingKey<D> {
} }
} }
impl<D: DataRef> fmt::Debug for GGLWESwitchingKey<D> { impl<D: DataRef> fmt::Debug for GLWESwitchingKey<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}") write!(f, "{self}")
} }
} }
impl<D: DataRef> fmt::Display for GGLWESwitchingKey<D> { impl<D: DataRef> fmt::Display for GLWESwitchingKey<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
@@ -127,19 +127,19 @@ impl<D: DataRef> fmt::Display for GGLWESwitchingKey<D> {
} }
} }
impl<D: DataMut> FillUniform for GGLWESwitchingKey<D> { impl<D: DataMut> FillUniform for GLWESwitchingKey<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.key.fill_uniform(log_bound, source); self.key.fill_uniform(log_bound, source);
} }
} }
impl GGLWESwitchingKey<Vec<u8>> { impl GLWESwitchingKey<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
GGLWESwitchingKey { GLWESwitchingKey {
key: GGLWECiphertext::alloc(infos), key: GGLWE::alloc(infos),
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
} }
@@ -154,8 +154,8 @@ impl GGLWESwitchingKey<Vec<u8>> {
dnum: Dnum, dnum: Dnum,
dsize: Dsize, dsize: Dsize,
) -> Self { ) -> Self {
GGLWESwitchingKey { GLWESwitchingKey {
key: GGLWECiphertext::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), key: GGLWE::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
} }
@@ -165,7 +165,7 @@ impl GGLWESwitchingKey<Vec<u8>> {
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
GGLWECiphertext::alloc_bytes(infos) GGLWE::alloc_bytes(infos)
} }
pub fn alloc_bytes_with( pub fn alloc_bytes_with(
@@ -177,20 +177,20 @@ impl GGLWESwitchingKey<Vec<u8>> {
dnum: Dnum, dnum: Dnum,
dsize: Dsize, dsize: Dsize,
) -> usize { ) -> usize {
GGLWECiphertext::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) GGLWE::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize)
} }
} }
pub trait GGLWESwitchingKeyToMut { pub trait GLWESwitchingKeyToMut {
fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]>; fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>;
} }
impl<D: DataMut> GGLWESwitchingKeyToMut for GGLWESwitchingKey<D> impl<D: DataMut> GLWESwitchingKeyToMut for GLWESwitchingKey<D>
where where
GGLWECiphertext<D>: GGLWECiphertextToMut, GGLWE<D>: GGLWECiphertextToMut,
{ {
fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]> { fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> {
GGLWESwitchingKey { GLWESwitchingKey {
key: self.key.to_mut(), key: self.key.to_mut(),
sk_in_n: self.sk_in_n, sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n, sk_out_n: self.sk_out_n,
@@ -198,19 +198,36 @@ where
} }
} }
impl<D: DataRef> GGLWESwitchingKey<D> { pub trait GLWESwitchingKeyToRef {
fn to_ref(&self) -> GLWESwitchingKey<&[u8]>;
}
impl<D: DataRef> GLWESwitchingKeyToRef for GLWESwitchingKey<D>
where
GGLWE<D>: GGLWEToRef,
{
fn to_ref(&self) -> GLWESwitchingKey<&[u8]> {
GLWESwitchingKey {
key: self.key.to_ref(),
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
}
}
}
impl<D: DataRef> GLWESwitchingKey<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
self.key.at(row, col) self.key.at(row, col)
} }
} }
impl<D: DataMut> GGLWESwitchingKey<D> { impl<D: DataMut> GLWESwitchingKey<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> {
self.key.at_mut(row, col) self.key.at_mut(row, col)
} }
} }
impl<D: DataMut> ReaderFrom for GGLWESwitchingKey<D> { impl<D: DataMut> ReaderFrom for GLWESwitchingKey<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize; self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize;
self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize; self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize;
@@ -218,7 +235,7 @@ impl<D: DataMut> ReaderFrom for GGLWESwitchingKey<D> {
} }
} }
impl<D: DataRef> WriterTo for GGLWESwitchingKey<D> { impl<D: DataRef> WriterTo for GLWESwitchingKey<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?; writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?;
writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?; writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?;

View File

@@ -3,13 +3,16 @@ use poulpy_hal::{
source::Source, source::Source,
}; };
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt; use std::fmt;
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct GGLWETensorKeyLayout { pub struct TensorKeyLayout {
pub n: Degree, pub n: Degree,
pub base2k: Base2K, pub base2k: Base2K,
pub k: TorusPrecision, pub k: TorusPrecision,
@@ -19,11 +22,11 @@ pub struct GGLWETensorKeyLayout {
} }
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GGLWETensorKey<D: Data> { pub struct TensorKey<D: Data> {
pub(crate) keys: Vec<GGLWESwitchingKey<D>>, pub(crate) keys: Vec<GLWESwitchingKey<D>>,
} }
impl<D: Data> LWEInfos for GGLWETensorKey<D> { impl<D: Data> LWEInfos for TensorKey<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.keys[0].n() self.keys[0].n()
} }
@@ -41,13 +44,13 @@ impl<D: Data> LWEInfos for GGLWETensorKey<D> {
} }
} }
impl<D: Data> GLWEInfos for GGLWETensorKey<D> { impl<D: Data> GLWEInfos for TensorKey<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.keys[0].rank_out() self.keys[0].rank_out()
} }
} }
impl<D: Data> GGLWEInfos for GGLWETensorKey<D> { impl<D: Data> GGLWEInfos for TensorKey<D> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.rank_out() self.rank_out()
} }
@@ -65,7 +68,7 @@ impl<D: Data> GGLWEInfos for GGLWETensorKey<D> {
} }
} }
impl LWEInfos for GGLWETensorKeyLayout { impl LWEInfos for TensorKeyLayout {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.n self.n
} }
@@ -79,13 +82,13 @@ impl LWEInfos for GGLWETensorKeyLayout {
} }
} }
impl GLWEInfos for GGLWETensorKeyLayout { impl GLWEInfos for TensorKeyLayout {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl GGLWEInfos for GGLWETensorKeyLayout { impl GGLWEInfos for TensorKeyLayout {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.rank self.rank
} }
@@ -103,21 +106,21 @@ impl GGLWEInfos for GGLWETensorKeyLayout {
} }
} }
impl<D: DataRef> fmt::Debug for GGLWETensorKey<D> { impl<D: DataRef> fmt::Debug for TensorKey<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}") write!(f, "{self}")
} }
} }
impl<D: DataMut> FillUniform for GGLWETensorKey<D> { impl<D: DataMut> FillUniform for TensorKey<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.keys self.keys
.iter_mut() .iter_mut()
.for_each(|key: &mut GGLWESwitchingKey<D>| key.fill_uniform(log_bound, source)) .for_each(|key: &mut GLWESwitchingKey<D>| key.fill_uniform(log_bound, source))
} }
} }
impl<D: DataRef> fmt::Display for GGLWETensorKey<D> { impl<D: DataRef> fmt::Display for TensorKey<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "(GLWETensorKey)",)?; writeln!(f, "(GLWETensorKey)",)?;
for (i, key) in self.keys.iter().enumerate() { for (i, key) in self.keys.iter().enumerate() {
@@ -127,7 +130,7 @@ impl<D: DataRef> fmt::Display for GGLWETensorKey<D> {
} }
} }
impl GGLWETensorKey<Vec<u8>> { impl TensorKey<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -148,10 +151,10 @@ impl GGLWETensorKey<Vec<u8>> {
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let mut keys: Vec<GGLWESwitchingKey<Vec<u8>>> = Vec::new(); let mut keys: Vec<GLWESwitchingKey<Vec<u8>>> = Vec::new();
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
(0..pairs).for_each(|_| { (0..pairs).for_each(|_| {
keys.push(GGLWESwitchingKey::alloc_with( keys.push(GLWESwitchingKey::alloc_with(
n, n,
base2k, base2k,
k, k,
@@ -176,7 +179,7 @@ impl GGLWETensorKey<Vec<u8>> {
let rank_out: usize = infos.rank_out().into(); let rank_out: usize = infos.rank_out().into();
let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1);
pairs pairs
* GGLWESwitchingKey::alloc_bytes_with( * GLWESwitchingKey::alloc_bytes_with(
infos.n(), infos.n(),
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
@@ -189,13 +192,13 @@ impl GGLWETensorKey<Vec<u8>> {
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { pub fn alloc_bytes_with(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 * GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize) pairs * GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize)
} }
} }
impl<D: DataMut> GGLWETensorKey<D> { impl<D: DataMut> TensorKey<D> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKey<D> { pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey<D> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -204,9 +207,9 @@ impl<D: DataMut> GGLWETensorKey<D> {
} }
} }
impl<D: DataRef> GGLWETensorKey<D> { impl<D: DataRef> TensorKey<D> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKey<D> { pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey<D> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -215,7 +218,7 @@ impl<D: DataRef> GGLWETensorKey<D> {
} }
} }
impl<D: DataMut> ReaderFrom for GGLWETensorKey<D> { impl<D: DataMut> ReaderFrom for TensorKey<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
let len: usize = reader.read_u64::<LittleEndian>()? as usize; let len: usize = reader.read_u64::<LittleEndian>()? as usize;
if self.keys.len() != len { if self.keys.len() != len {
@@ -231,7 +234,7 @@ impl<D: DataMut> ReaderFrom for GGLWETensorKey<D> {
} }
} }
impl<D: DataRef> WriterTo for GGLWETensorKey<D> { impl<D: DataRef> WriterTo for TensorKey<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.keys.len() as u64)?; writer.write_u64::<LittleEndian>(self.keys.len() as u64)?;
for key in &self.keys { for key in &self.keys {
@@ -240,3 +243,33 @@ impl<D: DataRef> WriterTo for GGLWETensorKey<D> {
Ok(()) Ok(())
} }
} }
pub trait TensorKeyToRef {
fn to_ref(&self) -> TensorKey<&[u8]>;
}
impl<D: DataRef> TensorKeyToRef for TensorKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> TensorKey<&[u8]> {
TensorKey {
keys: self.keys.iter().map(|c| c.to_ref()).collect(),
}
}
}
pub trait TensorKeyToMut {
fn to_ref(&mut self) -> TensorKey<&mut [u8]>;
}
impl<D: DataMut> TensorKeyToMut for TensorKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_ref(&mut self) -> TensorKey<&mut [u8]> {
TensorKey {
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
}
}
}

View File

@@ -64,14 +64,14 @@ impl GGSWInfos for GGSWCiphertextLayout {
} }
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GGSWCiphertext<D: Data> { pub struct GGSW<D: Data> {
pub(crate) data: MatZnx<D>, pub(crate) data: MatZnx<D>,
pub(crate) k: TorusPrecision, pub(crate) k: TorusPrecision,
pub(crate) base2k: Base2K, pub(crate) base2k: Base2K,
pub(crate) dsize: Dsize, pub(crate) dsize: Dsize,
} }
impl<D: Data> LWEInfos for GGSWCiphertext<D> { impl<D: Data> LWEInfos for GGSW<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
Degree(self.data.n() as u32) Degree(self.data.n() as u32)
} }
@@ -89,13 +89,13 @@ impl<D: Data> LWEInfos for GGSWCiphertext<D> {
} }
} }
impl<D: Data> GLWEInfos for GGSWCiphertext<D> { impl<D: Data> GLWEInfos for GGSW<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
Rank(self.data.cols_out() as u32 - 1) Rank(self.data.cols_out() as u32 - 1)
} }
} }
impl<D: Data> GGSWInfos for GGSWCiphertext<D> { impl<D: Data> GGSWInfos for GGSW<D> {
fn dsize(&self) -> Dsize { fn dsize(&self) -> Dsize {
self.dsize self.dsize
} }
@@ -112,7 +112,7 @@ pub struct GGSWCiphertextBuilder<D: Data> {
dsize: Option<Dsize>, dsize: Option<Dsize>,
} }
impl<D: Data> GGSWCiphertext<D> { impl<D: Data> GGSW<D> {
#[inline] #[inline]
pub fn builder() -> GGSWCiphertextBuilder<D> { pub fn builder() -> GGSWCiphertextBuilder<D> {
GGSWCiphertextBuilder { GGSWCiphertextBuilder {
@@ -182,7 +182,7 @@ impl<D: Data> GGSWCiphertextBuilder<D> {
self self
} }
pub fn build(self) -> Result<GGSWCiphertext<D>, BuildError> { pub fn build(self) -> Result<GGSW<D>, BuildError> {
let data: MatZnx<D> = self.data.ok_or(BuildError::MissingData)?; let data: MatZnx<D> = self.data.ok_or(BuildError::MissingData)?;
let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?;
let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?;
@@ -212,7 +212,7 @@ impl<D: Data> GGSWCiphertextBuilder<D> {
return Err(BuildError::ZeroLimbs); return Err(BuildError::ZeroLimbs);
} }
Ok(GGSWCiphertext { Ok(GGSW {
data, data,
base2k, base2k,
k, k,
@@ -221,13 +221,13 @@ impl<D: Data> GGSWCiphertextBuilder<D> {
} }
} }
impl<D: DataRef> fmt::Debug for GGSWCiphertext<D> { impl<D: DataRef> fmt::Debug for GGSW<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.data) write!(f, "{}", self.data)
} }
} }
impl<D: DataRef> fmt::Display for GGSWCiphertext<D> { impl<D: DataRef> fmt::Display for GGSW<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
@@ -240,13 +240,13 @@ impl<D: DataRef> fmt::Display for GGSWCiphertext<D> {
} }
} }
impl<D: DataMut> FillUniform for GGSWCiphertext<D> { impl<D: DataMut> FillUniform for GGSW<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.data.fill_uniform(log_bound, source); self.data.fill_uniform(log_bound, source);
} }
} }
impl<D: DataRef> GGSWCiphertext<D> { impl<D: DataRef> GGSW<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
GLWECiphertext::builder() GLWECiphertext::builder()
.data(self.data.at(row, col)) .data(self.data.at(row, col))
@@ -257,7 +257,7 @@ impl<D: DataRef> GGSWCiphertext<D> {
} }
} }
impl<D: DataMut> GGSWCiphertext<D> { impl<D: DataMut> GGSW<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> {
GLWECiphertext::builder() GLWECiphertext::builder()
.base2k(self.base2k()) .base2k(self.base2k())
@@ -268,7 +268,7 @@ impl<D: DataMut> GGSWCiphertext<D> {
} }
} }
impl GGSWCiphertext<Vec<u8>> { impl GGSW<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGSWInfos, A: GGSWInfos,
@@ -353,7 +353,7 @@ impl GGSWCiphertext<Vec<u8>> {
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
impl<D: DataMut> ReaderFrom for GGSWCiphertext<D> { impl<D: DataMut> ReaderFrom for GGSW<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?); self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
@@ -362,7 +362,7 @@ impl<D: DataMut> ReaderFrom for GGSWCiphertext<D> {
} }
} }
impl<D: DataRef> WriterTo for GGSWCiphertext<D> { impl<D: DataRef> WriterTo for GGSW<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u32::<LittleEndian>(self.k.into())?; writer.write_u32::<LittleEndian>(self.k.into())?;
writer.write_u32::<LittleEndian>(self.base2k.into())?; writer.write_u32::<LittleEndian>(self.base2k.into())?;
@@ -371,34 +371,32 @@ impl<D: DataRef> WriterTo for GGSWCiphertext<D> {
} }
} }
pub trait GGSWCiphertextToMut { pub trait GGSWToMut {
fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]>; fn to_mut(&mut self) -> GGSW<&mut [u8]>;
} }
impl<D: DataMut> GGSWCiphertextToMut for GGSWCiphertext<D> { impl<D: DataMut> GGSWToMut for GGSW<D> {
fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]> { fn to_mut(&mut self) -> GGSW<&mut [u8]> {
GGSWCiphertext::builder() GGSW {
.base2k(self.base2k()) dsize: self.dsize,
.dsize(self.dsize()) k: self.k,
.k(self.k()) base2k: self.base2k,
.data(self.data.to_mut()) data: self.data.to_mut(),
.build() }
.unwrap()
} }
} }
pub trait GGSWCiphertextToRef { pub trait GGSWToRef {
fn to_ref(&self) -> GGSWCiphertext<&[u8]>; fn to_ref(&self) -> GGSW<&[u8]>;
} }
impl<D: DataRef> GGSWCiphertextToRef for GGSWCiphertext<D> { impl<D: DataRef> GGSWToRef for GGSW<D> {
fn to_ref(&self) -> GGSWCiphertext<&[u8]> { fn to_ref(&self) -> GGSW<&[u8]> {
GGSWCiphertext::builder() GGSW {
.base2k(self.base2k()) dsize: self.dsize,
.dsize(self.dsize()) k: self.k,
.k(self.k()) base2k: self.base2k,
.data(self.data.to_ref()) data: self.data.to_ref(),
.build() }
.unwrap()
} }
} }

View File

@@ -253,36 +253,6 @@ impl GLWECiphertext<Vec<u8>> {
} }
} }
pub trait GLWECiphertextToRef {
fn to_ref(&self) -> GLWECiphertext<&[u8]>;
}
impl<D: DataRef> GLWECiphertextToRef for GLWECiphertext<D> {
fn to_ref(&self) -> GLWECiphertext<&[u8]> {
GLWECiphertext::builder()
.k(self.k())
.base2k(self.base2k())
.data(self.data.to_ref())
.build()
.unwrap()
}
}
pub trait GLWECiphertextToMut {
fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>;
}
impl<D: DataMut> GLWECiphertextToMut for GLWECiphertext<D> {
fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> {
GLWECiphertext::builder()
.k(self.k())
.base2k(self.base2k())
.data(self.data.to_mut())
.build()
.unwrap()
}
}
impl<D: DataMut> ReaderFrom for GLWECiphertext<D> { impl<D: DataMut> ReaderFrom for GLWECiphertext<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
@@ -298,3 +268,31 @@ impl<D: DataRef> WriterTo for GLWECiphertext<D> {
self.data.write_to(writer) self.data.write_to(writer)
} }
} }
pub trait GLWECiphertextToRef {
fn to_ref(&self) -> GLWECiphertext<&[u8]>;
}
impl<D: DataRef> GLWECiphertextToRef for GLWECiphertext<D> {
fn to_ref(&self) -> GLWECiphertext<&[u8]> {
GLWECiphertext {
k: self.k,
base2k: self.base2k,
data: self.data.to_ref(),
}
}
}
pub trait GLWECiphertextToMut {
fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>;
}
impl<D: DataMut> GLWECiphertextToMut for GLWECiphertext<D> {
fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> {
GLWECiphertext {
k: self.k,
base2k: self.base2k,
data: self.data.to_mut(),
}
}
}

View File

@@ -3,7 +3,10 @@ use poulpy_hal::{
source::Source, source::Source,
}; };
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
};
use std::fmt; use std::fmt;
@@ -56,9 +59,9 @@ impl GGLWEInfos for GLWEToLWEKeyLayout {
/// A special [GLWESwitchingKey] required to for the conversion from [GLWECiphertext] to [LWECiphertext]. /// A special [GLWESwitchingKey] required to for the conversion from [GLWECiphertext] to [LWECiphertext].
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct GLWEToLWEKey<D: Data>(pub(crate) GGLWESwitchingKey<D>); pub struct GLWEToLWESwitchingKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
impl<D: Data> LWEInfos for GLWEToLWEKey<D> { impl<D: Data> LWEInfos for GLWEToLWESwitchingKey<D> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
self.0.base2k() self.0.base2k()
} }
@@ -76,12 +79,12 @@ impl<D: Data> LWEInfos for GLWEToLWEKey<D> {
} }
} }
impl<D: Data> GLWEInfos for GLWEToLWEKey<D> { impl<D: Data> GLWEInfos for GLWEToLWESwitchingKey<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data> GGLWEInfos for GLWEToLWEKey<D> { impl<D: Data> GGLWEInfos for GLWEToLWESwitchingKey<D> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.0.rank_in() self.0.rank_in()
} }
@@ -99,37 +102,37 @@ impl<D: Data> GGLWEInfos for GLWEToLWEKey<D> {
} }
} }
impl<D: DataRef> fmt::Debug for GLWEToLWEKey<D> { impl<D: DataRef> fmt::Debug for GLWEToLWESwitchingKey<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}") write!(f, "{self}")
} }
} }
impl<D: DataMut> FillUniform for GLWEToLWEKey<D> { impl<D: DataMut> FillUniform for GLWEToLWESwitchingKey<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.0.fill_uniform(log_bound, source); self.0.fill_uniform(log_bound, source);
} }
} }
impl<D: DataRef> fmt::Display for GLWEToLWEKey<D> { impl<D: DataRef> fmt::Display for GLWEToLWESwitchingKey<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(GLWEToLWESwitchingKey) {}", self.0) write!(f, "(GLWEToLWESwitchingKey) {}", self.0)
} }
} }
impl<D: DataMut> ReaderFrom for GLWEToLWEKey<D> { impl<D: DataMut> ReaderFrom for GLWEToLWESwitchingKey<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.0.read_from(reader) self.0.read_from(reader)
} }
} }
impl<D: DataRef> WriterTo for GLWEToLWEKey<D> { impl<D: DataRef> WriterTo for GLWEToLWESwitchingKey<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
self.0.write_to(writer) self.0.write_to(writer)
} }
} }
impl GLWEToLWEKey<Vec<u8>> { impl GLWEToLWESwitchingKey<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self pub fn alloc<A>(infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -144,11 +147,11 @@ impl GLWEToLWEKey<Vec<u8>> {
1, 1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey" "dsize > 1 is not supported for GLWEToLWESwitchingKey"
); );
Self(GGLWESwitchingKey::alloc(infos)) Self(GLWESwitchingKey::alloc(infos))
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
Self(GGLWESwitchingKey::alloc_with( Self(GLWESwitchingKey::alloc_with(
n, n,
base2k, base2k,
k, k,
@@ -173,10 +176,36 @@ impl GLWEToLWEKey<Vec<u8>> {
1, 1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey" "dsize > 1 is not supported for GLWEToLWESwitchingKey"
); );
GGLWESwitchingKey::alloc_bytes(infos) GLWESwitchingKey::alloc_bytes(infos)
} }
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1))
}
}
pub trait GLWEToLWESwitchingKeyToRef {
fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>;
}
impl<D: DataRef> GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> {
GLWEToLWESwitchingKey(self.0.to_ref())
}
}
pub trait GLWEToLWESwitchingKeyToMut {
fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> {
GLWEToLWESwitchingKey(self.0.to_mut())
} }
} }

View File

@@ -27,6 +27,11 @@ pub trait LWEInfos {
} }
} }
pub trait SetLWEInfos {
fn set_k(&mut self, k: TorusPrecision);
fn set_base2k(&mut self, base2k: Base2K);
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct LWECiphertextLayout { pub struct LWECiphertextLayout {
pub n: Degree, pub n: Degree,
@@ -47,7 +52,6 @@ impl LWEInfos for LWECiphertextLayout {
self.n self.n
} }
} }
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct LWECiphertext<D: Data> { pub struct LWECiphertext<D: Data> {
pub(crate) data: Zn<D>, pub(crate) data: Zn<D>,
@@ -72,6 +76,16 @@ impl<D: Data> LWEInfos for LWECiphertext<D> {
} }
} }
impl<D: Data> SetLWEInfos for LWECiphertext<D> {
fn set_base2k(&mut self, base2k: Base2K) {
self.base2k = base2k
}
fn set_k(&mut self, k: TorusPrecision) {
self.k = k
}
}
impl<D: DataRef> LWECiphertext<D> { impl<D: DataRef> LWECiphertext<D> {
pub fn data(&self) -> &Zn<D> { pub fn data(&self) -> &Zn<D> {
&self.data &self.data
@@ -221,12 +235,11 @@ pub trait LWECiphertextToRef {
impl<D: DataRef> LWECiphertextToRef for LWECiphertext<D> { impl<D: DataRef> LWECiphertextToRef for LWECiphertext<D> {
fn to_ref(&self) -> LWECiphertext<&[u8]> { fn to_ref(&self) -> LWECiphertext<&[u8]> {
LWECiphertext::builder() LWECiphertext {
.base2k(self.base2k()) k: self.k,
.k(self.k()) base2k: self.base2k,
.data(self.data.to_ref()) data: self.data.to_ref(),
.build() }
.unwrap()
} }
} }
@@ -237,12 +250,11 @@ pub trait LWECiphertextToMut {
impl<D: DataMut> LWECiphertextToMut for LWECiphertext<D> { impl<D: DataMut> LWECiphertextToMut for LWECiphertext<D> {
fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> { fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> {
LWECiphertext::builder() LWECiphertext {
.base2k(self.base2k()) k: self.k,
.k(self.k()) base2k: self.base2k,
.data(self.data.to_mut()) data: self.data.to_mut(),
.build() }
.unwrap()
} }
} }

View File

@@ -5,7 +5,10 @@ use poulpy_hal::{
source::Source, source::Source,
}; };
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct LWESwitchingKeyLayout { pub struct LWESwitchingKeyLayout {
@@ -54,7 +57,7 @@ impl GGLWEInfos for LWESwitchingKeyLayout {
} }
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct LWESwitchingKey<D: Data>(pub(crate) GGLWESwitchingKey<D>); pub struct LWESwitchingKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
impl<D: Data> LWEInfos for LWESwitchingKey<D> { impl<D: Data> LWEInfos for LWESwitchingKey<D> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
@@ -118,11 +121,11 @@ impl LWESwitchingKey<Vec<u8>> {
1, 1,
"rank_out > 1 is not supported for LWESwitchingKey" "rank_out > 1 is not supported for LWESwitchingKey"
); );
Self(GGLWESwitchingKey::alloc(infos)) Self(GLWESwitchingKey::alloc(infos))
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
Self(GGLWESwitchingKey::alloc_with( Self(GLWESwitchingKey::alloc_with(
n, n,
base2k, base2k,
k, k,
@@ -152,11 +155,11 @@ impl LWESwitchingKey<Vec<u8>> {
1, 1,
"rank_out > 1 is not supported for LWESwitchingKey" "rank_out > 1 is not supported for LWESwitchingKey"
); );
GGLWESwitchingKey::alloc_bytes(infos) GLWESwitchingKey::alloc_bytes(infos)
} }
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
} }
} }
@@ -189,3 +192,29 @@ impl<D: DataRef> WriterTo for LWESwitchingKey<D> {
self.0.write_to(writer) self.0.write_to(writer)
} }
} }
pub trait LWEToLWEKeyToRef {
fn to_ref(&self) -> LWESwitchingKey<&[u8]>;
}
impl<D: DataRef> LWEToLWEKeyToRef for LWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> LWESwitchingKey<&[u8]> {
LWESwitchingKey(self.0.to_ref())
}
}
pub trait LWEToLWEKeyToMut {
fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> LWEToLWEKeyToMut for LWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> {
LWESwitchingKey(self.0.to_mut())
}
}

View File

@@ -1,5 +1,5 @@
use poulpy_hal::{ use poulpy_hal::{
layouts::{Data, DataMut, DataRef, ScalarZnx, ZnxInfos, ZnxView, ZnxZero}, layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero},
source::Source, source::Source,
}; };
@@ -84,3 +84,29 @@ impl<D: DataMut> LWESecret<D> {
self.dist = Distribution::ZERO; self.dist = Distribution::ZERO;
} }
} }
pub trait LWESecretToRef {
fn to_ref(&self) -> LWESecret<&[u8]>;
}
impl<D: DataRef> LWESecretToRef for LWESecret<D> {
fn to_ref(&self) -> LWESecret<&[u8]> {
LWESecret {
dist: self.dist,
data: self.data.to_ref(),
}
}
}
pub trait LWESecretToMut {
fn to_mut(&mut self) -> LWESecret<&mut [u8]>;
}
impl<D: DataMut> LWESecretToMut for LWESecret<D> {
fn to_mut(&mut self) -> LWESecret<&mut [u8]> {
LWESecret {
dist: self.dist,
data: self.data.to_mut(),
}
}
}

View File

@@ -5,7 +5,10 @@ use poulpy_hal::{
source::Source, source::Source,
}; };
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct LWEToGLWESwitchingKeyLayout { pub struct LWEToGLWESwitchingKeyLayout {
@@ -55,7 +58,7 @@ impl GGLWEInfos for LWEToGLWESwitchingKeyLayout {
} }
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct LWEToGLWESwitchingKey<D: Data>(pub(crate) GGLWESwitchingKey<D>); pub struct LWEToGLWESwitchingKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
impl<D: Data> LWEInfos for LWEToGLWESwitchingKey<D> { impl<D: Data> LWEInfos for LWEToGLWESwitchingKey<D> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
@@ -143,11 +146,11 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
1, 1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey" "dsize > 1 is not supported for LWEToGLWESwitchingKey"
); );
Self(GGLWESwitchingKey::alloc(infos)) Self(GLWESwitchingKey::alloc(infos))
} }
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
Self(GGLWESwitchingKey::alloc_with( Self(GLWESwitchingKey::alloc_with(
n, n,
base2k, base2k,
k, k,
@@ -172,10 +175,36 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
1, 1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey" "dsize > 1 is not supported for LWEToGLWESwitchingKey"
); );
GGLWESwitchingKey::alloc_bytes(infos) GLWESwitchingKey::alloc_bytes(infos)
} }
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize {
GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1))
}
}
pub trait LWEToGLWESwitchingKeyToRef {
fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>;
}
impl<D: DataRef> LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> {
LWEToGLWESwitchingKey(self.0.to_ref())
}
}
pub trait LWEToGLWESwitchingKeyToMut {
fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> {
LWEToGLWESwitchingKey(self.0.to_mut())
} }
} }

View File

@@ -47,6 +47,7 @@ pub enum BuildError {
ZeroLimbs, ZeroLimbs,
ZeroRank, ZeroRank,
ZeroDigits, ZeroDigits,
VecZnxColsNotOne,
} }
/// Newtype over `u32` with arithmetic and comparisons against same type and `u32`. /// Newtype over `u32` with arithmetic and comparisons against same type and `u32`.

View File

@@ -4,23 +4,25 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, prepared::{
GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef,
},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGLWEAutomorphismKeyPrepared<D: Data, B: Backend> { pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWESwitchingKeyPrepared<D, B>, pub(crate) key: GLWESwitchingKeyPrepared<D, B>,
pub(crate) p: i64, pub(crate) p: i64,
} }
impl<D: Data, B: Backend> GGLWEAutomorphismKeyPrepared<D, B> { impl<D: Data, B: Backend> AutomorphismKeyPrepared<D, B> {
pub fn p(&self) -> i64 { pub fn p(&self) -> i64 {
self.p self.p
} }
} }
impl<D: Data, B: Backend> LWEInfos for GGLWEAutomorphismKeyPrepared<D, B> { impl<D: Data, B: Backend> LWEInfos for AutomorphismKeyPrepared<D, B> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.key.n() self.key.n()
} }
@@ -38,13 +40,23 @@ impl<D: Data, B: Backend> LWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
} }
} }
impl<D: Data, B: Backend> GLWEInfos for GGLWEAutomorphismKeyPrepared<D, B> { pub trait SetP {
fn set_p(&mut self, p: i64);
}
impl<D: Data, B: Backend> SetP for AutomorphismKeyPrepared<D, B> {
fn set_p(&mut self, p: i64) {
self.p = p
}
}
impl<D: Data, B: Backend> GLWEInfos for AutomorphismKeyPrepared<D, B> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data, B: Backend> GGLWEInfos for GGLWEAutomorphismKeyPrepared<D, B> { impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.key.rank_in() self.key.rank_in()
} }
@@ -62,7 +74,7 @@ impl<D: Data, B: Backend> GGLWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
} }
} }
impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> { impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -73,8 +85,8 @@ impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
); );
GGLWEAutomorphismKeyPrepared::<Vec<u8>, B> { AutomorphismKeyPrepared::<Vec<u8>, B> {
key: GGLWESwitchingKeyPrepared::alloc(module, infos), key: GLWESwitchingKeyPrepared::alloc(module, infos),
p: 0, p: 0,
} }
} }
@@ -83,8 +95,8 @@ impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
GGLWEAutomorphismKeyPrepared { AutomorphismKeyPrepared {
key: GGLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize), key: GLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize),
p: 0, p: 0,
} }
} }
@@ -99,43 +111,104 @@ impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
infos.rank_out(), infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
); );
GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) GLWESwitchingKeyPrepared::alloc_bytes(module, infos)
} }
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize) GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize)
} }
} }
impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for GGLWEAutomorphismKeyPrepared<Vec<u8>, B> pub trait AutomorphismKeyPrepareTmpBytes {
fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A)
where
A: GGLWEInfos;
}
impl<B: Backend> AutomorphismKeyPrepareTmpBytes for Module<B>
where where
GGLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>, Module<B>: GLWESwitchingKeyPrepareTmpBytes,
{ {
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize { fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A)
GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) where
A: GGLWEInfos,
{
self.glwe_switching_key_prepare_tmp_bytes(infos)
} }
} }
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWEAutomorphismKey<DR>> for GGLWEAutomorphismKeyPrepared<D, B> impl<D: DataRef, B: Backend> AutomorphismKeyPrepared<D, B> {
where pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize
Module<B>: VmpPrepare<B>, where
{ Module<B>: AutomorphismKeyPrepareTmpBytes,
fn prepare(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DR>, scratch: &mut Scratch<B>) { {
self.key.prepare(module, &other.key, scratch); module.automorphism_key_prepare_tmp_bytes(self);
}
}
pub trait AutomorphismKeyPrepare<B: Backend> {
fn automorphism_key_prepare<R, O>(&self, res: &R, other: &O, scratch: &Scratch<B>)
where
R: AutomorphismKeyPreparedToMut<B>,
O: AutomorphismKeyToRef;
}
impl<B: Backend> AutomorphismKeyPrepare<B> for Module<B> {
fn automorphism_key_prepare<R, O>(&self, res: &R, other: &O, scratch: &Scratch<B>)
where
R: AutomorphismKeyPreparedToMut<B>,
O: AutomorphismKeyToRef,
{
self.key.prepare(self, &other.to_ref().key, scratch);
self.p = other.p; self.p = other.p;
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> for GGLWEAutomorphismKey<D> pub trait AutomorphismKeyPrepareAlloc<B: Backend> {
fn automorphism_key_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>) -> AutomorphismKeyPrepared<Vec<u8>, B>
where
O: AutomorphismKeyToRef;
}
impl<B: Backend> AutomorphismKeyPrepareAlloc<B> for Module<B>
where where
Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>, Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
{ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> { fn automorphism_key_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>) -> AutomorphismKeyPrepared<Vec<u8>, B>
let mut atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = GGLWEAutomorphismKeyPrepared::alloc(module, self); where
atk_prepared.prepare(module, self, scratch); O: AutomorphismKeyToRef,
{
let mut atk_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = AutomorphismKeyPrepared::alloc(self, &other.to_ref());
atk_prepared.prepare(self, &other.to_ref(), scratch);
atk_prepared atk_prepared
} }
} }
pub trait AutomorphismKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> AutomorphismKeyPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> {
AutomorphismKeyPrepared {
p: self.p,
key: self.key.to_mut(),
}
}
}
pub trait AutomorphismKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> AutomorphismKeyPreparedToRef<B> for AutomorphismKeyPrepared<D, B> {
fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> {
AutomorphismKeyPrepared {
p: self.p,
key: self.key.to_ref(),
}
}
}

View File

@@ -1,23 +1,22 @@
use poulpy_hal::{ use poulpy_hal::{
api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, ZnxInfos}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos},
oep::VmpPMatAllocBytesImpl, oep::VmpPMatAllocBytesImpl,
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, BuildError, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, BuildError, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{Prepare, PrepareAlloc, PrepareScratchSpace},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGLWECiphertextPrepared<D: Data, B: Backend> { pub struct GGLWEPrepared<D: Data, B: Backend> {
pub(crate) data: VmpPMat<D, B>, pub(crate) data: VmpPMat<D, B>,
pub(crate) k: TorusPrecision, pub(crate) k: TorusPrecision,
pub(crate) base2k: Base2K, pub(crate) base2k: Base2K,
pub(crate) dsize: Dsize, pub(crate) dsize: Dsize,
} }
impl<D: Data, B: Backend> LWEInfos for GGLWECiphertextPrepared<D, B> { impl<D: Data, B: Backend> LWEInfos for GGLWEPrepared<D, B> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
Degree(self.data.n() as u32) Degree(self.data.n() as u32)
} }
@@ -35,13 +34,13 @@ impl<D: Data, B: Backend> LWEInfos for GGLWECiphertextPrepared<D, B> {
} }
} }
impl<D: Data, B: Backend> GLWEInfos for GGLWECiphertextPrepared<D, B> { impl<D: Data, B: Backend> GLWEInfos for GGLWEPrepared<D, B> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data, B: Backend> GGLWEInfos for GGLWECiphertextPrepared<D, B> { impl<D: Data, B: Backend> GGLWEInfos for GGLWEPrepared<D, B> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
Rank(self.data.cols_in() as u32) Rank(self.data.cols_in() as u32)
} }
@@ -59,17 +58,17 @@ impl<D: Data, B: Backend> GGLWEInfos for GGLWECiphertextPrepared<D, B> {
} }
} }
pub struct GGLWECiphertextPreparedBuilder<D: Data, B: Backend> { pub struct GGLWEPreparedBuilder<D: Data, B: Backend> {
data: Option<VmpPMat<D, B>>, data: Option<VmpPMat<D, B>>,
base2k: Option<Base2K>, base2k: Option<Base2K>,
k: Option<TorusPrecision>, k: Option<TorusPrecision>,
dsize: Option<Dsize>, dsize: Option<Dsize>,
} }
impl<D: Data, B: Backend> GGLWECiphertextPrepared<D, B> { impl<D: Data, B: Backend> GGLWEPrepared<D, B> {
#[inline] #[inline]
pub fn builder() -> GGLWECiphertextPreparedBuilder<D, B> { pub fn builder() -> GGLWEPreparedBuilder<D, B> {
GGLWECiphertextPreparedBuilder { GGLWEPreparedBuilder {
data: None, data: None,
base2k: None, base2k: None,
k: None, k: None,
@@ -78,7 +77,7 @@ impl<D: Data, B: Backend> GGLWECiphertextPrepared<D, B> {
} }
} }
impl<B: Backend> GGLWECiphertextPreparedBuilder<Vec<u8>, B> { impl<B: Backend> GGLWEPreparedBuilder<Vec<u8>, B> {
#[inline] #[inline]
pub fn layout<A>(mut self, infos: &A) -> Self pub fn layout<A>(mut self, infos: &A) -> Self
where where
@@ -99,7 +98,7 @@ impl<B: Backend> GGLWECiphertextPreparedBuilder<Vec<u8>, B> {
} }
} }
impl<D: Data, B: Backend> GGLWECiphertextPreparedBuilder<D, B> { impl<D: Data, B: Backend> GGLWEPreparedBuilder<D, B> {
#[inline] #[inline]
pub fn data(mut self, data: VmpPMat<D, B>) -> Self { pub fn data(mut self, data: VmpPMat<D, B>) -> Self {
self.data = Some(data); self.data = Some(data);
@@ -122,7 +121,7 @@ impl<D: Data, B: Backend> GGLWECiphertextPreparedBuilder<D, B> {
self self
} }
pub fn build(self) -> Result<GGLWECiphertextPrepared<D, B>, BuildError> { pub fn build(self) -> Result<GGLWEPrepared<D, B>, BuildError> {
let data: VmpPMat<D, B> = self.data.ok_or(BuildError::MissingData)?; let data: VmpPMat<D, B> = self.data.ok_or(BuildError::MissingData)?;
let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?;
let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?;
@@ -152,7 +151,7 @@ impl<D: Data, B: Backend> GGLWECiphertextPreparedBuilder<D, B> {
return Err(BuildError::ZeroLimbs); return Err(BuildError::ZeroLimbs);
} }
Ok(GGLWECiphertextPrepared { Ok(GGLWEPrepared {
data, data,
base2k, base2k,
k, k,
@@ -161,7 +160,7 @@ impl<D: Data, B: Backend> GGLWECiphertextPreparedBuilder<D, B> {
} }
} }
impl<B: Backend> GGLWECiphertextPrepared<Vec<u8>, B> { impl<B: Backend> GGLWEPrepared<Vec<u8>, B> {
pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -260,12 +259,21 @@ impl<B: Backend> GGLWECiphertextPrepared<Vec<u8>, B> {
} }
} }
impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for GGLWECiphertextPrepared<Vec<u8>, B> pub trait GGLWEPrepareTmpBytes {
fn gglwe_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
}
impl<B: Backend> GGLWEPrepareTmpBytes for Module<B>
where where
Module<B>: VmpPrepareTmpBytes, Module<B>: VmpPrepareTmpBytes,
{ {
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize { fn gglwe_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
module.vmp_prepare_tmp_bytes( where
A: GGLWEInfos,
{
self.vmp_prepare_tmp_bytes(
infos.dnum().into(), infos.dnum().into(),
infos.rank_in().into(), infos.rank_in().into(),
(infos.rank() + 1).into(), (infos.rank() + 1).into(),
@@ -274,25 +282,106 @@ where
} }
} }
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWECiphertext<DR>> for GGLWECiphertextPrepared<D, B> impl<B: Backend> GGLWEPrepared<Vec<u8>, B> {
where pub fn prepare_tmp_bytes(&self, module: &Module<B>)
Module<B>: VmpPrepare<B>, where
{ Module<B>: GGLWEPrepareTmpBytes,
fn prepare(&mut self, module: &Module<B>, other: &GGLWECiphertext<DR>, scratch: &mut Scratch<B>) { {
module.vmp_prepare(&mut self.data, &other.data, scratch); module.gglwe_prepare_tmp_bytes(self)
self.k = other.k;
self.base2k = other.base2k;
self.dsize = other.dsize;
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWECiphertextPrepared<Vec<u8>, B>> for GGLWECiphertext<D> pub trait GGLWEPrepare<B: Backend> {
fn gglwe_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GGLWEPreparedToMut<B>,
O: GGLWEToRef;
}
impl<B: Backend> GGLWEPrepare<B> for Module<B>
where
Module<B>: VmpPrepare<B>,
{
fn gglwe_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GGLWEPreparedToMut<B>,
O: GGLWEToRef,
{
let mut res: GGLWEPrepared<&mut [u8], B> = self.to_mut();
let other: GGLWE<&[u8]> = other.to_ref();
assert_eq!(res.n(), self.n() as u32);
assert_eq!(other.n(), self.n() as u32);
assert_eq!(res.base2k, other.base2k);
assert_eq!(res.k, other.k);
assert_eq!(res.dsize, other.dsize);
self.vmp_prepare(&mut res.data, &other.data, scratch);
}
}
impl<D: DataMut, B: Backend> GGLWEPrepared<D, B>
where
Module<B>: GGLWEPrepare<B>,
{
fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)
where
O: GGLWEToRef,
{
module.gglwe_prepare(self, other, scratch);
}
}
pub trait GGLWEPrepareAlloc<B: Backend> {
fn gglwe_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>) -> GGLWEPrepared<Vec<u8>, B>;
}
impl<B: Backend> GGLWEPrepareAlloc<B> for Module<B>
where where
Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>, Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
{ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWECiphertextPrepared<Vec<u8>, B> { fn gglwe_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>) -> GGLWEPrepared<Vec<u8>, B> {
let mut atk_prepared: GGLWECiphertextPrepared<Vec<u8>, B> = GGLWECiphertextPrepared::alloc(module, self); let mut ct_prepared: GGLWEPrepared<Vec<u8>, B> = GGLWEPrepared::alloc(self, &other.to_ref());
atk_prepared.prepare(module, self, scratch); ct_prepared.prepare(self, &other.to_ref(), scratch);
atk_prepared ct_prepared
}
}
impl<D: DataRef> GGLWE<D> {
fn prepare_alloc<B: Backend>(&self, module: &Module<B>, scratch: &Scratch<B>) -> GGLWEPrepared<Vec<u8>, B>
where
Module<B>: GGLWEPrepareAlloc<B>,
{
module.gglwe_prepare_alloc(self, scratch)
}
}
pub trait GGLWEPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GGLWEPrepared<D, B> {
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
GGLWEPrepared {
k: self.k,
base2k: self.base2k,
dsize: self.dsize,
data: self.data.to_mut(),
}
}
}
pub trait GGLWEPreparedToRef<B: Backend> {
fn to_ref(&self) -> GGLWEPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GGLWEPrepared<D, B> {
fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
GGLWEPrepared {
k: self.k,
base2k: self.base2k,
dsize: self.dsize,
data: self.data.to_ref(),
}
} }
} }

View File

@@ -1,21 +1,36 @@
use poulpy_hal::{ use poulpy_hal::{
api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, api::{VmpPMatAlloc, VmpPMatAllocBytes},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
prepared::{GGLWECiphertextPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, prepared::{GGLWEPrepare, GGLWEPrepareTmpBytes, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGLWESwitchingKeyPrepared<D: Data, B: Backend> { pub struct GLWESwitchingKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWECiphertextPrepared<D, B>, pub(crate) key: GGLWEPrepared<D, B>,
pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out pub(crate) sk_out_n: usize, // Degree of sk_out
} }
impl<D: Data, B: Backend> LWEInfos for GGLWESwitchingKeyPrepared<D, B> { pub(crate) trait GLWESwitchingKeyPreparedSetMetaData {
fn set_sk_in_n(&mut self, sk_in_n: usize);
fn set_sk_out_n(&mut self, sk_out_n: usize);
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyPreparedSetMetaData for GLWESwitchingKeyPrepared<D, B> {
fn set_sk_in_n(&mut self, sk_in_n: usize) {
self.sk_in_n = sk_in_n
}
fn set_sk_out_n(&mut self, sk_out_n: usize) {
self.sk_out_n = self.sk_out_n
}
}
impl<D: Data, B: Backend> LWEInfos for GLWESwitchingKeyPrepared<D, B> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.key.n() self.key.n()
} }
@@ -33,13 +48,13 @@ impl<D: Data, B: Backend> LWEInfos for GGLWESwitchingKeyPrepared<D, B> {
} }
} }
impl<D: Data, B: Backend> GLWEInfos for GGLWESwitchingKeyPrepared<D, B> { impl<D: Data, B: Backend> GLWEInfos for GLWESwitchingKeyPrepared<D, B> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data, B: Backend> GGLWEInfos for GGLWESwitchingKeyPrepared<D, B> { impl<D: Data, B: Backend> GGLWEInfos for GLWESwitchingKeyPrepared<D, B> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.key.rank_in() self.key.rank_in()
} }
@@ -57,15 +72,15 @@ impl<D: Data, B: Backend> GGLWEInfos for GGLWESwitchingKeyPrepared<D, B> {
} }
} }
impl<B: Backend> GGLWESwitchingKeyPrepared<Vec<u8>, B> { impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()");
GGLWESwitchingKeyPrepared::<Vec<u8>, B> { GLWESwitchingKeyPrepared::<Vec<u8>, B> {
key: GGLWECiphertextPrepared::alloc(module, infos), key: GGLWEPrepared::alloc(module, infos),
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
} }
@@ -83,8 +98,8 @@ impl<B: Backend> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
GGLWESwitchingKeyPrepared::<Vec<u8>, B> { GLWESwitchingKeyPrepared::<Vec<u8>, B> {
key: GGLWECiphertextPrepared::alloc_with(module, base2k, k, rank_in, rank_out, dnum, dsize), key: GGLWEPrepared::alloc_with(module, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
} }
@@ -96,7 +111,7 @@ impl<B: Backend> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()");
GGLWECiphertextPrepared::alloc_bytes(module, infos) GGLWEPrepared::alloc_bytes(module, infos)
} }
pub fn alloc_bytes_with( pub fn alloc_bytes_with(
@@ -111,37 +126,122 @@ impl<B: Backend> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWECiphertextPrepared::alloc_bytes_with(module, base2k, k, rank_in, rank_out, dnum, dsize) GGLWEPrepared::alloc_bytes_with(module, base2k, k, rank_in, rank_out, dnum, dsize)
} }
} }
impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for GGLWESwitchingKeyPrepared<Vec<u8>, B> pub trait GLWESwitchingKeyPrepareTmpBytes {
fn glwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
}
impl<B: Backend> GLWESwitchingKeyPrepareTmpBytes for Module<B>
where where
GGLWECiphertextPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>, Module<B>: GGLWEPrepareTmpBytes,
{ {
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize { fn glwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
GGLWECiphertextPrepared::prepare_scratch_space(module, infos) where
A: GGLWEInfos,
{
self.gglwe_prepare_tmp_bytes(infos)
} }
} }
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWESwitchingKey<DR>> for GGLWESwitchingKeyPrepared<D, B> impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B>
where where
Module<B>: VmpPrepare<B>, Module<B>: GGLWEPrepareTmpBytes,
{ {
fn prepare(&mut self, module: &Module<B>, other: &GGLWESwitchingKey<DR>, scratch: &mut Scratch<B>) { pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize {
self.key.prepare(module, &other.key, scratch); module.gglwe_prepare_tmp_bytes(self)
self.sk_in_n = other.sk_in_n;
self.sk_out_n = other.sk_out_n;
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWESwitchingKeyPrepared<Vec<u8>, B>> for GGLWESwitchingKey<D> pub trait GLWESwitchingKeyPrepare<B: Backend> {
fn glwe_switching_prepare<R, O>(&self, res: &R, other: &O, scratch: &mut Scratch<B>)
where
R: GLWESwitchingKeyPreparedToMut<B> + GLWESwitchingKeyPreparedSetMetaData,
O: GLWESwitchingKeyToRef;
}
impl<B: Backend> GLWESwitchingKeyPrepare<B> for Module<B>
where where
Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>, Module<B>: GGLWEPrepare<B>,
{ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWESwitchingKeyPrepared<Vec<u8>, B> { fn glwe_switching_prepare<R, O>(&self, res: &R, other: &O, scratch: &mut Scratch<B>)
let mut atk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = GGLWESwitchingKeyPrepared::alloc(module, self); where
atk_prepared.prepare(module, self, scratch); R: GLWESwitchingKeyPreparedToMut<B> + GLWESwitchingKeyPreparedSetMetaData,
atk_prepared O: GLWESwitchingKeyToRef,
{
self.gglwe_prepare(&res.to_mut(), other, scratch);
res.set_sk_in_n(other.sk_in_n);
res.set_sk_out_n(other.sk_out_n);
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
pub fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)
where
O: GLWESwitchingKeyToRef,
Module<B>: GLWESwitchingKeyPrepare<B>,
{
module.glwe_switching_prepare(self, other, scratch);
}
}
pub trait GLWESwitchingKeyPrepareAlloc<B: Backend> {
fn glwe_switching_key_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>)
where
O: GLWESwitchingKeyToRef;
}
impl<B: Backend> GLWESwitchingKeyPrepareAlloc<B> for Module<B>
where
Module<B>: GLWESwitchingKeyPrepare<B>,
{
fn glwe_switching_key_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>)
where
O: GLWESwitchingKeyToRef,
{
let mut ct_prepared: GLWESwitchingKeyPrepared<Vec<u8>, B> = GLWESwitchingKeyPrepared::alloc(self, self);
self.glwe_switching_prepare(&mut ct_prepared, other, scratch);
ct_prepared
}
}
impl<D: DataRef> GLWESwitchingKey<D> {
pub fn prepare_alloc<B: Backend>(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GLWESwitchingKeyPrepared<Vec<u8>, B>
where
Module<B>: GLWESwitchingKeyPrepareAlloc<B>,
{
module.glwe_switching_key_prepare_alloc(self, scratch);
}
}
pub trait GLWESwitchingKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyPreparedToMut<B> for GLWESwitchingKeyPrepared<D, B> {
fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> {
GLWESwitchingKeyPrepared {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_mut(),
}
}
}
pub trait GLWESwitchingKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GLWESwitchingKeyPreparedToRef<B> for GLWESwitchingKeyPrepared<D, B> {
fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> {
GLWESwitchingKeyPrepared {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_ref(),
}
} }
} }

View File

@@ -1,19 +1,23 @@
use poulpy_hal::{ use poulpy_hal::{
api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, api::{VmpPMatAlloc, VmpPMatAllocBytes},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, compressed::TensorKeyCompressedToMut,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGLWETensorKeyPrepared<D: Data, B: Backend> { pub struct TensorKeyPrepared<D: Data, B: Backend> {
pub(crate) keys: Vec<GGLWESwitchingKeyPrepared<D, B>>, pub(crate) keys: Vec<GLWESwitchingKeyPrepared<D, B>>,
} }
impl<D: Data, B: Backend> LWEInfos for GGLWETensorKeyPrepared<D, B> { impl<D: Data, B: Backend> LWEInfos for TensorKeyPrepared<D, B> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.keys[0].n() self.keys[0].n()
} }
@@ -31,13 +35,13 @@ impl<D: Data, B: Backend> LWEInfos for GGLWETensorKeyPrepared<D, B> {
} }
} }
impl<D: Data, B: Backend> GLWEInfos for GGLWETensorKeyPrepared<D, B> { impl<D: Data, B: Backend> GLWEInfos for TensorKeyPrepared<D, B> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
} }
} }
impl<D: Data, B: Backend> GGLWEInfos for GGLWETensorKeyPrepared<D, B> { impl<D: Data, B: Backend> GGLWEInfos for TensorKeyPrepared<D, B> {
fn rank_in(&self) -> Rank { fn rank_in(&self) -> Rank {
self.rank_out() self.rank_out()
} }
@@ -55,7 +59,7 @@ impl<D: Data, B: Backend> GGLWEInfos for GGLWETensorKeyPrepared<D, B> {
} }
} }
impl<B: Backend> GGLWETensorKeyPrepared<Vec<u8>, B> { impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self
where where
A: GGLWEInfos, A: GGLWEInfos,
@@ -80,10 +84,10 @@ impl<B: Backend> GGLWETensorKeyPrepared<Vec<u8>, B> {
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
let mut keys: Vec<GGLWESwitchingKeyPrepared<Vec<u8>, B>> = Vec::new(); let mut keys: Vec<GLWESwitchingKeyPrepared<Vec<u8>, B>> = Vec::new();
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
(0..pairs).for_each(|_| { (0..pairs).for_each(|_| {
keys.push(GGLWESwitchingKeyPrepared::alloc_with( keys.push(GLWESwitchingKeyPrepared::alloc_with(
module, module,
base2k, base2k,
k, k,
@@ -109,7 +113,7 @@ impl<B: Backend> GGLWETensorKeyPrepared<Vec<u8>, B> {
let rank_out: usize = infos.rank_out().into(); let rank_out: usize = infos.rank_out().into();
let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1);
pairs pairs
* GGLWESwitchingKeyPrepared::alloc_bytes_with( * GLWESwitchingKeyPrepared::alloc_bytes_with(
module, module,
infos.base2k(), infos.base2k(),
infos.k(), infos.k(),
@@ -125,13 +129,13 @@ impl<B: Backend> GGLWETensorKeyPrepared<Vec<u8>, B> {
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
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 * GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank, dnum, dsize) pairs * GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank, dnum, dsize)
} }
} }
impl<D: DataMut, B: Backend> GGLWETensorKeyPrepared<D, B> { impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared<D, B> { pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared<D, B> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -140,9 +144,9 @@ impl<D: DataMut, B: Backend> GGLWETensorKeyPrepared<D, B> {
} }
} }
impl<D: DataRef, B: Backend> GGLWETensorKeyPrepared<D, B> { impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared<D, B> { pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared<D, B> {
if i > j { if i > j {
std::mem::swap(&mut i, &mut j); std::mem::swap(&mut i, &mut j);
}; };
@@ -151,40 +155,125 @@ impl<D: DataRef, B: Backend> GGLWETensorKeyPrepared<D, B> {
} }
} }
impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for GGLWETensorKeyPrepared<Vec<u8>, B> pub trait TensorKeyPrepareTmpBytes {
fn tensor_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize;
}
impl<B: Backend> TensorKeyPrepareTmpBytes for Module<B>
where where
GGLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>, Module<B>: GLWESwitchingKeyPrepareTmpBytes,
{ {
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize { fn tensor_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize {
GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) self.glwe_switching_key_prepare_tmp_bytes(infos)
} }
} }
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWETensorKey<DR>> for GGLWETensorKeyPrepared<D, B> impl<B: Backend> TensorKeyPrepared<Vec<u8>, B>
where where
Module<B>: VmpPrepare<B>, Module<B>: TensorKeyPrepareTmpBytes,
{ {
fn prepare(&mut self, module: &Module<B>, other: &GGLWETensorKey<DR>, scratch: &mut Scratch<B>) { fn prepare_tmp_bytes<A>(&self, module: &Module<B>, infos: &A) -> usize
#[cfg(debug_assertions)] where
A: GGLWEInfos,
{ {
assert_eq!(self.keys.len(), other.keys.len()); module.tensor_key_prepare_tmp_bytes(infos)
}
self.keys
.iter_mut()
.zip(other.keys.iter())
.for_each(|(a, b)| {
a.prepare(module, b, scratch);
});
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWETensorKeyPrepared<Vec<u8>, B>> for GGLWETensorKey<D> pub trait TensorKeyPrepare<B: Backend> {
fn tensor_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &Scratch<B>)
where
R: TensorKeyPreparedToMut<B>,
O: TensorKeyToRef;
}
impl<B: Backend> TensorKeyPrepare<B> for Module<B>
where where
Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>, Module<B>: GLWESwitchingKeyPrepare<B>,
{ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWETensorKeyPrepared<Vec<u8>, B> { fn tensor_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &Scratch<B>)
let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = GGLWETensorKeyPrepared::alloc(module, self); where
tsk_prepared.prepare(module, self, scratch); R: TensorKeyPreparedToMut<B>,
tsk_prepared O: TensorKeyToRef,
{
let res = res.to_mut();
let other = other.to_ref();
assert_eq!(self.keys.len(), other.keys.len());
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
self.glwe_switching_prepare(a, b, scratch);
}
}
}
impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B>
where
Module<B>: TensorKeyPrepare<B>,
{
fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)
where
O: TensorKeyToRef,
{
module.tensor_key_prepare(self, other, scratch);
}
}
pub trait TensorKeyPrepareAlloc<B: Backend> {
fn tensor_key_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>)
where
O: TensorKeyToRef;
}
impl<B: Backend> TensorKeyPrepareAlloc<B> for Module<B>
where
Module<B>: TensorKeyPrepare<B>,
{
fn tensor_key_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>)
where
O: TensorKeyToRef,
{
let mut ct_prepared: TensorKeyPrepared<Vec<u8>, B> = TensorKeyPrepared::alloc(self, other);
self.tensor_key_prepare(ct_prepared, other, scratch);
ct_prepared
}
}
impl<D: DataRef> TensorKey<D> {
pub fn prepare_alloc<B: Backend>(&self, module: &Module<B>, scratch: &Scratch<B>)
where
Module<B>: TensorKeyPrepareAlloc<B>,
{
module.tensor_key_prepare_alloc(self, scratch);
}
}
pub trait TensorKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> TensorKeyPreparedToMut<B> for TensorKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
{
fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> {
TensorKeyPrepared {
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
}
}
}
pub trait TensorKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> TensorKeyPreparedToRef<B> for TensorKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
{
fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> {
TensorKeyPrepared {
keys: self.keys.iter().map(|c| c.to_ref()).collect(),
}
} }
} }

View File

@@ -5,19 +5,18 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, BuildError, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, BuildError, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{Prepare, PrepareAlloc, PrepareScratchSpace},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GGSWCiphertextPrepared<D: Data, B: Backend> { pub struct GGSWPrepared<D: Data, B: Backend> {
pub(crate) data: VmpPMat<D, B>, pub(crate) data: VmpPMat<D, B>,
pub(crate) k: TorusPrecision, pub(crate) k: TorusPrecision,
pub(crate) base2k: Base2K, pub(crate) base2k: Base2K,
pub(crate) dsize: Dsize, pub(crate) dsize: Dsize,
} }
impl<D: Data, B: Backend> LWEInfos for GGSWCiphertextPrepared<D, B> { impl<D: Data, B: Backend> LWEInfos for GGSWPrepared<D, B> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
Degree(self.data.n() as u32) Degree(self.data.n() as u32)
} }
@@ -35,13 +34,13 @@ impl<D: Data, B: Backend> LWEInfos for GGSWCiphertextPrepared<D, B> {
} }
} }
impl<D: Data, B: Backend> GLWEInfos for GGSWCiphertextPrepared<D, B> { impl<D: Data, B: Backend> GLWEInfos for GGSWPrepared<D, B> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
Rank(self.data.cols_out() as u32 - 1) Rank(self.data.cols_out() as u32 - 1)
} }
} }
impl<D: Data, B: Backend> GGSWInfos for GGSWCiphertextPrepared<D, B> { impl<D: Data, B: Backend> GGSWInfos for GGSWPrepared<D, B> {
fn dsize(&self) -> Dsize { fn dsize(&self) -> Dsize {
self.dsize self.dsize
} }
@@ -58,7 +57,7 @@ pub struct GGSWCiphertextPreparedBuilder<D: Data, B: Backend> {
dsize: Option<Dsize>, dsize: Option<Dsize>,
} }
impl<D: Data, B: Backend> GGSWCiphertextPrepared<D, B> { impl<D: Data, B: Backend> GGSWPrepared<D, B> {
#[inline] #[inline]
pub fn builder() -> GGSWCiphertextPreparedBuilder<D, B> { pub fn builder() -> GGSWCiphertextPreparedBuilder<D, B> {
GGSWCiphertextPreparedBuilder { GGSWCiphertextPreparedBuilder {
@@ -129,7 +128,7 @@ impl<D: Data, B: Backend> GGSWCiphertextPreparedBuilder<D, B> {
self self
} }
pub fn build(self) -> Result<GGSWCiphertextPrepared<D, B>, BuildError> { pub fn build(self) -> Result<GGSWPrepared<D, B>, BuildError> {
let data: VmpPMat<D, B> = self.data.ok_or(BuildError::MissingData)?; let data: VmpPMat<D, B> = self.data.ok_or(BuildError::MissingData)?;
let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?;
let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?;
@@ -159,7 +158,7 @@ impl<D: Data, B: Backend> GGSWCiphertextPreparedBuilder<D, B> {
return Err(BuildError::ZeroLimbs); return Err(BuildError::ZeroLimbs);
} }
Ok(GGSWCiphertextPrepared { Ok(GGSWPrepared {
data, data,
base2k, base2k,
k, k,
@@ -168,7 +167,7 @@ impl<D: Data, B: Backend> GGSWCiphertextPreparedBuilder<D, B> {
} }
} }
impl<B: Backend> GGSWCiphertextPrepared<Vec<u8>, B> { impl<B: Backend> GGSWPrepared<Vec<u8>, B> {
pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self
where where
A: GGSWInfos, A: GGSWInfos,
@@ -252,18 +251,27 @@ impl<B: Backend> GGSWCiphertextPrepared<Vec<u8>, B> {
} }
} }
impl<D: DataRef, B: Backend> GGSWCiphertextPrepared<D, B> { impl<D: DataRef, B: Backend> GGSWPrepared<D, B> {
pub fn data(&self) -> &VmpPMat<D, B> { pub fn data(&self) -> &VmpPMat<D, B> {
&self.data &self.data
} }
} }
impl<B: Backend, A: GGSWInfos> PrepareScratchSpace<B, A> for GGSWCiphertextPrepared<Vec<u8>, B> pub trait GGSWPrepareTmpBytes {
fn ggsw_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGSWInfos;
}
impl<B: Backend> GGSWPrepareTmpBytes for Module<B>
where where
Module<B>: VmpPrepareTmpBytes, Module<B>: VmpPrepareTmpBytes,
{ {
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize { fn ggsw_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
module.vmp_prepare_tmp_bytes( where
A: GGSWInfos,
{
self.vmp_prepare_tmp_bytes(
infos.dnum().into(), infos.dnum().into(),
(infos.rank() + 1).into(), (infos.rank() + 1).into(),
(infos.rank() + 1).into(), (infos.rank() + 1).into(),
@@ -272,57 +280,110 @@ where
} }
} }
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGSWCiphertext<DR>> for GGSWCiphertextPrepared<D, B> impl<B: Backend> GGSWPrepared<Vec<u8>, B>
where
Module<B>: GGSWPrepareTmpBytes,
{
pub fn prepare_tmp_bytes<A>(&self, module: Module<B>, infos: &A) -> usize
where
A: GGSWInfos,
{
module.ggsw_prepare_tmp_bytes(self)
}
}
pub trait GGSWPrepare<B: Backend> {
fn ggsw_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GGSWPreparedToMut<B>,
O: GGSWToRef;
}
impl<B: Backend> GGSWPrepare<B> for Module<B>
where where
Module<B>: VmpPrepare<B>, Module<B>: VmpPrepare<B>,
{ {
fn prepare(&mut self, module: &Module<B>, other: &GGSWCiphertext<DR>, scratch: &mut Scratch<B>) { fn ggsw_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
module.vmp_prepare(&mut self.data, &other.data, scratch); where
self.k = other.k; R: GGSWPreparedToMut<B>,
self.base2k = other.base2k; O: GGSWToRef,
self.dsize = other.dsize; {
let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut();
let other: GGSW<&[u8]> = other.to_ref();
assert_eq!(res.k, other.k);
assert_eq!(res.base2k, other.base2k);
assert_eq!(res.dsize, other.dsize);
self.vmp_prepare(&mut res.data, &other.data, scratch);
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGSWCiphertextPrepared<Vec<u8>, B>> for GGSWCiphertext<D> impl<D: DataMut, B: Backend> GGSWPrepared<D, B>
where where
Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>, Module<B>: GGSWPrepare<B>,
{ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGSWCiphertextPrepared<Vec<u8>, B> { pub fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)
let mut ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = GGSWCiphertextPrepared::alloc(module, self); where
ggsw_prepared.prepare(module, self, scratch); O: GGSWToRef,
ggsw_prepared {
module.ggsw_prepare(self, other, scratch);
} }
} }
pub trait GGSWCiphertextPreparedToMut<B: Backend> { pub trait GGSWPrepareAlloc<B: Backend> {
fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B>; fn ggsw_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>)
where
O: GGSWToRef;
} }
impl<D: DataMut, B: Backend> GGSWCiphertextPreparedToMut<B> for GGSWCiphertextPrepared<D, B> { impl<B: Backend> GGSWPrepareAlloc<B> for Module<B>
fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B> { where
GGSWCiphertextPrepared::builder() Module<B>: GGSWPrepare<B>,
.base2k(self.base2k()) {
.dsize(self.dsize()) fn ggsw_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>)
.k(self.k()) where
.data(self.data.to_mut()) O: GGSWToRef,
.build() {
.unwrap() let mut ct_prepared: GGSWPrepared<Vec<u8>, B> = GGSWPrepared::alloc(self, other);
self.ggsw_prepare(&mut ct_prepared, other, scratch);
ct_prepared
}
}
impl<D: DataRef> GGSW<D> {
fn prepare_alloc<B: Backend>(&self, module: &Module<B>, scratch: &mut Scratch<B>)
where
Module<B>: GGSWPrepareAlloc<B>,
{
module.ggsw_prepare_alloc(self, scratch);
}
}
pub trait GGSWPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GGSWPreparedToMut<B> for GGSWPrepared<D, B> {
fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B> {
GGSWPrepared {
base2k: self.base2k,
k: self.k,
dsize: self.dsize,
data: self.data.to_mut(),
}
} }
} }
pub trait GGSWCiphertextPreparedToRef<B: Backend> { pub trait GGSWCiphertextPreparedToRef<B: Backend> {
fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B>; fn to_ref(&self) -> GGSWPrepared<&[u8], B>;
} }
impl<D: DataRef, B: Backend> GGSWCiphertextPreparedToRef<B> for GGSWCiphertextPrepared<D, B> { impl<D: DataRef, B: Backend> GGSWCiphertextPreparedToRef<B> for GGSWPrepared<D, B> {
fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B> { fn to_ref(&self) -> GGSWPrepared<&[u8], B> {
GGSWCiphertextPrepared::builder() GGSWPrepared {
.base2k(self.base2k()) base2k: self.base2k,
.dsize(self.dsize()) k: self.k,
.k(self.k()) dsize: self.dsize,
.data(self.data.to_ref()) data: self.data.to_mut(),
.build() }
.unwrap()
} }
} }

View File

@@ -6,10 +6,7 @@ use poulpy_hal::{
use crate::{ use crate::{
dist::Distribution, dist::Distribution,
layouts::{ layouts::{Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision},
Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision,
prepared::{Prepare, PrepareAlloc, PrepareScratchSpace},
},
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]

View File

@@ -5,10 +5,7 @@ use poulpy_hal::{
use crate::{ use crate::{
dist::Distribution, dist::Distribution,
layouts::{ layouts::{Base2K, Degree, GLWEInfos, GLWESecret, LWEInfos, Rank, TorusPrecision},
Base2K, Degree, GLWEInfos, GLWESecret, LWEInfos, Rank, TorusPrecision,
prepared::{Prepare, PrepareAlloc, PrepareScratchSpace},
},
}; };
pub struct GLWESecretPrepared<D: Data, B: Backend> { pub struct GLWESecretPrepared<D: Data, B: Backend> {
@@ -128,11 +125,11 @@ impl<D: DataRef, B: Backend> GLWESecretPreparedToRef<B> for GLWESecretPrepared<D
} }
pub trait GLWESecretPreparedToMut<B: Backend> { pub trait GLWESecretPreparedToMut<B: Backend> {
fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B>; fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B>;
} }
impl<D: DataMut, B: Backend> GLWESecretPreparedToMut<B> for GLWESecretPrepared<D, B> { impl<D: DataMut, B: Backend> GLWESecretPreparedToMut<B> for GLWESecretPrepared<D, B> {
fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B> { fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B> {
GLWESecretPrepared { GLWESecretPrepared {
dist: self.dist, dist: self.dist,
data: self.data.to_mut(), data: self.data.to_mut(),

View File

@@ -4,12 +4,12 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWEKey, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, prepared::GLWESwitchingKeyPrepared,
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct GLWEToLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>); pub struct GLWEToLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> LWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> { impl<D: Data, B: Backend> LWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
@@ -69,14 +69,14 @@ impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
1, 1,
"dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
); );
Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) Self(GLWESwitchingKeyPrepared::alloc(module, infos))
} }
pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
Self(GGLWESwitchingKeyPrepared::alloc_with( Self(GLWESwitchingKeyPrepared::alloc_with(
module, module,
base2k, base2k,
k, k,
@@ -102,27 +102,27 @@ impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
1, 1,
"dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
); );
GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) GLWESwitchingKeyPrepared::alloc_bytes(module, infos)
} }
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1))
} }
} }
impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>
where where
GGLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>, GLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>,
{ {
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize { fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize {
GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos)
} }
} }
impl<D: DataRef, B: Backend> PrepareAlloc<B, GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>> for GLWEToLWEKey<D> impl<D: DataRef, B: Backend> PrepareAlloc<B, GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>> for GLWEToLWESwitchingKey<D>
where where
Module<B>: VmpPrepare<B> + VmpPMatAlloc<B>, Module<B>: VmpPrepare<B> + VmpPMatAlloc<B>,
{ {
@@ -133,11 +133,11 @@ where
} }
} }
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, GLWEToLWEKey<DR>> for GLWEToLWESwitchingKeyPrepared<DM, B> impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, GLWEToLWESwitchingKey<DR>> for GLWEToLWESwitchingKeyPrepared<DM, B>
where where
Module<B>: VmpPrepare<B>, Module<B>: VmpPrepare<B>,
{ {
fn prepare(&mut self, module: &Module<B>, other: &GLWEToLWEKey<DR>, scratch: &mut Scratch<B>) { fn prepare(&mut self, module: &Module<B>, other: &GLWEToLWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.0.prepare(module, &other.0, scratch); self.0.prepare(module, &other.0, scratch);
} }
} }

View File

@@ -5,11 +5,11 @@ use poulpy_hal::{
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, prepared::GLWESwitchingKeyPrepared,
}; };
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct LWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>); pub struct LWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> LWEInfos for LWESwitchingKeyPrepared<D, B> { impl<D: Data, B: Backend> LWEInfos for LWESwitchingKeyPrepared<D, B> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
@@ -73,14 +73,14 @@ impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
1, 1,
"rank_out > 1 is not supported for LWESwitchingKey" "rank_out > 1 is not supported for LWESwitchingKey"
); );
Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) Self(GLWESwitchingKeyPrepared::alloc(module, infos))
} }
pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
Self(GGLWESwitchingKeyPrepared::alloc_with( Self(GLWESwitchingKeyPrepared::alloc_with(
module, module,
base2k, base2k,
k, k,
@@ -111,23 +111,23 @@ impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
1, 1,
"rank_out > 1 is not supported for LWESwitchingKey" "rank_out > 1 is not supported for LWESwitchingKey"
); );
GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) GLWESwitchingKeyPrepared::alloc_bytes(module, infos)
} }
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
} }
} }
impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for LWESwitchingKeyPrepared<Vec<u8>, B> impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for LWESwitchingKeyPrepared<Vec<u8>, B>
where where
GGLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>, GLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>,
{ {
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize { fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize {
GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos)
} }
} }

View File

@@ -5,12 +5,12 @@ use poulpy_hal::{
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, prepared::GLWESwitchingKeyPrepared,
}; };
/// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. /// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext].
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct LWEToGLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>); pub struct LWEToGLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> LWEInfos for LWEToGLWESwitchingKeyPrepared<D, B> { impl<D: Data, B: Backend> LWEInfos for LWEToGLWESwitchingKeyPrepared<D, B> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
@@ -70,14 +70,14 @@ impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
1, 1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey" "dsize > 1 is not supported for LWEToGLWESwitchingKey"
); );
Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) Self(GLWESwitchingKeyPrepared::alloc(module, infos))
} }
pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self
where where
Module<B>: VmpPMatAlloc<B>, Module<B>: VmpPMatAlloc<B>,
{ {
Self(GGLWESwitchingKeyPrepared::alloc_with( Self(GLWESwitchingKeyPrepared::alloc_with(
module, module,
base2k, base2k,
k, k,
@@ -103,23 +103,23 @@ impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
1, 1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey" "dsize > 1 is not supported for LWEToGLWESwitchingKey"
); );
GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) GLWESwitchingKeyPrepared::alloc_bytes(module, infos)
} }
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize
where where
Module<B>: VmpPMatAllocBytes, Module<B>: VmpPMatAllocBytes,
{ {
GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1))
} }
} }
impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for LWEToGLWESwitchingKeyPrepared<Vec<u8>, B>
where where
GGLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>, GLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>,
{ {
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize { fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize {
GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos)
} }
} }

View File

@@ -19,16 +19,3 @@ pub use glwe_sk::*;
pub use glwe_to_lwe_ksk::*; pub use glwe_to_lwe_ksk::*;
pub use lwe_ksk::*; pub use lwe_ksk::*;
pub use lwe_to_glwe_ksk::*; pub use lwe_to_glwe_ksk::*;
use poulpy_hal::layouts::{Backend, Module, Scratch};
pub trait PrepareScratchSpace<B: Backend, T> {
fn prepare_scratch_space(module: &Module<B>, infos: &T) -> usize;
}
pub trait PrepareAlloc<B: Backend, T> {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> T;
}
pub trait Prepare<B: Backend, T> {
fn prepare(&mut self, module: &Module<B>, other: &T, scratch: &mut Scratch<B>);
}

View File

@@ -8,9 +8,9 @@ use poulpy_hal::{
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
}; };
use crate::layouts::{GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; use crate::layouts::{GGLWE, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
impl<D: DataRef> GGLWECiphertext<D> { impl<D: DataRef> GGLWE<D> {
pub fn assert_noise<B, DataSk, DataWant>( pub fn assert_noise<B, DataSk, DataWant>(
&self, &self,
module: &Module<B>, module: &Module<B>,

View File

@@ -9,11 +9,9 @@ use poulpy_hal::{
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
}; };
use crate::layouts::{ use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared,
};
impl<D: DataRef> GGSWCiphertext<D> { impl<D: DataRef> GGSW<D> {
pub fn assert_noise<B, DataSk, DataScalar, F>( pub fn assert_noise<B, DataSk, DataScalar, F>(
&self, &self,
module: &Module<B>, module: &Module<B>,
@@ -87,7 +85,7 @@ impl<D: DataRef> GGSWCiphertext<D> {
} }
} }
impl<D: DataRef> GGSWCiphertext<D> { impl<D: DataRef> GGSW<D> {
pub fn print_noise<B, DataSk, DataScalar>( pub fn print_noise<B, DataSk, DataScalar>(
&self, &self,
module: &Module<B>, module: &Module<B>,

View File

@@ -6,11 +6,11 @@ use poulpy_hal::{
use crate::{ use crate::{
dist::Distribution, dist::Distribution,
layouts::{ layouts::{
Degree, GGLWEAutomorphismKey, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GGSWInfos, AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey,
GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, Rank, GLWESecret, GLWESwitchingKey, Rank, TensorKey,
prepared::{ prepared::{
GGLWEAutomorphismKeyPrepared, GGLWECiphertextPrepared, GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared,
GGSWCiphertextPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared,
}, },
}, },
}; };
@@ -34,31 +34,31 @@ pub trait TakeGLWEPt<B: Backend> {
} }
pub trait TakeGGLWE { pub trait TakeGGLWE {
fn take_gglwe<A>(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self) fn take_gglwe<A>(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self)
where where
A: GGLWEInfos; A: GGLWEInfos;
} }
pub trait TakeGGLWEPrepared<B: Backend> { pub trait TakeGGLWEPrepared<B: Backend> {
fn take_gglwe_prepared<A>(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) fn take_gglwe_prepared<A>(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self)
where where
A: GGLWEInfos; A: GGLWEInfos;
} }
pub trait TakeGGSW { pub trait TakeGGSW {
fn take_ggsw<A>(&mut self, infos: &A) -> (GGSWCiphertext<&mut [u8]>, &mut Self) fn take_ggsw<A>(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self)
where where
A: GGSWInfos; A: GGSWInfos;
} }
pub trait TakeGGSWPrepared<B: Backend> { pub trait TakeGGSWPrepared<B: Backend> {
fn take_ggsw_prepared<A>(&mut self, infos: &A) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) fn take_ggsw_prepared<A>(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self)
where where
A: GGSWInfos; A: GGSWInfos;
} }
pub trait TakeGGSWPreparedSlice<B: Backend> { pub trait TakeGGSWPreparedSlice<B: Backend> {
fn take_ggsw_prepared_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GGSWCiphertextPrepared<&mut [u8], B>>, &mut Self) fn take_ggsw_prepared_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GGSWPrepared<&mut [u8], B>>, &mut Self)
where where
A: GGSWInfos; A: GGSWInfos;
} }
@@ -84,37 +84,37 @@ pub trait TakeGLWEPkPrepared<B: Backend> {
} }
pub trait TakeGLWESwitchingKey { pub trait TakeGLWESwitchingKey {
fn take_glwe_switching_key<A>(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self) fn take_glwe_switching_key<A>(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self)
where where
A: GGLWEInfos; A: GGLWEInfos;
} }
pub trait TakeGGLWESwitchingKeyPrepared<B: Backend> { pub trait TakeGGLWESwitchingKeyPrepared<B: Backend> {
fn take_gglwe_switching_key_prepared<A>(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) fn take_gglwe_switching_key_prepared<A>(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self)
where where
A: GGLWEInfos; A: GGLWEInfos;
} }
pub trait TakeTensorKey { pub trait TakeTensorKey {
fn take_tensor_key<A>(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self) fn take_tensor_key<A>(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self)
where where
A: GGLWEInfos; A: GGLWEInfos;
} }
pub trait TakeGGLWETensorKeyPrepared<B: Backend> { pub trait TakeGGLWETensorKeyPrepared<B: Backend> {
fn take_gglwe_tensor_key_prepared<A>(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) fn take_gglwe_tensor_key_prepared<A>(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self)
where where
A: GGLWEInfos; A: GGLWEInfos;
} }
pub trait TakeGGLWEAutomorphismKey { pub trait TakeGGLWEAutomorphismKey {
fn take_gglwe_automorphism_key<A>(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self) fn take_gglwe_automorphism_key<A>(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self)
where where
A: GGLWEInfos; A: GGLWEInfos;
} }
pub trait TakeGGLWEAutomorphismKeyPrepared<B: Backend> { pub trait TakeGGLWEAutomorphismKeyPrepared<B: Backend> {
fn take_gglwe_automorphism_key_prepared<A>(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) fn take_gglwe_automorphism_key_prepared<A>(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self)
where where
A: GGLWEInfos; A: GGLWEInfos;
} }
@@ -184,7 +184,7 @@ impl<B: Backend> TakeGGLWE for Scratch<B>
where where
Scratch<B>: TakeMatZnx, Scratch<B>: TakeMatZnx,
{ {
fn take_gglwe<A>(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self) fn take_gglwe<A>(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self)
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
@@ -196,7 +196,7 @@ where
infos.size(), infos.size(),
); );
( (
GGLWECiphertext::builder() GGLWE::builder()
.base2k(infos.base2k()) .base2k(infos.base2k())
.k(infos.k()) .k(infos.k())
.dsize(infos.dsize()) .dsize(infos.dsize())
@@ -212,7 +212,7 @@ impl<B: Backend> TakeGGLWEPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeVmpPMat<B>, Scratch<B>: TakeVmpPMat<B>,
{ {
fn take_gglwe_prepared<A>(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) fn take_gglwe_prepared<A>(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self)
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
@@ -224,7 +224,7 @@ where
infos.size(), infos.size(),
); );
( (
GGLWECiphertextPrepared::builder() GGLWEPrepared::builder()
.base2k(infos.base2k()) .base2k(infos.base2k())
.dsize(infos.dsize()) .dsize(infos.dsize())
.k(infos.k()) .k(infos.k())
@@ -240,7 +240,7 @@ impl<B: Backend> TakeGGSW for Scratch<B>
where where
Scratch<B>: TakeMatZnx, Scratch<B>: TakeMatZnx,
{ {
fn take_ggsw<A>(&mut self, infos: &A) -> (GGSWCiphertext<&mut [u8]>, &mut Self) fn take_ggsw<A>(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self)
where where
A: GGSWInfos, A: GGSWInfos,
{ {
@@ -252,7 +252,7 @@ where
infos.size(), infos.size(),
); );
( (
GGSWCiphertext::builder() GGSW::builder()
.base2k(infos.base2k()) .base2k(infos.base2k())
.dsize(infos.dsize()) .dsize(infos.dsize())
.k(infos.k()) .k(infos.k())
@@ -268,7 +268,7 @@ impl<B: Backend> TakeGGSWPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeVmpPMat<B>, Scratch<B>: TakeVmpPMat<B>,
{ {
fn take_ggsw_prepared<A>(&mut self, infos: &A) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) fn take_ggsw_prepared<A>(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self)
where where
A: GGSWInfos, A: GGSWInfos,
{ {
@@ -280,7 +280,7 @@ where
infos.size(), infos.size(),
); );
( (
GGSWCiphertextPrepared::builder() GGSWPrepared::builder()
.base2k(infos.base2k()) .base2k(infos.base2k())
.dsize(infos.dsize()) .dsize(infos.dsize())
.k(infos.k()) .k(infos.k())
@@ -296,12 +296,12 @@ impl<B: Backend> TakeGGSWPreparedSlice<B> for Scratch<B>
where where
Scratch<B>: TakeGGSWPrepared<B>, Scratch<B>: TakeGGSWPrepared<B>,
{ {
fn take_ggsw_prepared_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GGSWCiphertextPrepared<&mut [u8], B>>, &mut Self) fn take_ggsw_prepared_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GGSWPrepared<&mut [u8], B>>, &mut Self)
where where
A: GGSWInfos, A: GGSWInfos,
{ {
let mut scratch: &mut Scratch<B> = self; let mut scratch: &mut Scratch<B> = self;
let mut cts: Vec<GGSWCiphertextPrepared<&mut [u8], B>> = Vec::with_capacity(size); let mut cts: Vec<GGSWPrepared<&mut [u8], B>> = Vec::with_capacity(size);
for _ in 0..size { for _ in 0..size {
let (ct, new_scratch) = scratch.take_ggsw_prepared(infos); let (ct, new_scratch) = scratch.take_ggsw_prepared(infos);
scratch = new_scratch; scratch = new_scratch;
@@ -390,13 +390,13 @@ impl<B: Backend> TakeGLWESwitchingKey for Scratch<B>
where where
Scratch<B>: TakeMatZnx, Scratch<B>: TakeMatZnx,
{ {
fn take_glwe_switching_key<A>(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self) fn take_glwe_switching_key<A>(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self)
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
let (data, scratch) = self.take_gglwe(infos); let (data, scratch) = self.take_gglwe(infos);
( (
GGLWESwitchingKey { GLWESwitchingKey {
key: data, key: data,
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
@@ -410,13 +410,13 @@ impl<B: Backend> TakeGGLWESwitchingKeyPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeGGLWEPrepared<B>, Scratch<B>: TakeGGLWEPrepared<B>,
{ {
fn take_gglwe_switching_key_prepared<A>(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) fn take_gglwe_switching_key_prepared<A>(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self)
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
let (data, scratch) = self.take_gglwe_prepared(infos); let (data, scratch) = self.take_gglwe_prepared(infos);
( (
GGLWESwitchingKeyPrepared { GLWESwitchingKeyPrepared {
key: data, key: data,
sk_in_n: 0, sk_in_n: 0,
sk_out_n: 0, sk_out_n: 0,
@@ -430,12 +430,12 @@ impl<B: Backend> TakeGGLWEAutomorphismKey for Scratch<B>
where where
Scratch<B>: TakeMatZnx, Scratch<B>: TakeMatZnx,
{ {
fn take_gglwe_automorphism_key<A>(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self) fn take_gglwe_automorphism_key<A>(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self)
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
let (data, scratch) = self.take_glwe_switching_key(infos); let (data, scratch) = self.take_glwe_switching_key(infos);
(GGLWEAutomorphismKey { key: data, p: 0 }, scratch) (AutomorphismKey { key: data, p: 0 }, scratch)
} }
} }
@@ -443,12 +443,12 @@ impl<B: Backend> TakeGGLWEAutomorphismKeyPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeGGLWESwitchingKeyPrepared<B>, Scratch<B>: TakeGGLWESwitchingKeyPrepared<B>,
{ {
fn take_gglwe_automorphism_key_prepared<A>(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) fn take_gglwe_automorphism_key_prepared<A>(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self)
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
let (data, scratch) = self.take_gglwe_switching_key_prepared(infos); let (data, scratch) = self.take_gglwe_switching_key_prepared(infos);
(GGLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) (AutomorphismKeyPrepared { key: data, p: 0 }, scratch)
} }
} }
@@ -456,7 +456,7 @@ impl<B: Backend> TakeTensorKey for Scratch<B>
where where
Scratch<B>: TakeMatZnx, Scratch<B>: TakeMatZnx,
{ {
fn take_tensor_key<A>(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self) fn take_tensor_key<A>(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self)
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
@@ -465,7 +465,7 @@ 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"
); );
let mut keys: Vec<GGLWESwitchingKey<&mut [u8]>> = Vec::new(); let mut keys: Vec<GLWESwitchingKey<&mut [u8]>> = Vec::new();
let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize;
let mut scratch: &mut Scratch<B> = self; let mut scratch: &mut Scratch<B> = self;
@@ -483,7 +483,7 @@ where
scratch = s; scratch = s;
keys.push(gglwe); keys.push(gglwe);
} }
(GGLWETensorKey { keys }, scratch) (TensorKey { keys }, scratch)
} }
} }
@@ -491,7 +491,7 @@ impl<B: Backend> TakeGGLWETensorKeyPrepared<B> for Scratch<B>
where where
Scratch<B>: TakeVmpPMat<B>, Scratch<B>: TakeVmpPMat<B>,
{ {
fn take_gglwe_tensor_key_prepared<A>(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) fn take_gglwe_tensor_key_prepared<A>(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self)
where where
A: GGLWEInfos, A: GGLWEInfos,
{ {
@@ -501,7 +501,7 @@ where
"rank_in != rank_out is not supported for GGLWETensorKeyPrepared" "rank_in != rank_out is not supported for GGLWETensorKeyPrepared"
); );
let mut keys: Vec<GGLWESwitchingKeyPrepared<&mut [u8], B>> = Vec::new(); let mut keys: Vec<GLWESwitchingKeyPrepared<&mut [u8], B>> = Vec::new();
let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize;
let mut scratch: &mut Scratch<B> = self; let mut scratch: &mut Scratch<B> = self;
@@ -519,6 +519,6 @@ where
scratch = s; scratch = s;
keys.push(gglwe); keys.push(gglwe);
} }
(GGLWETensorKeyPrepared { keys }, scratch) (TensorKeyPrepared { keys }, scratch)
} }
} }

View File

@@ -1,12 +1,12 @@
use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use poulpy_hal::test_suite::serialization::test_reader_writer_interface;
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWECiphertext, GLWESwitchingKey, GLWEToLWESwitchingKey,
GLWECiphertext, GLWEToLWEKey, LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TorusPrecision, LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision,
compressed::{ compressed::{
GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGLWEKeyCompressed, GGLWETensorKeyCompressed, AutomorphismKeyCompressed, GGLWECiphertextCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed,
GGSWCiphertextCompressed, GLWECiphertextCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, GLWESwitchingKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, LWESwitchingKeyCompressed,
LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, TensorKeyCompressed,
}, },
}; };
@@ -44,7 +44,7 @@ fn lwe_compressed_serialization() {
#[test] #[test]
fn test_gglwe_serialization() { fn test_gglwe_serialization() {
let original: GGLWECiphertext<Vec<u8>> = GGLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); let original: GGLWE<Vec<u8>> = GGLWE::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
@@ -57,44 +57,45 @@ fn test_gglwe_compressed_serialization() {
#[test] #[test]
fn test_glwe_switching_key_serialization() { fn test_glwe_switching_key_serialization() {
let original: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); let original: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_with(N_GLWE, 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 original: GGLWEKeyCompressed<Vec<u8>> = GGLWEKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); let original: GLWESwitchingKeyCompressed<Vec<u8>> =
GLWESwitchingKeyCompressed::alloc_with(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 original: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); let original: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_with(N_GLWE, 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 original: GGLWEAutomorphismKeyCompressed<Vec<u8>> = let original: AutomorphismKeyCompressed<Vec<u8>> =
GGLWEAutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); AutomorphismKeyCompressed::alloc_with(N_GLWE, 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 original: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); let original: TensorKey<Vec<u8>> = TensorKey::alloc_with(N_GLWE, 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 original: GGLWETensorKeyCompressed<Vec<u8>> = GGLWETensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); let original: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc_with(N_GLWE, 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 original: GLWEToLWEKey<Vec<u8>> = GLWEToLWEKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); let original: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }
@@ -132,7 +133,7 @@ fn lwe_switching_key_compressed_serialization() {
#[test] #[test]
fn ggsw_serialization() { fn ggsw_serialization() {
let original: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); let original: GGSW<Vec<u8>> = GGSW::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original); test_reader_writer_interface(original);
} }

View File

@@ -18,8 +18,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret,
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
}; };
@@ -84,7 +84,7 @@ where
let dnum_out: usize = k_out / (base2k * di); let dnum_out: usize = k_out / (base2k * di);
let dnum_apply: usize = k_in.div_ceil(base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di);
let auto_key_in_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let auto_key_in_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_in.into(), k: k_in.into(),
@@ -93,7 +93,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let auto_key_out_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let auto_key_out_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_out.into(), k: k_out.into(),
@@ -102,7 +102,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let auto_key_apply_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let auto_key_apply_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_apply.into(), k: k_apply.into(),
@@ -111,18 +111,18 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut auto_key_in: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&auto_key_in_infos); let mut auto_key_in: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_in_infos);
let mut auto_key_out: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&auto_key_out_infos); let mut auto_key_out: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_out_infos);
let mut auto_key_apply: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&auto_key_apply_infos); let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_apply_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<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_in_infos) AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_in_infos)
| GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply_infos) | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply_infos)
| GGLWEAutomorphismKey::automorphism_scratch_space( | AutomorphismKey::automorphism_scratch_space(
module, module,
&auto_key_out_infos, &auto_key_out_infos,
&auto_key_in_infos, &auto_key_in_infos,
@@ -153,8 +153,8 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_apply_infos); AutomorphismKeyPrepared::alloc(module, &auto_key_apply_infos);
auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());
@@ -293,7 +293,7 @@ where
let dnum_in: usize = k_in / (base2k * di); let dnum_in: usize = k_in / (base2k * di);
let dnum_apply: usize = k_in.div_ceil(base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di);
let auto_key_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let auto_key_layout: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_in.into(), k: k_in.into(),
@@ -302,7 +302,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let auto_key_apply_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let auto_key_apply_layout: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_apply.into(), k: k_apply.into(),
@@ -311,17 +311,17 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut auto_key: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&auto_key_layout); let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_layout);
let mut auto_key_apply: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&auto_key_apply_layout); let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_apply_layout);
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<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key)
| GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply) | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply)
| GGLWEAutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), | AutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply),
); );
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key);
@@ -347,8 +347,8 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_apply_layout); AutomorphismKeyPrepared::alloc(module, &auto_key_apply_layout);
auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());

View File

@@ -19,8 +19,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWETensorKey, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, AutomorphismKey, GGSW, GGSWCiphertextLayout, GLWESecret, TensorKey, TensorKeyLayout,
prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc, TensorKeyPrepared},
}, },
noise::noise_ggsw_keyswitch, noise::noise_ggsw_keyswitch,
}; };
@@ -110,7 +110,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let tensor_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { let tensor_key_layout: TensorKeyLayout = TensorKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_tsk.into(), k: k_tsk.into(),
@@ -119,7 +119,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let auto_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { let auto_key_layout: TensorKeyLayout = TensorKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -128,10 +128,10 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut ct_in: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_in_layout); let mut ct_in: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_in_layout);
let mut ct_out: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_out_layout); let mut ct_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_layout);
let mut tensor_key: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(&tensor_key_layout); let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc(&tensor_key_layout);
let mut auto_key: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&auto_key_layout); let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_layout);
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -139,10 +139,10 @@ where
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(module, &ct_in) GGSW::encrypt_sk_scratch_space(module, &ct_in)
| GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key)
| GGLWETensorKey::encrypt_sk_scratch_space(module, &tensor_key) | TensorKey::encrypt_sk_scratch_space(module, &tensor_key)
| GGSWCiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &auto_key, &tensor_key), | GGSW::automorphism_scratch_space(module, &ct_out, &ct_in, &auto_key, &tensor_key),
); );
let var_xs: f64 = 0.5; let var_xs: f64 = 0.5;
@@ -178,11 +178,11 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_layout); AutomorphismKeyPrepared::alloc(module, &auto_key_layout);
auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = GGLWETensorKeyPrepared::alloc(module, &tensor_key_layout); let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout);
tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
ct_out.automorphism( ct_out.automorphism(
@@ -291,7 +291,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let tensor_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { let tensor_key_layout: TensorKeyLayout = TensorKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_tsk.into(), k: k_tsk.into(),
@@ -300,7 +300,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let auto_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { let auto_key_layout: TensorKeyLayout = TensorKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -309,9 +309,9 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut ct: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_out_layout); let mut ct: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_layout);
let mut tensor_key: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(&tensor_key_layout); let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc(&tensor_key_layout);
let mut auto_key: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&auto_key_layout); let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_layout);
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -319,10 +319,10 @@ where
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(module, &ct) GGSW::encrypt_sk_scratch_space(module, &ct)
| GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key)
| GGLWETensorKey::encrypt_sk_scratch_space(module, &tensor_key) | TensorKey::encrypt_sk_scratch_space(module, &tensor_key)
| GGSWCiphertext::automorphism_inplace_scratch_space(module, &ct, &auto_key, &tensor_key), | GGSW::automorphism_inplace_scratch_space(module, &ct, &auto_key, &tensor_key),
); );
let var_xs: f64 = 0.5; let var_xs: f64 = 0.5;
@@ -358,11 +358,11 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_layout); AutomorphismKeyPrepared::alloc(module, &auto_key_layout);
auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = GGLWETensorKeyPrepared::alloc(module, &tensor_key_layout); let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout);
tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow());

View File

@@ -18,8 +18,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret,
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
}; };
@@ -91,7 +91,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let autokey_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_out.into(), k: k_out.into(),
@@ -100,7 +100,7 @@ where
dsize: di.into(), dsize: di.into(),
}; };
let mut autokey: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&autokey_infos); let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&autokey_infos);
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_in_infos); let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_in_infos);
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_out_infos); let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ct_out_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ct_out_infos);
@@ -112,7 +112,7 @@ where
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);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &autokey) AutomorphismKey::encrypt_sk_scratch_space(module, &autokey)
| GLWECiphertext::decrypt_scratch_space(module, &ct_out) | GLWECiphertext::decrypt_scratch_space(module, &ct_out)
| GLWECiphertext::encrypt_sk_scratch_space(module, &ct_in) | GLWECiphertext::encrypt_sk_scratch_space(module, &ct_in)
| GLWECiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey), | GLWECiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey),
@@ -140,8 +140,8 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, &autokey_infos); AutomorphismKeyPrepared::alloc(module, &autokey_infos);
autokey_prepared.prepare(module, &autokey, scratch.borrow()); autokey_prepared.prepare(module, &autokey, scratch.borrow());
ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow()); ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow());
@@ -226,7 +226,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let autokey_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -235,7 +235,7 @@ where
dsize: di.into(), dsize: di.into(),
}; };
let mut autokey: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&autokey_infos); let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&autokey_infos);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_out_infos); let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ct_out_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ct_out_infos);
@@ -246,7 +246,7 @@ where
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);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &autokey) AutomorphismKey::encrypt_sk_scratch_space(module, &autokey)
| GLWECiphertext::decrypt_scratch_space(module, &ct) | GLWECiphertext::decrypt_scratch_space(module, &ct)
| GLWECiphertext::encrypt_sk_scratch_space(module, &ct) | GLWECiphertext::encrypt_sk_scratch_space(module, &ct)
| GLWECiphertext::automorphism_inplace_scratch_space(module, &ct, &autokey), | GLWECiphertext::automorphism_inplace_scratch_space(module, &ct, &autokey),
@@ -274,8 +274,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = AutomorphismKeyPrepared::alloc(module, &autokey);
GGLWEAutomorphismKeyPrepared::alloc(module, &autokey);
autokey_prepared.prepare(module, &autokey, scratch.borrow()); autokey_prepared.prepare(module, &autokey, scratch.borrow());
ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow()); ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow());

View File

@@ -16,9 +16,9 @@ use poulpy_hal::{
}; };
use crate::layouts::{ use crate::layouts::{
Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout,
LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, GLWEToLWESwitchingKey, LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey,
TorusPrecision, LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision,
prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc},
}; };
@@ -214,7 +214,7 @@ where
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GLWEToLWEKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos) GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos)
| LWECiphertext::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) | LWECiphertext::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos)
| GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos),
); );
@@ -241,7 +241,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut ksk: GLWEToLWEKey<Vec<u8>> = GLWEToLWEKey::alloc(&glwe_to_lwe_infos); let mut ksk: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(&glwe_to_lwe_infos);
ksk.encrypt_sk( ksk.encrypt_sk(
module, module,

View File

@@ -18,8 +18,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret, AutomorphismKey, AutomorphismKeyLayout, GLWEInfos, GLWESecret,
compressed::{Decompress, GGLWEAutomorphismKeyCompressed}, compressed::{AutomorphismKeyCompressed, Decompress},
prepared::{GLWESecretPrepared, PrepareAlloc}, prepared::{GLWESecretPrepared, PrepareAlloc},
}, },
}; };
@@ -75,7 +75,7 @@ where
let n: usize = module.n(); let n: usize = module.n();
let dnum: usize = (k_ksk - di * base2k) / (di * base2k); let dnum: usize = (k_ksk - di * base2k) / (di * base2k);
let atk_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -84,13 +84,13 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut atk: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&atk_infos); let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&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<B> = ScratchOwned::alloc(GGLWEAutomorphismKey::encrypt_sk_scratch_space( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space(
module, &atk_infos, module, &atk_infos,
)); ));
@@ -178,7 +178,7 @@ where
let n: usize = module.n(); let n: usize = module.n();
let dnum: usize = (k_ksk - di * base2k) / (di * base2k); let dnum: usize = (k_ksk - di * base2k) / (di * base2k);
let atk_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -187,12 +187,12 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut atk_compressed: GGLWEAutomorphismKeyCompressed<Vec<u8>> = GGLWEAutomorphismKeyCompressed::alloc(&atk_infos); let mut atk_compressed: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc(&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<B> = ScratchOwned::alloc(GGLWEAutomorphismKey::encrypt_sk_scratch_space( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space(
module, &atk_infos, module, &atk_infos,
)); ));
@@ -217,7 +217,7 @@ where
}); });
let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow());
let mut atk: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&atk_infos); let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&atk_infos);
atk.decompress(module, &atk_compressed); atk.decompress(module, &atk_compressed);
atk.key atk.key

View File

@@ -17,8 +17,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWECiphertextLayout, GGLWESwitchingKey, GLWESecret, GGLWECiphertextLayout, GLWESecret, GLWESwitchingKey,
compressed::{Decompress, GGLWEKeyCompressed}, compressed::{Decompress, GLWESwitchingKeyCompressed},
prepared::{GLWESecretPrepared, PrepareAlloc}, prepared::{GLWESecretPrepared, PrepareAlloc},
}, },
}; };
@@ -81,13 +81,13 @@ where
rank_out: rank_out.into(), rank_out: rank_out.into(),
}; };
let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_infos); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&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<B> = ScratchOwned::alloc(GGLWESwitchingKey::encrypt_sk_scratch_space( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_scratch_space(
module, module,
&gglwe_infos, &gglwe_infos,
)); ));
@@ -173,12 +173,12 @@ where
rank_out: rank_out.into(), rank_out: rank_out.into(),
}; };
let mut ksk_compressed: GGLWEKeyCompressed<Vec<u8>> = GGLWEKeyCompressed::alloc(&gglwe_infos); let mut ksk_compressed: GLWESwitchingKeyCompressed<Vec<u8>> = GLWESwitchingKeyCompressed::alloc(&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 scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWEKeyCompressed::encrypt_sk_scratch_space( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(
module, module,
&gglwe_infos, &gglwe_infos,
)); ));
@@ -201,7 +201,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_infos); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_infos);
ksk.decompress(module, &ksk_compressed); ksk.decompress(module, &ksk_compressed);
ksk.key ksk.key

View File

@@ -17,53 +17,16 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, GGSW, GGSWCiphertextLayout, GLWESecret,
compressed::{Decompress, GGSWCiphertextCompressed}, compressed::{Decompress, GGSWCiphertextCompressed},
prepared::{GLWESecretPrepared, PrepareAlloc}, prepared::{GLWESecretPrepared, PrepareAlloc},
}, },
}; };
pub fn test_ggsw_encrypt_sk<B>(module: &Module<B>) pub fn test_ggsw_encrypt_sk<B: Backend>(module: &Module<B>)
where where
Module<B>: VecZnxDftAllocBytes ScratchOwned<B>: ScratchOwnedAlloc<B>,
+ VecZnxBigNormalize<B> Module<B>: SvpPrepare<B>,
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolAllocBytes
+ SvpPPolAlloc<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxAddScalarInplace
+ VecZnxBigAllocBytes
+ VecZnxBigAddInplace<B>
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VecZnxBigAlloc<B>
+ VecZnxDftAlloc<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ TakeSvpPPolImpl<B>,
{ {
let base2k: usize = 12; let base2k: usize = 12;
let k: usize = 54; let k: usize = 54;
@@ -82,7 +45,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut ct: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos); let mut ct: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
@@ -92,10 +55,7 @@ where
pt_scalar.fill_ternary_hw(0, n, &mut source_xs); pt_scalar.fill_ternary_hw(0, n, &mut source_xs);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSWCiphertext::encrypt_sk_scratch_space( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSW::encrypt_sk_scratch_space(module, &ggsw_infos));
module,
&ggsw_infos,
));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ggsw_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ggsw_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
@@ -206,7 +166,7 @@ where
let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5;
let mut ct: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos); let mut ct: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
ct.decompress(module, &ct_compressed); ct.decompress(module, &ct_compressed);
ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f);

View File

@@ -17,8 +17,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
Dsize, GGLWETensorKey, GGLWETensorKeyLayout, GLWEPlaintext, GLWESecret, Dsize, GLWEPlaintext, GLWESecret, TensorKey, TensorKeyLayout,
compressed::{Decompress, GGLWETensorKeyCompressed}, compressed::{Decompress, TensorKeyCompressed},
prepared::{GLWESecretPrepared, PrepareAlloc}, prepared::{GLWESecretPrepared, PrepareAlloc},
}, },
}; };
@@ -71,7 +71,7 @@ where
let n: usize = module.n(); let n: usize = module.n();
let dnum: usize = k / base2k; let dnum: usize = k / base2k;
let tensor_key_infos = GGLWETensorKeyLayout { let tensor_key_infos = TensorKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k.into(), k: k.into(),
@@ -80,13 +80,13 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut tensor_key: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(&tensor_key_infos); let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc(&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<B> = ScratchOwned::alloc(GGLWETensorKey::encrypt_sk_scratch_space( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(TensorKey::encrypt_sk_scratch_space(
module, module,
&tensor_key_infos, &tensor_key_infos,
)); ));
@@ -190,7 +190,7 @@ where
let n: usize = module.n(); let n: usize = module.n();
let dnum: usize = k / base2k; let dnum: usize = k / base2k;
let tensor_key_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { let tensor_key_infos: TensorKeyLayout = TensorKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k.into(), k: k.into(),
@@ -199,12 +199,12 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut tensor_key_compressed: GGLWETensorKeyCompressed<Vec<u8>> = GGLWETensorKeyCompressed::alloc(&tensor_key_infos); let mut tensor_key_compressed: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc(&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 scratch: ScratchOwned<B> = ScratchOwned::alloc(GGLWETensorKeyCompressed::encrypt_sk_scratch_space( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_scratch_space(
module, module,
&tensor_key_infos, &tensor_key_infos,
)); ));
@@ -217,7 +217,7 @@ 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: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(&tensor_key_infos); let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc(&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(&tensor_key_infos); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&tensor_key_infos);

View File

@@ -18,8 +18,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWESwitchingKey, GGLWESwitchingKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::noise_ggsw_product, noise::noise_ggsw_product,
}; };
@@ -81,7 +81,7 @@ where
let dnum: usize = k_in.div_ceil(base2k * di); let dnum: usize = k_in.div_ceil(base2k * di);
let dsize_in: usize = 1; let dsize_in: usize = 1;
let gglwe_in_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_in_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_in.into(), k: k_in.into(),
@@ -91,7 +91,7 @@ where
rank_out: rank_out.into(), rank_out: rank_out.into(),
}; };
let gglwe_out_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_out_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_out.into(), k: k_out.into(),
@@ -110,9 +110,9 @@ where
rank: rank_out.into(), rank: rank_out.into(),
}; };
let mut ct_gglwe_in: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_in_infos); let mut ct_gglwe_in: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_in_infos);
let mut ct_gglwe_out: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_out_infos); let mut ct_gglwe_out: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_out_infos);
let mut ct_rgsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos); let mut ct_rgsw: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
let mut pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
@@ -121,14 +121,14 @@ where
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_in_infos) GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_in_infos)
| GGLWESwitchingKey::external_product_scratch_space( | GLWESwitchingKey::external_product_scratch_space(
module, module,
&gglwe_out_infos, &gglwe_out_infos,
&gglwe_in_infos, &gglwe_in_infos,
&ggsw_infos, &ggsw_infos,
) )
| GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_infos), | GGSW::encrypt_sk_scratch_space(module, &ggsw_infos),
); );
let r: usize = 1; let r: usize = 1;
@@ -163,7 +163,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ct_rgsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); let ct_rgsw_prepared: GGSWPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow());
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow()); ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow());
@@ -263,7 +263,7 @@ where
let dsize_in: usize = 1; let dsize_in: usize = 1;
let gglwe_out_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_out_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_out.into(), k: k_out.into(),
@@ -282,8 +282,8 @@ where
rank: rank_out.into(), rank: rank_out.into(),
}; };
let mut ct_gglwe: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_out_infos); let mut ct_gglwe: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_out_infos);
let mut ct_rgsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos); let mut ct_rgsw: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
let mut pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
@@ -292,9 +292,9 @@ where
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_out_infos) GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_out_infos)
| GGLWESwitchingKey::external_product_inplace_scratch_space(module, &gglwe_out_infos, &ggsw_infos) | GLWESwitchingKey::external_product_inplace_scratch_space(module, &gglwe_out_infos, &ggsw_infos)
| GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_infos), | GGSW::encrypt_sk_scratch_space(module, &ggsw_infos),
); );
let r: usize = 1; let r: usize = 1;
@@ -329,7 +329,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ct_rgsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); let ct_rgsw_prepared: GGSWPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow());
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow()); ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow());

View File

@@ -18,8 +18,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, GGSW, GGSWCiphertextLayout, GLWESecret,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::noise_ggsw_product, noise::noise_ggsw_product,
}; };
@@ -111,9 +111,9 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut ggsw_in: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_in_infos); let mut ggsw_in: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_in_infos);
let mut ggsw_out: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_out_infos); let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_infos);
let mut ggsw_apply: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_apply_infos); let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_apply_infos);
let mut pt_in: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_in: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut pt_apply: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
@@ -128,9 +128,9 @@ where
pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} pt_apply.to_mut().raw_mut()[k] = 1; //X^{k}
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos)
| GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_in_infos) | GGSW::encrypt_sk_scratch_space(module, &ggsw_in_infos)
| GGSWCiphertext::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), | GGSW::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos),
); );
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into());
@@ -155,7 +155,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ct_rhs_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); let ct_rhs_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow());
ggsw_out.external_product(module, &ggsw_in, &ct_rhs_prepared, scratch.borrow()); ggsw_out.external_product(module, &ggsw_in, &ct_rhs_prepared, scratch.borrow());
@@ -265,8 +265,8 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut ggsw_out: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_out_infos); let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_infos);
let mut ggsw_apply: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_apply_infos); let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_apply_infos);
let mut pt_in: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_in: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut pt_apply: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
@@ -282,9 +282,9 @@ where
pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} pt_apply.to_mut().raw_mut()[k] = 1; //X^{k}
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos)
| GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_out_infos) | GGSW::encrypt_sk_scratch_space(module, &ggsw_out_infos)
| GGSWCiphertext::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), | GGSW::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos),
); );
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into());
@@ -309,7 +309,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ct_rhs_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); let ct_rhs_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow());
ggsw_out.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow()); ggsw_out.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow());

View File

@@ -17,8 +17,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::noise_ggsw_product, noise::noise_ggsw_product,
}; };
@@ -96,7 +96,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut ggsw_apply: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_apply_infos); let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_apply_infos);
let mut glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_infos); let mut glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_infos);
let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos);
let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
@@ -116,7 +116,7 @@ where
pt_ggsw.raw_mut()[k] = 1; // X^{k} pt_ggsw.raw_mut()[k] = 1; // X^{k}
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos)
| GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos)
| GLWECiphertext::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), | GLWECiphertext::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos),
); );
@@ -143,7 +143,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ct_ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); let ct_ggsw_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow());
glwe_out.external_product(module, &glwe_in, &ct_ggsw_prepared, scratch.borrow()); glwe_out.external_product(module, &glwe_in, &ct_ggsw_prepared, scratch.borrow());
@@ -240,7 +240,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut ggsw_apply: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_apply_infos); let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_apply_infos);
let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos);
let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos);
@@ -259,7 +259,7 @@ where
pt_ggsw.raw_mut()[k] = 1; // X^{k} pt_ggsw.raw_mut()[k] = 1; // X^{k}
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos)
| GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos)
| GLWECiphertext::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos), | GLWECiphertext::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos),
); );
@@ -286,7 +286,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ct_ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); let ct_ggsw_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow());
glwe_out.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow()); glwe_out.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow());

View File

@@ -18,8 +18,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWESwitchingKey, GGLWESwitchingKeyLayout, GLWESecret, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout,
prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
}; };
@@ -80,7 +80,7 @@ where
let dnum_apply: usize = k_in.div_ceil(base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di);
let dsize_in: usize = 1; let dsize_in: usize = 1;
let gglwe_s0s1_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_s0s1_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_in.into(), k: k_in.into(),
@@ -90,7 +90,7 @@ where
rank_out: rank_out_s0s1.into(), rank_out: rank_out_s0s1.into(),
}; };
let gglwe_s1s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_s1s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -100,7 +100,7 @@ where
rank_out: rank_out_s1s2.into(), rank_out: rank_out_s1s2.into(),
}; };
let gglwe_s0s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_s0s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_out.into(), k: k_out.into(),
@@ -110,20 +110,20 @@ where
rank_out: rank_out_s1s2.into(), rank_out: rank_out_s1s2.into(),
}; };
let mut gglwe_s0s1: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_s0s1_infos); let mut gglwe_s0s1: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos);
let mut gglwe_s1s2: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_s1s2_infos); let mut gglwe_s1s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos);
let mut gglwe_s0s2: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_s0s2_infos); let mut gglwe_s0s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s0s2_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_enc: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch_enc: ScratchOwned<B> = ScratchOwned::alloc(
GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos)
| GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos) | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos)
| GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s2_infos), | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s2_infos),
); );
let mut scratch_apply: ScratchOwned<B> = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_scratch_space( let mut scratch_apply: ScratchOwned<B> = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_scratch_space(
module, module,
&gglwe_s0s1_infos, &gglwe_s0s1_infos,
&gglwe_s0s2_infos, &gglwe_s0s2_infos,
@@ -160,7 +160,7 @@ where
scratch_enc.borrow(), scratch_enc.borrow(),
); );
let gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared<Vec<u8>, B> =
gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
@@ -246,7 +246,7 @@ where
let dnum: usize = k_out.div_ceil(base2k * di); let dnum: usize = k_out.div_ceil(base2k * di);
let dsize_in: usize = 1; let dsize_in: usize = 1;
let gglwe_s0s1_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_s0s1_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_out.into(), k: k_out.into(),
@@ -256,7 +256,7 @@ where
rank_out: rank_out.into(), rank_out: rank_out.into(),
}; };
let gglwe_s1s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let gglwe_s1s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -266,18 +266,18 @@ where
rank_out: rank_out.into(), rank_out: rank_out.into(),
}; };
let mut gglwe_s0s1: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_s0s1_infos); let mut gglwe_s0s1: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos);
let mut gglwe_s1s2: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&gglwe_s1s2_infos); let mut gglwe_s1s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s1s2_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_enc: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch_enc: ScratchOwned<B> = ScratchOwned::alloc(
GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos)
| GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos), | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos),
); );
let mut scratch_apply: ScratchOwned<B> = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_inplace_scratch_space( let mut scratch_apply: ScratchOwned<B> = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_inplace_scratch_space(
module, module,
&gglwe_s0s1_infos, &gglwe_s0s1_infos,
&gglwe_s1s2_infos, &gglwe_s1s2_infos,
@@ -315,13 +315,13 @@ where
scratch_enc.borrow(), scratch_enc.borrow(),
); );
let gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared<Vec<u8>, B> =
gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
gglwe_s0s1.keyswitch_inplace(module, &gglwe_s1s2_prepared, scratch_apply.borrow()); gglwe_s0s1.keyswitch_inplace(module, &gglwe_s1s2_prepared, scratch_apply.borrow());
let gglwe_s0s2: GGLWESwitchingKey<Vec<u8>> = gglwe_s0s1; let gglwe_s0s2: GLWESwitchingKey<Vec<u8>> = gglwe_s0s1;
let max_noise: f64 = log2_std_noise_gglwe_product( let max_noise: f64 = log2_std_noise_gglwe_product(
n as f64, n as f64,

View File

@@ -18,9 +18,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWESwitchingKey, GGLWESwitchingKeyLayout, GGLWETensorKey, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, TensorKey, TensorKeyLayout,
GLWESecret, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc, TensorKeyPrepared},
prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::noise_ggsw_keyswitch, noise::noise_ggsw_keyswitch,
}; };
@@ -103,7 +102,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let tsk_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { let tsk_infos: TensorKeyLayout = TensorKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_tsk.into(), k: k_tsk.into(),
@@ -112,7 +111,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let ksk_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let ksk_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -122,10 +121,10 @@ where
rank_out: rank.into(), rank_out: rank.into(),
}; };
let mut ggsw_in: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_in_infos); let mut ggsw_in: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_in_infos);
let mut ggsw_out: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_out_infos); let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_infos);
let mut tsk: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(&tsk_infos); let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc(&tsk_infos);
let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&ksk_apply_infos); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&ksk_apply_infos);
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -133,10 +132,10 @@ where
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_in_infos) GGSW::encrypt_sk_scratch_space(module, &ggsw_in_infos)
| GGLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) | GLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos)
| GGLWETensorKey::encrypt_sk_scratch_space(module, &tsk_infos) | TensorKey::encrypt_sk_scratch_space(module, &tsk_infos)
| GGSWCiphertext::keyswitch_scratch_space( | GGSW::keyswitch_scratch_space(
module, module,
&ggsw_out_infos, &ggsw_out_infos,
&ggsw_in_infos, &ggsw_in_infos,
@@ -182,8 +181,8 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow()); let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
let tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = tsk.prepare_alloc(module, scratch.borrow()); let tsk_prepared: TensorKeyPrepared<Vec<u8>, B> = tsk.prepare_alloc(module, scratch.borrow());
ggsw_out.keyswitch( ggsw_out.keyswitch(
module, module,
@@ -282,7 +281,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let tsk_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { let tsk_infos: TensorKeyLayout = TensorKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_tsk.into(), k: k_tsk.into(),
@@ -291,7 +290,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let ksk_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let ksk_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -301,9 +300,9 @@ where
rank_out: rank.into(), rank_out: rank.into(),
}; };
let mut ggsw_out: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_out_infos); let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_infos);
let mut tsk: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(&tsk_infos); let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc(&tsk_infos);
let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&ksk_apply_infos); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&ksk_apply_infos);
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
@@ -311,10 +310,10 @@ where
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_out_infos) GGSW::encrypt_sk_scratch_space(module, &ggsw_out_infos)
| GGLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) | GLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos)
| GGLWETensorKey::encrypt_sk_scratch_space(module, &tsk_infos) | TensorKey::encrypt_sk_scratch_space(module, &tsk_infos)
| GGSWCiphertext::keyswitch_inplace_scratch_space(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), | GGSW::keyswitch_inplace_scratch_space(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos),
); );
let var_xs: f64 = 0.5; let var_xs: f64 = 0.5;
@@ -354,8 +353,8 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow()); let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
let tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = tsk.prepare_alloc(module, scratch.borrow()); let tsk_prepared: TensorKeyPrepared<Vec<u8>, B> = tsk.prepare_alloc(module, scratch.borrow());
ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow());

View File

@@ -18,8 +18,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWESwitchingKey, GGLWESwitchingKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout,
prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc},
}, },
noise::log2_std_noise_gglwe_product, noise::log2_std_noise_gglwe_product,
}; };
@@ -91,7 +91,7 @@ where
rank: rank_out.into(), rank: rank_out.into(),
}; };
let key_apply: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -101,7 +101,7 @@ where
rank_out: rank_out.into(), rank_out: rank_out.into(),
}; };
let mut ksk: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&key_apply); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&key_apply);
let mut glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_infos); let mut glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_infos);
let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_in_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_in_infos);
@@ -113,7 +113,7 @@ where
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);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply) GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply)
| GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos)
| GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply), | GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply),
); );
@@ -144,7 +144,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow()); let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow()); glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow());
@@ -224,7 +224,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let key_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -234,7 +234,7 @@ where
rank_out: rank.into(), rank_out: rank.into(),
}; };
let mut key_apply: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(&key_apply_infos); let mut key_apply: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&key_apply_infos);
let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos);
@@ -245,7 +245,7 @@ where
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);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GGLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos)
| GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos)
| GLWECiphertext::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos), | GLWECiphertext::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos),
); );
@@ -276,7 +276,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = key_apply.prepare_alloc(module, scratch.borrow()); let ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, B> = key_apply.prepare_alloc(module, scratch.borrow());
glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow()); glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow());

View File

@@ -21,8 +21,8 @@ use poulpy_hal::{
use crate::{ use crate::{
GLWEOperations, GLWEPacker, GLWEOperations, GLWEPacker,
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret,
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
}; };
@@ -95,7 +95,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_ksk.into(), k: k_ksk.into(),
@@ -106,7 +106,7 @@ where
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos)
| GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos)
| GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos), | GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos),
); );
@@ -124,8 +124,8 @@ where
let gal_els: Vec<i64> = GLWEPacker::galois_elements(module); let gal_els: Vec<i64> = GLWEPacker::galois_elements(module);
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new(); let mut auto_keys: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&key_infos); let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&key_infos);
gal_els.iter().for_each(|gal_el| { gal_els.iter().for_each(|gal_el| {
tmp.encrypt_sk( tmp.encrypt_sk(
module, module,
@@ -135,7 +135,7 @@ where
&mut source_xe, &mut source_xe,
scratch.borrow(), scratch.borrow(),
); );
let atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow()); let atk_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow());
auto_keys.insert(*gal_el, atk_prepared); auto_keys.insert(*gal_el, atk_prepared);
}); });

View File

@@ -21,9 +21,8 @@ use poulpy_hal::{
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWEInfos,
LWEInfos, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
noise::var_noise_gglwe_product, noise::var_noise_gglwe_product,
}; };
@@ -90,7 +89,7 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_autokey.into(), k: k_autokey.into(),
@@ -110,7 +109,7 @@ where
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos)
| GLWECiphertext::decrypt_scratch_space(module, &glwe_out_infos) | GLWECiphertext::decrypt_scratch_space(module, &glwe_out_infos)
| GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos)
| GLWECiphertext::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), | GLWECiphertext::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos),
); );
@@ -135,9 +134,9 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new(); let mut auto_keys: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module); let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module);
let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&key_infos); let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&key_infos);
gal_els.iter().for_each(|gal_el| { gal_els.iter().for_each(|gal_el| {
tmp.encrypt_sk( tmp.encrypt_sk(
module, module,
@@ -147,7 +146,7 @@ where
&mut source_xe, &mut source_xe,
scratch.borrow(), scratch.borrow(),
); );
let atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow()); let atk_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow());
auto_keys.insert(*gal_el, atk_prepared); auto_keys.insert(*gal_el, atk_prepared);
}); });

View File

@@ -3,8 +3,8 @@ use std::hint::black_box;
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios};
use poulpy_core::layouts::{ use poulpy_core::layouts::{
Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, LWECiphertext, AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWECiphertext, LWECiphertextLayout, LWESecret,
LWECiphertextLayout, LWESecret, prepared::PrepareAlloc, TensorKeyLayout, prepared::PrepareAlloc,
}; };
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{
@@ -218,7 +218,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&params.ggsw_infos); let mut res: GGSW<Vec<u8>> = GGSW::alloc(&params.ggsw_infos);
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow());
move || { move || {
@@ -261,7 +261,7 @@ where
dnum: 3_u32.into(), dnum: 3_u32.into(),
rank: 2_u32.into(), rank: 2_u32.into(),
}, },
layout_atk: GGLWEAutomorphismKeyLayout { layout_atk: AutomorphismKeyLayout {
n: 1024_u32.into(), n: 1024_u32.into(),
base2k: 13_u32.into(), base2k: 13_u32.into(),
k: 52_u32.into(), k: 52_u32.into(),
@@ -269,7 +269,7 @@ where
dsize: Dsize(1), dsize: Dsize(1),
rank: 2_u32.into(), rank: 2_u32.into(),
}, },
layout_tsk: GGLWETensorKeyLayout { layout_tsk: TensorKeyLayout {
n: 1024_u32.into(), n: 1024_u32.into(),
base2k: 13_u32.into(), base2k: 13_u32.into(),
k: 52_u32.into(), k: 52_u32.into(),

View File

@@ -1,9 +1,9 @@
use poulpy_core::{ use poulpy_core::{
GLWEOperations, GLWEOperations,
layouts::{ layouts::{
GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret,
GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
}, },
}; };
use std::time::Instant; use std::time::Instant;
@@ -89,7 +89,7 @@ fn main() {
dnum: rows_brk.into(), dnum: rows_brk.into(),
rank: rank.into(), rank: rank.into(),
}, },
layout_atk: GGLWEAutomorphismKeyLayout { layout_atk: AutomorphismKeyLayout {
n: n_glwe.into(), n: n_glwe.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_trace.into(), k: k_trace.into(),
@@ -97,7 +97,7 @@ fn main() {
dsize: 1_u32.into(), dsize: 1_u32.into(),
rank: rank.into(), rank: rank.into(),
}, },
layout_tsk: GGLWETensorKeyLayout { layout_tsk: TensorKeyLayout {
n: n_glwe.into(), n: n_glwe.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_tsk.into(), k: k_tsk.into(),
@@ -187,7 +187,7 @@ fn main() {
println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
// Output GGSW // Output GGSW
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos); let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
// Circuit bootstrapping key prepared (opaque backend dependant write only struct) // Circuit bootstrapping key prepared (opaque backend dependant write only struct)
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, CGGI, BackendImpl> = let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, CGGI, BackendImpl> =
@@ -249,7 +249,7 @@ fn main() {
); );
// Prepare GGSW output of circuit bootstrapping (opaque backend dependant write only struct) // Prepare GGSW output of circuit bootstrapping (opaque backend dependant write only struct)
let res_prepared: GGSWCiphertextPrepared<Vec<u8>, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); let res_prepared: GGSWPrepared<Vec<u8>, BackendImpl> = res.prepare_alloc(&module, scratch.borrow());
// Apply GLWE x GGSW // Apply GLWE x GGSW
ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow());

View File

@@ -1,12 +1,10 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use poulpy_core::layouts::{ use poulpy_core::layouts::{Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared};
Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWCiphertextPrepared,
};
#[cfg(test)] #[cfg(test)]
use poulpy_core::{ use poulpy_core::{
TakeGGSW, TakeGGSW,
layouts::{GGSWCiphertext, prepared::GLWESecretPrepared}, layouts::{GGSW, prepared::GLWESecretPrepared},
}; };
use poulpy_hal::{ use poulpy_hal::{
api::VmpPMatAlloc, api::VmpPMatAlloc,
@@ -29,7 +27,7 @@ use crate::tfhe::bdd_arithmetic::{FheUintBlocks, FheUintPrepare, ToBits, Unsigne
#[cfg(test)] #[cfg(test)]
pub(crate) struct FheUintBlocksPrepDebug<D: Data, T: UnsignedInteger> { pub(crate) struct FheUintBlocksPrepDebug<D: Data, T: UnsignedInteger> {
pub(crate) blocks: Vec<GGSWCiphertext<D>>, pub(crate) blocks: Vec<GGSW<D>>,
pub(crate) _base: u8, pub(crate) _base: u8,
pub(crate) _phantom: PhantomData<T>, pub(crate) _phantom: PhantomData<T>,
} }
@@ -62,7 +60,7 @@ impl<T: UnsignedInteger> FheUintBlocksPrepDebug<Vec<u8>, T> {
) -> Self { ) -> Self {
Self { Self {
blocks: (0..T::WORD_SIZE) blocks: (0..T::WORD_SIZE)
.map(|_| GGSWCiphertext::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize)) .map(|_| GGSW::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize))
.collect(), .collect(),
_base: 1, _base: 1,
_phantom: PhantomData, _phantom: PhantomData,
@@ -72,7 +70,7 @@ impl<T: UnsignedInteger> FheUintBlocksPrepDebug<Vec<u8>, T> {
/// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger]. /// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger].
pub struct FheUintBlocksPrep<D: Data, B: Backend, T: UnsignedInteger> { pub struct FheUintBlocksPrep<D: Data, B: Backend, T: UnsignedInteger> {
pub(crate) blocks: Vec<GGSWCiphertextPrepared<D, B>>, pub(crate) blocks: Vec<GGSWPrepared<D, B>>,
pub(crate) _base: u8, pub(crate) _base: u8,
pub(crate) _phantom: PhantomData<T>, pub(crate) _phantom: PhantomData<T>,
} }
@@ -103,7 +101,7 @@ where
{ {
Self { Self {
blocks: (0..T::WORD_SIZE) blocks: (0..T::WORD_SIZE)
.map(|_| GGSWCiphertextPrepared::alloc_with(module, base2k, k, dnum, dsize, rank)) .map(|_| GGSWPrepared::alloc_with(module, base2k, k, dnum, dsize, rank))
.collect(), .collect(),
_base: 1, _base: 1,
_phantom: PhantomData, _phantom: PhantomData,

View File

@@ -3,7 +3,7 @@ use poulpy_core::{
GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing, GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing,
layouts::{ layouts::{
GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision,
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared}, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared},
}, },
}; };
use poulpy_hal::{ use poulpy_hal::{
@@ -32,7 +32,7 @@ impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> {
&mut self, &mut self,
module: &Module<BE>, module: &Module<BE>,
mut tmp_res: Vec<GLWECiphertext<&mut [u8]>>, mut tmp_res: Vec<GLWECiphertext<&mut [u8]>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<ATK, BE>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<ATK, BE>>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
ATK: DataRef, ATK: DataRef,

View File

@@ -3,7 +3,7 @@ use poulpy_core::{
GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice, GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice,
layouts::{ layouts::{
GLWECiphertext, GLWECiphertextToMut, LWEInfos, GLWECiphertext, GLWECiphertextToMut, LWEInfos,
prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef}, prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared},
}, },
}; };
use poulpy_hal::{ use poulpy_hal::{
@@ -164,7 +164,7 @@ pub trait Cmux<BE: Backend> {
out: &mut GLWECiphertext<O>, out: &mut GLWECiphertext<O>,
t: &GLWECiphertext<T>, t: &GLWECiphertext<T>,
f: &GLWECiphertext<F>, f: &GLWECiphertext<F>,
s: &GGSWCiphertextPrepared<S, BE>, s: &GGSWPrepared<S, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
O: DataMut, O: DataMut,
@@ -182,7 +182,7 @@ where
out: &mut GLWECiphertext<O>, out: &mut GLWECiphertext<O>,
t: &GLWECiphertext<T>, t: &GLWECiphertext<T>,
f: &GLWECiphertext<F>, f: &GLWECiphertext<F>,
s: &GGSWCiphertextPrepared<S, BE>, s: &GGSWPrepared<S, BE>,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
O: DataMut, O: DataMut,

View File

@@ -11,7 +11,7 @@ use crate::tfhe::{
use poulpy_core::{ use poulpy_core::{
TakeGGSW, TakeGLWECt, TakeGGSW, TakeGLWECt,
layouts::{ layouts::{
GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, LWECiphertext, LWESecret, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWECiphertext, LWESecret,
prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
}, },
}; };
@@ -56,7 +56,7 @@ where
BRA: BlindRotationAlgo, BRA: BlindRotationAlgo,
{ {
cbt: CircuitBootstrappingKey<CBT, BRA>, cbt: CircuitBootstrappingKey<CBT, BRA>,
ks: GLWEToLWEKey<LWE>, ks: GLWEToLWESwitchingKey<LWE>,
} }
impl<BRA: BlindRotationAlgo> BDDKey<Vec<u8>, Vec<u8>, BRA> { impl<BRA: BlindRotationAlgo> BDDKey<Vec<u8>, Vec<u8>, BRA> {
@@ -98,7 +98,7 @@ impl<BRA: BlindRotationAlgo> BDDKey<Vec<u8>, Vec<u8>, BRA> {
+ VecZnxAutomorphismInplace<BE>, + VecZnxAutomorphismInplace<BE>,
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol<BE> + TakeVecZnxBig<BE>, Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol<BE> + TakeVecZnxBig<BE>,
{ {
let mut ks: GLWEToLWEKey<Vec<u8>> = GLWEToLWEKey::alloc(&infos.ks_infos()); let mut ks: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(&infos.ks_infos());
ks.encrypt_sk(module, sk_lwe, sk_glwe, source_xa, source_xe, scratch); ks.encrypt_sk(module, sk_lwe, sk_glwe, source_xa, source_xe, scratch);
Self { Self {
@@ -131,7 +131,7 @@ impl<CBT: DataMut, LWE: DataMut, BRA: BlindRotationAlgo, BE: Backend> PrepareAll
for BDDKey<CBT, LWE, BRA> for BDDKey<CBT, LWE, BRA>
where where
CircuitBootstrappingKey<CBT, BRA>: PrepareAlloc<BE, CircuitBootstrappingKeyPrepared<CBT, BRA, BE>>, CircuitBootstrappingKey<CBT, BRA>: PrepareAlloc<BE, CircuitBootstrappingKeyPrepared<CBT, BRA, BE>>,
GLWEToLWEKey<LWE>: PrepareAlloc<BE, GLWEToLWESwitchingKeyPrepared<LWE, BE>>, GLWEToLWESwitchingKey<LWE>: PrepareAlloc<BE, GLWEToLWESwitchingKeyPrepared<LWE, BE>>,
{ {
fn prepare_alloc(&self, module: &Module<BE>, scratch: &mut Scratch<BE>) -> BDDKeyPrepared<CBT, LWE, BRA, BE> { fn prepare_alloc(&self, module: &Module<BE>, scratch: &mut Scratch<BE>) -> BDDKeyPrepared<CBT, LWE, BRA, BE> {
BDDKeyPrepared { BDDKeyPrepared {

View File

@@ -1,7 +1,7 @@
#[cfg(test)] #[cfg(test)]
use poulpy_core::layouts::{ use poulpy_core::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, GLWECiphertextLayout, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWECiphertextLayout, GLWEToLWEKeyLayout, Rank,
GLWEToLWEKeyLayout, Rank, TorusPrecision, TensorKeyLayout, TorusPrecision,
}; };
#[cfg(test)] #[cfg(test)]
@@ -53,7 +53,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
dnum: Dnum(3), dnum: Dnum(3),
rank: Rank(TEST_RANK), rank: Rank(TEST_RANK),
}, },
layout_atk: GGLWEAutomorphismKeyLayout { layout_atk: AutomorphismKeyLayout {
n: Degree(TEST_N_GLWE), n: Degree(TEST_N_GLWE),
base2k: Base2K(TEST_BASE2K), base2k: Base2K(TEST_BASE2K),
k: TorusPrecision(52), k: TorusPrecision(52),
@@ -61,7 +61,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
dnum: Dnum(3), dnum: Dnum(3),
dsize: Dsize(1), dsize: Dsize(1),
}, },
layout_tsk: GGLWETensorKeyLayout { layout_tsk: TensorKeyLayout {
n: Degree(TEST_N_GLWE), n: Degree(TEST_N_GLWE),
base2k: Base2K(TEST_BASE2K), base2k: Base2K(TEST_BASE2K),
k: TorusPrecision(52), k: TorusPrecision(52),

View File

@@ -14,9 +14,9 @@ use std::marker::PhantomData;
use poulpy_core::{ use poulpy_core::{
Distribution, Distribution,
layouts::{ layouts::{
GGSWCiphertext, GGSWInfos, LWESecret, GGSW, GGSWInfos, LWESecret,
compressed::GGSWCiphertextCompressed, compressed::GGSWCiphertextCompressed,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, prepared::{GGSWPrepared, GLWESecretPrepared},
}, },
}; };
@@ -30,9 +30,9 @@ impl BlindRotationKeyAlloc for BlindRotationKey<Vec<u8>, CGGI> {
where where
A: BlindRotationKeyInfos, A: BlindRotationKeyInfos,
{ {
let mut data: Vec<GGSWCiphertext<Vec<u8>>> = Vec::with_capacity(infos.n_lwe().into()); let mut data: Vec<GGSW<Vec<u8>>> = Vec::with_capacity(infos.n_lwe().into());
for _ in 0..infos.n_lwe().as_usize() { for _ in 0..infos.n_lwe().as_usize() {
data.push(GGSWCiphertext::alloc(infos)); data.push(GGSW::alloc(infos));
} }
Self { Self {
@@ -49,7 +49,7 @@ impl BlindRotationKey<Vec<u8>, CGGI> {
A: GGSWInfos, A: GGSWInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
{ {
GGSWCiphertext::encrypt_sk_scratch_space(module, infos) GGSW::encrypt_sk_scratch_space(module, infos)
} }
} }
@@ -121,8 +121,8 @@ where
where where
A: BlindRotationKeyInfos, A: BlindRotationKeyInfos,
{ {
let mut data: Vec<GGSWCiphertextPrepared<Vec<u8>, B>> = Vec::with_capacity(infos.n_lwe().into()); let mut data: Vec<GGSWPrepared<Vec<u8>, B>> = Vec::with_capacity(infos.n_lwe().into());
(0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextPrepared::alloc(module, infos))); (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc(module, infos)));
Self { Self {
data, data,
dist: Distribution::NONE, dist: Distribution::NONE,

View File

@@ -8,7 +8,7 @@ use std::{fmt, marker::PhantomData};
use poulpy_core::{ use poulpy_core::{
Distribution, Distribution,
layouts::{ layouts::{
Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision,
prepared::GLWESecretPrepared, prepared::GLWESecretPrepared,
}, },
}; };
@@ -98,7 +98,7 @@ pub trait BlindRotationKeyEncryptSk<B: Backend> {
#[derive(Clone)] #[derive(Clone)]
pub struct BlindRotationKey<D: Data, BRT: BlindRotationAlgo> { pub struct BlindRotationKey<D: Data, BRT: BlindRotationAlgo> {
pub(crate) keys: Vec<GGSWCiphertext<D>>, pub(crate) keys: Vec<GGSW<D>>,
pub(crate) dist: Distribution, pub(crate) dist: Distribution,
pub(crate) _phantom: PhantomData<BRT>, pub(crate) _phantom: PhantomData<BRT>,
} }

View File

@@ -9,7 +9,7 @@ use poulpy_core::{
Distribution, Distribution,
layouts::{ layouts::{
Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{GGSWCiphertextPrepared, Prepare, PrepareAlloc}, prepared::{GGSWPrepared, Prepare, PrepareAlloc},
}, },
}; };
@@ -23,7 +23,7 @@ pub trait BlindRotationKeyPreparedAlloc<B: Backend> {
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct BlindRotationKeyPrepared<D: Data, BRT: BlindRotationAlgo, B: Backend> { pub struct BlindRotationKeyPrepared<D: Data, BRT: BlindRotationAlgo, B: Backend> {
pub(crate) data: Vec<GGSWCiphertextPrepared<D, B>>, pub(crate) data: Vec<GGSWPrepared<D, B>>,
pub(crate) dist: Distribution, pub(crate) dist: Distribution,
pub(crate) x_pow_a: Option<Vec<SvpPPol<Vec<u8>, B>>>, pub(crate) x_pow_a: Option<Vec<SvpPPol<Vec<u8>, B>>>,
pub(crate) _phantom: PhantomData<BRT>, pub(crate) _phantom: PhantomData<BRT>,

View File

@@ -20,7 +20,7 @@ use poulpy_core::{
}; };
use poulpy_core::glwe_packing; use poulpy_core::glwe_packing;
use poulpy_core::layouts::{GGSWCiphertext, GLWECiphertext, LWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; use poulpy_core::layouts::{GGSW, GLWECiphertext, LWECiphertext, prepared::AutomorphismKeyPrepared};
use crate::tfhe::{ use crate::tfhe::{
blind_rotation::{ blind_rotation::{
@@ -74,7 +74,7 @@ where
fn execute_to_constant<DM: DataMut, DR: DataRef>( fn execute_to_constant<DM: DataMut, DR: DataRef>(
&self, &self,
module: &Module<B>, module: &Module<B>,
res: &mut GGSWCiphertext<DM>, res: &mut GGSW<DM>,
lwe: &LWECiphertext<DR>, lwe: &LWECiphertext<DR>,
log_domain: usize, log_domain: usize,
extension_factor: usize, extension_factor: usize,
@@ -97,7 +97,7 @@ where
&self, &self,
module: &Module<B>, module: &Module<B>,
log_gap_out: usize, log_gap_out: usize,
res: &mut GGSWCiphertext<DM>, res: &mut GGSW<DM>,
lwe: &LWECiphertext<DR>, lwe: &LWECiphertext<DR>,
log_domain: usize, log_domain: usize,
extension_factor: usize, extension_factor: usize,
@@ -122,7 +122,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
to_exponent: bool, to_exponent: bool,
module: &Module<B>, module: &Module<B>,
log_gap_out: usize, log_gap_out: usize,
res: &mut GGSWCiphertext<DRes>, res: &mut GGSW<DRes>,
lwe: &LWECiphertext<DLwe>, lwe: &LWECiphertext<DLwe>,
log_domain: usize, log_domain: usize,
extension_factor: usize, extension_factor: usize,
@@ -268,7 +268,7 @@ fn post_process<DataRes, DataA, B: Backend>(
log_gap_in: usize, log_gap_in: usize,
log_gap_out: usize, log_gap_out: usize,
log_domain: usize, log_domain: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>>,
scratch: &mut Scratch<B>, scratch: &mut Scratch<B>,
) where ) where
DataRes: DataMut, DataRes: DataMut,

View File

@@ -1,7 +1,7 @@
use poulpy_core::layouts::{ use poulpy_core::layouts::{
GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GGLWETensorKey, GGLWETensorKeyLayout, GGSWInfos, AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret,
GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey, TensorKeyLayout,
prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared},
}; };
use std::collections::HashMap; use std::collections::HashMap;
@@ -24,19 +24,19 @@ use crate::tfhe::blind_rotation::{
pub trait CircuitBootstrappingKeyInfos { pub trait CircuitBootstrappingKeyInfos {
fn brk_infos(&self) -> BlindRotationKeyLayout; fn brk_infos(&self) -> BlindRotationKeyLayout;
fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout; fn atk_infos(&self) -> AutomorphismKeyLayout;
fn tsk_infos(&self) -> GGLWETensorKeyLayout; fn tsk_infos(&self) -> TensorKeyLayout;
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct CircuitBootstrappingKeyLayout { pub struct CircuitBootstrappingKeyLayout {
pub layout_brk: BlindRotationKeyLayout, pub layout_brk: BlindRotationKeyLayout,
pub layout_atk: GGLWEAutomorphismKeyLayout, pub layout_atk: AutomorphismKeyLayout,
pub layout_tsk: GGLWETensorKeyLayout, pub layout_tsk: TensorKeyLayout,
} }
impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout {
fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout { fn atk_infos(&self) -> AutomorphismKeyLayout {
self.layout_atk self.layout_atk
} }
@@ -44,7 +44,7 @@ impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout {
self.layout_brk self.layout_brk
} }
fn tsk_infos(&self) -> GGLWETensorKeyLayout { fn tsk_infos(&self) -> TensorKeyLayout {
self.layout_tsk self.layout_tsk
} }
} }
@@ -68,8 +68,8 @@ pub trait CircuitBootstrappingKeyEncryptSk<B: Backend> {
pub struct CircuitBootstrappingKey<D: Data, BRA: BlindRotationAlgo> { pub struct CircuitBootstrappingKey<D: Data, BRA: BlindRotationAlgo> {
pub(crate) brk: BlindRotationKey<D, BRA>, pub(crate) brk: BlindRotationKey<D, BRA>,
pub(crate) tsk: GGLWETensorKey<Vec<u8>>, pub(crate) tsk: TensorKey<Vec<u8>>,
pub(crate) atk: HashMap<i64, GGLWEAutomorphismKey<Vec<u8>>>, pub(crate) atk: HashMap<i64, AutomorphismKey<Vec<u8>>>,
} }
impl<BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyEncryptSk<B> for CircuitBootstrappingKey<Vec<u8>, BRA> impl<BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyEncryptSk<B> for CircuitBootstrappingKey<Vec<u8>, BRA>
@@ -117,14 +117,14 @@ where
assert_eq!(sk_glwe.n(), cbt_infos.atk_infos().n()); assert_eq!(sk_glwe.n(), cbt_infos.atk_infos().n());
assert_eq!(sk_glwe.n(), cbt_infos.tsk_infos().n()); assert_eq!(sk_glwe.n(), cbt_infos.tsk_infos().n());
let atk_infos: GGLWEAutomorphismKeyLayout = cbt_infos.atk_infos(); let atk_infos: AutomorphismKeyLayout = cbt_infos.atk_infos();
let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos(); let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos();
let trk_infos: GGLWETensorKeyLayout = cbt_infos.tsk_infos(); let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos();
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKey<Vec<u8>>> = HashMap::new(); let mut auto_keys: HashMap<i64, AutomorphismKey<Vec<u8>>> = HashMap::new();
let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module); let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module);
gal_els.iter().for_each(|gal_el| { gal_els.iter().for_each(|gal_el| {
let mut key: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(&atk_infos); let mut key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&atk_infos);
key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch); key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch);
auto_keys.insert(*gal_el, key); auto_keys.insert(*gal_el, key);
}); });
@@ -141,7 +141,7 @@ where
scratch, scratch,
); );
let mut tsk: GGLWETensorKey<Vec<u8>> = GGLWETensorKey::alloc(&trk_infos); let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc(&trk_infos);
tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch);
Self { Self {
@@ -154,14 +154,14 @@ where
pub struct CircuitBootstrappingKeyPrepared<D: Data, BRA: BlindRotationAlgo, B: Backend> { pub struct CircuitBootstrappingKeyPrepared<D: Data, BRA: BlindRotationAlgo, B: Backend> {
pub(crate) brk: BlindRotationKeyPrepared<D, BRA, B>, pub(crate) brk: BlindRotationKeyPrepared<D, BRA, B>,
pub(crate) tsk: GGLWETensorKeyPrepared<Vec<u8>, B>, pub(crate) tsk: TensorKeyPrepared<Vec<u8>, B>,
pub(crate) atk: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>>, pub(crate) atk: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>>,
} }
impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared<D, BRA, B> { impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared<D, BRA, B> {
fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout { fn atk_infos(&self) -> AutomorphismKeyLayout {
let (_, atk) = self.atk.iter().next().expect("atk is empty"); let (_, atk) = self.atk.iter().next().expect("atk is empty");
GGLWEAutomorphismKeyLayout { AutomorphismKeyLayout {
n: atk.n(), n: atk.n(),
base2k: atk.base2k(), base2k: atk.base2k(),
k: atk.k(), k: atk.k(),
@@ -182,8 +182,8 @@ impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyInfo
} }
} }
fn tsk_infos(&self) -> GGLWETensorKeyLayout { fn tsk_infos(&self) -> TensorKeyLayout {
GGLWETensorKeyLayout { TensorKeyLayout {
n: self.tsk.n(), n: self.tsk.n(),
base2k: self.tsk.base2k(), base2k: self.tsk.base2k(),
k: self.tsk.k(), k: self.tsk.k(),
@@ -199,13 +199,13 @@ impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> PrepareAlloc<B, CircuitBoot
where where
Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>, Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
BlindRotationKey<D, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>, BlindRotationKey<D, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>,
GGLWETensorKey<D>: PrepareAlloc<B, GGLWETensorKeyPrepared<Vec<u8>, B>>, TensorKey<D>: PrepareAlloc<B, TensorKeyPrepared<Vec<u8>, B>>,
GGLWEAutomorphismKey<D>: PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>>, AutomorphismKey<D>: PrepareAlloc<B, AutomorphismKeyPrepared<Vec<u8>, B>>,
{ {
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> { fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> {
let brk: BlindRotationKeyPrepared<Vec<u8>, BRA, B> = self.brk.prepare_alloc(module, scratch); let brk: BlindRotationKeyPrepared<Vec<u8>, BRA, B> = self.brk.prepare_alloc(module, scratch);
let tsk: GGLWETensorKeyPrepared<Vec<u8>, B> = self.tsk.prepare_alloc(module, scratch); let tsk: TensorKeyPrepared<Vec<u8>, B> = self.tsk.prepare_alloc(module, scratch);
let mut atk: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new(); let mut atk: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
for (key, value) in &self.atk { for (key, value) in &self.atk {
atk.insert(*key, value.prepare_alloc(module, scratch)); atk.insert(*key, value.prepare_alloc(module, scratch));
} }

View File

@@ -5,7 +5,7 @@ pub mod tests;
pub use circuit::*; pub use circuit::*;
pub use key::*; pub use key::*;
use poulpy_core::layouts::{GGSWCiphertext, LWECiphertext}; use poulpy_core::layouts::{GGSW, LWECiphertext};
use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
@@ -13,7 +13,7 @@ pub trait CirtuitBootstrappingExecute<B: Backend> {
fn execute_to_constant<DM: DataMut, DR: DataRef>( fn execute_to_constant<DM: DataMut, DR: DataRef>(
&self, &self,
module: &Module<B>, module: &Module<B>,
res: &mut GGSWCiphertext<DM>, res: &mut GGSW<DM>,
lwe: &LWECiphertext<DR>, lwe: &LWECiphertext<DR>,
log_domain: usize, log_domain: usize,
extension_factor: usize, extension_factor: usize,
@@ -25,7 +25,7 @@ pub trait CirtuitBootstrappingExecute<B: Backend> {
&self, &self,
module: &Module<B>, module: &Module<B>,
log_gap_out: usize, log_gap_out: usize,
res: &mut GGSWCiphertext<DM>, res: &mut GGSW<DM>,
lwe: &LWECiphertext<DR>, lwe: &LWECiphertext<DR>,
log_domain: usize, log_domain: usize,
extension_factor: usize, extension_factor: usize,

View File

@@ -32,12 +32,12 @@ use crate::tfhe::{
}; };
use poulpy_core::layouts::{ use poulpy_core::layouts::{
Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, LWECiphertextLayout, prepared::PrepareAlloc, AutomorphismKeyLayout, Dsize, GGSWCiphertextLayout, LWECiphertextLayout, TensorKeyLayout, prepared::PrepareAlloc,
}; };
use poulpy_core::layouts::{ use poulpy_core::layouts::{
GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, GGSW, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, prepared::{GGSWPrepared, GLWESecretPrepared},
}; };
pub fn test_circuit_bootstrapping_to_exponent<B, BRA: BlindRotationAlgo>(module: &Module<B>) pub fn test_circuit_bootstrapping_to_exponent<B, BRA: BlindRotationAlgo>(module: &Module<B>)
@@ -143,7 +143,7 @@ where
dnum: rows_brk.into(), dnum: rows_brk.into(),
rank: rank.into(), rank: rank.into(),
}, },
layout_atk: GGLWEAutomorphismKeyLayout { layout_atk: AutomorphismKeyLayout {
n: n_glwe.into(), n: n_glwe.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_atk.into(), k: k_atk.into(),
@@ -151,7 +151,7 @@ where
rank: rank.into(), rank: rank.into(),
dsize: Dsize(1), dsize: Dsize(1),
}, },
layout_tsk: GGLWETensorKeyLayout { layout_tsk: TensorKeyLayout {
n: n_glwe.into(), n: n_glwe.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_tsk.into(), k: k_tsk.into(),
@@ -206,7 +206,7 @@ where
); );
println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos); let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
let log_gap_out = 1; let log_gap_out = 1;
@@ -249,7 +249,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let res_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow()); let res_prepared: GGSWPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());
ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());
@@ -365,7 +365,7 @@ where
dnum: rows_brk.into(), dnum: rows_brk.into(),
rank: rank.into(), rank: rank.into(),
}, },
layout_atk: GGLWEAutomorphismKeyLayout { layout_atk: AutomorphismKeyLayout {
n: n_glwe.into(), n: n_glwe.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_atk.into(), k: k_atk.into(),
@@ -373,7 +373,7 @@ where
rank: rank.into(), rank: rank.into(),
dsize: Dsize(1), dsize: Dsize(1),
}, },
layout_tsk: GGLWETensorKeyLayout { layout_tsk: TensorKeyLayout {
n: n_glwe.into(), n: n_glwe.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k_tsk.into(), k: k_tsk.into(),
@@ -428,7 +428,7 @@ where
); );
println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
let mut res: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(&ggsw_infos); let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow());
@@ -462,7 +462,7 @@ where
scratch.borrow(), scratch.borrow(),
); );
let res_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow()); let res_prepared: GGSWPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());
ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());