keyswitch tests

This commit is contained in:
Pro7ech
2025-10-20 15:32:52 +02:00
parent 0c894c19db
commit 252eda36fe
60 changed files with 918 additions and 945 deletions

View File

@@ -5,11 +5,10 @@ use poulpy_hal::{
};
use crate::{
ScratchTakeCore,
encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk,
GGLWECompressedEncryptSk, ScratchTakeCore,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut},
GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared,
GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, compressed::AutomorphismKeyCompressed,
},
};
@@ -34,7 +33,7 @@ impl<DataSelf: DataMut> AutomorphismKeyCompressed<DataSelf> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
S: GLWESecretToRef,
S: GLWESecretToRef + GLWEInfos,
M: AutomorphismKeyCompressedEncryptSk<BE>,
{
module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch);
@@ -55,20 +54,28 @@ pub trait AutomorphismKeyCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: AutomorphismKeyCompressedToMut,
S: GLWESecretToRef;
R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef + GLWEInfos;
}
impl<BE: Backend> AutomorphismKeyCompressedEncryptSk<BE> for Module<BE>
where
Self: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk<BE>,
Self: ModuleN
+ GaloisElement
+ VecZnxAutomorphism
+ GGLWECompressedEncryptSk<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWESecretPrepare<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of(self.n().into(), infos.rank())
assert_eq!(self.n() as u32, infos.n());
self.gglwe_compressed_encrypt_sk_tmp_bytes(infos)
.max(GLWESecret::bytes_of_from_infos(infos))
+ GLWESecretPrepared::bytes_of_from_infos(self, infos)
}
fn automorphism_key_compressed_encrypt_sk<R, S>(
@@ -80,12 +87,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: AutomorphismKeyCompressedToMut,
S: GLWESecretToRef,
R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef + GLWEInfos,
{
let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
assert_eq!(res.n(), sk.n());
assert_eq!(res.rank_out(), res.rank_in());
assert_eq!(sk.rank(), res.rank_out());
@@ -96,9 +101,9 @@ where
AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res)
);
let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank());
let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank());
{
let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank());
for i in 0..res.rank_out().into() {
self.vec_znx_automorphism(
self.galois_element_inv(p),
@@ -108,10 +113,18 @@ where
i,
);
}
sk_out_prepared.prepare(self, &sk_out);
}
self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1);
self.gglwe_compressed_encrypt_sk(
res,
&sk.data,
&sk_out_prepared,
seed_xa,
source_xe,
scratch_1,
);
res.p = p;
res.set_p(p);
}
}

View File

@@ -8,8 +8,9 @@ use crate::{
ScratchTakeCore,
encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut},
GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef,
GLWESwitchingKeyDegreesMut, LWEInfos,
compressed::GLWESwitchingKeyCompressed,
prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
},
};
@@ -57,7 +58,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWESwitchingKeyCompressedToMut,
R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef,
S2: GLWESecretToRef;
}
@@ -86,11 +87,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWESwitchingKeyCompressedToMut,
R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef,
S2: GLWESecretToRef,
{
let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
@@ -122,15 +122,9 @@ where
}
}
self.gglwe_compressed_encrypt_sk(
&mut res.key,
&sk_in_tmp,
&sk_out_tmp,
seed_xa,
source_xe,
scratch_2,
);
res.sk_in_n = sk_in.n().into();
res.sk_out_n = sk_out.n().into();
self.gglwe_compressed_encrypt_sk(res, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2);
*res.input_degree() = sk_in.n();
*res.output_degree() = sk_out.n();
}
}

View File

@@ -4,16 +4,15 @@ use poulpy_hal::{
VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA,
},
layouts::{Backend, DataMut, Module, Scratch},
oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl},
source::Source,
};
use crate::{
GetDistribution, ScratchTakeCore,
encryption::{compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, gglwe_tsk::TensorKeyEncryptSk},
GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore,
encryption::gglwe_tsk::TensorKeyEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank,
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, Rank,
TensorKeyCompressedAtMut, compressed::TensorKeyCompressed,
},
};
@@ -23,7 +22,7 @@ impl TensorKeyCompressed<Vec<u8>> {
A: GGLWEInfos,
M: GGLWETensorKeyCompressedEncryptSk<BE>,
{
module.gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(infos)
module.tensor_key_compressed_encrypt_sk_tmp_bytes(infos)
}
}
@@ -39,16 +38,16 @@ impl<DataSelf: DataMut> TensorKeyCompressed<DataSelf> {
S: GLWESecretToRef + GetDistribution,
M: GGLWETensorKeyCompressedEncryptSk<BE>,
{
module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch);
module.tensor_key_compressed_encrypt_sk(self, sk, seed_xa, source_xe, scratch);
}
}
pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> {
fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
fn tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
fn gglwe_tensor_key_encrypt_sk<R, S>(
fn tensor_key_compressed_encrypt_sk<R, S, D>(
&self,
res: &mut R,
sk: &S,
@@ -56,36 +55,40 @@ pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: TensorKeyCompressedToMut,
D: DataMut,
R: TensorKeyCompressedAtMut<D> + GGLWEInfos,
S: GLWESecretToRef + GetDistribution;
}
impl<BE: Backend> GGLWETensorKeyCompressedEncryptSk<BE> for Module<BE>
where
Self: ModuleN
+ GLWESwitchingKeyCompressedEncryptSk<BE>
+ GGLWECompressedEncryptSk<BE>
+ TensorKeyEncryptSk<BE>
+ VecZnxDftApply<BE>
+ SvpApplyDftToDft<BE>
+ VecZnxIdftApplyTmpA<BE>
+ VecZnxBigNormalize<BE>
+ SvpPrepare<BE>
+ SvpPPolAllocBytesImpl<BE>
+ SvpPPolBytesOf
+ VecZnxDftAllocBytesImpl<BE>
+ VecZnxBigAllocBytesImpl<BE>
+ VecZnxDftBytesOf
+ VecZnxBigBytesOf,
+ VecZnxBigBytesOf
+ GLWESecretPreparedAlloc<BE>,
Scratch<BE>: ScratchTakeBasic + ScratchTakeCore<BE>,
{
fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
fn tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.tensor_key_encrypt_sk_tmp_bytes(infos)
GLWESecretPrepared::bytes_of(self, infos.rank_out())
+ self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1)
+ self.bytes_of_vec_znx_big(1, 1)
+ self.bytes_of_vec_znx_dft(1, 1)
+ GLWESecret::bytes_of(self.n().into(), Rank(1))
+ self.gglwe_compressed_encrypt_sk_tmp_bytes(infos)
}
fn gglwe_tensor_key_encrypt_sk<R, S>(
fn tensor_key_compressed_encrypt_sk<R, S, D>(
&self,
res: &mut R,
sk: &S,
@@ -93,11 +96,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: TensorKeyCompressedToMut,
D: DataMut,
R: GGLWEInfos + TensorKeyCompressedAtMut<D>,
S: GLWESecretToRef + GetDistribution,
{
let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut();
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out());
sk_dft_prep.prepare(self, sk);
@@ -141,10 +143,10 @@ where
let (seed_xa_tmp, _) = source_xa.branch();
self.glwe_switching_key_compressed_encrypt_sk(
self.gglwe_compressed_encrypt_sk(
res.at_mut(i, j),
&sk_ij,
sk,
&sk_ij.data,
&sk_dft_prep,
seed_xa_tmp,
source_xe,
scratch_5,

View File

@@ -1,6 +1,6 @@
use poulpy_hal::{
api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace},
layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero},
layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero},
source::Source,
};
@@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GGSWCompressedToMut + GGSWCompressedSeedMut,
R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>;
}
@@ -83,30 +83,29 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GGSWCompressedToMut + GGSWCompressedSeedMut,
R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>,
{
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()];
let base2k: usize = res.base2k().into();
let rank: usize = res.rank().into();
let cols: usize = rank + 1;
let dsize: usize = res.dsize().into();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
assert_eq!(res.rank(), sk.rank());
assert_eq!(pt.n(), self.n());
assert_eq!(res.n(), self.n() as u32);
assert_eq!(sk.n(), self.n() as u32);
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.dnum().as_usize() * (res.rank().as_usize() + 1)];
{
let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
#[cfg(debug_assertions)]
{
use poulpy_hal::layouts::ZnxInfos;
assert_eq!(res.rank(), sk.rank());
assert_eq!(res.n(), sk.n());
assert_eq!(pt.n() as u32, sk.n());
}
let base2k: usize = res.base2k().into();
let rank: usize = res.rank().into();
let cols: usize = rank + 1;
let dsize: usize = res.dsize().into();
println!("res.seed: {:?}", res.seed);
let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout());

View File

@@ -1,13 +1,15 @@
use poulpy_hal::{
api::{ScratchAvailable, VecZnxAutomorphism},
api::{ScratchAvailable, SvpPPolBytesOf, VecZnxAutomorphism},
layouts::{Backend, DataMut, GaloisElement, Module, Scratch},
source::Source,
};
use crate::{
ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos},
GGLWEEncryptSk, ScratchTakeCore,
layouts::{
AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared,
GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement,
},
};
impl AutomorphismKey<Vec<u8>> {
@@ -30,7 +32,7 @@ impl AutomorphismKey<Vec<u8>> {
impl<DM: DataMut> AutomorphismKey<DM>
where
Self: AutomorphismKeyToMut,
Self: GGLWEToRef,
{
pub fn encrypt_sk<S, M, BE: Backend>(
&mut self,
@@ -62,13 +64,18 @@ pub trait AutomorphismKeyEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: AutomorphismKeyToMut,
R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef;
}
impl<BE: Backend> AutomorphismKeyEncryptSk<BE> for Module<BE>
where
Self: GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphism + GaloisElement,
Self: GGLWEEncryptSk<BE>
+ VecZnxAutomorphism
+ GaloisElement
+ SvpPPolBytesOf
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -80,7 +87,10 @@ where
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
);
self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(infos)
GLWESecretPrepared::bytes_of_from_infos(self, infos)
+ self
.gglwe_encrypt_sk_tmp_bytes(infos)
.max(GLWESecret::bytes_of_from_infos(infos))
}
fn automorphism_key_encrypt_sk<R, S>(
@@ -92,10 +102,9 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: AutomorphismKeyToMut,
R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos,
S: GLWESecretToRef,
{
let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
assert_eq!(res.n(), sk.n());
@@ -108,9 +117,10 @@ where
self.automorphism_key_encrypt_sk_tmp_bytes(res)
);
let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank());
let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank());
{
let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank());
(0..res.rank_out().into()).for_each(|i| {
self.vec_znx_automorphism(
self.galois_element_inv(p),
@@ -120,12 +130,19 @@ where
i,
);
});
sk_out_prepared.prepare(self, &sk_out);
}
res.key
.encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1);
self.gglwe_encrypt_sk(
res,
&sk.data,
&sk_out_prepared,
source_xa,
source_xe,
scratch_1,
);
res.p = p;
res.set_p(p);
}
}

View File

@@ -8,7 +8,7 @@ use crate::{
ScratchTakeCore,
encryption::gglwe_ct::GGLWEEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos,
GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos,
prepared::GLWESecretPreparedAlloc,
},
};
@@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWESwitchingKeyToMut,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef,
S2: GLWESecretToRef;
}
@@ -93,11 +93,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWESwitchingKeyToMut,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: GLWESecretToRef,
S2: GLWESecretToRef,
{
let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
@@ -129,16 +128,17 @@ where
}
}
res.key.encrypt_sk(
self,
self.gglwe_encrypt_sk(
res,
&sk_in_tmp,
&sk_out_tmp,
source_xa,
source_xe,
scratch_2,
);
res.sk_in_n = sk_in.n().into();
res.sk_out_n = sk_out.n().into();
*res.input_degree() = sk_in.n();
*res.output_degree() = sk_out.n();
}
}

View File

@@ -8,10 +8,9 @@ use poulpy_hal::{
};
use crate::{
GetDistribution, ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
GGLWEEncryptSk, GetDistribution, ScratchTakeCore,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut,
GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, TensorKeyToMut,
prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
},
};
@@ -63,7 +62,7 @@ pub trait TensorKeyEncryptSk<BE: Backend> {
impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE>
where
Self: ModuleN
+ GLWESwitchingKeyEncryptSk<BE>
+ GGLWEEncryptSk<BE>
+ VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ GLWESecretPreparedAlloc<BE>
@@ -83,7 +82,7 @@ where
+ self.bytes_of_vec_znx_big(1, 1)
+ self.bytes_of_vec_znx_dft(1, 1)
+ GLWESecret::bytes_of(self.n().into(), Rank(1))
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
+ GGLWE::encrypt_sk_tmp_bytes(self, infos)
}
fn tensor_key_encrypt_sk<R, S>(
@@ -102,8 +101,8 @@ where
// let n: RingDegree = sk.n();
let rank: Rank = res.rank_out();
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, rank);
sk_dft_prep.prepare(self, sk);
let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, rank);
sk_prepared.prepare(self, sk);
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
@@ -122,7 +121,7 @@ where
(0..rank.into()).for_each(|i| {
(i..rank.into()).for_each(|j| {
self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i);
self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
self.vec_znx_big_normalize(
@@ -135,8 +134,14 @@ where
scratch_5,
);
res.at_mut(i, j)
.encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5);
res.at_mut(i, j).encrypt_sk(
self,
&sk_ij.data,
&sk_prepared,
source_xa,
source_xe,
scratch_5,
);
});
})
}

View File

@@ -79,7 +79,7 @@ impl<D: DataMut> GLWE<D> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>,
M: GLWEEncryptPk<BE>,
{
@@ -245,7 +245,7 @@ pub trait GLWEEncryptPk<BE: Backend> {
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut,
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>;
fn glwe_encrypt_zero_pk<R, K>(
@@ -285,7 +285,7 @@ where
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut,
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>,
{
self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch);
@@ -324,7 +324,7 @@ pub(crate) trait GLWEEncryptPkInternal<BE: Backend> {
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut,
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>;
}
@@ -351,7 +351,7 @@ where
scratch: &mut Scratch<BE>,
) where
R: GLWEToMut,
P: GLWEPlaintextToRef,
P: GLWEPlaintextToRef + GLWEInfos,
K: GLWEPublicKeyPreparedToRef<BE>,
{
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
@@ -361,8 +361,8 @@ where
assert_eq!(res.n(), pk.n());
assert_eq!(res.rank(), pk.rank());
if let Some((pt, _)) = pt {
assert_eq!(pt.to_ref().base2k(), pk.base2k());
assert_eq!(pt.to_ref().n(), pk.n());
assert_eq!(pt.base2k(), pk.base2k());
assert_eq!(pt.n(), pk.n());
}
let base2k: usize = pk.base2k().into();

View File

@@ -1,14 +1,13 @@
use poulpy_hal::{
api::{ModuleN, VecZnxAutomorphismInplace},
api::{ModuleN, VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes},
layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero},
source::Source,
};
use crate::{
ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
GGLWEEncryptSk, ScratchTakeCore,
layouts::{
GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos,
GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPrepare, GLWESecretToRef, GLWEToLWESwitchingKey, LWEInfos,
LWESecret, LWESecretToRef, Rank,
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
},
@@ -59,12 +58,17 @@ pub trait GLWEToLWESwitchingKeyEncrypt<BE: Backend> {
) where
S1: LWESecretToRef,
S2: GLWESecretToRef,
R: GLWEToLWESwitchingKeyToMut;
R: GGLWEToMut;
}
impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE>
where
Self: ModuleN + GLWESwitchingKeyEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + VecZnxAutomorphismInplace<BE>,
Self: ModuleN
+ GGLWEEncryptSk<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxAutomorphismInplace<BE>
+ VecZnxAutomorphismInplaceTmpBytes
+ GLWESecretPrepare<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -72,7 +76,8 @@ where
A: GGLWEInfos,
{
GLWESecretPrepared::bytes_of(self, infos.rank_in())
+ (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self.n().into(), infos.rank_in()))
+ GGLWE::encrypt_sk_tmp_bytes(self, infos)
.max(GLWESecret::bytes_of(self.n().into(), infos.rank_in()) + self.vec_znx_automorphism_inplace_tmp_bytes())
}
fn glwe_to_lwe_switching_key_encrypt_sk<R, S1, S2>(
@@ -86,23 +91,27 @@ where
) where
S1: LWESecretToRef,
S2: GLWESecretToRef,
R: GLWEToLWESwitchingKeyToMut,
R: GGLWEToMut,
{
let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref();
let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref();
assert!(sk_lwe.n().0 <= self.n() as u32);
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1));
sk_lwe_as_glwe.data.zero();
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
let (mut sk_lwe_as_glwe_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, Rank(1));
res.0.encrypt_sk(
self,
sk_glwe,
&sk_lwe_as_glwe,
{
let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1));
sk_lwe_as_glwe.data.zero();
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_2);
sk_lwe_as_glwe_prep.prepare(self, &sk_lwe_as_glwe);
}
self.gglwe_encrypt_sk(
res,
&sk_glwe.data,
&sk_lwe_as_glwe_prep,
source_xa,
source_xe,
scratch_1,

View File

@@ -1,7 +1,6 @@
use poulpy_hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace},
layouts::{Backend, DataMut, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
source::Source,
};
@@ -16,7 +15,6 @@ impl<DataSelf: DataMut> LWE<DataSelf> {
P: LWEPlaintextToRef,
S: LWESecretToRef,
M: LWEEncryptSk<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
{
module.lwe_encrypt_sk(self, pt, sk, source_xa, source_xe);
}
@@ -33,7 +31,7 @@ pub trait LWEEncryptSk<BE: Backend> {
impl<BE: Backend> LWEEncryptSk<BE> for Module<BE>
where
Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
{
fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
where

View File

@@ -8,8 +8,8 @@ use crate::{
ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{
GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, LWESwitchingKeyToMut,
Rank,
GGLWEInfos, GGLWEToMut, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, LWESecret, LWESecretToRef,
LWESwitchingKey, Rank,
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
},
};
@@ -56,7 +56,7 @@ pub trait LWESwitchingKeyEncrypt<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: LWESwitchingKeyToMut,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: LWESecretToRef,
S2: LWESecretToRef;
}
@@ -100,13 +100,12 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: LWESwitchingKeyToMut,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
S1: LWESecretToRef,
S2: LWESecretToRef,
{
let res: &mut LWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_lwe_in: &LWESecret<&[u8]> = &sk_lwe_in.to_ref();
let sk_lwe_out = &sk_lwe_out.to_ref();
let sk_lwe_out: &LWESecret<&[u8]> = &sk_lwe_out.to_ref();
assert!(sk_lwe_in.n().0 <= res.n().0);
assert!(sk_lwe_out.n().0 <= res.n().0);
@@ -124,7 +123,7 @@ where
self.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2);
self.glwe_switching_key_encrypt_sk(
&mut res.0,
res,
&sk_in_glwe,
&sk_out_glwe,
source_xa,

View File

@@ -1,15 +1,14 @@
use poulpy_hal::{
api::{ModuleN, VecZnxAutomorphismInplace},
api::{ModuleN, VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes},
layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut},
source::Source,
};
use crate::{
ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
GGLWEEncryptSk, ScratchTakeCore,
layouts::{
GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey,
LWEToGLWESwitchingKeyToMut, Rank,
GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPreparedAlloc, GLWESecretPreparedToRef, LWEInfos, LWESecret,
LWESecretToRef, LWEToGLWESwitchingKey, Rank,
},
};
@@ -34,7 +33,7 @@ impl<D: DataMut> LWEToGLWESwitchingKey<D> {
scratch: &mut Scratch<BE>,
) where
S1: LWESecretToRef,
S2: GLWESecretToRef,
S2: GLWESecretPreparedToRef<BE>,
M: LWEToGLWESwitchingKeyEncrypt<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
@@ -57,13 +56,17 @@ pub trait LWEToGLWESwitchingKeyEncrypt<BE: Backend> {
scratch: &mut Scratch<BE>,
) where
S1: LWESecretToRef,
S2: GLWESecretToRef,
R: LWEToGLWESwitchingKeyToMut;
S2: GLWESecretPreparedToRef<BE>,
R: GGLWEToMut;
}
impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE>
where
Self: ModuleN + GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphismInplace<BE>,
Self: ModuleN
+ GGLWEEncryptSk<BE>
+ VecZnxAutomorphismInplace<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxAutomorphismInplaceTmpBytes,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -75,7 +78,8 @@ where
Rank(1),
"rank_in != 1 is not supported for LWEToGLWESwitchingKey"
);
GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self.n().into(), infos.rank_in())
GLWESecret::bytes_of(self.n().into(), infos.rank_in())
+ GGLWE::encrypt_sk_tmp_bytes(self, infos).max(self.vec_znx_automorphism_inplace_tmp_bytes())
}
fn lwe_to_glwe_switching_key_encrypt_sk<R, S1, S2>(
@@ -88,12 +92,10 @@ where
scratch: &mut Scratch<BE>,
) where
S1: LWESecretToRef,
S2: GLWESecretToRef,
R: LWEToGLWESwitchingKeyToMut,
S2: GLWESecretPreparedToRef<BE>,
R: GGLWEToMut,
{
let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref();
let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref();
assert!(sk_lwe.n().0 <= self.n() as u32);
@@ -102,9 +104,9 @@ where
sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0);
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
res.0.encrypt_sk(
self,
&sk_lwe_as_glwe,
self.gglwe_encrypt_sk(
res,
&sk_lwe_as_glwe.data,
sk_glwe,
source_xa,
source_xe,