Add schemes (#71)

* Move br + cbt to schemes/tfhe

* refactor blind rotation

* refactor circuit bootstrapping

* renamed exec -> prepared
This commit is contained in:
Jean-Philippe Bossuat
2025-08-15 15:06:26 +02:00
committed by GitHub
parent 8d9897b88b
commit c7219c35e9
130 changed files with 2631 additions and 3270 deletions

View File

@@ -4,7 +4,7 @@ use backend::hal::{
};
use crate::{
layouts::{GGLWEAutomorphismKey, GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec},
layouts::{GGLWEAutomorphismKey, GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared},
trait_families::GLWEKeyswitchFamily,
};
@@ -46,7 +46,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphism + VecZnxAutomorphismInplace,
@@ -120,7 +120,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphism + VecZnxAutomorphismInplace,

View File

@@ -6,7 +6,7 @@ use backend::hal::{
use crate::{
layouts::{
GGSWCiphertext, GLWECiphertext, Infos,
prepared::{GGLWEAutomorphismKeyExec, GGLWETensorKeyExec},
prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared},
},
trait_families::{GGSWKeySwitchFamily, GLWEKeyswitchFamily},
};
@@ -60,8 +60,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>,
auto_key: &GGLWEAutomorphismKeyExec<DataAk, B>,
tensor_key: &GGLWETensorKeyExec<DataTsk, B>,
auto_key: &GGLWEAutomorphismKeyPrepared<DataAk, B>,
tensor_key: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes,
@@ -117,8 +117,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn automorphism_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
auto_key: &GGLWEAutomorphismKeyExec<DataKsk, B>,
tensor_key: &GGLWETensorKeyExec<DataTsk, B>,
auto_key: &GGLWEAutomorphismKeyPrepared<DataKsk, B>,
tensor_key: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes,
@@ -134,7 +134,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>,
auto_key: &GGLWEAutomorphismKeyExec<DataAk, B>,
auto_key: &GGLWEAutomorphismKeyPrepared<DataAk, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxAutomorphismInplace + VecZnxNormalizeTmpBytes,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
};
use crate::{
layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec},
layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared},
trait_families::GLWEKeyswitchFamily,
};
@@ -49,7 +49,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphismInplace,
@@ -64,7 +64,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxAutomorphismInplace,
@@ -80,7 +80,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B>,
@@ -102,7 +102,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_add_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B>,
@@ -118,7 +118,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallAInplace<B>,
@@ -140,7 +140,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_sub_ab_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallAInplace<B>,
@@ -156,7 +156,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallBInplace<B>,
@@ -178,7 +178,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_sub_ba_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + VecZnxBigAutomorphismInplace<B> + VecZnxBigSubSmallBInplace<B>,

View File

@@ -5,7 +5,7 @@ use backend::hal::{
use crate::{
TakeGLWECt,
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::GLWEToLWESwitchingKeyExec},
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::GLWEToLWESwitchingKeyPrepared},
};
use crate::trait_families::GLWEKeyswitchFamily;
@@ -50,7 +50,7 @@ impl<DLwe: DataMut> LWECiphertext<DLwe> {
&mut self,
module: &Module<B>,
a: &GLWECiphertext<DGlwe>,
ks: &GLWEToLWESwitchingKeyExec<DKs, B>,
ks: &GLWEToLWESwitchingKeyPrepared<DKs, B>,
scratch: &mut Scratch<B>,
) where
DGlwe: DataRef,

View File

@@ -5,7 +5,7 @@ use backend::hal::{
use crate::{
TakeGLWECt,
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWEToGLWESwitchingKeyExec},
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWEToGLWESwitchingKeyPrepared},
};
use crate::trait_families::GLWEKeyswitchFamily;
@@ -33,7 +33,7 @@ impl<D: DataMut> GLWECiphertext<D> {
&mut self,
module: &Module<B>,
lwe: &LWECiphertext<DLwe>,
ksk: &LWEToGLWESwitchingKeyExec<DKsk, B>,
ksk: &LWEToGLWESwitchingKeyPrepared<DKsk, B>,
scratch: &mut Scratch<B>,
) where
DLwe: DataRef,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
};
use crate::{
layouts::{GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec},
layouts::{GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
trait_families::GLWEDecryptFamily,
};
@@ -27,7 +27,7 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
&self,
module: &Module<B>,
pt: &mut GLWEPlaintext<DataPt>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEDecryptFamily<B>,

View File

@@ -1,7 +1,7 @@
use std::io::{Read, Result, Write};
#[derive(Clone, Copy, Debug)]
pub(crate) enum Distribution {
pub enum Distribution {
TernaryFixed(usize), // Ternary with fixed Hamming weight
TernaryProb(f64), // Ternary with probabilistic Hamming weight
BinaryFixed(usize), // Binary with fixed Hamming weight

View File

@@ -8,19 +8,19 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecret, TakeGLWESecretExec,
TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{
GLWESecret, Infos,
compressed::{GGLWEAutomorphismKeyCompressed, GGLWESwitchingKeyCompressed},
},
};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWEAutomorphismKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank)
}
@@ -41,8 +41,8 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKeyCompressed<DataSelf> {
+ VecZnxSwithcDegree
+ VecZnxAutomorphism
+ VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx,
+ GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{
#[cfg(debug_assertions)]
{

View File

@@ -7,7 +7,7 @@ use sampling::source::Source;
use crate::{
TakeGLWEPt,
encryption::glwe_encrypt_sk_internal,
layouts::{GGLWECiphertext, Infos, compressed::GGLWECiphertextCompressed, prepared::GLWESecretExec},
layouts::{GGLWECiphertext, Infos, compressed::GGLWECiphertextCompressed, prepared::GLWESecretPrepared},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily};
@@ -26,7 +26,7 @@ impl<D: DataMut> GGLWECiphertextCompressed<D> {
&mut self,
module: &Module<B>,
pt: &ScalarZnx<DataPt>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
seed: [u8; 32],
source_xe: &mut Source,
sigma: f64,

View File

@@ -5,13 +5,14 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecretExec,
TakeGLWESecretPrepared,
layouts::{
GGLWECiphertext, GLWESecret, GGLWESwitchingKey, Infos, compressed::GGLWESwitchingKeyCompressed, prepared::GLWESecretExec,
GGLWECiphertext, GGLWESwitchingKey, GLWESecret, Infos, compressed::GGLWESwitchingKeyCompressed,
prepared::GLWESecretPrepared,
},
};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(
@@ -23,11 +24,11 @@ impl GGLWESwitchingKeyCompressed<Vec<u8>> {
rank_out: usize,
) -> usize
where
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
(GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1))
+ ScalarZnx::alloc_bytes(n, rank_in)
+ GLWESecretExec::bytes_of(module, n, rank_out)
+ GLWESecretPrepared::bytes_of(module, n, rank_out)
}
}
@@ -43,8 +44,9 @@ impl<DataSelf: DataMut> GGLWESwitchingKeyCompressed<DataSelf> {
scratch: &mut Scratch<B>,
) where
Module<B>:
GGLWESwitchingKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + ScratchAvailable + TakeVecZnx,
GGLWESwitchingKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily<B>,
Scratch<B>:
ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + ScratchAvailable + TakeVecZnx,
{
#[cfg(debug_assertions)]
{
@@ -85,7 +87,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKeyCompressed<DataSelf> {
);
});
let (mut sk_out_tmp, scratch2) = scratch1.take_glwe_secret_exec(n, sk_out.rank());
let (mut sk_out_tmp, scratch2) = scratch1.take_glwe_secret_prepared(n, sk_out.rank());
{
let (mut tmp, _) = scratch2.take_scalar_znx(n, 1);
(0..sk_out.rank()).for_each(|i| {

View File

@@ -8,17 +8,17 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecret, TakeGLWESecretExec,
layouts::{GGLWETensorKey, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed},
TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWETensorKey, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed, prepared::Prepare},
trait_families::GLWEDecryptFamily,
};
use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWETensorKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
GGLWETensorKey::encrypt_sk_scratch_space(module, n, basek, k, rank)
}
@@ -34,8 +34,10 @@ impl<DataSelf: DataMut> GGLWETensorKeyCompressed<DataSelf> {
sigma: f64,
scratch: &mut Scratch<B>,
) where
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecretExec<B> + TakeScalarZnx + TakeVecZnx,
Module<B>:
GGLWETensorKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily<B>,
Scratch<B>:
ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecretPrepared<B> + TakeScalarZnx + TakeVecZnx,
{
#[cfg(debug_assertions)]
{
@@ -46,8 +48,8 @@ impl<DataSelf: DataMut> GGLWETensorKeyCompressed<DataSelf> {
let n: usize = sk.n();
let rank: usize = self.rank();
let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_exec(n, rank);
sk_dft_prep.prepare(module, &sk);
let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_prepared(n, rank);
sk_dft_prep.prepare(module, &sk, scratch1);
let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1);

View File

@@ -7,7 +7,7 @@ use sampling::source::Source;
use crate::{
TakeGLWEPt,
encryption::glwe_encrypt_sk_internal,
layouts::{GGSWCiphertext, Infos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretExec},
layouts::{GGSWCiphertext, Infos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretPrepared},
};
use crate::trait_families::GGSWEncryptSkFamily;
@@ -26,7 +26,7 @@ impl<DataSelf: DataMut> GGSWCiphertextCompressed<DataSelf> {
&mut self,
module: &Module<B>,
pt: &ScalarZnx<DataPt>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
seed_xa: [u8; 32],
source_xe: &mut Source,
sigma: f64,

View File

@@ -6,7 +6,7 @@ use sampling::source::Source;
use crate::{
encryption::glwe_ct::glwe_encrypt_sk_internal,
layouts::{GLWECiphertext, GLWEPlaintext, Infos, compressed::GLWECiphertextCompressed, prepared::GLWESecretExec},
layouts::{GLWECiphertext, GLWEPlaintext, Infos, compressed::GLWECiphertextCompressed, prepared::GLWESecretPrepared},
};
use crate::trait_families::GLWEEncryptSkFamily;
@@ -25,7 +25,7 @@ impl<D: DataMut> GLWECiphertextCompressed<D> {
&mut self,
module: &Module<B>,
pt: &GLWEPlaintext<DataPt>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
seed_xa: [u8; 32],
source_xe: &mut Source,
sigma: f64,
@@ -49,7 +49,7 @@ impl<D: DataMut> GLWECiphertextCompressed<D> {
&mut self,
module: &Module<B>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
seed_xa: [u8; 32],
source_xe: &mut Source,
sigma: f64,

View File

@@ -8,16 +8,16 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecret, TakeGLWESecretExec,
layouts::{GGLWEAutomorphismKey, GLWESecret, GGLWESwitchingKey, Infos},
TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, GLWESecret, Infos},
};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWEAutomorphismKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank, rank) + GLWESecret::bytes_of(n, rank)
}
@@ -42,8 +42,8 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
+ VecZnxAutomorphism
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx,
+ GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{
#[cfg(debug_assertions)]
{

View File

@@ -9,7 +9,7 @@ use sampling::source::Source;
use crate::{
TakeGLWEPt,
layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec},
layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
};
use crate::trait_families::GGLWEEncryptSkFamily;
@@ -33,7 +33,7 @@ impl<DataSelf: DataMut> GGLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
pt: &ScalarZnx<DataPt>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,

View File

@@ -5,11 +5,11 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecretExec,
layouts::{GGLWECiphertext, GLWESecret, GGLWESwitchingKey, Infos, prepared::GLWESecretExec},
TakeGLWESecretPrepared,
layouts::{GGLWECiphertext, GGLWESwitchingKey, GLWESecret, Infos, prepared::GLWESecretPrepared},
};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(
@@ -21,11 +21,11 @@ impl GGLWESwitchingKey<Vec<u8>> {
rank_out: usize,
) -> usize
where
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
(GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1))
+ ScalarZnx::alloc_bytes(n, rank_in)
+ GLWESecretExec::bytes_of(module, n, rank_out)
+ GLWESecretPrepared::bytes_of(module, n, rank_out)
}
pub fn encrypt_pk_scratch_space<B: Backend>(
@@ -52,8 +52,9 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
scratch: &mut Scratch<B>,
) where
Module<B>:
GGLWESwitchingKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + ScratchAvailable + TakeVecZnx,
GGLWESwitchingKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily<B>,
Scratch<B>:
ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + ScratchAvailable + TakeVecZnx,
{
#[cfg(debug_assertions)]
{
@@ -94,7 +95,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
);
});
let (mut sk_out_tmp, scratch2) = scratch1.take_glwe_secret_exec(n, sk_out.rank());
let (mut sk_out_tmp, scratch2) = scratch1.take_glwe_secret_prepared(n, sk_out.rank());
{
let (mut tmp, _) = scratch2.take_scalar_znx(n, 1);
(0..sk_out.rank()).for_each(|i| {

View File

@@ -8,19 +8,22 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecret, TakeGLWESecretExec,
layouts::{GGLWETensorKey, GLWESecret, GGLWESwitchingKey, Infos, prepared::GLWESecretExec},
TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{
GGLWESwitchingKey, GGLWETensorKey, GLWESecret, Infos,
prepared::{GLWESecretPrepared, Prepare},
},
trait_families::GLWEDecryptFamily,
};
use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GGLWETensorKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> usize
where
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
GLWESecretExec::bytes_of(module, n, rank)
GLWESecretPrepared::bytes_of(module, n, rank)
+ module.vec_znx_dft_alloc_bytes(n, rank, 1)
+ module.vec_znx_big_alloc_bytes(n, 1, 1)
+ module.vec_znx_dft_alloc_bytes(n, 1, 1)
@@ -39,8 +42,10 @@ impl<DataSelf: DataMut> GGLWETensorKey<DataSelf> {
sigma: f64,
scratch: &mut Scratch<B>,
) where
Module<B>: GGLWETensorKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecretExec<B> + TakeScalarZnx + TakeVecZnx,
Module<B>:
GGLWETensorKeyEncryptSkFamily<B> + VecZnxSwithcDegree + VecZnxAddScalarInplace + GLWESecretPreparedModuleFamily<B>,
Scratch<B>:
ScratchAvailable + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeGLWESecretPrepared<B> + TakeScalarZnx + TakeVecZnx,
{
#[cfg(debug_assertions)]
{
@@ -52,8 +57,8 @@ impl<DataSelf: DataMut> GGLWETensorKey<DataSelf> {
let rank: usize = self.rank();
let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_exec(n, rank);
sk_dft_prep.prepare(module, &sk);
let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_prepared(n, rank);
sk_dft_prep.prepare(module, &sk, scratch1);
let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1);

View File

@@ -6,7 +6,7 @@ use sampling::source::Source;
use crate::{
TakeGLWEPt,
layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GLWESecretExec},
layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GLWESecretPrepared},
};
use crate::trait_families::GLWEEncryptSkFamily;
@@ -31,7 +31,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self,
module: &Module<B>,
pt: &ScalarZnx<DataPt>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,

View File

@@ -13,7 +13,7 @@ use crate::{
dist::Distribution,
layouts::{
GLWECiphertext, GLWEPlaintext, Infos,
prepared::{GLWEPublicKeyExec, GLWESecretExec},
prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared},
},
trait_families::{GLWEEncryptPkFamily, GLWEEncryptSkFamily},
};
@@ -42,7 +42,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
pt: &GLWEPlaintext<DataPt>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -78,7 +78,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn encrypt_zero_sk<DataSk: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -113,7 +113,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -143,7 +143,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
pt: &GLWEPlaintext<DataPt>,
pk: &GLWEPublicKeyExec<DataPk, B>,
pk: &GLWEPublicKeyPrepared<DataPk, B>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -166,7 +166,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn encrypt_zero_pk<DataPk: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
pk: &GLWEPublicKeyExec<DataPk, B>,
pk: &GLWEPublicKeyPrepared<DataPk, B>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -190,7 +190,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
pk: &GLWEPublicKeyExec<DataPk, B>,
pk: &GLWEPublicKeyPrepared<DataPk, B>,
source_xu: &mut Source,
source_xe: &mut Source,
sigma: f64,
@@ -283,7 +283,7 @@ pub(crate) fn glwe_encrypt_sk_internal<DataCt: DataMut, DataPt: DataRef, DataSk:
cols: usize,
compressed: bool,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,

View File

@@ -7,7 +7,7 @@ use sampling::source::Source;
use crate::{
dist::Distribution,
layouts::{GLWECiphertext, GLWEPublicKey, Infos, prepared::GLWESecretExec},
layouts::{GLWECiphertext, GLWEPublicKey, Infos, prepared::GLWESecretPrepared},
};
use crate::trait_families::GLWEEncryptSkFamily;
@@ -16,7 +16,7 @@ impl<D: DataMut> GLWEPublicKey<D> {
pub fn generate_from_sk<S: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
sk: &GLWESecretExec<S, B>,
sk: &GLWESecretPrepared<S, B>,
source_xa: &mut Source,
source_xe: &mut Source,
sigma: f64,

View File

@@ -8,18 +8,18 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecret, TakeGLWESecretExec,
layouts::{GLWESecret, GGLWESwitchingKey, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretExec},
TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWESwitchingKey, GLWESecret, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretPrepared},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl GLWEToLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize
where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
GLWESecretExec::bytes_of(module, n, rank_in)
GLWESecretPrepared::bytes_of(module, n, rank_in)
+ (GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in, 1) | GLWESecret::bytes_of(n, rank_in))
}
}
@@ -41,8 +41,8 @@ impl<D: DataMut> GLWEToLWESwitchingKey<D> {
+ VecZnxAutomorphismInplace
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx,
+ GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{
#[cfg(debug_assertions)]
{

View File

@@ -8,19 +8,19 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecret, TakeGLWESecretExec,
layouts::{GLWESecret, GGLWESwitchingKey, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretExec},
TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWESwitchingKey, GLWESecret, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretPrepared},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl LWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize
where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
GLWESecret::bytes_of(n, 1)
+ GLWESecretExec::bytes_of(module, n, 1)
+ GLWESecretPrepared::bytes_of(module, n, 1)
+ GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, 1)
}
}
@@ -42,8 +42,8 @@ impl<D: DataMut> LWESwitchingKey<D> {
+ VecZnxAutomorphismInplace
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx,
+ GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{
#[cfg(debug_assertions)]
{

View File

@@ -8,16 +8,16 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
TakeGLWESecret, TakeGLWESecretExec,
layouts::{GLWESecret, GGLWESwitchingKey, LWESecret, LWEToGLWESwitchingKey},
TakeGLWESecret, TakeGLWESecretPrepared,
layouts::{GGLWESwitchingKey, GLWESecret, LWESecret, LWEToGLWESwitchingKey},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
impl LWEToGLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize
where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, rank_out) + GLWESecret::bytes_of(n, 1)
}
@@ -40,8 +40,8 @@ impl<D: DataMut> LWEToGLWESwitchingKey<D> {
+ VecZnxAutomorphismInplace
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ GLWESecretExecModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretExec<B> + TakeVecZnx,
+ GLWESecretPreparedModuleFamily<B>,
Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeVecZnxDft<B> + TakeGLWESecretPrepared<B> + TakeVecZnx,
{
#[cfg(debug_assertions)]
{

View File

@@ -4,7 +4,7 @@ use backend::hal::{
};
use crate::{
layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, prepared::GGSWCiphertextExec},
layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, prepared::GGSWCiphertextPrepared},
trait_families::GLWEExternalProductFamily,
};
@@ -46,7 +46,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>,
rhs: &GGSWCiphertextExec<DataRhs, B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductFamily<B>,
@@ -58,7 +58,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGSWCiphertextExec<DataRhs, B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductFamily<B>,

View File

@@ -4,7 +4,7 @@ use backend::hal::{
};
use crate::{
layouts::{GLWECiphertext, GGLWESwitchingKey, Infos, prepared::GGSWCiphertextExec},
layouts::{GGLWESwitchingKey, GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared},
trait_families::GLWEExternalProductFamily,
};
@@ -46,7 +46,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGLWESwitchingKey<DataLhs>,
rhs: &GGSWCiphertextExec<DataRhs, B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductFamily<B>,
@@ -94,7 +94,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGSWCiphertextExec<DataRhs, B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductFamily<B>,

View File

@@ -4,7 +4,7 @@ use backend::hal::{
};
use crate::{
layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GGSWCiphertextExec},
layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared},
trait_families::GLWEExternalProductFamily,
};
@@ -46,7 +46,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>,
rhs: &GGSWCiphertextExec<DataRhs, B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductFamily<B>,
@@ -105,7 +105,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGSWCiphertextExec<DataRhs, B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductFamily<B>,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
};
use crate::{
layouts::{GLWECiphertext, Infos, prepared::GGSWCiphertextExec},
layouts::{GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared},
trait_families::GLWEExternalProductFamily,
};
@@ -64,7 +64,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GGSWCiphertextExec<DataRhs, B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductFamily<B>,
@@ -141,7 +141,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn external_product_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGSWCiphertextExec<DataRhs, B>,
rhs: &GGSWCiphertextPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductFamily<B>,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
use crate::{
GLWEOperations, TakeGLWECt,
layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyExec},
layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared},
};
use crate::trait_families::{GLWEKeyswitchFamily, GLWEPackingFamily};
@@ -115,7 +115,7 @@ impl GLWEPacker {
&mut self,
module: &Module<B>,
a: Option<&GLWECiphertext<DataA>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEPackingFamily<B>,
@@ -174,7 +174,7 @@ fn pack_core<D: DataRef, DataAK: DataRef, B: Backend>(
a: Option<&GLWECiphertext<D>>,
accumulators: &mut [Accumulator],
i: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEPackingFamily<B>,
@@ -252,7 +252,7 @@ fn combine<D: DataRef, DataAK: DataRef, B: Backend>(
acc: &mut Accumulator,
b: Option<&GLWECiphertext<D>>,
i: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEPackingFamily<B>,

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use backend::hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
use crate::{
layouts::{GLWECiphertext, prepared::GGLWEAutomorphismKeyExec},
layouts::{GLWECiphertext, prepared::GGLWEAutomorphismKeyPrepared},
operations::GLWEOperations,
};
@@ -61,7 +61,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
start: usize,
end: usize,
lhs: &GLWECiphertext<DataLhs>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWETraceModuleFamily<B>,
@@ -76,7 +76,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
module: &Module<B>,
start: usize,
end: usize,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyExec<DataAK, B>>,
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWETraceModuleFamily<B>,

View File

@@ -5,8 +5,8 @@ use backend::hal::{
use crate::{
layouts::{
GGLWEAutomorphismKey, GLWECiphertext, GGLWESwitchingKey, Infos,
prepared::{GGLWEAutomorphismKeyExec, GGLWESwitchingKeyExec},
GGLWEAutomorphismKey, GGLWESwitchingKey, GLWECiphertext, Infos,
prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared},
},
trait_families::GLWEKeyswitchFamily,
};
@@ -49,7 +49,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGLWEAutomorphismKey<DataLhs>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B>,
@@ -61,7 +61,7 @@ impl<DataSelf: DataMut> GGLWEAutomorphismKey<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGLWEAutomorphismKeyExec<DataRhs, B>,
rhs: &GGLWEAutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B>,
@@ -112,7 +112,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGLWESwitchingKey<DataLhs>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B>,
@@ -160,7 +160,7 @@ impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B>,

View File

@@ -9,7 +9,7 @@ use backend::hal::{
use crate::{
layouts::{
GGLWECiphertext, GGSWCiphertext, GLWECiphertext, Infos,
prepared::{GGLWESwitchingKeyExec, GGLWETensorKeyExec},
prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared},
},
operations::GLWEOperations,
trait_families::{GGSWKeySwitchFamily, GLWEKeyswitchFamily},
@@ -97,7 +97,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self,
module: &Module<B>,
a: &GGLWECiphertext<DataA>,
tsk: &GGLWETensorKeyExec<DataTsk, B>,
tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>,
) where
DataA: DataRef,
@@ -123,8 +123,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>,
ksk: &GGLWESwitchingKeyExec<DataKsk, B>,
tsk: &GGLWETensorKeyExec<DataTsk, B>,
ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes,
@@ -137,8 +137,8 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn keyswitch_inplace<DataKsk: DataRef, DataTsk: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
ksk: &GGLWESwitchingKeyExec<DataKsk, B>,
tsk: &GGLWETensorKeyExec<DataTsk, B>,
ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes,
@@ -153,7 +153,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
pub fn expand_row<DataTsk: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
tsk: &GGLWETensorKeyExec<DataTsk, B>,
tsk: &GGLWETensorKeyPrepared<DataTsk, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes,
@@ -278,7 +278,7 @@ impl<DataSelf: DataMut> GGSWCiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GGSWCiphertext<DataLhs>,
ksk: &GGLWESwitchingKeyExec<DataKsk, B>,
ksk: &GGLWESwitchingKeyPrepared<DataKsk, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B> + GGSWKeySwitchFamily<B> + VecZnxNormalizeTmpBytes,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
};
use crate::{
layouts::{GLWECiphertext, Infos, prepared::GGLWESwitchingKeyExec},
layouts::{GLWECiphertext, Infos, prepared::GGLWESwitchingKeyPrepared},
trait_families::GLWEKeyswitchFamily,
};
@@ -65,7 +65,7 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
&self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &Scratch<B>,
) where
DataLhs: DataRef,
@@ -136,7 +136,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B>,
@@ -156,7 +156,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
rhs: &GGLWESwitchingKeyExec<DataRhs, B>,
rhs: &GGLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEKeyswitchFamily<B>,
@@ -174,7 +174,7 @@ impl<D: DataRef> GLWECiphertext<D> {
&self,
module: &Module<B>,
res_dft: VecZnxDft<DataRes, B>,
rhs: &GGLWESwitchingKeyExec<DataKey, B>,
rhs: &GGLWESwitchingKeyPrepared<DataKey, B>,
scratch: &mut Scratch<B>,
) -> VecZnxBig<DataRes, B>
where

View File

@@ -5,7 +5,7 @@ use backend::hal::{
use crate::{
TakeGLWECt,
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWESwitchingKeyExec},
layouts::{GLWECiphertext, Infos, LWECiphertext, prepared::LWESwitchingKeyPrepared},
};
use crate::trait_families::GLWEKeyswitchFamily;
@@ -32,7 +32,7 @@ impl<DLwe: DataMut> LWECiphertext<DLwe> {
&mut self,
module: &Module<B>,
a: &LWECiphertext<A>,
ksk: &LWESwitchingKeyExec<DKs, B>,
ksk: &LWESwitchingKeyPrepared<DKs, B>,
scratch: &mut Scratch<B>,
) where
A: DataRef,

View File

@@ -7,7 +7,7 @@ use backend::hal::{
use crate::layouts::{GLWEToLWESwitchingKey, Infos, compressed::GGLWESwitchingKeyCompressed};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
#[derive(PartialEq, Eq, Clone)]
pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
@@ -91,7 +91,7 @@ impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize
where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in)
}

View File

@@ -6,7 +6,7 @@ use backend::hal::{
use crate::layouts::{Infos, LWESwitchingKey, compressed::GGLWESwitchingKeyCompressed};
use std::fmt;
use crate::trait_families::{Decompress, GGLWEEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{Decompress, GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
#[derive(PartialEq, Eq, Clone)]
pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
@@ -90,7 +90,7 @@ impl LWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize) -> usize
where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
LWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k)
}

View File

@@ -9,7 +9,7 @@ use crate::{
};
use std::fmt;
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
#[derive(PartialEq, Eq, Clone)]
pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
@@ -93,7 +93,7 @@ impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize
where
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretExecModuleFamily<B>,
Module<B>: GGLWEEncryptSkFamily<B> + GLWESecretPreparedModuleFamily<B>,
{
LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_out)
}

View File

@@ -3,7 +3,7 @@ use backend::hal::{
layouts::{Data, DataMut, DataRef, MatZnx, ReaderFrom, WriterTo},
};
use crate::layouts::{GLWECiphertext, GGLWESwitchingKey, Infos};
use crate::layouts::{GGLWESwitchingKey, GLWECiphertext, Infos};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;

View File

@@ -1,3 +1,5 @@
use std::fmt;
use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef};
use crate::layouts::{GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, Infos, SetMetaData};
@@ -8,6 +10,18 @@ pub struct GLWEPlaintext<D: Data> {
pub k: usize,
}
impl<D: DataRef> fmt::Display for GLWEPlaintext<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"GLWEPlaintext: basek={} k={}: {}",
self.basek(),
self.k(),
self.data
)
}
}
impl<D: Data> Infos for GLWEPlaintext<D> {
type Inner = VecZnx<D>;

View File

@@ -13,6 +13,18 @@ pub struct LWECiphertext<D: Data> {
pub(crate) basek: usize,
}
impl<D: DataRef> LWECiphertext<D> {
pub fn data(&self) -> &VecZnx<D> {
&self.data
}
}
impl<D: DataMut> LWECiphertext<D> {
pub fn data_mut(&mut self) -> &VecZnx<D> {
&mut self.data
}
}
impl<D: DataRef> fmt::Debug for LWECiphertext<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
@@ -31,10 +43,7 @@ impl<D: DataRef> fmt::Display for LWECiphertext<D> {
}
}
impl<D: DataMut> Reset for LWECiphertext<D>
where
VecZnx<D>: Reset,
{
impl<D: DataMut> Reset for LWECiphertext<D> {
fn reset(&mut self) {
self.data.reset();
self.basek = 0;

View File

@@ -1,3 +1,5 @@
use std::fmt;
use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef};
use crate::layouts::{Infos, SetMetaData};
@@ -18,6 +20,18 @@ impl LWEPlaintext<Vec<u8>> {
}
}
impl<D: DataRef> fmt::Display for LWEPlaintext<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"LWEPlaintext: basek={} k={}: {}",
self.basek(),
self.k(),
self.data
)
}
}
impl<D: Data> Infos for LWEPlaintext<D> {
type Inner = VecZnx<D>;

View File

@@ -1,6 +1,6 @@
use backend::hal::{
api::{ZnxInfos, ZnxZero},
layouts::{Data, DataMut, ScalarZnx},
api::{ZnxInfos, ZnxView, ZnxZero},
layouts::{Data, DataMut, DataRef, ScalarZnx},
};
use sampling::source::Source;
@@ -20,6 +20,20 @@ impl LWESecret<Vec<u8>> {
}
}
impl<D: DataRef> LWESecret<D> {
pub fn raw(&self) -> &[i64] {
self.data.at(0, 0)
}
pub fn dist(&self) -> Distribution {
self.dist
}
pub fn data(&self) -> &ScalarZnx<D> {
&self.data
}
}
impl<D: Data> LWESecret<D> {
pub fn n(&self) -> usize {
self.data.n()

View File

@@ -3,21 +3,24 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{GGLWEAutomorphismKey, Infos, prepared::GGLWESwitchingKeyExec};
use crate::layouts::{
GGLWEAutomorphismKey, Infos,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct GGLWEAutomorphismKeyExec<D: Data, B: Backend> {
pub(crate) key: GGLWESwitchingKeyExec<D, B>,
pub struct GGLWEAutomorphismKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWESwitchingKeyPrepared<D, B>,
pub(crate) p: i64,
}
impl<B: Backend> GGLWEAutomorphismKeyExec<Vec<u8>, B> {
impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
where
Module<B>: VmpPMatAlloc<B>,
{
GGLWEAutomorphismKeyExec::<Vec<u8>, B> {
key: GGLWESwitchingKeyExec::alloc(module, n, basek, k, rows, digits, rank, rank),
GGLWEAutomorphismKeyPrepared::<Vec<u8>, B> {
key: GGLWESwitchingKeyPrepared::alloc(module, n, basek, k, rows, digits, rank, rank),
p: 0,
}
}
@@ -26,28 +29,11 @@ impl<B: Backend> GGLWEAutomorphismKeyExec<Vec<u8>, B> {
where
Module<B>: VmpPMatAllocBytes,
{
GGLWESwitchingKeyExec::bytes_of(module, n, basek, k, rows, digits, rank, rank)
}
pub fn from<DataOther: DataRef>(module: &Module<B>, other: &GGLWEAutomorphismKey<DataOther>, scratch: &mut Scratch<B>) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut atk_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank(),
);
atk_exec.prepare(module, other, scratch);
atk_exec
GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, rank, rank)
}
}
impl<D: Data, B: Backend> Infos for GGLWEAutomorphismKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for GGLWEAutomorphismKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -63,7 +49,7 @@ impl<D: Data, B: Backend> Infos for GGLWEAutomorphismKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> GGLWEAutomorphismKeyExec<D, B> {
impl<D: Data, B: Backend> GGLWEAutomorphismKeyPrepared<D, B> {
pub fn p(&self) -> i64 {
self.p
}
@@ -85,13 +71,31 @@ impl<D: Data, B: Backend> GGLWEAutomorphismKeyExec<D, B> {
}
}
impl<D: DataMut, B: Backend> GGLWEAutomorphismKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWEAutomorphismKey<DR>> for GGLWEAutomorphismKeyPrepared<D, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DR>, scratch: &mut Scratch<B>) {
self.key.prepare(module, &other.key, scratch);
self.p = other.p;
}
}
}
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> for GGLWEAutomorphismKey<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
let mut atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = GGLWEAutomorphismKeyPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank(),
);
atk_prepared.prepare(module, self, scratch);
atk_prepared
}
}

View File

@@ -3,17 +3,20 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{GGLWECiphertext, Infos};
use crate::layouts::{
GGLWECiphertext, Infos,
prepared::{Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct GGLWECiphertextExec<D: Data, B: Backend> {
pub struct GGLWECiphertextPrepared<D: Data, B: Backend> {
pub(crate) data: VmpPMat<D, B>,
pub(crate) basek: usize,
pub(crate) k: usize,
pub(crate) digits: usize,
}
impl<B: Backend> GGLWECiphertextExec<Vec<u8>, B> {
impl<B: Backend> GGLWECiphertextPrepared<Vec<u8>, B> {
pub fn alloc(
module: &Module<B>,
n: usize,
@@ -84,7 +87,7 @@ impl<B: Backend> GGLWECiphertextExec<Vec<u8>, B> {
}
}
impl<D: Data, B: Backend> Infos for GGLWECiphertextExec<D, B> {
impl<D: Data, B: Backend> Infos for GGLWECiphertextPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -100,7 +103,7 @@ impl<D: Data, B: Backend> Infos for GGLWECiphertextExec<D, B> {
}
}
impl<D: Data, B: Backend> GGLWECiphertextExec<D, B> {
impl<D: Data, B: Backend> GGLWECiphertextPrepared<D, B> {
pub fn rank(&self) -> usize {
self.data.cols_out() - 1
}
@@ -118,15 +121,34 @@ impl<D: Data, B: Backend> GGLWECiphertextExec<D, B> {
}
}
impl<D: DataMut, B: Backend> GGLWECiphertextExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWECiphertext<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWECiphertext<DR>> for GGLWECiphertextPrepared<D, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GGLWECiphertext<DR>, scratch: &mut Scratch<B>) {
module.vmp_prepare(&mut self.data, &other.data, scratch);
self.basek = other.basek;
self.k = other.k;
self.digits = other.digits;
}
}
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWECiphertextPrepared<Vec<u8>, B>> for GGLWECiphertext<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWECiphertextPrepared<Vec<u8>, B> {
let mut atk_prepared: GGLWECiphertextPrepared<Vec<u8>, B> = GGLWECiphertextPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank_in(),
self.rank_out(),
);
atk_prepared.prepare(module, self, scratch);
atk_prepared
}
}

View File

@@ -3,16 +3,19 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{GGLWESwitchingKey, Infos, prepared::GGLWECiphertextExec};
use crate::layouts::{
GGLWESwitchingKey, Infos,
prepared::{GGLWECiphertextPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct GGLWESwitchingKeyExec<D: Data, B: Backend> {
pub(crate) key: GGLWECiphertextExec<D, B>,
pub struct GGLWESwitchingKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWECiphertextPrepared<D, B>,
pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out
}
impl<B: Backend> GGLWESwitchingKeyExec<Vec<u8>, B> {
impl<B: Backend> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc(
module: &Module<B>,
n: usize,
@@ -26,8 +29,8 @@ impl<B: Backend> GGLWESwitchingKeyExec<Vec<u8>, B> {
where
Module<B>: VmpPMatAlloc<B>,
{
GGLWESwitchingKeyExec::<Vec<u8>, B> {
key: GGLWECiphertextExec::alloc(module, n, basek, k, rows, digits, rank_in, rank_out),
GGLWESwitchingKeyPrepared::<Vec<u8>, B> {
key: GGLWECiphertextPrepared::alloc(module, n, basek, k, rows, digits, rank_in, rank_out),
sk_in_n: 0,
sk_out_n: 0,
}
@@ -46,29 +49,11 @@ impl<B: Backend> GGLWESwitchingKeyExec<Vec<u8>, B> {
where
Module<B>: VmpPMatAllocBytes,
{
GGLWECiphertextExec::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out)
}
pub fn from<DataOther: DataRef>(module: &Module<B>, other: &GGLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank_in(),
other.rank_out(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
GGLWECiphertextPrepared::bytes_of(module, n, basek, k, rows, digits, rank_in, rank_out)
}
}
impl<D: Data, B: Backend> Infos for GGLWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for GGLWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -84,7 +69,7 @@ impl<D: Data, B: Backend> Infos for GGLWESwitchingKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> GGLWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> GGLWESwitchingKeyPrepared<D, B> {
pub fn rank(&self) -> usize {
self.key.data.cols_out() - 1
}
@@ -110,14 +95,33 @@ impl<D: Data, B: Backend> GGLWESwitchingKeyExec<D, B> {
}
}
impl<D: DataMut, B: Backend> GGLWESwitchingKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWESwitchingKey<DR>> for GGLWESwitchingKeyPrepared<D, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GGLWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.key.prepare(module, &other.key, scratch);
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>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWESwitchingKeyPrepared<Vec<u8>, B> {
let mut atk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = GGLWESwitchingKeyPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank_in(),
self.rank_out(),
);
atk_prepared.prepare(module, self, scratch);
atk_prepared
}
}

View File

@@ -3,22 +3,25 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{GGLWETensorKey, Infos, prepared::GGLWESwitchingKeyExec};
use crate::layouts::{
GGLWETensorKey, Infos,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct GGLWETensorKeyExec<D: Data, B: Backend> {
pub(crate) keys: Vec<GGLWESwitchingKeyExec<D, B>>,
pub struct GGLWETensorKeyPrepared<D: Data, B: Backend> {
pub(crate) keys: Vec<GGLWESwitchingKeyPrepared<D, B>>,
}
impl<B: Backend> GGLWETensorKeyExec<Vec<u8>, B> {
impl<B: Backend> GGLWETensorKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
where
Module<B>: VmpPMatAlloc<B>,
{
let mut keys: Vec<GGLWESwitchingKeyExec<Vec<u8>, B>> = Vec::new();
let mut keys: Vec<GGLWESwitchingKeyPrepared<Vec<u8>, B>> = Vec::new();
let pairs: usize = (((rank + 1) * rank) >> 1).max(1);
(0..pairs).for_each(|_| {
keys.push(GGLWESwitchingKeyExec::alloc(
keys.push(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, digits, 1, rank,
));
});
@@ -30,32 +33,11 @@ impl<B: Backend> GGLWETensorKeyExec<Vec<u8>, B> {
Module<B>: VmpPMatAllocBytes,
{
let pairs: usize = (((rank + 1) * rank) >> 1).max(1);
pairs * GGLWESwitchingKeyExec::bytes_of(module, n, basek, k, rows, digits, 1, rank)
}
pub fn from<D: DataRef>(
module: &Module<B>,
other: &GGLWETensorKey<D>,
scratch: &mut Scratch<B>,
) -> GGLWETensorKeyExec<Vec<u8>, B>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank(),
);
tsk_exec.prepare(module, other, scratch);
tsk_exec
pairs * GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, 1, rank)
}
}
impl<D: Data, B: Backend> Infos for GGLWETensorKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for GGLWETensorKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -71,7 +53,7 @@ impl<D: Data, B: Backend> Infos for GGLWETensorKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> GGLWETensorKeyExec<D, B> {
impl<D: Data, B: Backend> GGLWETensorKeyPrepared<D, B> {
pub fn rank(&self) -> usize {
self.keys[0].rank()
}
@@ -89,9 +71,9 @@ impl<D: Data, B: Backend> GGLWETensorKeyExec<D, B> {
}
}
impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
impl<D: DataMut, B: Backend> GGLWETensorKeyPrepared<D, B> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyExec<D, B> {
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared<D, B> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -100,9 +82,9 @@ impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
}
}
impl<D: DataRef, B: Backend> GGLWETensorKeyExec<D, B> {
impl<D: DataRef, B: Backend> GGLWETensorKeyPrepared<D, B> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyExec<D, B> {
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared<D, B> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -111,12 +93,11 @@ impl<D: DataRef, B: Backend> GGLWETensorKeyExec<D, B> {
}
}
impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWETensorKey<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWETensorKey<DR>> for GGLWETensorKeyPrepared<D, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GGLWETensorKey<DR>, scratch: &mut Scratch<B>) {
#[cfg(debug_assertions)]
{
assert_eq!(self.keys.len(), other.keys.len());
@@ -129,3 +110,22 @@ impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
});
}
}
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWETensorKeyPrepared<Vec<u8>, B>> for GGLWETensorKey<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWETensorKeyPrepared<Vec<u8>, B> {
let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = GGLWETensorKeyPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank(),
);
tsk_prepared.prepare(module, self, scratch);
tsk_prepared
}
}

View File

@@ -3,17 +3,20 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{GGSWCiphertext, Infos};
use crate::layouts::{
GGSWCiphertext, Infos,
prepared::{Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct GGSWCiphertextExec<D: Data, B: Backend> {
pub struct GGSWCiphertextPrepared<D: Data, B: Backend> {
pub(crate) data: VmpPMat<D, B>,
pub(crate) basek: usize,
pub(crate) k: usize,
pub(crate) digits: usize,
}
impl<B: Backend> GGSWCiphertextExec<Vec<u8>, B> {
impl<B: Backend> GGSWCiphertextPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
where
Module<B>: VmpPMatAlloc<B>,
@@ -66,30 +69,9 @@ impl<B: Backend> GGSWCiphertextExec<Vec<u8>, B> {
module.vmp_pmat_alloc_bytes(n, rows, rank + 1, rank + 1, size)
}
pub fn from<DataOther: DataRef>(
module: &Module<B>,
other: &GGSWCiphertext<DataOther>,
scratch: &mut Scratch<B>,
) -> GGSWCiphertextExec<Vec<u8>, B>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ggsw_exec: GGSWCiphertextExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank(),
);
ggsw_exec.prepare(module, other, scratch);
ggsw_exec
}
}
impl<D: Data, B: Backend> Infos for GGSWCiphertextExec<D, B> {
impl<D: Data, B: Backend> Infos for GGSWCiphertextPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -105,7 +87,7 @@ impl<D: Data, B: Backend> Infos for GGSWCiphertextExec<D, B> {
}
}
impl<D: Data, B: Backend> GGSWCiphertextExec<D, B> {
impl<D: Data, B: Backend> GGSWCiphertextPrepared<D, B> {
pub fn rank(&self) -> usize {
self.data.cols_out() - 1
}
@@ -115,15 +97,39 @@ impl<D: Data, B: Backend> GGSWCiphertextExec<D, B> {
}
}
impl<DataSelf: DataMut, B: Backend> GGSWCiphertextExec<DataSelf, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGSWCiphertext<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataRef, B: Backend> GGSWCiphertextPrepared<D, B> {
pub fn data(&self) -> &VmpPMat<D, B> {
&self.data
}
}
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGSWCiphertext<DR>> for GGSWCiphertextPrepared<D, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GGSWCiphertext<DR>, scratch: &mut Scratch<B>) {
module.vmp_prepare(&mut self.data, &other.data, scratch);
self.k = other.k;
self.basek = other.basek;
self.digits = other.digits;
}
}
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGSWCiphertextPrepared<Vec<u8>, B>> for GGSWCiphertext<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGSWCiphertextPrepared<Vec<u8>, B> {
let mut ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = GGSWCiphertextPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank(),
);
ggsw_prepared.prepare(module, self, scratch);
ggsw_prepared
}
}

View File

@@ -5,18 +5,21 @@ use backend::hal::{
use crate::{
dist::Distribution,
layouts::{GLWEPublicKey, Infos},
layouts::{
GLWEPublicKey, Infos,
prepared::{Prepare, PrepareAlloc},
},
};
#[derive(PartialEq, Eq)]
pub struct GLWEPublicKeyExec<D: Data, B: Backend> {
pub struct GLWEPublicKeyPrepared<D: Data, B: Backend> {
pub(crate) data: VecZnxDft<D, B>,
pub(crate) basek: usize,
pub(crate) k: usize,
pub(crate) dist: Distribution,
}
impl<D: Data, B: Backend> Infos for GLWEPublicKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for GLWEPublicKeyPrepared<D, B> {
type Inner = VecZnxDft<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -32,13 +35,13 @@ impl<D: Data, B: Backend> Infos for GLWEPublicKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> GLWEPublicKeyExec<D, B> {
impl<D: Data, B: Backend> GLWEPublicKeyPrepared<D, B> {
pub fn rank(&self) -> usize {
self.cols() - 1
}
}
impl<B: Backend> GLWEPublicKeyExec<Vec<u8>, B> {
impl<B: Backend> GLWEPublicKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rank: usize) -> Self
where
Module<B>: VecZnxDftAlloc<B>,
@@ -57,25 +60,25 @@ impl<B: Backend> GLWEPublicKeyExec<Vec<u8>, B> {
{
module.vec_znx_dft_alloc_bytes(n, rank + 1, k.div_ceil(basek))
}
}
pub fn from<DataOther>(module: &Module<B>, other: &GLWEPublicKey<DataOther>, scratch: &mut Scratch<B>) -> Self
where
DataOther: DataRef,
Module<B>: VecZnxDftAlloc<B> + VecZnxDftFromVecZnx<B>,
{
let mut pk_exec: GLWEPublicKeyExec<Vec<u8>, B> =
GLWEPublicKeyExec::alloc(module, other.n(), other.basek(), other.k(), other.rank());
pk_exec.prepare(module, other, scratch);
pk_exec
impl<D: DataRef, B: Backend> PrepareAlloc<B, GLWEPublicKeyPrepared<Vec<u8>, B>> for GLWEPublicKey<D>
where
Module<B>: VecZnxDftAlloc<B> + VecZnxDftFromVecZnx<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GLWEPublicKeyPrepared<Vec<u8>, B> {
let mut pk_prepared: GLWEPublicKeyPrepared<Vec<u8>, B> =
GLWEPublicKeyPrepared::alloc(module, self.n(), self.basek(), self.k(), self.rank());
pk_prepared.prepare(module, self, scratch);
pk_prepared
}
}
impl<D: DataMut, B: Backend> GLWEPublicKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GLWEPublicKey<DataOther>, _scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VecZnxDftFromVecZnx<B>,
{
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, GLWEPublicKey<DR>> for GLWEPublicKeyPrepared<DM, B>
where
Module<B>: VecZnxDftFromVecZnx<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GLWEPublicKey<DR>, _scratch: &mut Scratch<B>) {
#[cfg(debug_assertions)]
{
assert_eq!(self.n(), other.n());

View File

@@ -3,17 +3,24 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, SvpPPol},
};
use crate::{dist::Distribution, layouts::GLWESecret, trait_families::GLWESecretExecModuleFamily};
use crate::{
dist::Distribution,
layouts::{
GLWESecret,
prepared::{Prepare, PrepareAlloc},
},
trait_families::GLWESecretPreparedModuleFamily,
};
pub struct GLWESecretExec<D: Data, B: Backend> {
pub struct GLWESecretPrepared<D: Data, B: Backend> {
pub(crate) data: SvpPPol<D, B>,
pub(crate) dist: Distribution,
}
impl<B: Backend> GLWESecretExec<Vec<u8>, B> {
impl<B: Backend> GLWESecretPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, rank: usize) -> Self
where
Module<B>: GLWESecretExecModuleFamily<B>,
Module<B>: GLWESecretPreparedModuleFamily<B>,
{
Self {
data: module.svp_ppol_alloc(n, rank),
@@ -23,25 +30,13 @@ impl<B: Backend> GLWESecretExec<Vec<u8>, B> {
pub fn bytes_of(module: &Module<B>, n: usize, rank: usize) -> usize
where
Module<B>: GLWESecretExecModuleFamily<B>,
Module<B>: GLWESecretPreparedModuleFamily<B>,
{
module.svp_ppol_alloc_bytes(n, rank)
}
}
impl<B: Backend> GLWESecretExec<Vec<u8>, B> {
pub fn from<D>(module: &Module<B>, sk: &GLWESecret<D>) -> Self
where
D: DataRef,
Module<B>: GLWESecretExecModuleFamily<B>,
{
let mut sk_dft: GLWESecretExec<Vec<u8>, B> = Self::alloc(module, sk.n(), sk.rank());
sk_dft.prepare(module, sk);
sk_dft
}
}
impl<D: Data, B: Backend> GLWESecretExec<D, B> {
impl<D: Data, B: Backend> GLWESecretPrepared<D, B> {
pub fn n(&self) -> usize {
self.data.n()
}
@@ -55,15 +50,29 @@ impl<D: Data, B: Backend> GLWESecretExec<D, B> {
}
}
impl<D: DataMut, B: Backend> GLWESecretExec<D, B> {
pub(crate) fn prepare<O>(&mut self, module: &Module<B>, sk: &GLWESecret<O>)
where
O: DataRef,
Module<B>: GLWESecretExecModuleFamily<B>,
{
(0..self.rank()).for_each(|i| {
module.svp_prepare(&mut self.data, i, &sk.data, i);
});
self.dist = sk.dist
impl<D: DataRef, B: Backend> PrepareAlloc<B, GLWESecretPrepared<Vec<u8>, B>> for GLWESecret<D>
where
Module<B>: SvpPrepare<B> + SvpPPolAllocBytes + SvpPPolAlloc<B>,
{
fn prepare_alloc(
&self,
module: &Module<B>,
scratch: &mut backend::hal::layouts::Scratch<B>,
) -> GLWESecretPrepared<Vec<u8>, B> {
let mut sk_dft: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, self.n(), self.rank());
sk_dft.prepare(module, self, scratch);
sk_dft
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, GLWESecret<DR>> for GLWESecretPrepared<DM, B>
where
Module<B>: SvpPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GLWESecret<DR>, _scratch: &mut backend::hal::layouts::Scratch<B>) {
(0..self.rank()).for_each(|i| {
module.svp_prepare(&mut self.data, i, &other.data, i);
});
self.dist = other.dist
}
}

View File

@@ -3,12 +3,15 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{GLWEToLWESwitchingKey, Infos, prepared::GGLWESwitchingKeyExec};
use crate::layouts::{
GLWEToLWESwitchingKey, Infos,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct GLWEToLWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>);
pub struct GLWEToLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> Infos for GLWEToLWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for GLWEToLWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -24,7 +27,7 @@ impl<D: Data, B: Backend> Infos for GLWEToLWESwitchingKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> GLWEToLWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
pub fn digits(&self) -> usize {
self.0.digits()
}
@@ -42,12 +45,12 @@ impl<D: Data, B: Backend> GLWEToLWESwitchingKeyExec<D, B> {
}
}
impl<B: Backend> GLWEToLWESwitchingKeyExec<Vec<u8>, B> {
impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_in: usize) -> Self
where
Module<B>: VmpPMatAlloc<B>,
{
Self(GGLWESwitchingKeyExec::alloc(
Self(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, 1, rank_in, 1,
))
}
@@ -56,36 +59,33 @@ impl<B: Backend> GLWEToLWESwitchingKeyExec<Vec<u8>, B> {
where
Module<B>: VmpPMatAllocBytes,
{
GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, rank_in, 1)
}
pub fn from<DataOther: DataRef>(
module: &Module<B>,
other: &GLWEToLWESwitchingKey<DataOther>,
scratch: &mut Scratch<B>,
) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: GLWEToLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.0.n(),
other.0.basek(),
other.0.k(),
other.0.rows(),
other.0.rank_in(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, rank_in, 1)
}
}
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GLWEToLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataRef, B: Backend> PrepareAlloc<B, GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>> for GLWEToLWESwitchingKey<D>
where
Module<B>: VmpPMatPrepare<B> + VmpPMatAlloc<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = GLWEToLWESwitchingKeyPrepared::alloc(
module,
self.0.n(),
self.0.basek(),
self.0.k(),
self.0.rows(),
self.0.rank_in(),
);
ksk_prepared.prepare(module, self, scratch);
ksk_prepared
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, GLWEToLWESwitchingKey<DR>> for GLWEToLWESwitchingKeyPrepared<DM, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GLWEToLWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.0.prepare(module, &other.0, scratch);
}
}

View File

@@ -3,12 +3,15 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{Infos, LWESwitchingKey, prepared::GGLWESwitchingKeyExec};
use crate::layouts::{
Infos, LWESwitchingKey,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct LWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>);
pub struct LWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> Infos for LWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for LWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -24,7 +27,7 @@ impl<D: Data, B: Backend> Infos for LWESwitchingKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> LWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> LWESwitchingKeyPrepared<D, B> {
pub fn digits(&self) -> usize {
self.0.digits()
}
@@ -42,12 +45,12 @@ impl<D: Data, B: Backend> LWESwitchingKeyExec<D, B> {
}
}
impl<B: Backend> LWESwitchingKeyExec<Vec<u8>, B> {
impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize) -> Self
where
Module<B>: VmpPMatAlloc<B>,
{
Self(GGLWESwitchingKeyExec::alloc(
Self(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, 1, 1, 1,
))
}
@@ -56,31 +59,32 @@ impl<B: Backend> LWESwitchingKeyExec<Vec<u8>, B> {
where
Module<B>: VmpPMatAllocBytes,
{
GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1)
}
pub fn from<DataOther: DataRef>(module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: LWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.0.n(),
other.0.basek(),
other.0.k(),
other.0.rows(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1)
}
}
impl<D: DataMut, B: Backend> LWESwitchingKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataRef, B: Backend> PrepareAlloc<B, LWESwitchingKeyPrepared<Vec<u8>, B>> for LWESwitchingKey<D>
where
Module<B>: VmpPMatPrepare<B> + VmpPMatAlloc<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> LWESwitchingKeyPrepared<Vec<u8>, B> {
let mut ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = LWESwitchingKeyPrepared::alloc(
module,
self.0.n(),
self.0.basek(),
self.0.k(),
self.0.rows(),
);
ksk_prepared.prepare(module, self, scratch);
ksk_prepared
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, LWESwitchingKey<DR>> for LWESwitchingKeyPrepared<DM, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &LWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.0.prepare(module, &other.0, scratch);
}
}

View File

@@ -3,13 +3,16 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{Infos, LWEToGLWESwitchingKey, prepared::GGLWESwitchingKeyExec};
use crate::layouts::{
Infos, LWEToGLWESwitchingKey,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
/// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext].
#[derive(PartialEq, Eq)]
pub struct LWEToGLWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>);
pub struct LWEToGLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> Infos for LWEToGLWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for LWEToGLWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -25,7 +28,7 @@ impl<D: Data, B: Backend> Infos for LWEToGLWESwitchingKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> LWEToGLWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> LWEToGLWESwitchingKeyPrepared<D, B> {
pub fn digits(&self) -> usize {
self.0.digits()
}
@@ -43,12 +46,12 @@ impl<D: Data, B: Backend> LWEToGLWESwitchingKeyExec<D, B> {
}
}
impl<B: Backend> LWEToGLWESwitchingKeyExec<Vec<u8>, B> {
impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self
where
Module<B>: VmpPMatAlloc<B>,
{
Self(GGLWESwitchingKeyExec::alloc(
Self(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, 1, 1, rank_out,
))
}
@@ -57,36 +60,33 @@ impl<B: Backend> LWEToGLWESwitchingKeyExec<Vec<u8>, B> {
where
Module<B>: VmpPMatAllocBytes,
{
GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out)
}
pub fn from<DataOther: DataRef>(
module: &Module<B>,
other: &LWEToGLWESwitchingKey<DataOther>,
scratch: &mut Scratch<B>,
) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: LWEToGLWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.0.n(),
other.0.basek(),
other.0.k(),
other.0.rows(),
other.0.rank(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, rank_out)
}
}
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataRef, B: Backend> PrepareAlloc<B, LWEToGLWESwitchingKeyPrepared<Vec<u8>, B>> for LWEToGLWESwitchingKey<D>
where
Module<B>: VmpPMatPrepare<B> + VmpPMatAlloc<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = LWEToGLWESwitchingKeyPrepared::alloc(
module,
self.0.n(),
self.0.basek(),
self.0.k(),
self.0.rows(),
self.0.rank_out(),
);
ksk_prepared.prepare(module, self, scratch);
ksk_prepared
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, LWEToGLWESwitchingKey<DR>> for LWEToGLWESwitchingKeyPrepared<DM, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.0.prepare(module, &other.0, scratch);
}
}

View File

@@ -9,6 +9,7 @@ mod glwe_to_lwe_ksk;
mod lwe_ksk;
mod lwe_to_glwe_ksk;
use backend::hal::layouts::{Backend, Module, Scratch};
pub use gglwe_atk::*;
pub use gglwe_ct::*;
pub use gglwe_ksk::*;
@@ -19,3 +20,11 @@ pub use glwe_sk::*;
pub use glwe_to_lwe_ksk::*;
pub use lwe_ksk::*;
pub use lwe_to_glwe_ksk::*;
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

@@ -11,10 +11,12 @@ mod keyswitching;
mod noise;
mod operations;
mod scratch;
mod utils;
pub use operations::*;
pub mod layouts;
pub mod trait_families;
pub use dist::*;
pub use glwe_packing::*;
pub use scratch::*;

View File

@@ -1,11 +1,11 @@
use backend::hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxStd, VecZnxSubScalarInplace, ZnxZero},
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxSubScalarInplace, ZnxZero},
layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
};
use crate::{
layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec},
layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
trait_families::GLWEDecryptFamily,
};
@@ -13,13 +13,13 @@ impl<D: DataRef> GGLWECiphertext<D> {
pub fn assert_noise<B: Backend, DataSk, DataWant>(
self,
module: &Module<B>,
sk: &GLWESecretExec<DataSk, B>,
sk: &GLWESecretPrepared<DataSk, B>,
pt_want: &ScalarZnx<DataWant>,
max_noise: f64,
) where
DataSk: DataRef,
DataWant: DataRef,
Module<B>: GLWEDecryptFamily<B> + VecZnxStd + VecZnxSubScalarInplace,
Module<B>: GLWEDecryptFamily<B> + VecZnxSubScalarInplace,
B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{
let digits: usize = self.digits();
@@ -47,7 +47,7 @@ impl<D: DataRef> GGLWECiphertext<D> {
col_i,
);
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2();
let noise_have: f64 = pt.data.std(basek, 0).log2();
assert!(
noise_have <= max_noise,

View File

@@ -1,14 +1,14 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxDftAlloc,
VecZnxDftToVecZnxBigTmpA, VecZnxNormalizeTmpBytes, VecZnxStd, VecZnxSubABInplace, ZnxZero,
VecZnxDftToVecZnxBigTmpA, VecZnxNormalizeTmpBytes, VecZnxSubABInplace, ZnxZero,
},
layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
};
use crate::{
layouts::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretExec},
layouts::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared},
trait_families::GGSWAssertNoiseFamily,
};
@@ -16,13 +16,13 @@ impl<D: DataRef> GGSWCiphertext<D> {
pub fn assert_noise<B: Backend, DataSk, DataScalar, F>(
&self,
module: &Module<B>,
sk_exec: &GLWESecretExec<DataSk, B>,
sk_prepared: &GLWESecretPrepared<DataSk, B>,
pt_want: &ScalarZnx<DataScalar>,
max_noise: F,
) where
DataSk: DataRef,
DataScalar: DataRef,
Module<B>: GGSWAssertNoiseFamily<B> + VecZnxAddScalarInplace + VecZnxSubABInplace + VecZnxStd,
Module<B>: GGSWAssertNoiseFamily<B> + VecZnxAddScalarInplace + VecZnxSubABInplace,
B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
F: Fn(usize) -> f64,
{
@@ -46,17 +46,17 @@ impl<D: DataRef> GGSWCiphertext<D> {
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft_from_vec_znx(1, 0, &mut pt_dft, 0, &pt.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_exec.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt.data, 0, &pt_big, 0, scratch.borrow());
}
self.at(row_i, col_j)
.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow());
.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt.data, 0);
let std_pt: f64 = module.vec_znx_std(basek, &pt_have.data, 0).log2();
let std_pt: f64 = pt_have.data.std(basek, 0).log2();
let noise: f64 = max_noise(col_j);
println!("{} {}", std_pt, noise);
assert!(std_pt <= noise, "{} > {}", std_pt, noise);
@@ -71,12 +71,12 @@ impl<D: DataRef> GGSWCiphertext<D> {
pub fn print_noise<B: Backend, DataSk, DataScalar>(
&self,
module: &Module<B>,
sk_exec: &GLWESecretExec<DataSk, B>,
sk_prepared: &GLWESecretPrepared<DataSk, B>,
pt_want: &ScalarZnx<DataScalar>,
) where
DataSk: DataRef,
DataScalar: DataRef,
Module<B>: GGSWAssertNoiseFamily<B> + VecZnxAddScalarInplace + VecZnxSubABInplace + VecZnxStd,
Module<B>: GGSWAssertNoiseFamily<B> + VecZnxAddScalarInplace + VecZnxSubABInplace,
B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{
let basek: usize = self.basek();
@@ -99,17 +99,17 @@ impl<D: DataRef> GGSWCiphertext<D> {
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft_from_vec_znx(1, 0, &mut pt_dft, 0, &pt.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_exec.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt.data, 0, &pt_big, 0, scratch.borrow());
}
self.at(row_i, col_j)
.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow());
.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt.data, 0);
let std_pt: f64 = module.vec_znx_std(basek, &pt_have.data, 0).log2();
let std_pt: f64 = pt_have.data.std(basek, 0).log2();
println!("{}", std_pt);
pt.data.zero();
});

View File

@@ -1,12 +1,12 @@
use backend::hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxStd, VecZnxSubABInplace},
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubABInplace},
layouts::{Backend, DataRef, Module, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
};
use crate::{
layouts::GLWEPlaintext,
layouts::prepared::GLWESecretExec,
layouts::prepared::GLWESecretPrepared,
layouts::{GLWECiphertext, Infos},
trait_families::GLWEDecryptFamily,
};
@@ -15,13 +15,13 @@ impl<D: DataRef> GLWECiphertext<D> {
pub fn assert_noise<B: Backend, DataSk, DataPt>(
&self,
module: &Module<B>,
sk_exec: &GLWESecretExec<DataSk, B>,
sk_prepared: &GLWESecretPrepared<DataSk, B>,
pt_want: &GLWEPlaintext<DataPt>,
max_noise: f64,
) where
DataSk: DataRef,
DataPt: DataRef,
Module<B>: GLWEDecryptFamily<B> + VecZnxSubABInplace + VecZnxNormalizeInplace<B> + VecZnxStd,
Module<B>: GLWEDecryptFamily<B> + VecZnxSubABInplace + VecZnxNormalizeInplace<B>,
B: TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self.n(), self.basek(), self.k());
@@ -33,12 +33,12 @@ impl<D: DataRef> GLWECiphertext<D> {
self.k(),
));
self.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow());
self.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
module.vec_znx_normalize_inplace(self.basek(), &mut pt_have.data, 0, scratch.borrow());
let noise_have: f64 = module.vec_znx_std(self.basek(), &pt_have.data, 0).log2();
let noise_have: f64 = pt_have.data.std(self.basek(), 0).log2();
assert!(noise_have <= max_noise, "{} {}", noise_have, max_noise);
}
}

View File

@@ -7,11 +7,11 @@ use backend::hal::{
use crate::{
dist::Distribution,
layouts::{
GGLWEAutomorphismKey, GGLWECiphertext, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWEPublicKey,
GLWESecret, GGLWESwitchingKey, Infos,
GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GLWEPlaintext,
GLWEPublicKey, GLWESecret, Infos,
prepared::{
GGLWEAutomorphismKeyExec, GGLWECiphertextExec, GGSWCiphertextExec, GLWEPublicKeyExec, GLWESecretExec,
GGLWESwitchingKeyExec, GGLWETensorKeyExec,
GGLWEAutomorphismKeyPrepared, GGLWECiphertextPrepared, GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared,
GGSWCiphertextPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared,
},
},
};
@@ -53,8 +53,8 @@ pub trait TakeGGLWE<B: Backend> {
) -> (GGLWECiphertext<&mut [u8]>, &mut Self);
}
pub trait TakeGGLWEExec<B: Backend> {
fn take_gglwe_exec(
pub trait TakeGGLWEPrepared<B: Backend> {
fn take_gglwe_prepared(
&mut self,
n: usize,
basek: usize,
@@ -63,7 +63,7 @@ pub trait TakeGGLWEExec<B: Backend> {
digits: usize,
rank_in: usize,
rank_out: usize,
) -> (GGLWECiphertextExec<&mut [u8], B>, &mut Self);
) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self);
}
pub trait TakeGGSW<B: Backend> {
@@ -78,8 +78,8 @@ pub trait TakeGGSW<B: Backend> {
) -> (GGSWCiphertext<&mut [u8]>, &mut Self);
}
pub trait TakeGGSWExec<B: Backend> {
fn take_ggsw_exec(
pub trait TakeGGSWPrepared<B: Backend> {
fn take_ggsw_prepared(
&mut self,
n: usize,
basek: usize,
@@ -87,29 +87,29 @@ pub trait TakeGGSWExec<B: Backend> {
rows: usize,
digits: usize,
rank: usize,
) -> (GGSWCiphertextExec<&mut [u8], B>, &mut Self);
) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self);
}
pub trait TakeGLWESecret<B: Backend> {
fn take_glwe_secret(&mut self, n: usize, rank: usize) -> (GLWESecret<&mut [u8]>, &mut Self);
}
pub trait TakeGLWESecretExec<B: Backend> {
fn take_glwe_secret_exec(&mut self, n: usize, rank: usize) -> (GLWESecretExec<&mut [u8], B>, &mut Self);
pub trait TakeGLWESecretPrepared<B: Backend> {
fn take_glwe_secret_prepared(&mut self, n: usize, rank: usize) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self);
}
pub trait TakeGLWEPk<B: Backend> {
fn take_glwe_pk(&mut self, n: usize, basek: usize, k: usize, rank: usize) -> (GLWEPublicKey<&mut [u8]>, &mut Self);
}
pub trait TakeGLWEPkExec<B: Backend> {
fn take_glwe_pk_exec(
pub trait TakeGLWEPkPrepared<B: Backend> {
fn take_glwe_pk_prepared(
&mut self,
n: usize,
basek: usize,
k: usize,
rank: usize,
) -> (GLWEPublicKeyExec<&mut [u8], B>, &mut Self);
) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self);
}
pub trait TakeGLWESwitchingKey<B: Backend> {
@@ -125,8 +125,8 @@ pub trait TakeGLWESwitchingKey<B: Backend> {
) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self);
}
pub trait TakeGLWESwitchingKeyExec<B: Backend> {
fn take_glwe_switching_key_exec(
pub trait TakeGLWESwitchingKeyPrepared<B: Backend> {
fn take_glwe_switching_key_prepared(
&mut self,
n: usize,
basek: usize,
@@ -135,7 +135,7 @@ pub trait TakeGLWESwitchingKeyExec<B: Backend> {
digits: usize,
rank_in: usize,
rank_out: usize,
) -> (GGLWESwitchingKeyExec<&mut [u8], B>, &mut Self);
) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self);
}
pub trait TakeTensorKey<B: Backend> {
@@ -150,8 +150,8 @@ pub trait TakeTensorKey<B: Backend> {
) -> (GGLWETensorKey<&mut [u8]>, &mut Self);
}
pub trait TakeTensorKeyExec<B: Backend> {
fn take_tensor_key_exec(
pub trait TakeTensorKeyPrepared<B: Backend> {
fn take_tensor_key_prepared(
&mut self,
n: usize,
basek: usize,
@@ -159,7 +159,7 @@ pub trait TakeTensorKeyExec<B: Backend> {
rows: usize,
digits: usize,
rank: usize,
) -> (GGLWETensorKeyExec<&mut [u8], B>, &mut Self);
) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self);
}
pub trait TakeAutomorphismKey<B: Backend> {
@@ -174,8 +174,8 @@ pub trait TakeAutomorphismKey<B: Backend> {
) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self);
}
pub trait TakeAutomorphismKeyExec<B: Backend> {
fn take_automorphism_key_exec(
pub trait TakeAutomorphismKeyPrepared<B: Backend> {
fn take_automorphism_key_prepared(
&mut self,
n: usize,
basek: usize,
@@ -183,7 +183,7 @@ pub trait TakeAutomorphismKeyExec<B: Backend> {
rows: usize,
digits: usize,
rank: usize,
) -> (GGLWEAutomorphismKeyExec<&mut [u8], B>, &mut Self);
) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self);
}
impl<B: Backend> TakeGLWECt<B> for Scratch<B>
@@ -330,11 +330,11 @@ where
}
}
impl<B: Backend> TakeGGLWEExec<B> for Scratch<B>
impl<B: Backend> TakeGGLWEPrepared<B> for Scratch<B>
where
Scratch<B>: TakeVmpPMat<B>,
{
fn take_gglwe_exec(
fn take_gglwe_prepared(
&mut self,
n: usize,
basek: usize,
@@ -343,7 +343,7 @@ where
digits: usize,
rank_in: usize,
rank_out: usize,
) -> (GGLWECiphertextExec<&mut [u8], B>, &mut Self) {
) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_vmp_pmat(
n,
rows.div_ceil(digits),
@@ -352,7 +352,7 @@ where
k.div_ceil(basek),
);
(
GGLWECiphertextExec {
GGLWECiphertextPrepared {
data: data,
basek: basek,
k,
@@ -363,14 +363,14 @@ where
}
}
impl<'a, B, D> TakeLike<'a, B, GGLWECiphertextExec<D, B>> for Scratch<B>
impl<'a, B, D> TakeLike<'a, B, GGLWECiphertextPrepared<D, B>> for Scratch<B>
where
B: Backend + TakeVmpPMatImpl<B>,
D: DataRef,
{
type Output = GGLWECiphertextExec<&'a mut [u8], B>;
type Output = GGLWECiphertextPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGLWECiphertextExec<D, B>) -> (Self::Output, &'a mut Self) {
fn take_like(&'a mut self, template: &GGLWECiphertextPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (data, scratch) = B::take_vmp_pmat_impl(
self,
template.n(),
@@ -380,7 +380,7 @@ where
template.size(),
);
(
GGLWECiphertextExec {
GGLWECiphertextPrepared {
data,
basek: template.basek(),
k: template.k(),
@@ -451,11 +451,11 @@ where
}
}
impl<B: Backend> TakeGGSWExec<B> for Scratch<B>
impl<B: Backend> TakeGGSWPrepared<B> for Scratch<B>
where
Scratch<B>: TakeVmpPMat<B>,
{
fn take_ggsw_exec(
fn take_ggsw_prepared(
&mut self,
n: usize,
basek: usize,
@@ -463,7 +463,7 @@ where
rows: usize,
digits: usize,
rank: usize,
) -> (GGSWCiphertextExec<&mut [u8], B>, &mut Self) {
) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_vmp_pmat(
n,
rows.div_ceil(digits),
@@ -472,7 +472,7 @@ where
k.div_ceil(basek),
);
(
GGSWCiphertextExec {
GGSWCiphertextPrepared {
data,
basek,
k,
@@ -483,14 +483,14 @@ where
}
}
impl<'a, B, D> TakeLike<'a, B, GGSWCiphertextExec<D, B>> for Scratch<B>
impl<'a, B, D> TakeLike<'a, B, GGSWCiphertextPrepared<D, B>> for Scratch<B>
where
B: Backend + TakeVmpPMatImpl<B>,
D: DataRef,
{
type Output = GGSWCiphertextExec<&'a mut [u8], B>;
type Output = GGSWCiphertextPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGSWCiphertextExec<D, B>) -> (Self::Output, &'a mut Self) {
fn take_like(&'a mut self, template: &GGSWCiphertextPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (data, scratch) = B::take_vmp_pmat_impl(
self,
template.n(),
@@ -500,7 +500,7 @@ where
template.size(),
);
(
GGSWCiphertextExec {
GGSWCiphertextPrepared {
data,
basek: template.basek(),
k: template.k(),
@@ -550,20 +550,20 @@ where
}
}
impl<B: Backend> TakeGLWEPkExec<B> for Scratch<B>
impl<B: Backend> TakeGLWEPkPrepared<B> for Scratch<B>
where
Scratch<B>: TakeVecZnxDft<B>,
{
fn take_glwe_pk_exec(
fn take_glwe_pk_prepared(
&mut self,
n: usize,
basek: usize,
k: usize,
rank: usize,
) -> (GLWEPublicKeyExec<&mut [u8], B>, &mut Self) {
) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_vec_znx_dft(n, rank + 1, k.div_ceil(basek));
(
GLWEPublicKeyExec {
GLWEPublicKeyPrepared {
data,
k,
basek,
@@ -574,17 +574,17 @@ where
}
}
impl<'a, B, D> TakeLike<'a, B, GLWEPublicKeyExec<D, B>> for Scratch<B>
impl<'a, B, D> TakeLike<'a, B, GLWEPublicKeyPrepared<D, B>> for Scratch<B>
where
B: Backend + TakeVecZnxDftImpl<B>,
D: DataRef,
{
type Output = GLWEPublicKeyExec<&'a mut [u8], B>;
type Output = GLWEPublicKeyPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GLWEPublicKeyExec<D, B>) -> (Self::Output, &'a mut Self) {
fn take_like(&'a mut self, template: &GLWEPublicKeyPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (data, scratch) = B::take_vec_znx_dft_impl(self, template.n(), template.cols(), template.size());
(
GLWEPublicKeyExec {
GLWEPublicKeyPrepared {
data,
basek: template.basek(),
k: template.k(),
@@ -630,14 +630,14 @@ where
}
}
impl<B: Backend> TakeGLWESecretExec<B> for Scratch<B>
impl<B: Backend> TakeGLWESecretPrepared<B> for Scratch<B>
where
Scratch<B>: TakeSvpPPol<B>,
{
fn take_glwe_secret_exec(&mut self, n: usize, rank: usize) -> (GLWESecretExec<&mut [u8], B>, &mut Self) {
fn take_glwe_secret_prepared(&mut self, n: usize, rank: usize) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_svp_ppol(n, rank);
(
GLWESecretExec {
GLWESecretPrepared {
data,
dist: Distribution::NONE,
},
@@ -646,17 +646,17 @@ where
}
}
impl<'a, B, D> TakeLike<'a, B, GLWESecretExec<D, B>> for Scratch<B>
impl<'a, B, D> TakeLike<'a, B, GLWESecretPrepared<D, B>> for Scratch<B>
where
B: Backend + TakeSvpPPolImpl<B>,
D: DataRef,
{
type Output = GLWESecretExec<&'a mut [u8], B>;
type Output = GLWESecretPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GLWESecretExec<D, B>) -> (Self::Output, &'a mut Self) {
fn take_like(&'a mut self, template: &GLWESecretPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (data, scratch) = B::take_svp_ppol_impl(self, template.n(), template.rank());
(
GLWESecretExec {
GLWESecretPrepared {
data,
dist: template.dist,
},
@@ -712,11 +712,11 @@ where
}
}
impl<B: Backend> TakeGLWESwitchingKeyExec<B> for Scratch<B>
impl<B: Backend> TakeGLWESwitchingKeyPrepared<B> for Scratch<B>
where
Scratch<B>: TakeGGLWEExec<B>,
Scratch<B>: TakeGGLWEPrepared<B>,
{
fn take_glwe_switching_key_exec(
fn take_glwe_switching_key_prepared(
&mut self,
n: usize,
basek: usize,
@@ -725,10 +725,10 @@ where
digits: usize,
rank_in: usize,
rank_out: usize,
) -> (GGLWESwitchingKeyExec<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_gglwe_exec(n, basek, k, rows, digits, rank_in, rank_out);
) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_gglwe_prepared(n, basek, k, rows, digits, rank_in, rank_out);
(
GGLWESwitchingKeyExec {
GGLWESwitchingKeyPrepared {
key: data,
sk_in_n: 0,
sk_out_n: 0,
@@ -738,18 +738,18 @@ where
}
}
impl<'a, B, D> TakeLike<'a, B, GGLWESwitchingKeyExec<D, B>> for Scratch<B>
impl<'a, B, D> TakeLike<'a, B, GGLWESwitchingKeyPrepared<D, B>> for Scratch<B>
where
Scratch<B>: TakeLike<'a, B, GGLWECiphertextExec<D, B>, Output = GGLWECiphertextExec<&'a mut [u8], B>>,
Scratch<B>: TakeLike<'a, B, GGLWECiphertextPrepared<D, B>, Output = GGLWECiphertextPrepared<&'a mut [u8], B>>,
B: Backend + TakeMatZnxImpl<B>,
D: DataRef,
{
type Output = GGLWESwitchingKeyExec<&'a mut [u8], B>;
type Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGLWESwitchingKeyExec<D, B>) -> (Self::Output, &'a mut Self) {
fn take_like(&'a mut self, template: &GGLWESwitchingKeyPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (key, scratch) = self.take_like(&template.key);
(
GGLWESwitchingKeyExec {
GGLWESwitchingKeyPrepared {
key,
sk_in_n: template.sk_in_n,
sk_out_n: template.sk_out_n,
@@ -791,11 +791,11 @@ where
}
}
impl<B: Backend> TakeAutomorphismKeyExec<B> for Scratch<B>
impl<B: Backend> TakeAutomorphismKeyPrepared<B> for Scratch<B>
where
Scratch<B>: TakeGLWESwitchingKeyExec<B>,
Scratch<B>: TakeGLWESwitchingKeyPrepared<B>,
{
fn take_automorphism_key_exec(
fn take_automorphism_key_prepared(
&mut self,
n: usize,
basek: usize,
@@ -803,23 +803,23 @@ where
rows: usize,
digits: usize,
rank: usize,
) -> (GGLWEAutomorphismKeyExec<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_glwe_switching_key_exec(n, basek, k, rows, digits, rank, rank);
(GGLWEAutomorphismKeyExec { key: data, p: 0 }, scratch)
) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_glwe_switching_key_prepared(n, basek, k, rows, digits, rank, rank);
(GGLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch)
}
}
impl<'a, B, D> TakeLike<'a, B, GGLWEAutomorphismKeyExec<D, B>> for Scratch<B>
impl<'a, B, D> TakeLike<'a, B, GGLWEAutomorphismKeyPrepared<D, B>> for Scratch<B>
where
Scratch<B>: TakeLike<'a, B, GGLWESwitchingKeyExec<D, B>, Output = GGLWESwitchingKeyExec<&'a mut [u8], B>>,
Scratch<B>: TakeLike<'a, B, GGLWESwitchingKeyPrepared<D, B>, Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>>,
B: Backend + TakeMatZnxImpl<B>,
D: DataRef,
{
type Output = GGLWEAutomorphismKeyExec<&'a mut [u8], B>;
type Output = GGLWEAutomorphismKeyPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGLWEAutomorphismKeyExec<D, B>) -> (Self::Output, &'a mut Self) {
fn take_like(&'a mut self, template: &GGLWEAutomorphismKeyPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let (key, scratch) = self.take_like(&template.key);
(GGLWEAutomorphismKeyExec { key, p: template.p }, scratch)
(GGLWEAutomorphismKeyPrepared { key, p: template.p }, scratch)
}
}
@@ -884,11 +884,11 @@ where
}
}
impl<B: Backend> TakeTensorKeyExec<B> for Scratch<B>
impl<B: Backend> TakeTensorKeyPrepared<B> for Scratch<B>
where
Scratch<B>: TakeVmpPMat<B>,
{
fn take_tensor_key_exec(
fn take_tensor_key_prepared(
&mut self,
n: usize,
basek: usize,
@@ -896,36 +896,36 @@ where
rows: usize,
digits: usize,
rank: usize,
) -> (GGLWETensorKeyExec<&mut [u8], B>, &mut Self) {
let mut keys: Vec<GGLWESwitchingKeyExec<&mut [u8], B>> = Vec::new();
) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) {
let mut keys: Vec<GGLWESwitchingKeyPrepared<&mut [u8], B>> = Vec::new();
let pairs: usize = (((rank + 1) * rank) >> 1).max(1);
let mut scratch: &mut Scratch<B> = self;
if pairs != 0 {
let (gglwe, s) = scratch.take_glwe_switching_key_exec(n, basek, k, rows, digits, 1, rank);
let (gglwe, s) = scratch.take_glwe_switching_key_prepared(n, basek, k, rows, digits, 1, rank);
scratch = s;
keys.push(gglwe);
}
for _ in 1..pairs {
let (gglwe, s) = scratch.take_glwe_switching_key_exec(n, basek, k, rows, digits, 1, rank);
let (gglwe, s) = scratch.take_glwe_switching_key_prepared(n, basek, k, rows, digits, 1, rank);
scratch = s;
keys.push(gglwe);
}
(GGLWETensorKeyExec { keys }, scratch)
(GGLWETensorKeyPrepared { keys }, scratch)
}
}
impl<'a, B, D> TakeLike<'a, B, GGLWETensorKeyExec<D, B>> for Scratch<B>
impl<'a, B, D> TakeLike<'a, B, GGLWETensorKeyPrepared<D, B>> for Scratch<B>
where
Scratch<B>: TakeLike<'a, B, GGLWESwitchingKeyExec<D, B>, Output = GGLWESwitchingKeyExec<&'a mut [u8], B>>,
Scratch<B>: TakeLike<'a, B, GGLWESwitchingKeyPrepared<D, B>, Output = GGLWESwitchingKeyPrepared<&'a mut [u8], B>>,
B: Backend + TakeMatZnxImpl<B>,
D: DataRef,
{
type Output = GGLWETensorKeyExec<&'a mut [u8], B>;
type Output = GGLWETensorKeyPrepared<&'a mut [u8], B>;
fn take_like(&'a mut self, template: &GGLWETensorKeyExec<D, B>) -> (Self::Output, &'a mut Self) {
let mut keys: Vec<GGLWESwitchingKeyExec<&mut [u8], B>> = Vec::new();
fn take_like(&'a mut self, template: &GGLWETensorKeyPrepared<D, B>) -> (Self::Output, &'a mut Self) {
let mut keys: Vec<GGLWESwitchingKeyPrepared<&mut [u8], B>> = Vec::new();
let pairs: usize = template.keys.len();
let mut scratch: &mut Scratch<B> = self;
@@ -941,6 +941,6 @@ where
keys.push(gglwe);
}
(GGLWETensorKeyExec { keys }, scratch)
(GGLWETensorKeyPrepared { keys }, scratch)
}
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy,
VecZnxStd, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -14,13 +14,13 @@ use sampling::source::Source;
use crate::{
layouts::{
GGLWEAutomorphismKey, GLWEPlaintext, GLWESecret, Infos,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec},
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
},
noise::log2_std_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
module: &Module<B>,
@@ -35,7 +35,7 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
rank: usize,
) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism
+ VecZnxSwithcDegree
@@ -44,7 +44,6 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
+ VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
@@ -102,13 +101,18 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
scratch.borrow(),
);
let mut auto_key_apply_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_apply, rows_apply, digits, rank);
let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_apply, rows_apply, digits, rank);
auto_key_apply_exec.prepare(module, &auto_key_apply, scratch.borrow());
auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
auto_key_out.automorphism(module, &auto_key_in, &auto_key_apply_exec, scratch.borrow());
auto_key_out.automorphism(
module,
&auto_key_in,
&auto_key_apply_prepared,
scratch.borrow(),
);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_out);
@@ -124,7 +128,7 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
);
});
let sk_auto_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_auto);
let sk_auto_dft: GLWESecretPrepared<Vec<u8>, B> = sk_auto.prepare_alloc(module, scratch.borrow());
(0..auto_key_out.rank_in()).for_each(|col_i| {
(0..auto_key_out.rows()).for_each(|row_i| {
@@ -140,7 +144,7 @@ pub fn test_gglwe_automorphism_key_automorphism<B: Backend>(
col_i,
);
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2();
let noise_have: f64 = pt.data.std(basek, 0).log2();
let noise_want: f64 = log2_std_noise_gglwe_product(
n as f64,
basek * digits,
@@ -176,7 +180,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
rank: usize,
) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism
+ VecZnxSwithcDegree
@@ -185,7 +189,6 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
+ VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
@@ -242,13 +245,13 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
scratch.borrow(),
);
let mut auto_key_apply_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_apply, rows_apply, digits, rank);
let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_apply, rows_apply, digits, rank);
auto_key_apply_exec.prepare(module, &auto_key_apply, scratch.borrow());
auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
auto_key.automorphism_inplace(module, &auto_key_apply_exec, scratch.borrow());
auto_key.automorphism_inplace(module, &auto_key_apply_prepared, scratch.borrow());
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_in);
@@ -265,7 +268,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
);
});
let sk_auto_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_auto);
let sk_auto_dft: GLWESecretPrepared<Vec<u8>, B> = sk_auto.prepare_alloc(module, scratch.borrow());
(0..auto_key.rank_in()).for_each(|col_i| {
(0..auto_key.rows()).for_each(|row_i| {
@@ -280,7 +283,7 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace<B: Backend>(
col_i,
);
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2();
let noise_have: f64 = pt.data.std(basek, 0).log2();
let noise_want: f64 = log2_std_noise_gglwe_product(
n as f64,
basek * digits,

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy,
VecZnxStd, VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScalarZnx, ScratchOwned},
oep::{
@@ -14,14 +14,14 @@ use sampling::source::Source;
use crate::{
layouts::{
GGLWEAutomorphismKey, GGLWETensorKey, GGSWCiphertext, GLWESecret,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec, GGLWETensorKeyExec},
prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
},
noise::noise_ggsw_keyswitch,
trait_families::GGSWAssertNoiseFamily,
};
use crate::trait_families::{
GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWKeySwitchFamily, GLWESecretExecModuleFamily,
GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWKeySwitchFamily, GLWESecretPreparedModuleFamily,
};
pub fn test_ggsw_automorphism<B: Backend>(
@@ -37,10 +37,9 @@ pub fn test_ggsw_automorphism<B: Backend>(
sigma: f64,
) where
Module<B>: GGSWAssertNoiseFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxCopy
+ VecZnxStd
+ VecZnxSubABInplace
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
@@ -92,7 +91,7 @@ pub fn test_ggsw_automorphism<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(var_xs, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
auto_key.encrypt_sk(
module,
@@ -117,21 +116,28 @@ pub fn test_ggsw_automorphism<B: Backend>(
ct_in.encrypt_sk(
module,
&pt_scalar,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut auto_key_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank);
auto_key_exec.prepare(module, &auto_key, scratch.borrow());
let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_tsk, rows, digits, rank);
tsk_exec.prepare(module, &tensor_key, scratch.borrow());
let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> =
GGLWETensorKeyPrepared::alloc(module, n, basek, k_tsk, rows, digits, rank);
tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
ct_out.automorphism(module, &ct_in, &auto_key_exec, &tsk_exec, scratch.borrow());
ct_out.automorphism(
module,
&ct_in,
&auto_key_prepared,
&tsk_prepared,
scratch.borrow(),
);
module.vec_znx_automorphism_inplace(p, &mut pt_scalar.as_vec_znx_mut(), 0);
@@ -151,7 +157,7 @@ pub fn test_ggsw_automorphism<B: Backend>(
) + 0.5
};
ct_out.assert_noise(module, &sk_exec, &pt_scalar, &max_noise);
ct_out.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise);
}
pub fn test_ggsw_automorphism_inplace<B: Backend>(
@@ -166,10 +172,9 @@ pub fn test_ggsw_automorphism_inplace<B: Backend>(
sigma: f64,
) where
Module<B>: GGSWAssertNoiseFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxCopy
+ VecZnxStd
+ VecZnxSubABInplace
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
@@ -217,7 +222,7 @@ pub fn test_ggsw_automorphism_inplace<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(var_xs, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
auto_key.encrypt_sk(
module,
@@ -242,21 +247,22 @@ pub fn test_ggsw_automorphism_inplace<B: Backend>(
ct.encrypt_sk(
module,
&pt_scalar,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut auto_key_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank);
auto_key_exec.prepare(module, &auto_key, scratch.borrow());
let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_tsk, rows, digits, rank);
tsk_exec.prepare(module, &tensor_key, scratch.borrow());
let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> =
GGLWETensorKeyPrepared::alloc(module, n, basek, k_tsk, rows, digits, rank);
tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
ct.automorphism_inplace(module, &auto_key_exec, &tsk_exec, scratch.borrow());
ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow());
module.vec_znx_automorphism_inplace(p, &mut pt_scalar.as_vec_znx_mut(), 0);
@@ -276,5 +282,5 @@ pub fn test_ggsw_automorphism_inplace<B: Backend>(
) + 0.5
};
ct.assert_noise(module, &sk_exec, &pt_scalar, &max_noise);
ct.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
VecZnxFillUniform, VecZnxStd, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
VecZnxFillUniform, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -14,13 +14,13 @@ use sampling::source::Source;
use crate::{
layouts::{
GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec},
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
},
noise::log2_std_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_glwe_automorphism<B: Backend>(
module: &Module<B>,
@@ -34,14 +34,13 @@ pub fn test_glwe_automorphism<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxAutomorphismInplace
+ VecZnxStd
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
B: TakeVecZnxDftImpl<B>
@@ -85,7 +84,7 @@ pub fn test_glwe_automorphism<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
autokey.encrypt_sk(
module,
@@ -100,18 +99,18 @@ pub fn test_glwe_automorphism<B: Backend>(
ct_in.encrypt_sk(
module,
&pt_want,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut autokey_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank);
autokey_exec.prepare(module, &autokey, scratch.borrow());
let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
autokey_prepared.prepare(module, &autokey, scratch.borrow());
ct_out.automorphism(module, &ct_in, &autokey_exec, scratch.borrow());
ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow());
let max_noise: f64 = log2_std_noise_gglwe_product(
module.n() as f64,
@@ -128,7 +127,7 @@ pub fn test_glwe_automorphism<B: Backend>(
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
ct_out.assert_noise(module, &sk_exec, &pt_want, max_noise + 1.0);
ct_out.assert_noise(module, &sk_prepared, &pt_want, max_noise + 1.0);
}
pub fn test_glwe_automorphism_inplace<B: Backend>(
@@ -142,14 +141,13 @@ pub fn test_glwe_automorphism_inplace<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxAutomorphismInplace
+ VecZnxStd
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
B: TakeVecZnxDftImpl<B>
@@ -183,7 +181,7 @@ pub fn test_glwe_automorphism_inplace<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
autokey.encrypt_sk(
module,
@@ -198,18 +196,18 @@ pub fn test_glwe_automorphism_inplace<B: Backend>(
ct.encrypt_sk(
module,
&pt_want,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut autokey_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> =
GGLWEAutomorphismKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank);
autokey_exec.prepare(module, &autokey, scratch.borrow());
let mut autokey_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> =
GGLWEAutomorphismKeyPrepared::alloc(module, n, basek, k_ksk, rows, digits, rank);
autokey_prepared.prepare(module, &autokey, scratch.borrow());
ct.automorphism_inplace(module, &autokey_exec, scratch.borrow());
ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow());
let max_noise: f64 = log2_std_noise_gglwe_product(
module.n() as f64,
@@ -226,5 +224,5 @@ pub fn test_glwe_automorphism_inplace<B: Backend>(
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
ct.assert_noise(module, &sk_exec, &pt_want, max_noise + 1.0);
ct.assert_noise(module, &sk_prepared, &pt_want, max_noise + 1.0);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxEncodeCoeffsi64,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxSwithcDegree,
VmpPMatAlloc, VmpPMatPrepare, ZnxView,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -14,10 +14,10 @@ use sampling::source::Source;
use crate::layouts::{
GLWECiphertext, GLWEPlaintext, GLWESecret, GLWEToLWESwitchingKey, Infos, LWECiphertext, LWEPlaintext, LWESecret,
LWEToGLWESwitchingKey,
prepared::{GLWESecretExec, GLWEToLWESwitchingKeyExec, LWEToGLWESwitchingKeyExec},
prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWEDecryptFamily, GLWEKeyswitchFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWEDecryptFamily, GLWEKeyswitchFamily, GLWESecretPreparedModuleFamily};
pub fn test_lwe_to_glwe<B: Backend>(module: &Module<B>)
where
@@ -26,11 +26,10 @@ where
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ GLWEKeyswitchFamily<B>
+ VecZnxEncodeCoeffsi64
+ VecZnxAutomorphismInplace
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
+ GLWESecretExecModuleFamily<B>,
+ GLWESecretPreparedModuleFamily<B>,
B: TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B>
+ ScratchAvailableImpl<B>
@@ -67,7 +66,7 @@ where
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_glwe);
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
@@ -75,7 +74,7 @@ where
let data: i64 = 17;
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
module.encode_coeff_i64(basek, &mut lwe_pt.data, 0, k_lwe_pt, 0, data, k_lwe_pt);
lwe_pt.encode_i64(data, k_lwe_pt);
let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
lwe_ct.encrypt_sk(
@@ -101,12 +100,12 @@ where
let mut glwe_ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank);
let ksk_exec: LWEToGLWESwitchingKeyExec<Vec<u8>, B> = LWEToGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow());
let ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
glwe_ct.from_lwe(module, &lwe_ct, &ksk_exec, scratch.borrow());
glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct);
glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_exec, scratch.borrow());
glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow());
assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
}
@@ -118,11 +117,10 @@ where
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ GLWEKeyswitchFamily<B>
+ VecZnxEncodeCoeffsi64
+ VecZnxAutomorphismInplace
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
+ GLWESecretExecModuleFamily<B>,
+ GLWESecretPreparedModuleFamily<B>,
B: TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B>
+ ScratchAvailableImpl<B>
@@ -159,20 +157,20 @@ where
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_glwe);
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
let mut sk_lwe = LWESecret::alloc(n_lwe);
sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
let data: i64 = 17;
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_glwe_ct);
module.encode_coeff_i64(basek, &mut glwe_pt.data, 0, k_lwe_pt, 0, data, k_lwe_pt);
glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0);
let mut glwe_ct = GLWECiphertext::alloc(n, basek, k_glwe_ct, rank);
glwe_ct.encrypt_sk(
module,
&glwe_pt,
&sk_glwe_exec,
&sk_glwe_prepared,
&mut source_xa,
&mut source_xe,
sigma,
@@ -193,9 +191,9 @@ where
let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe_ct);
let ksk_exec: GLWEToLWESwitchingKeyExec<Vec<u8>, B> = GLWEToLWESwitchingKeyExec::from(module, &ksk, scratch.borrow());
let ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
lwe_ct.from_glwe(module, &glwe_ct, &ksk_exec, scratch.borrow());
lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct);
lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe);

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy,
VecZnxStd, VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -12,11 +12,15 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
layouts::{GGLWEAutomorphismKey, GLWESecret, compressed::GGLWEAutomorphismKeyCompressed, prepared::GLWESecretExec},
layouts::{
GGLWEAutomorphismKey, GLWESecret,
compressed::GGLWEAutomorphismKeyCompressed,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
trait_families::{Decompress, GLWEDecryptFamily, GLWEKeyswitchFamily},
};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEAutomorphismKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>(
module: &Module<B>,
@@ -27,7 +31,7 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism
+ VecZnxSwithcDegree
@@ -36,7 +40,6 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>(
+ VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
@@ -87,11 +90,11 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk<B: Backend>(
i,
);
});
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
atk.key
.key
.assert_noise(module, &sk_out_exec, &sk.data, sigma);
.assert_noise(module, &sk_out_prepared, &sk.data, sigma);
}
pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>(
@@ -103,7 +106,7 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEAutomorphismKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B>
+ VecZnxAutomorphism
+ VecZnxSwithcDegree
@@ -112,7 +115,6 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>(
+ VecZnxAutomorphismInplace
+ GLWEDecryptFamily<B>
+ VecZnxSubScalarInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
@@ -165,12 +167,12 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B: Backend>(
i,
);
});
let sk_out_exec = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow());
let mut atk: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
atk.decompress(module, &atk_compressed);
atk.key
.key
.assert_noise(module, &sk_out_exec, &sk.data, sigma);
.assert_noise(module, &sk_out_prepared, &sk.data, sigma);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubScalarInplace,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubScalarInplace, VecZnxSwithcDegree,
VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -12,11 +12,15 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
layouts::{GLWESecret, GGLWESwitchingKey, compressed::GGLWESwitchingKeyCompressed, prepared::GLWESecretExec},
layouts::{
GGLWESwitchingKey, GLWESecret,
compressed::GGLWESwitchingKeyCompressed,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
trait_families::{Decompress, GLWEDecryptFamily},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>(
module: &Module<B>,
@@ -28,11 +32,10 @@ pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
@@ -67,7 +70,7 @@ pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>(
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk(
module,
@@ -80,7 +83,7 @@ pub fn test_gglwe_switching_key_encrypt_sk<B: Backend>(
);
ksk.key
.assert_noise(module, &sk_out_exec, &sk_in.data, sigma);
.assert_noise(module, &sk_out_prepared, &sk_in.data, sigma);
}
pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>(
@@ -93,11 +96,10 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
@@ -132,7 +134,7 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>(
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
let seed_xa = [1u8; 32];
@@ -150,5 +152,5 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk<B: Backend>(
ksk.decompress(module, &ksk_compressed);
ksk.key
.assert_noise(module, &sk_out_exec, &sk_in.data, sigma);
.assert_noise(module, &sk_out_prepared, &sk_in.data, sigma);
}

View File

@@ -1,6 +1,6 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubABInplace, VmpPMatAlloc,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubABInplace, VmpPMatAlloc,
VmpPMatPrepare,
},
layouts::{Backend, Module, ScalarZnx, ScratchOwned},
@@ -12,20 +12,23 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
layouts::{GGSWCiphertext, GLWESecret, compressed::GGSWCiphertextCompressed, prepared::GLWESecretExec},
layouts::{
GGSWCiphertext, GLWESecret,
compressed::GGSWCiphertextCompressed,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
trait_families::{Decompress, GGSWAssertNoiseFamily},
};
use crate::trait_families::{GGSWEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGSWEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_ggsw_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, digits: usize, rank: usize, sigma: f64)
where
Module<B>: GLWESecretExecModuleFamily<B>
Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
@@ -60,13 +63,12 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
sk_exec.prepare(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct.encrypt_sk(
module,
&pt_scalar,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
@@ -75,7 +77,7 @@ where
let noise_f = |_col_i: usize| -(k as f64) + sigma.log2() + 0.5;
ct.assert_noise(module, &sk_exec, &pt_scalar, &noise_f);
ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f);
}
pub fn test_ggsw_compressed_encrypt_sk<B: Backend>(
@@ -86,12 +88,11 @@ pub fn test_ggsw_compressed_encrypt_sk<B: Backend>(
rank: usize,
sigma: f64,
) where
Module<B>: GLWESecretExecModuleFamily<B>
Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
@@ -125,15 +126,14 @@ pub fn test_ggsw_compressed_encrypt_sk<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
sk_exec.prepare(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let seed_xa: [u8; 32] = [1u8; 32];
ct_compressed.encrypt_sk(
module,
&pt_scalar,
&sk_exec,
&sk_prepared,
seed_xa,
&mut source_xe,
sigma,
@@ -145,5 +145,5 @@ pub fn test_ggsw_compressed_encrypt_sk<B: Backend>(
let mut ct: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n, basek, k, rows, digits, rank);
ct.decompress(module, &ct_compressed);
ct.assert_noise(module, &sk_exec, &pt_scalar, &noise_f);
ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f);
}

View File

@@ -1,5 +1,5 @@
use backend::hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxCopy, VecZnxDftAlloc, VecZnxFillUniform, VecZnxStd, VecZnxSubABInplace},
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxCopy, VecZnxDftAlloc, VecZnxFillUniform, VecZnxSubABInplace},
layouts::{Backend, Module, ScratchOwned},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
@@ -12,16 +12,16 @@ use crate::{
layouts::{
GLWECiphertext, GLWEPlaintext, GLWEPublicKey, GLWESecret, Infos,
compressed::GLWECiphertextCompressed,
prepared::{GLWEPublicKeyExec, GLWESecretExec},
prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc},
},
operations::GLWEOperations,
trait_families::Decompress,
};
use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptPkFamily, GLWEEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptPkFamily, GLWEEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub trait EncryptionTestModuleFamily<B: Backend> =
GLWEDecryptFamily<B> + VecZnxStd + GLWESecretExecModuleFamily<B> + GLWEEncryptPkFamily<B>;
GLWEDecryptFamily<B> + GLWESecretPreparedModuleFamily<B> + GLWEEncryptPkFamily<B>;
pub fn test_glwe_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize)
where
@@ -51,25 +51,25 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa);
ct.encrypt_sk(
module,
&pt_want,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow());
ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
pt_want.sub_inplace_ab(module, &pt_have);
let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0) * (ct.k() as f64).exp2();
let noise_have: f64 = pt_want.data.std(basek, 0) * (ct.k() as f64).exp2();
let noise_want: f64 = sigma;
assert!(noise_have <= noise_want + 0.2);
@@ -110,7 +110,7 @@ pub fn test_glwe_compressed_encrypt_sk<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa);
@@ -119,7 +119,7 @@ pub fn test_glwe_compressed_encrypt_sk<B: Backend>(
ct_compressed.encrypt_sk(
module,
&pt_want,
&sk_exec,
&sk_prepared,
seed_xa,
&mut source_xe,
sigma,
@@ -129,11 +129,11 @@ pub fn test_glwe_compressed_encrypt_sk<B: Backend>(
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
ct.decompress(module, &ct_compressed);
ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow());
ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
pt_want.sub_inplace_ab(module, &pt_have);
let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0) * (ct.k() as f64).exp2();
let noise_have: f64 = pt_want.data.std(basek, 0) * (ct.k() as f64).exp2();
let noise_want: f64 = sigma;
assert!(
@@ -163,28 +163,28 @@ where
let mut source_xe: Source = Source::new([1u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GLWECiphertext::decrypt_scratch_space(module, n, basek, k_ct)
| GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(n, basek, k_ct, rank);
ct.encrypt_zero_sk(
module,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
ct.decrypt(module, &mut pt, &sk_exec, scratch.borrow());
ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
assert!((sigma - module.vec_znx_std(basek, &pt.data, 0) * (k_ct as f64).exp2()) <= 0.2);
assert!((sigma - pt.data.std(basek, 0) * (k_ct as f64).exp2()) <= 0.2);
}
pub fn test_glwe_encrypt_pk<B: Backend>(module: &Module<B>, basek: usize, k_ct: usize, k_pk: usize, sigma: f64, rank: usize)
@@ -210,38 +210,38 @@ where
let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xu: Source = Source::new([0u8; 32]);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let mut pk: GLWEPublicKey<Vec<u8>> = GLWEPublicKey::alloc(n, basek, k_pk, rank);
pk.generate_from_sk(module, &sk_exec, &mut source_xa, &mut source_xe, sigma);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k())
| GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k())
| GLWECiphertext::encrypt_pk_scratch_space(module, n, basek, pk.k()),
| GLWECiphertext::encrypt_pk_scratch_space(module, n, basek, k_pk),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut pk: GLWEPublicKey<Vec<u8>> = GLWEPublicKey::alloc(n, basek, k_pk, rank);
pk.generate_from_sk(module, &sk_prepared, &mut source_xa, &mut source_xe, sigma);
module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_ct, &mut source_xa);
let pk_exec: GLWEPublicKeyExec<Vec<u8>, B> = GLWEPublicKeyExec::from(module, &pk, scratch.borrow());
let pk_prepared: GLWEPublicKeyPrepared<Vec<u8>, B> = pk.prepare_alloc(module, scratch.borrow());
ct.encrypt_pk(
module,
&pt_want,
&pk_exec,
&pk_prepared,
&mut source_xu,
&mut source_xe,
sigma,
scratch.borrow(),
);
ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow());
ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
pt_want.sub_inplace_ab(module, &pt_have);
let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0).log2();
let noise_have: f64 = pt_want.data.std(basek, 0).log2();
let noise_want: f64 = ((((rank as f64) + 1.0) * n as f64 * 0.5 * sigma * sigma).sqrt()).log2() - (k_ct as f64);
assert!(

View File

@@ -1,6 +1,6 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxCopy, VecZnxDftAlloc, VecZnxStd,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxBigAlloc, VecZnxCopy, VecZnxDftAlloc,
VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScratchOwned, VecZnxDft},
@@ -12,20 +12,23 @@ use backend::hal::{
use sampling::source::Source;
use crate::{
layouts::{GGLWETensorKey, GLWEPlaintext, GLWESecret, Infos, compressed::GGLWETensorKeyCompressed, prepared::GLWESecretExec},
layouts::{
GGLWETensorKey, GLWEPlaintext, GLWESecret, Infos,
compressed::GGLWETensorKeyCompressed,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
trait_families::{Decompress, GLWEDecryptFamily},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_glwe_tensor_key_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, sigma: f64, rank: usize)
where
Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
@@ -64,8 +67,7 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
sk_exec.prepare(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
tensor_key.encrypt_sk(
module,
@@ -89,7 +91,7 @@ where
(0..rank).for_each(|i| {
(0..rank).for_each(|j| {
module.svp_apply(&mut sk_ij_dft, 0, &sk_exec.data, j, &sk_dft, i);
module.svp_apply(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
module.vec_znx_big_normalize(
basek,
@@ -104,11 +106,11 @@ where
tensor_key
.at(i, j)
.at(row_i, col_i)
.decrypt(module, &mut pt, &sk_exec, scratch.borrow());
.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i);
let std_pt: f64 = module.vec_znx_std(basek, &pt.data, 0) * (k as f64).exp2();
let std_pt: f64 = pt.data.std(basek, 0) * (k as f64).exp2();
assert!((sigma - std_pt).abs() <= 0.5, "{} {}", sigma, std_pt);
});
});
@@ -119,11 +121,10 @@ where
pub fn test_glwe_tensor_key_compressed_encrypt_sk<B: Backend>(module: &Module<B>, basek: usize, k: usize, sigma: f64, rank: usize)
where
Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
@@ -147,7 +148,8 @@ where
let n: usize = module.n();
let rows: usize = k / basek;
let mut tensor_key_compressed: GGLWETensorKeyCompressed<Vec<u8>> = GGLWETensorKeyCompressed::alloc(n, basek, k, rows, 1, rank);
let mut tensor_key_compressed: GGLWETensorKeyCompressed<Vec<u8>> =
GGLWETensorKeyCompressed::alloc(n, basek, k, rows, 1, rank);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
@@ -162,8 +164,7 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
sk_exec.prepare(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let seed_xa: [u8; 32] = [1u8; 32];
@@ -192,7 +193,7 @@ where
(0..rank).for_each(|i| {
(0..rank).for_each(|j| {
module.svp_apply(&mut sk_ij_dft, 0, &sk_exec.data, j, &sk_dft, i);
module.svp_apply(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
module.vec_znx_dft_to_vec_znx_big_tmp_a(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
module.vec_znx_big_normalize(
basek,
@@ -207,11 +208,11 @@ where
tensor_key
.at(i, j)
.at(row_i, col_i)
.decrypt(module, &mut pt, &sk_exec, scratch.borrow());
.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i);
let std_pt: f64 = module.vec_znx_std(basek, &pt.data, 0) * (k as f64).exp2();
let std_pt: f64 = pt.data.std(basek, 0) * (k as f64).exp2();
assert!((sigma - std_pt).abs() <= 0.5, "{} {}", sigma, std_pt);
});
});

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxStd,
VecZnxSubScalarInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxSubScalarInplace,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut,
},
layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned},
oep::{
@@ -13,14 +13,14 @@ use sampling::source::Source;
use crate::{
layouts::{
GGSWCiphertext, GLWESecret, GGLWESwitchingKey,
prepared::{GGSWCiphertextExec, GLWESecretExec},
GGLWESwitchingKey, GGSWCiphertext, GLWESecret,
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc},
},
noise::noise_ggsw_product,
trait_families::{GLWEDecryptFamily, GLWEExternalProductFamily},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_switching_key_external_product<B: Backend>(
module: &Module<B>,
@@ -34,11 +34,10 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
@@ -63,7 +62,8 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
let rows: usize = k_in.div_ceil(basek * digits);
let digits_in: usize = 1;
let mut ct_gglwe_in: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(n, basek, k_in, rows, digits_in, rank_in, rank_out);
let mut ct_gglwe_in: GGLWESwitchingKey<Vec<u8>> =
GGLWESwitchingKey::alloc(n, basek, k_in, rows, digits_in, rank_in, rank_out);
let mut ct_gglwe_out: GGLWESwitchingKey<Vec<u8>> =
GGLWESwitchingKey::alloc(n, basek, k_out, rows, digits_in, rank_in, rank_out);
let mut ct_rgsw: GGSWCiphertext<Vec<u8>> = GGSWCiphertext::alloc(n, basek, k_ggsw, rows, digits, rank_out);
@@ -91,7 +91,7 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_in.encrypt_sk(
@@ -107,20 +107,17 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
ct_rgsw.encrypt_sk(
module,
&pt_rgsw,
&sk_out_exec,
&sk_out_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut ct_rgsw_exec: GGSWCiphertextExec<Vec<u8>, B> =
GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank_out);
ct_rgsw_exec.prepare(module, &ct_rgsw, scratch.borrow());
let ct_rgsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow());
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_exec, scratch.borrow());
ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow());
(0..rank_in).for_each(|i| {
module.vec_znx_rotate_inplace(r as i64, &mut sk_in.data.as_vec_znx_mut(), i); // * X^{r}
@@ -149,7 +146,7 @@ pub fn test_gglwe_switching_key_external_product<B: Backend>(
ct_gglwe_out
.key
.assert_noise(module, &sk_out_exec, &sk_in.data, max_noise + 0.5);
.assert_noise(module, &sk_out_prepared, &sk_in.data, max_noise + 0.5);
}
pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
@@ -163,11 +160,10 @@ pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
@@ -219,7 +215,7 @@ pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe.encrypt_sk(
@@ -235,20 +231,17 @@ pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
ct_rgsw.encrypt_sk(
module,
&pt_rgsw,
&sk_out_exec,
&sk_out_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut ct_rgsw_exec: GGSWCiphertextExec<Vec<u8>, B> =
GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank_out);
ct_rgsw_exec.prepare(module, &ct_rgsw, scratch.borrow());
let ct_rgsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow());
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
ct_gglwe.external_product_inplace(module, &ct_rgsw_exec, scratch.borrow());
ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow());
(0..rank_in).for_each(|i| {
module.vec_znx_rotate_inplace(r as i64, &mut sk_in.data.as_vec_znx_mut(), i); // * X^{r}
@@ -277,5 +270,5 @@ pub fn test_gglwe_switching_key_external_product_inplace<B: Backend>(
ct_gglwe
.key
.assert_noise(module, &sk_out_exec, &sk_in.data, max_noise + 0.5);
.assert_noise(module, &sk_out_prepared, &sk_in.data, max_noise + 0.5);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxStd,
VecZnxSubABInplace, VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxRotateInplace, VecZnxSubABInplace,
VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut,
},
layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned},
oep::{
@@ -14,7 +14,7 @@ use sampling::source::Source;
use crate::{
layouts::{
GGSWCiphertext, GLWESecret,
prepared::{GGSWCiphertextExec, GLWESecretExec},
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc},
},
noise::noise_ggsw_product,
trait_families::GGSWAssertNoiseFamily,
@@ -22,7 +22,7 @@ use crate::{
use crate::trait_families::{
GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GGSWKeySwitchFamily,
GLWESecretExecModuleFamily,
GLWESecretPreparedModuleFamily,
};
pub fn test_ggsw_external_product<B: Backend>(
@@ -35,12 +35,11 @@ pub fn test_ggsw_external_product<B: Backend>(
rank: usize,
sigma: f64,
) where
Module<B>: GLWESecretExecModuleFamily<B>
Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
@@ -89,12 +88,12 @@ pub fn test_ggsw_external_product<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct_ggsw_rhs.encrypt_sk(
module,
&pt_ggsw_rhs,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
@@ -104,17 +103,16 @@ pub fn test_ggsw_external_product<B: Backend>(
ct_ggsw_lhs_in.encrypt_sk(
module,
&pt_ggsw_lhs,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut ct_rhs_exec: GGSWCiphertextExec<Vec<u8>, B> = GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank);
ct_rhs_exec.prepare(module, &ct_ggsw_rhs, scratch.borrow());
let ct_rhs_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_ggsw_rhs.prepare_alloc(module, scratch.borrow());
ct_ggsw_lhs_out.external_product(module, &ct_ggsw_lhs_in, &ct_rhs_exec, scratch.borrow());
ct_ggsw_lhs_out.external_product(module, &ct_ggsw_lhs_in, &ct_rhs_prepared, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_ggsw_lhs.as_vec_znx_mut(), 0);
@@ -141,7 +139,7 @@ pub fn test_ggsw_external_product<B: Backend>(
) + 0.5
};
ct_ggsw_lhs_out.assert_noise(module, &sk_exec, &pt_ggsw_lhs, &max_noise);
ct_ggsw_lhs_out.assert_noise(module, &sk_prepared, &pt_ggsw_lhs, &max_noise);
}
pub fn test_ggsw_external_product_inplace<B: Backend>(
@@ -153,12 +151,11 @@ pub fn test_ggsw_external_product_inplace<B: Backend>(
rank: usize,
sigma: f64,
) where
Module<B>: GLWESecretExecModuleFamily<B>
Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
@@ -207,12 +204,12 @@ pub fn test_ggsw_external_product_inplace<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct_ggsw_rhs.encrypt_sk(
module,
&pt_ggsw_rhs,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
@@ -222,17 +219,16 @@ pub fn test_ggsw_external_product_inplace<B: Backend>(
ct_ggsw_lhs.encrypt_sk(
module,
&pt_ggsw_lhs,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let mut ct_rhs_exec: GGSWCiphertextExec<Vec<u8>, B> = GGSWCiphertextExec::alloc(module, n, basek, k_ggsw, rows, digits, rank);
ct_rhs_exec.prepare(module, &ct_ggsw_rhs, scratch.borrow());
let ct_rhs_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_ggsw_rhs.prepare_alloc(module, scratch.borrow());
ct_ggsw_lhs.external_product_inplace(module, &ct_rhs_exec, scratch.borrow());
ct_ggsw_lhs.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_ggsw_lhs.as_vec_znx_mut(), 0);
@@ -259,5 +255,5 @@ pub fn test_ggsw_external_product_inplace<B: Backend>(
) + 0.5
};
ct_ggsw_lhs.assert_noise(module, &sk_exec, &pt_ggsw_lhs, &max_noise);
ct_ggsw_lhs.assert_noise(module, &sk_prepared, &pt_ggsw_lhs, &max_noise);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxRotateInplace, VecZnxStd,
VmpPMatAlloc, VmpPMatPrepare, ZnxViewMut,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxRotateInplace, VmpPMatAlloc,
VmpPMatPrepare, ZnxViewMut,
},
layouts::{Backend, Module, ScalarZnx, ScratchOwned},
oep::{
@@ -14,12 +14,12 @@ use sampling::source::Source;
use crate::{
layouts::{
GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
prepared::{GGSWCiphertextExec, GLWESecretExec},
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc},
},
noise::noise_ggsw_product,
};
use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptSkFamily, GLWEExternalProductFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GLWEDecryptFamily, GLWEEncryptSkFamily, GLWEExternalProductFamily, GLWESecretPreparedModuleFamily};
pub fn test_glwe_external_product<B: Backend>(
module: &Module<B>,
@@ -32,12 +32,11 @@ pub fn test_glwe_external_product<B: Backend>(
sigma: f64,
) where
Module<B>: GLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ GLWEExternalProductFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxRotateInplace
+ VecZnxStd
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
B: TakeVecZnxDftImpl<B>
@@ -88,12 +87,12 @@ pub fn test_glwe_external_product<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct_ggsw.encrypt_sk(
module,
&pt_rgsw,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
@@ -103,16 +102,16 @@ pub fn test_glwe_external_product<B: Backend>(
ct_glwe_in.encrypt_sk(
module,
&pt_want,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let ct_ggsw_exec: GGSWCiphertextExec<Vec<u8>, B> = GGSWCiphertextExec::from(module, &ct_ggsw, scratch.borrow());
let ct_ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_ggsw.prepare_alloc(module, scratch.borrow());
ct_glwe_out.external_product(module, &ct_glwe_in, &ct_ggsw_exec, scratch.borrow());
ct_glwe_out.external_product(module, &ct_glwe_in, &ct_ggsw_prepared, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
@@ -137,7 +136,7 @@ pub fn test_glwe_external_product<B: Backend>(
k_ggsw,
);
ct_glwe_out.assert_noise(module, &sk_exec, &pt_want, max_noise + 0.5);
ct_glwe_out.assert_noise(module, &sk_prepared, &pt_want, max_noise + 0.5);
}
pub fn test_glwe_external_product_inplace<B: Backend>(
@@ -150,12 +149,11 @@ pub fn test_glwe_external_product_inplace<B: Backend>(
sigma: f64,
) where
Module<B>: GLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ GLWEExternalProductFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxRotateInplace
+ VecZnxStd
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>,
B: TakeVecZnxDftImpl<B>
@@ -196,12 +194,12 @@ pub fn test_glwe_external_product_inplace<B: Backend>(
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
ct_ggsw.encrypt_sk(
module,
&pt_rgsw,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
@@ -211,16 +209,16 @@ pub fn test_glwe_external_product_inplace<B: Backend>(
ct_glwe.encrypt_sk(
module,
&pt_want,
&sk_exec,
&sk_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let ct_ggsw_exec: GGSWCiphertextExec<Vec<u8>, B> = GGSWCiphertextExec::from(module, &ct_ggsw, scratch.borrow());
let ct_ggsw_prepared: GGSWCiphertextPrepared<Vec<u8>, B> = ct_ggsw.prepare_alloc(module, scratch.borrow());
ct_glwe.external_product_inplace(module, &ct_ggsw_exec, scratch.borrow());
ct_glwe.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
@@ -245,5 +243,5 @@ pub fn test_glwe_external_product_inplace<B: Backend>(
k_ggsw,
);
ct_glwe.assert_noise(module, &sk_exec, &pt_want, max_noise + 0.5);
ct_glwe.assert_noise(module, &sk_prepared, &pt_want, max_noise + 0.5);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubScalarInplace,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubScalarInplace, VecZnxSwithcDegree,
VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -13,14 +13,14 @@ use sampling::source::Source;
use crate::{
layouts::{
GLWESecret, GGLWESwitchingKey,
prepared::{GLWESecretExec, GGLWESwitchingKeyExec},
GGLWESwitchingKey, GLWESecret,
prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc},
},
noise::log2_std_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
module: &Module<B>,
@@ -35,11 +35,10 @@ pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
@@ -109,7 +108,7 @@ pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out_s1s2);
sk2.fill_ternary_prob(0.5, &mut source_xs);
let sk2_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk2);
let sk2_prepared: GLWESecretPrepared<Vec<u8>, B> = sk2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_s0s1.encrypt_sk(
@@ -133,14 +132,14 @@ pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
scratch_enc.borrow(),
);
let ct_gglwe_s1s2_exec: GGLWESwitchingKeyExec<Vec<u8>, B> =
GGLWESwitchingKeyExec::from(module, &ct_gglwe_s1s2, scratch_apply.borrow());
let ct_gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> =
ct_gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
ct_gglwe_s0s2.keyswitch(
module,
&ct_gglwe_s0s1,
&ct_gglwe_s1s2_exec,
&ct_gglwe_s1s2_prepared,
scratch_apply.borrow(),
);
@@ -159,7 +158,7 @@ pub fn test_gglwe_switching_key_keyswitch<B: Backend>(
ct_gglwe_s0s2
.key
.assert_noise(module, &sk2_exec, &sk0.data, max_noise + 0.5);
.assert_noise(module, &sk2_prepared, &sk0.data, max_noise + 0.5);
}
pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
@@ -173,11 +172,10 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWEEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxStd
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
@@ -203,7 +201,8 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
let mut ct_gglwe_s0s1: GGLWESwitchingKey<Vec<u8>> =
GGLWESwitchingKey::alloc(n, basek, k_ct, rows, digits_in, rank_in, rank_out);
let mut ct_gglwe_s1s2: GGLWESwitchingKey<Vec<u8>> = GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank_out, rank_out);
let mut ct_gglwe_s1s2: GGLWESwitchingKey<Vec<u8>> =
GGLWESwitchingKey::alloc(n, basek, k_ksk, rows, digits, rank_out, rank_out);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
@@ -231,7 +230,7 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk2.fill_ternary_prob(var_xs, &mut source_xs);
let sk2_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk2);
let sk2_prepared: GLWESecretPrepared<Vec<u8>, B> = sk2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_s0s1.encrypt_sk(
@@ -255,11 +254,11 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
scratch_enc.borrow(),
);
let ct_gglwe_s1s2_exec: GGLWESwitchingKeyExec<Vec<u8>, B> =
GGLWESwitchingKeyExec::from(module, &ct_gglwe_s1s2, scratch_apply.borrow());
let ct_gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> =
ct_gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow());
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
ct_gglwe_s0s1.keyswitch_inplace(module, &ct_gglwe_s1s2_exec, scratch_apply.borrow());
ct_gglwe_s0s1.keyswitch_inplace(module, &ct_gglwe_s1s2_prepared, scratch_apply.borrow());
let ct_gglwe_s0s2: GGLWESwitchingKey<Vec<u8>> = ct_gglwe_s0s1;
@@ -278,5 +277,5 @@ pub fn test_gglwe_switching_key_keyswitch_inplace<B: Backend>(
ct_gglwe_s0s2
.key
.assert_noise(module, &sk2_exec, &sk0.data, max_noise + 0.5);
.assert_noise(module, &sk2_prepared, &sk0.data, max_noise + 0.5);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxStd, VecZnxSubABInplace,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxCopy, VecZnxSubABInplace, VecZnxSwithcDegree,
VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScalarZnx, ScratchOwned},
oep::{
@@ -13,8 +13,8 @@ use sampling::source::Source;
use crate::{
layouts::{
GGLWETensorKey, GGSWCiphertext, GLWESecret, GGLWESwitchingKey,
prepared::{GLWESecretExec, GGLWESwitchingKeyExec, GGLWETensorKeyExec},
GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWESecret,
prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc},
},
noise::noise_ggsw_keyswitch,
trait_families::GGSWAssertNoiseFamily,
@@ -22,7 +22,7 @@ use crate::{
use crate::trait_families::{
GGLWESwitchingKeyEncryptSkFamily, GGLWETensorKeyEncryptSkFamily, GGSWEncryptSkFamily, GGSWKeySwitchFamily,
GLWESecretExecModuleFamily,
GLWESecretPreparedModuleFamily,
};
pub fn test_ggsw_keyswitch<B: Backend>(
@@ -36,12 +36,11 @@ pub fn test_ggsw_keyswitch<B: Backend>(
rank: usize,
sigma: f64,
) where
Module<B>: GLWESecretExecModuleFamily<B>
Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
@@ -93,11 +92,11 @@ pub fn test_ggsw_keyswitch<B: Backend>(
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let sk_in_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_in);
let sk_in_dft: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk(
module,
@@ -129,14 +128,16 @@ pub fn test_ggsw_keyswitch<B: Backend>(
scratch.borrow(),
);
let mut ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> =
GGLWESwitchingKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank, rank);
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank);
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
let tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = tsk.prepare_alloc(module, scratch.borrow());
ksk_exec.prepare(module, &ksk, scratch.borrow());
tsk_exec.prepare(module, &tsk, scratch.borrow());
ct_out.keyswitch(module, &ct_in, &ksk_exec, &tsk_exec, scratch.borrow());
ct_out.keyswitch(
module,
&ct_in,
&ksk_prepared,
&tsk_prepared,
scratch.borrow(),
);
let max_noise = |col_j: usize| -> f64 {
noise_ggsw_keyswitch(
@@ -154,7 +155,7 @@ pub fn test_ggsw_keyswitch<B: Backend>(
) + 0.5
};
ct_out.assert_noise(module, &sk_out_exec, &pt_scalar, &max_noise);
ct_out.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise);
}
pub fn test_ggsw_keyswitch_inplace<B: Backend>(
@@ -167,12 +168,11 @@ pub fn test_ggsw_keyswitch_inplace<B: Backend>(
rank: usize,
sigma: f64,
) where
Module<B>: GLWESecretExecModuleFamily<B>
Module<B>: GLWESecretPreparedModuleFamily<B>
+ GGSWEncryptSkFamily<B>
+ GGSWAssertNoiseFamily<B>
+ VecZnxAddScalarInplace
+ VecZnxSubABInplace
+ VecZnxStd
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
@@ -218,11 +218,11 @@ pub fn test_ggsw_keyswitch_inplace<B: Backend>(
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let sk_in_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_in);
let sk_in_dft: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk(
module,
@@ -254,14 +254,10 @@ pub fn test_ggsw_keyswitch_inplace<B: Backend>(
scratch.borrow(),
);
let mut ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> =
GGLWESwitchingKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank, rank);
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = GGLWETensorKeyExec::alloc(module, n, basek, k_ksk, rows, digits, rank);
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
let tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = tsk.prepare_alloc(module, scratch.borrow());
ksk_exec.prepare(module, &ksk, scratch.borrow());
tsk_exec.prepare(module, &tsk, scratch.borrow());
ct.keyswitch_inplace(module, &ksk_exec, &tsk_exec, scratch.borrow());
ct.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow());
let max_noise = |col_j: usize| -> f64 {
noise_ggsw_keyswitch(
@@ -279,5 +275,5 @@ pub fn test_ggsw_keyswitch_inplace<B: Backend>(
) + 0.5
};
ct.assert_noise(module, &sk_out_exec, &pt_scalar, &max_noise);
ct.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxStd, VecZnxSwithcDegree,
VmpPMatAlloc, VmpPMatPrepare,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxFillUniform, VecZnxSwithcDegree, VmpPMatAlloc,
VmpPMatPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -13,14 +13,14 @@ use sampling::source::Source;
use crate::{
layouts::{
GLWECiphertext, GLWEPlaintext, GLWESecret, GGLWESwitchingKey, Infos,
prepared::{GLWESecretExec, GGLWESwitchingKeyExec},
GGLWESwitchingKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc},
},
noise::log2_std_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub fn test_glwe_keyswitch<B: Backend>(
module: &Module<B>,
@@ -34,10 +34,9 @@ pub fn test_glwe_keyswitch<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxStd
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VmpPMatAlloc<B>
@@ -83,11 +82,11 @@ pub fn test_glwe_keyswitch<B: Backend>(
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_in);
let sk_in_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk(
module,
@@ -102,16 +101,16 @@ pub fn test_glwe_keyswitch<B: Backend>(
ct_in.encrypt_sk(
module,
&pt_want,
&sk_in_exec,
&sk_in_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = GGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow());
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
ct_out.keyswitch(module, &ct_in, &ksk_exec, scratch.borrow());
ct_out.keyswitch(module, &ct_in, &ksk_prepared, scratch.borrow());
let max_noise: f64 = log2_std_noise_gglwe_product(
module.n() as f64,
@@ -126,7 +125,7 @@ pub fn test_glwe_keyswitch<B: Backend>(
k_ksk,
);
ct_out.assert_noise(module, &sk_out_exec, &pt_want, max_noise + 0.5);
ct_out.assert_noise(module, &sk_out_prepared, &pt_want, max_noise + 0.5);
}
pub fn test_glwe_keyswitch_inplace<B: Backend>(
@@ -139,10 +138,9 @@ pub fn test_glwe_keyswitch_inplace<B: Backend>(
sigma: f64,
) where
Module<B>: GGLWESwitchingKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxStd
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VmpPMatAlloc<B>
@@ -177,11 +175,11 @@ pub fn test_glwe_keyswitch_inplace<B: Backend>(
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_in);
let sk_in_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let sk_out_exec: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk_out);
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
ksk.encrypt_sk(
module,
@@ -196,16 +194,16 @@ pub fn test_glwe_keyswitch_inplace<B: Backend>(
ct_glwe.encrypt_sk(
module,
&pt_want,
&sk_in_exec,
&sk_in_prepared,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
let ksk_exec: GGLWESwitchingKeyExec<Vec<u8>, B> = GGLWESwitchingKeyExec::from(module, &ksk, scratch.borrow());
let ksk_prepared: GGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
ct_glwe.keyswitch_inplace(module, &ksk_exec, scratch.borrow());
ct_glwe.keyswitch_inplace(module, &ksk_prepared, scratch.borrow());
let max_noise: f64 = log2_std_noise_gglwe_product(
module.n() as f64,
@@ -220,5 +218,5 @@ pub fn test_glwe_keyswitch_inplace<B: Backend>(
k_ksk,
);
ct_glwe.assert_noise(module, &sk_out_exec, &pt_want, max_noise + 0.5);
ct_glwe.assert_noise(module, &sk_out_prepared, &pt_want, max_noise + 0.5);
}

View File

@@ -1,7 +1,7 @@
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxEncodeCoeffsi64,
VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView,
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxSwithcDegree,
VmpPMatAlloc, VmpPMatPrepare, ZnxView,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -11,9 +11,12 @@ use backend::hal::{
};
use sampling::source::Source;
use crate::layouts::{Infos, LWECiphertext, LWEPlaintext, LWESecret, LWESwitchingKey, prepared::LWESwitchingKeyExec};
use crate::layouts::{
Infos, LWECiphertext, LWEPlaintext, LWESecret, LWESwitchingKey,
prepared::{LWESwitchingKeyPrepared, PrepareAlloc},
};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWEDecryptFamily, GLWEKeyswitchFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWEEncryptSkFamily, GLWEDecryptFamily, GLWEKeyswitchFamily, GLWESecretPreparedModuleFamily};
pub fn test_lwe_keyswitch<B: Backend>(module: &Module<B>)
where
@@ -22,11 +25,10 @@ where
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ GLWEKeyswitchFamily<B>
+ VecZnxEncodeCoeffsi64
+ VecZnxAutomorphismInplace
+ VmpPMatAlloc<B>
+ VmpPMatPrepare<B>
+ GLWESecretExecModuleFamily<B>,
+ GLWESecretPreparedModuleFamily<B>,
B: TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B>
+ ScratchAvailableImpl<B>
@@ -65,7 +67,8 @@ where
let data: i64 = 17;
let mut lwe_pt_in: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
module.encode_coeff_i64(basek, &mut lwe_pt_in.data, 0, k_lwe_pt, 0, data, k_lwe_pt);
lwe_pt_in.encode_i64(data, k_lwe_pt);
let mut lwe_ct_in: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe_in, basek, k_lwe_ct);
lwe_ct_in.encrypt_sk(
@@ -91,9 +94,9 @@ where
let mut lwe_ct_out: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe_out, basek, k_lwe_ct);
let ksk_exec: LWESwitchingKeyExec<Vec<u8>, B> = LWESwitchingKeyExec::from(module, &ksk, scratch.borrow());
let ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_exec, scratch.borrow());
lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow());
let mut lwe_pt_out: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct);
lwe_ct_out.decrypt(module, &mut lwe_pt_out, &sk_lwe_out);

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigSubSmallBInplace,
VecZnxEncodeVeci64, VecZnxRotateInplace, VecZnxStd, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
VecZnxRotateInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -17,22 +17,20 @@ use crate::{
GLWEOperations, GLWEPacker,
layouts::{
GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec},
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
},
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily, GLWEPackingFamily},
};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub trait PackingTestModuleFamily<B: Backend> = GLWEPackingFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GGLWESwitchingKeyEncryptSkFamily<B>
+ GLWEKeyswitchFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxStd
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxEncodeVeci64
+ VecZnxRotateInplace
+ VecZnxAutomorphism
+ VecZnxBigSubSmallBInplace<B>
@@ -76,7 +74,7 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(n, basek, k_ct);
let mut data: Vec<i64> = vec![0i64; n];
@@ -84,11 +82,11 @@ where
*x = i as i64;
});
module.encode_vec_i64(basek, &mut pt.data, 0, pt_k, &data, 32);
pt.encode_vec_i64(&data, pt_k);
let gal_els: Vec<i64> = GLWEPacker::galois_elements(module);
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyExec<Vec<u8>, B>> = HashMap::new();
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_ksk, rows, digits, rank);
gal_els.iter().for_each(|gal_el| {
tmp.encrypt_sk(
@@ -100,8 +98,8 @@ where
sigma,
scratch.borrow(),
);
let atk_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = GGLWEAutomorphismKeyExec::from(module, &tmp, scratch.borrow());
auto_keys.insert(*gal_el, atk_exec);
let atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow());
auto_keys.insert(*gal_el, atk_prepared);
});
let log_batch: usize = 0;
@@ -158,13 +156,13 @@ where
}
});
module.encode_vec_i64(basek, &mut pt_want.data, 0, pt_k, &data, 32);
pt_want.encode_vec_i64(&data, pt_k);
res.decrypt(module, &mut pt, &sk_dft, scratch.borrow());
pt.sub_inplace_ab(module, &pt_want);
let noise_have: f64 = module.vec_znx_std(basek, &pt.data, 0).log2();
let noise_have: f64 = pt.std().log2();
// println!("noise_have: {}", noise_have);
assert!(
noise_have < -((k_ct - basek) as f64),

View File

@@ -3,9 +3,8 @@ use std::collections::HashMap;
use backend::hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigAutomorphismInplace,
VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxEncodeVeci64, VecZnxFillUniform, VecZnxNormalizeInplace,
VecZnxRotateInplace, VecZnxRshInplace, VecZnxStd, VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare,
ZnxView, ZnxViewMut,
VecZnxBigSubSmallBInplace, VecZnxCopy, VecZnxFillUniform, VecZnxNormalizeInplace, VecZnxRotateInplace, VecZnxRshInplace,
VecZnxSubABInplace, VecZnxSwithcDegree, VmpPMatAlloc, VmpPMatPrepare, ZnxView, ZnxViewMut,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
@@ -18,22 +17,20 @@ use sampling::source::Source;
use crate::{
layouts::{
GGLWEAutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos,
prepared::{GGLWEAutomorphismKeyExec, GLWESecretExec},
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc},
},
noise::var_noise_gglwe_product,
trait_families::{GLWEDecryptFamily, GLWEKeyswitchFamily},
};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretExecModuleFamily};
use crate::trait_families::{GGLWESwitchingKeyEncryptSkFamily, GLWESecretPreparedModuleFamily};
pub trait TraceTestModuleFamily<B: Backend> = GGLWESwitchingKeyEncryptSkFamily<B>
+ GLWESecretExecModuleFamily<B>
+ GLWESecretPreparedModuleFamily<B>
+ GLWEKeyswitchFamily<B>
+ GLWEDecryptFamily<B>
+ VecZnxStd
+ VecZnxSwithcDegree
+ VecZnxAddScalarInplace
+ VecZnxEncodeVeci64
+ VecZnxRotateInplace
+ VecZnxBigSubSmallBInplace<B>
+ VecZnxBigAutomorphismInplace<B>
@@ -80,7 +77,7 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretExec<Vec<u8>, B> = GLWESecretExec::from(module, &sk);
let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
let mut data_want: Vec<i64> = vec![0i64; n];
@@ -100,7 +97,7 @@ where
scratch.borrow(),
);
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyExec<Vec<u8>, B>> = HashMap::new();
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new();
let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module);
let mut tmp: GGLWEAutomorphismKey<Vec<u8>> = GGLWEAutomorphismKey::alloc(n, basek, k_autokey, rows, digits, rank);
gal_els.iter().for_each(|gal_el| {
@@ -113,8 +110,8 @@ where
sigma,
scratch.borrow(),
);
let atk_exec: GGLWEAutomorphismKeyExec<Vec<u8>, B> = GGLWEAutomorphismKeyExec::from(module, &tmp, scratch.borrow());
auto_keys.insert(*gal_el, atk_exec);
let atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = tmp.prepare_alloc(module, scratch.borrow());
auto_keys.insert(*gal_el, atk_prepared);
});
ct.trace_inplace(module, 0, 5, &auto_keys, scratch.borrow());
@@ -127,7 +124,7 @@ where
module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
module.vec_znx_normalize_inplace(basek, &mut pt_want.data, 0, scratch.borrow());
let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0).log2();
let noise_have: f64 = pt_want.std().log2();
let mut noise_want: f64 = var_noise_gglwe_product(
n as f64,

View File

@@ -1,11 +1,11 @@
use backend::hal::tests::serialization::test_reader_writer_interface;
use crate::layouts::{
GGLWEAutomorphismKey, GGLWECiphertext, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GGLWESwitchingKey,
GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWECiphertext,
GLWEToLWESwitchingKey, LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey,
compressed::{
GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed,
GGLWESwitchingKeyCompressed, GGLWETensorKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed,
GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGLWESwitchingKeyCompressed, GGLWETensorKeyCompressed,
GGSWCiphertextCompressed, GLWECiphertextCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed,
LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed,
},
};

View File

@@ -89,8 +89,8 @@ pub trait GLWEPackingFamily<B: Backend> = GLWEKeyswitchFamily<B>
+ VecZnxAutomorphismInplace
+ VecZnxBigSubSmallBInplace<B>
+ VecZnxBigAutomorphismInplace<B>;
pub trait GLWESecretExecModuleFamily<B: Backend> = SvpPrepare<B> + SvpPPolAllocBytes + SvpPPolAlloc<B>;
pub trait GGLWEExecLayoutFamily<B: Backend> = VmpPMatAlloc<B> + VmpPMatAllocBytes + VmpPMatPrepare<B>;
pub trait GLWESecretPreparedModuleFamily<B: Backend> = SvpPrepare<B> + SvpPPolAllocBytes + SvpPPolAlloc<B>;
pub trait GGLWEPreparedLayoutFamily<B: Backend> = VmpPMatAlloc<B> + VmpPMatAllocBytes + VmpPMatPrepare<B>;
pub trait GLWETraceModuleFamily<B: Backend> =
GLWEKeyswitchFamily<B> + VecZnxCopy + VecZnxRshInplace + VecZnxBigAutomorphismInplace<B>;
pub trait GLWETraceScratchFamily<B: Backend> = TakeVecZnxDft<B> + ScratchAvailable;

55
core/src/utils.rs Normal file
View File

@@ -0,0 +1,55 @@
use crate::layouts::{GLWEPlaintext, Infos, LWEPlaintext};
use backend::hal::layouts::{DataMut, DataRef};
use rug::Float;
impl<D: DataMut> GLWEPlaintext<D> {
pub fn encode_vec_i64(&mut self, data: &[i64], k: usize) {
let basek: usize = self.basek();
self.data
.encode_vec_i64(basek, 0, k, data, i64::BITS as usize);
}
pub fn encode_coeff_i64(&mut self, data: i64, k: usize, idx: usize) {
let basek: usize = self.basek();
self.data
.encode_coeff_i64(basek, 0, k, idx, data, i64::BITS as usize);
}
}
impl<D: DataRef> GLWEPlaintext<D> {
pub fn decode_vec_i64(&self, data: &mut [i64], k: usize) {
self.data.decode_vec_i64(self.basek(), 0, k, data);
}
pub fn decode_coeff_i64(&self, k: usize, idx: usize) -> i64 {
self.data.decode_coeff_i64(self.basek(), 0, k, idx)
}
pub fn decode_vec_float(&self, data: &mut [Float]) {
self.data.decode_vec_float(self.basek(), 0, data);
}
pub fn std(&self) -> f64 {
self.data.std(self.basek(), 0)
}
}
impl<D: DataMut> LWEPlaintext<D> {
pub fn encode_i64(&mut self, data: i64, k: usize) {
let basek: usize = self.basek();
self.data
.encode_coeff_i64(basek, 0, k, 0, data, i64::BITS as usize);
}
}
impl<D: DataRef> LWEPlaintext<D> {
pub fn decode_i64(&self, k: usize) -> i64 {
self.data.decode_coeff_i64(self.basek(), 0, k, 0)
}
pub fn decode_float(&self) -> Float {
let mut data: Vec<Float> = vec![Float::new(self.k() as u32)];
self.data.decode_vec_float(self.basek(), 0, &mut data);
data[0].clone()
}
}