mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
non-compressed finished
This commit is contained in:
@@ -5,58 +5,36 @@ use poulpy_hal::{
|
|||||||
VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
|
VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
|
||||||
VecZnxSwitchRing,
|
VecZnxSwitchRing,
|
||||||
},
|
},
|
||||||
layouts::{Backend, DataMut, Module, Scratch},
|
layouts::{Backend, DataMut, GaloisElement, Module, Scratch},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{
|
layouts::{
|
||||||
AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos,
|
AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl AutomorphismKey<Vec<u8>> {
|
impl AutomorphismKey<Vec<u8>> {
|
||||||
pub fn encrypt_sk_tmp_bytes<BE: Backend, A>(module: &Module<BE>, infos: &A) -> usize
|
|
||||||
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
Module<BE>: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc<BE>,
|
M: GGLWEAutomorphismKeyEncryptSk<BE>
|
||||||
{
|
{
|
||||||
assert_eq!(
|
module.gglwe_automorphism_key_encrypt_sk_tmp_bytes(infos)
|
||||||
infos.rank_in(),
|
|
||||||
infos.rank_out(),
|
|
||||||
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
|
|
||||||
);
|
|
||||||
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_pk_tmp_bytes<BE: Backend, A>(module: &Module<BE>, _infos: &A) -> usize
|
pub fn encrypt_pk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
|
M: GGLWEAutomorphismKeyEncryptPk<BE>
|
||||||
{
|
{
|
||||||
assert_eq!(
|
module.gglwe_automorphism_key_encrypt_pk_tmp_bytes(infos)
|
||||||
_infos.rank_in(),
|
|
||||||
_infos.rank_out(),
|
|
||||||
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
|
|
||||||
);
|
|
||||||
GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait GGLWEAutomorphismKeyEncryptSk<BE: Backend> {
|
|
||||||
fn gglwe_automorphism_key_encrypt_sk<A, B>(
|
|
||||||
&self,
|
|
||||||
res: &mut A,
|
|
||||||
p: i64,
|
|
||||||
sk: &B,
|
|
||||||
source_xa: &mut Source,
|
|
||||||
source_xe: &mut Source,
|
|
||||||
scratch: &mut Scratch<BE>,
|
|
||||||
) where
|
|
||||||
A: AutomorphismKeyToMut,
|
|
||||||
B: GLWESecretToRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<DM: DataMut> AutomorphismKey<DM>
|
impl<DM: DataMut> AutomorphismKey<DM>
|
||||||
where
|
where
|
||||||
Self: AutomorphismKeyToMut,
|
Self: AutomorphismKeyToMut,
|
||||||
@@ -77,6 +55,24 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait GGLWEAutomorphismKeyEncryptSk<BE: Backend> {
|
||||||
|
fn gglwe_automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos;
|
||||||
|
|
||||||
|
fn gglwe_automorphism_key_encrypt_sk<A, B>(
|
||||||
|
&self,
|
||||||
|
res: &mut A,
|
||||||
|
p: i64,
|
||||||
|
sk: &B,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
A: AutomorphismKeyToMut,
|
||||||
|
B: GLWESecretToRef;
|
||||||
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GGLWEAutomorphismKeyEncryptSk<BE> for Module<BE>
|
impl<BE: Backend> GGLWEAutomorphismKeyEncryptSk<BE> for Module<BE>
|
||||||
where
|
where
|
||||||
Module<BE>: ModuleN
|
Module<BE>: ModuleN
|
||||||
@@ -99,9 +95,22 @@ where
|
|||||||
+ SvpPPolBytesOf
|
+ SvpPPolBytesOf
|
||||||
+ VecZnxAutomorphism
|
+ VecZnxAutomorphism
|
||||||
+ SvpPPolAlloc<BE>
|
+ SvpPPolAlloc<BE>
|
||||||
+ SvpPPolBytesOf,
|
+ GaloisElement,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
|
|
||||||
|
fn gglwe_automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos,
|
||||||
|
{
|
||||||
|
assert_eq!(
|
||||||
|
infos.rank_in(),
|
||||||
|
infos.rank_out(),
|
||||||
|
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
|
||||||
|
);
|
||||||
|
GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of_from_infos(self, &infos.glwe_layout())
|
||||||
|
}
|
||||||
|
|
||||||
fn gglwe_automorphism_key_encrypt_sk<A, B>(
|
fn gglwe_automorphism_key_encrypt_sk<A, B>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut A,
|
res: &mut A,
|
||||||
@@ -152,3 +161,46 @@ where
|
|||||||
res.p = p;
|
res.p = p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub trait GGLWEAutomorphismKeyEncryptPk<BE: Backend> {
|
||||||
|
fn gglwe_automorphism_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BE: Backend> GGLWEAutomorphismKeyEncryptPk<BE> for Module<BE>
|
||||||
|
where
|
||||||
|
Module<BE>: ModuleN
|
||||||
|
+ VecZnxAddScalarInplace
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxBigNormalize<BE>
|
||||||
|
+ VecZnxDftApply<BE>
|
||||||
|
+ SvpApplyDftToDftInplace<BE>
|
||||||
|
+ VecZnxIdftApplyConsume<BE>
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxFillUniform
|
||||||
|
+ VecZnxSubInplace
|
||||||
|
+ VecZnxAddInplace
|
||||||
|
+ VecZnxNormalizeInplace<BE>
|
||||||
|
+ VecZnxAddNormal
|
||||||
|
+ VecZnxNormalize<BE>
|
||||||
|
+ VecZnxSub
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ SvpPPolAlloc<BE>,
|
||||||
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
|
{
|
||||||
|
|
||||||
|
fn gglwe_automorphism_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos,
|
||||||
|
{
|
||||||
|
assert_eq!(
|
||||||
|
infos.rank_in(),
|
||||||
|
infos.rank_out(),
|
||||||
|
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
|
||||||
|
);
|
||||||
|
GLWESwitchingKey::encrypt_pk_tmp_bytes(self, infos)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -64,7 +64,6 @@ where
|
|||||||
+ VecZnxDftBytesOf
|
+ VecZnxDftBytesOf
|
||||||
+ VecZnxAddScalarInplace
|
+ VecZnxAddScalarInplace
|
||||||
+ VecZnxNormalizeInplace<B>,
|
+ VecZnxNormalizeInplace<B>,
|
||||||
// + SvpPPolAllocBytesImpl<B>,
|
|
||||||
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
|
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
|
||||||
{
|
{
|
||||||
fn gglwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn gglwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use poulpy_hal::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{
|
layouts::{
|
||||||
GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared,
|
GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, prepared::GLWESecretPrepared,
|
||||||
},
|
},
|
||||||
encryption::gglwe_ct::GGLWEEncryptSk,
|
encryption::gglwe_ct::GGLWEEncryptSk,
|
||||||
};
|
};
|
||||||
@@ -26,109 +26,108 @@ impl GLWESwitchingKey<Vec<u8>> {
|
|||||||
module.glwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
module.glwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn encrypt_pk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
pub fn encrypt_pk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
M: GLWESwitchingKeyEncryptSk<BE>,
|
M: GLWESwitchingKeyEncryptPk<BE>,
|
||||||
{
|
{
|
||||||
module.glwe_switching_key_encrypt_pk_tmp_bytes(infos)
|
module.glwe_switching_key_encrypt_pk_tmp_bytes(infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
|
||||||
// where
|
|
||||||
// A: GGLWEInfos,
|
|
||||||
// Module<B>: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc<B>,
|
|
||||||
// {
|
|
||||||
// (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1))
|
|
||||||
// + ScalarZnx::bytes_of(module.n(), infos.rank_in().into())
|
|
||||||
// + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// pub fn encrypt_pk_tmp_bytes<B: Backend, A>(module: &Module<B>, _infos: &A) -> usize
|
|
||||||
// where
|
|
||||||
// A: GGLWEInfos,
|
|
||||||
// {
|
|
||||||
// GGLWE::encrypt_pk_tmp_bytes(module, _infos)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
|
impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
|
pub fn encrypt_sk<M, DataSkIn: DataRef, DataSkOut: DataRef, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<B>,
|
module: &M,
|
||||||
sk_in: &GLWESecret<DataSkIn>,
|
sk_in: &GLWESecret<DataSkIn>,
|
||||||
sk_out: &GLWESecret<DataSkOut>,
|
sk_out: &GLWESecret<DataSkOut>,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<B>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
Module<B>: ModuleN
|
M: GLWESwitchingKeyEncryptSk<BE>,
|
||||||
+ VecZnxAddScalarInplace
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxBigNormalize<B>
|
|
||||||
+ VecZnxDftApply<B>
|
|
||||||
+ SvpApplyDftToDftInplace<B>
|
|
||||||
+ VecZnxIdftApplyConsume<B>
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxFillUniform
|
|
||||||
+ VecZnxSubInplace
|
|
||||||
+ VecZnxAddInplace
|
|
||||||
+ VecZnxNormalizeInplace<B>
|
|
||||||
+ VecZnxAddNormal
|
|
||||||
+ VecZnxNormalize<B>
|
|
||||||
+ VecZnxSub
|
|
||||||
+ SvpPrepare<B>
|
|
||||||
+ VecZnxSwitchRing
|
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ SvpPPolAlloc<B>,
|
|
||||||
Scratch<B>: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore<B>,
|
|
||||||
{
|
{
|
||||||
#[cfg(debug_assertions)]
|
module.glwe_switching_key_encrypt_sk(self, sk_in, sk_out, source_xa, source_xe, scratch);
|
||||||
{
|
|
||||||
assert!(sk_in.n().0 <= module.n() as u32);
|
|
||||||
assert!(sk_out.n().0 <= module.n() as u32);
|
|
||||||
assert!(
|
|
||||||
scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self),
|
|
||||||
"scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
|
|
||||||
scratch.available(),
|
|
||||||
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let n: usize = sk_in.n().max(sk_out.n()).into();
|
|
||||||
|
|
||||||
let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(module, sk_in.rank().into());
|
// #[allow(clippy::too_many_arguments)]
|
||||||
(0..sk_in.rank().into()).for_each(|i| {
|
// pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
|
||||||
module.vec_znx_switch_ring(
|
// &mut self,
|
||||||
&mut sk_in_tmp.as_vec_znx_mut(),
|
// module: &Module<B>,
|
||||||
i,
|
// sk_in: &GLWESecret<DataSkIn>,
|
||||||
&sk_in.data.as_vec_znx(),
|
// sk_out: &GLWESecret<DataSkOut>,
|
||||||
i,
|
// source_xa: &mut Source,
|
||||||
);
|
// source_xe: &mut Source,
|
||||||
});
|
// scratch: &mut Scratch<B>,
|
||||||
|
// ) where
|
||||||
|
// Module<B>: ModuleN
|
||||||
|
// + VecZnxAddScalarInplace
|
||||||
|
// + VecZnxDftBytesOf
|
||||||
|
// + VecZnxBigNormalize<B>
|
||||||
|
// + VecZnxDftApply<B>
|
||||||
|
// + SvpApplyDftToDftInplace<B>
|
||||||
|
// + VecZnxIdftApplyConsume<B>
|
||||||
|
// + VecZnxNormalizeTmpBytes
|
||||||
|
// + VecZnxFillUniform
|
||||||
|
// + VecZnxSubInplace
|
||||||
|
// + VecZnxAddInplace
|
||||||
|
// + VecZnxNormalizeInplace<B>
|
||||||
|
// + VecZnxAddNormal
|
||||||
|
// + VecZnxNormalize<B>
|
||||||
|
// + VecZnxSub
|
||||||
|
// + SvpPrepare<B>
|
||||||
|
// + VecZnxSwitchRing
|
||||||
|
// + SvpPPolBytesOf
|
||||||
|
// + SvpPPolAlloc<B>,
|
||||||
|
// Scratch<B>: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore<B>,
|
||||||
|
// {
|
||||||
|
// #[cfg(debug_assertions)]
|
||||||
|
// {
|
||||||
|
// assert!(sk_in.n().0 <= module.n() as u32);
|
||||||
|
// assert!(sk_out.n().0 <= module.n() as u32);
|
||||||
|
// assert!(
|
||||||
|
// scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self),
|
||||||
|
// "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
|
||||||
|
// scratch.available(),
|
||||||
|
// GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self)
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
|
||||||
let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(module, sk_out.rank());
|
// // let n: usize = sk_in.n().max(sk_out.n()).into();
|
||||||
{
|
|
||||||
let (mut tmp, _) = scratch_2.take_scalar_znx(module, 1);
|
|
||||||
(0..sk_out.rank().into()).for_each(|i| {
|
|
||||||
module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
|
|
||||||
module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
self.key.encrypt_sk(
|
// let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(module, sk_in.rank().into());
|
||||||
module,
|
// (0..sk_in.rank().into()).for_each(|i| {
|
||||||
&sk_in_tmp,
|
// module.vec_znx_switch_ring(
|
||||||
&sk_out_tmp,
|
// &mut sk_in_tmp.as_vec_znx_mut(),
|
||||||
source_xa,
|
// i,
|
||||||
source_xe,
|
// &sk_in.data.as_vec_znx(),
|
||||||
scratch_2,
|
// i,
|
||||||
);
|
// );
|
||||||
self.sk_in_n = sk_in.n().into();
|
// });
|
||||||
self.sk_out_n = sk_out.n().into();
|
|
||||||
}
|
// let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(module, sk_out.rank());
|
||||||
|
// {
|
||||||
|
// let (mut tmp, _) = scratch_2.take_scalar_znx(module, 1);
|
||||||
|
// (0..sk_out.rank().into()).for_each(|i| {
|
||||||
|
// module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
|
||||||
|
// module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// self.key.encrypt_sk(
|
||||||
|
// module,
|
||||||
|
// &sk_in_tmp,
|
||||||
|
// &sk_out_tmp,
|
||||||
|
// source_xa,
|
||||||
|
// source_xe,
|
||||||
|
// scratch_2,
|
||||||
|
// );
|
||||||
|
// self.sk_in_n = sk_in.n().into();
|
||||||
|
// self.sk_out_n = sk_out.n().into();
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -146,9 +145,21 @@ where
|
|||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
fn glwe_switching_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn glwe_switching_key_encrypt_sk<R, DataSkIn, DataSkOut>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk_in: &GLWESecret<DataSkIn>,
|
||||||
|
sk_out: &GLWESecret<DataSkOut>,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
)
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
R: GLWESwitchingKeyToMut,
|
||||||
|
DataSkIn: DataRef,
|
||||||
|
DataSkOut: DataRef,
|
||||||
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE> where
|
impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE> where
|
||||||
@@ -157,7 +168,15 @@ impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE> where
|
|||||||
+ VecZnxDftBytesOf
|
+ VecZnxDftBytesOf
|
||||||
+ VecZnxNormalizeTmpBytes
|
+ VecZnxNormalizeTmpBytes
|
||||||
+ SvpPPolAlloc<BE>
|
+ SvpPPolAlloc<BE>
|
||||||
+ GGLWEEncryptSk<BE>,
|
+ GGLWEEncryptSk<BE>
|
||||||
|
+ VecZnxSwitchRing
|
||||||
|
+ SvpPrepare<BE>
|
||||||
|
+ VecZnxIdftApplyConsume<BE>
|
||||||
|
+ VecZnxNormalize<BE>
|
||||||
|
+ VecZnxNormalizeInplace<BE>
|
||||||
|
+ VecZnxAddInplace
|
||||||
|
+ VecZnxAddNormal
|
||||||
|
+ VecZnxSub
|
||||||
{
|
{
|
||||||
|
|
||||||
fn glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
@@ -169,11 +188,102 @@ impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE> where
|
|||||||
+ GLWESecretPrepared::bytes_of_from_infos(self, &infos.glwe_layout())
|
+ GLWESecretPrepared::bytes_of_from_infos(self, &infos.glwe_layout())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn glwe_switching_key_encrypt_sk<R, DataSkIn, DataSkOut>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk_in: &GLWESecret<DataSkIn>,
|
||||||
|
sk_out: &GLWESecret<DataSkOut>,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
)
|
||||||
|
where
|
||||||
|
R: GLWESwitchingKeyToMut,
|
||||||
|
DataSkIn: DataRef,
|
||||||
|
DataSkOut: DataRef,
|
||||||
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
|
{
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
assert!(sk_in.n().0 <= self.n() as u32);
|
||||||
|
assert!(sk_out.n().0 <= self.n() as u32);
|
||||||
|
assert!(
|
||||||
|
scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res),
|
||||||
|
"scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
|
||||||
|
scratch.available(),
|
||||||
|
GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// let n: usize = sk_in.n().max(sk_out.n()).into();
|
||||||
|
|
||||||
|
let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into());
|
||||||
|
(0..sk_in.rank().into()).for_each(|i| {
|
||||||
|
self.vec_znx_switch_ring(
|
||||||
|
&mut sk_in_tmp.as_vec_znx_mut(),
|
||||||
|
i,
|
||||||
|
&sk_in.data.as_vec_znx(),
|
||||||
|
i,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank());
|
||||||
|
{
|
||||||
|
let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1);
|
||||||
|
(0..sk_out.rank().into()).for_each(|i| {
|
||||||
|
self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
|
||||||
|
self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.key.encrypt_sk(
|
||||||
|
self,
|
||||||
|
&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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait GLWESwitchingKeyEncryptPk<BE: Backend>
|
||||||
|
where
|
||||||
|
Self: Sized
|
||||||
|
+ ModuleN
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ SvpPPolAlloc<BE>
|
||||||
|
+ GGLWEEncryptSk<BE>,
|
||||||
|
{
|
||||||
|
|
||||||
|
fn glwe_switching_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BE: Backend> GLWESwitchingKeyEncryptPk<BE> for Module<BE> where
|
||||||
|
Self: ModuleN
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ SvpPPolAlloc<BE>
|
||||||
|
+ GGLWEEncryptSk<BE>,
|
||||||
|
{
|
||||||
|
|
||||||
fn glwe_switching_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn glwe_switching_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
{
|
{
|
||||||
GGLWE::encrypt_pk_tmp_bytes(self, infos)
|
GGLWE::encrypt_pk_tmp_bytes(self, infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,111 +1,255 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{
|
||||||
ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
|
ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
|
||||||
VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
|
VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
|
||||||
VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
||||||
VecZnxSubInplace, VecZnxSwitchRing,
|
VecZnxSubInplace, VecZnxSwitchRing,
|
||||||
},
|
},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, Scratch},
|
layouts::{Backend, DataMut, DataRef, Module, Scratch},
|
||||||
oep::VecZnxBigAllocBytesImpl,
|
oep::{VecZnxAddScalarInplaceImpl, VecZnxBigAllocBytesImpl, VecZnxDftApplyImpl, SvpApplyDftToDftImpl, VecZnxIdftApplyTmpAImpl, VecZnxBigNormalizeImpl},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{
|
layouts::{
|
||||||
GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared,
|
GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut,
|
||||||
|
prepared::GLWESecretPrepared,
|
||||||
},
|
},
|
||||||
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl TensorKey<Vec<u8>> {
|
impl TensorKey<Vec<u8>> {
|
||||||
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf,
|
M: GGLWETensorKeyEncryptSk<BE>
|
||||||
{
|
{
|
||||||
GLWESecretPrepared::bytes_of(module, infos.rank_out())
|
module.gglwe_tensor_key_encrypt_sk_tmp_bytes(infos)
|
||||||
+ module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1)
|
|
||||||
+ module.bytes_of_vec_znx_big(1, 1)
|
|
||||||
+ module.bytes_of_vec_znx_dft(1, 1)
|
|
||||||
+ GLWESecret::bytes_of(module, Rank(1))
|
|
||||||
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos)
|
|
||||||
}
|
}
|
||||||
|
// pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
||||||
|
// where
|
||||||
|
// A: GGLWEInfos,
|
||||||
|
// Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf,
|
||||||
|
// {
|
||||||
|
// GLWESecretPrepared::bytes_of(module, infos.rank_out())
|
||||||
|
// + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1)
|
||||||
|
// + module.bytes_of_vec_znx_big(1, 1)
|
||||||
|
// + module.bytes_of_vec_znx_dft(1, 1)
|
||||||
|
// + GLWESecret::bytes_of(module, Rank(1))
|
||||||
|
// + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<DataSelf: DataMut> TensorKey<DataSelf> {
|
impl<DataSelf: DataMut> TensorKey<DataSelf> {
|
||||||
pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
|
pub fn encrypt_sk<M, DataSk: DataRef, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<B>,
|
module: &M,
|
||||||
sk: &GLWESecret<DataSk>,
|
sk: &GLWESecret<DataSk>,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<B>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
|
M: GGLWETensorKeyEncryptSk<BE>,
|
||||||
GLWESecret<DataSk>: GetDist,
|
GLWESecret<DataSk>: GetDist,
|
||||||
Module<B>: ModuleN
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
+ SvpApplyDftToDft<B>
|
|
||||||
+ VecZnxIdftApplyTmpA<B>
|
|
||||||
+ VecZnxAddScalarInplace
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxBigNormalize<B>
|
|
||||||
+ VecZnxDftApply<B>
|
|
||||||
+ SvpApplyDftToDftInplace<B>
|
|
||||||
+ VecZnxIdftApplyConsume<B>
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxFillUniform
|
|
||||||
+ VecZnxSubInplace
|
|
||||||
+ VecZnxAddInplace
|
|
||||||
+ VecZnxNormalizeInplace<B>
|
|
||||||
+ VecZnxAddNormal
|
|
||||||
+ VecZnxNormalize<B>
|
|
||||||
+ VecZnxSub
|
|
||||||
+ SvpPrepare<B>
|
|
||||||
+ VecZnxSwitchRing
|
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ VecZnxBigAllocBytesImpl<B>
|
|
||||||
+ VecZnxBigBytesOf
|
|
||||||
+ SvpPPolAlloc<B>,
|
|
||||||
Scratch<B>: ScratchTakeBasic + ScratchTakeCore<B>,
|
|
||||||
{
|
{
|
||||||
#[cfg(debug_assertions)]
|
module.gglwe_tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch);
|
||||||
{
|
|
||||||
assert_eq!(self.rank_out(), sk.rank());
|
|
||||||
assert_eq!(self.n(), sk.n());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
|
||||||
|
// &mut self,
|
||||||
|
// module: &Module<B>,
|
||||||
|
// sk: &GLWESecret<DataSk>,
|
||||||
|
// source_xa: &mut Source,
|
||||||
|
// source_xe: &mut Source,
|
||||||
|
// scratch: &mut Scratch<B>,
|
||||||
|
// ) where
|
||||||
|
// GLWESecret<DataSk>: GetDist,
|
||||||
|
// Module<B>: ModuleN
|
||||||
|
// + SvpApplyDftToDft<B>
|
||||||
|
// + VecZnxIdftApplyTmpA<B>
|
||||||
|
// + VecZnxAddScalarInplace
|
||||||
|
// + VecZnxDftBytesOf
|
||||||
|
// + VecZnxBigNormalize<B>
|
||||||
|
// + VecZnxDftApply<B>
|
||||||
|
// + SvpApplyDftToDftInplace<B>
|
||||||
|
// + VecZnxIdftApplyConsume<B>
|
||||||
|
// + VecZnxNormalizeTmpBytes
|
||||||
|
// + VecZnxFillUniform
|
||||||
|
// + VecZnxSubInplace
|
||||||
|
// + VecZnxAddInplace
|
||||||
|
// + VecZnxNormalizeInplace<B>
|
||||||
|
// + VecZnxAddNormal
|
||||||
|
// + VecZnxNormalize<B>
|
||||||
|
// + VecZnxSub
|
||||||
|
// + SvpPrepare<B>
|
||||||
|
// + VecZnxSwitchRing
|
||||||
|
// + SvpPPolBytesOf
|
||||||
|
// + VecZnxBigAllocBytesImpl<B>
|
||||||
|
// + VecZnxBigBytesOf
|
||||||
|
// + SvpPPolAlloc<B>,
|
||||||
|
// Scratch<B>: ScratchTakeBasic + ScratchTakeCore<B>,
|
||||||
|
// {
|
||||||
|
// #[cfg(debug_assertions)]
|
||||||
|
// {
|
||||||
|
// assert_eq!(self.rank_out(), sk.rank());
|
||||||
|
// assert_eq!(self.n(), sk.n());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // let n: RingDegree = sk.n();
|
||||||
|
// let rank: Rank = self.rank_out();
|
||||||
|
|
||||||
|
// let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(module, rank);
|
||||||
|
// sk_dft_prep.prepare(module, sk);
|
||||||
|
|
||||||
|
// let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(module, rank.into(), 1);
|
||||||
|
|
||||||
|
// (0..rank.into()).for_each(|i| {
|
||||||
|
// module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(module, 1, 1);
|
||||||
|
// let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(module, Rank(1));
|
||||||
|
// let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(module, 1, 1);
|
||||||
|
|
||||||
|
// (0..rank.into()).for_each(|i| {
|
||||||
|
// (i..rank.into()).for_each(|j| {
|
||||||
|
// module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i);
|
||||||
|
|
||||||
|
// module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
|
||||||
|
// module.vec_znx_big_normalize(
|
||||||
|
// self.base2k().into(),
|
||||||
|
// &mut sk_ij.data.as_vec_znx_mut(),
|
||||||
|
// 0,
|
||||||
|
// self.base2k().into(),
|
||||||
|
// &sk_ij_big,
|
||||||
|
// 0,
|
||||||
|
// scratch_5,
|
||||||
|
// );
|
||||||
|
|
||||||
|
// self.at_mut(i, j)
|
||||||
|
// .encrypt_sk(module, &sk_ij, sk, source_xa, source_xe, scratch_5);
|
||||||
|
// });
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait GGLWETensorKeyEncryptSk<BE: Backend>
|
||||||
|
where
|
||||||
|
Self: Sized
|
||||||
|
+ ModuleN
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ SvpPPolAlloc<BE>
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxBigBytesOf,
|
||||||
|
{
|
||||||
|
fn gglwe_tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos;
|
||||||
|
|
||||||
|
fn gglwe_tensor_key_encrypt_sk<R, S>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk: &S,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
R: TensorKeyToMut,
|
||||||
|
S: GLWESecretToRef + GetDist;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BE: Backend> GGLWETensorKeyEncryptSk<BE> for Module<BE> where
|
||||||
|
Module<BE>: ModuleN
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ SvpPPolAlloc<BE>
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxBigBytesOf
|
||||||
|
+ VecZnxAddScalarInplaceImpl<BE>
|
||||||
|
+ VecZnxDftApply<BE>
|
||||||
|
+ VecZnxDftApplyImpl<BE>
|
||||||
|
+ SvpApplyDftToDftImpl<BE>
|
||||||
|
+ GLWESwitchingKeyEncryptSk<BE>
|
||||||
|
+ SvpApplyDftToDft<BE>
|
||||||
|
+ VecZnxIdftApplyTmpAImpl<BE>
|
||||||
|
+ VecZnxBigNormalizeImpl<BE>
|
||||||
|
+ VecZnxIdftApplyTmpA<BE>
|
||||||
|
+ VecZnxBigNormalize<BE>
|
||||||
|
+ VecZnxAddScalarInplaceImpl<BE>
|
||||||
|
+ SvpPrepare<BE>,
|
||||||
|
Scratch<BE>: ScratchTakeBasic + ScratchTakeCore<BE>,
|
||||||
|
{
|
||||||
|
fn gglwe_tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos,
|
||||||
|
{
|
||||||
|
GLWESecretPrepared::bytes_of(self, infos.rank_out())
|
||||||
|
+ self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1)
|
||||||
|
+ self.bytes_of_vec_znx_big(1, 1)
|
||||||
|
+ self.bytes_of_vec_znx_dft(1, 1)
|
||||||
|
+ GLWESecret::bytes_of(self, Rank(1))
|
||||||
|
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gglwe_tensor_key_encrypt_sk<R, S>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk: &S,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
R: TensorKeyToMut,
|
||||||
|
S: GLWESecretToRef + GetDist,
|
||||||
|
{
|
||||||
|
let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut();
|
||||||
|
|
||||||
// let n: RingDegree = sk.n();
|
// let n: RingDegree = sk.n();
|
||||||
let rank: Rank = self.rank_out();
|
let rank: Rank = res.rank_out();
|
||||||
|
|
||||||
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(module, rank);
|
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, rank);
|
||||||
sk_dft_prep.prepare(module, sk);
|
sk_dft_prep.prepare(self, sk);
|
||||||
|
|
||||||
let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(module, rank.into(), 1);
|
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
assert_eq!(res.rank_out(), sk.rank());
|
||||||
|
assert_eq!(res.n(), sk.n());
|
||||||
|
}
|
||||||
|
|
||||||
|
let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, rank.into(), 1);
|
||||||
|
|
||||||
(0..rank.into()).for_each(|i| {
|
(0..rank.into()).for_each(|i| {
|
||||||
module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
|
self.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
|
||||||
});
|
});
|
||||||
|
|
||||||
let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(module, 1, 1);
|
let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(self, 1, 1);
|
||||||
let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(module, Rank(1));
|
let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self, Rank(1));
|
||||||
let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(module, 1, 1);
|
let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(self, 1, 1);
|
||||||
|
|
||||||
(0..rank.into()).for_each(|i| {
|
(0..rank.into()).for_each(|i| {
|
||||||
(i..rank.into()).for_each(|j| {
|
(i..rank.into()).for_each(|j| {
|
||||||
module.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_dft_prep.data, j, &sk_dft, i);
|
||||||
|
|
||||||
module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
|
self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
|
||||||
module.vec_znx_big_normalize(
|
self.vec_znx_big_normalize(
|
||||||
self.base2k().into(),
|
res.base2k().into(),
|
||||||
&mut sk_ij.data.as_vec_znx_mut(),
|
&mut sk_ij.data.as_vec_znx_mut(),
|
||||||
0,
|
0,
|
||||||
self.base2k().into(),
|
res.base2k().into(),
|
||||||
&sk_ij_big,
|
&sk_ij_big,
|
||||||
0,
|
0,
|
||||||
scratch_5,
|
scratch_5,
|
||||||
);
|
);
|
||||||
|
|
||||||
self.at_mut(i, j)
|
res.at_mut(i, j)
|
||||||
.encrypt_sk(module, &sk_ij, sk, source_xa, source_xe, scratch_5);
|
.encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,20 +14,48 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
impl GGSW<Vec<u8>> {
|
impl GGSW<Vec<u8>> {
|
||||||
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGSWInfos,
|
A: GGSWInfos,
|
||||||
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf,
|
M: GGSWEncryptSk<BE>,
|
||||||
{
|
{
|
||||||
let size = infos.size();
|
module.ggsw_encrypt_sk_tmp_bytes(infos)
|
||||||
GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout())
|
}
|
||||||
+ VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size)
|
// pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
||||||
+ VecZnx::bytes_of(module.n(), 1, size)
|
// where
|
||||||
+ module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size)
|
// A: GGSWInfos,
|
||||||
|
// Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf,
|
||||||
|
// {
|
||||||
|
// let size = infos.size();
|
||||||
|
// GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout())
|
||||||
|
// + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size)
|
||||||
|
// + VecZnx::bytes_of(module.n(), 1, size)
|
||||||
|
// + module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<DataSelf: DataMut> GGSW<DataSelf> {
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
|
||||||
|
&mut self,
|
||||||
|
module: &Module<B>,
|
||||||
|
pt: &ScalarZnx<DataPt>,
|
||||||
|
sk: &GLWESecretPrepared<DataSk, B>,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<B>,
|
||||||
|
) where
|
||||||
|
Module<B>: GGSWEncryptSk<B>,
|
||||||
|
{
|
||||||
|
module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait GGSWEncryptSk<B: Backend> {
|
pub trait GGSWEncryptSk<B: Backend> {
|
||||||
|
fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGSWInfos;
|
||||||
|
|
||||||
fn ggsw_encrypt_sk<R, P, S>(
|
fn ggsw_encrypt_sk<R, P, S>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
@@ -44,9 +72,26 @@ pub trait GGSWEncryptSk<B: Backend> {
|
|||||||
|
|
||||||
impl<B: Backend> GGSWEncryptSk<B> for Module<B>
|
impl<B: Backend> GGSWEncryptSk<B> for Module<B>
|
||||||
where
|
where
|
||||||
Module<B>: ModuleN + GLWEEncryptSkInternal<B> + VecZnxAddScalarInplace + VecZnxNormalizeInplace<B>,
|
Module<B>: ModuleN
|
||||||
|
+ GLWEEncryptSkInternal<B>
|
||||||
|
+ VecZnxAddScalarInplace
|
||||||
|
+ VecZnxNormalizeInplace<B>
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxNormalizeTmpBytes,
|
||||||
Scratch<B>: ScratchTakeCore<B>,
|
Scratch<B>: ScratchTakeCore<B>,
|
||||||
{
|
{
|
||||||
|
|
||||||
|
fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGSWInfos,
|
||||||
|
{
|
||||||
|
let size = infos.size();
|
||||||
|
GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout())
|
||||||
|
+ VecZnx::bytes_of(self.n(), (infos.rank() + 1).into(), size)
|
||||||
|
+ VecZnx::bytes_of(self.n(), 1, size)
|
||||||
|
+ self.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size)
|
||||||
|
}
|
||||||
|
|
||||||
fn ggsw_encrypt_sk<R, P, S>(
|
fn ggsw_encrypt_sk<R, P, S>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
@@ -105,20 +150,3 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<DataSelf: DataMut> GGSW<DataSelf> {
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
|
|
||||||
&mut self,
|
|
||||||
module: &Module<B>,
|
|
||||||
pt: &ScalarZnx<DataPt>,
|
|
||||||
sk: &GLWESecretPrepared<DataSk, B>,
|
|
||||||
source_xa: &mut Source,
|
|
||||||
source_xe: &mut Source,
|
|
||||||
scratch: &mut Scratch<B>,
|
|
||||||
) where
|
|
||||||
Module<B>: GGSWEncryptSk<B>,
|
|
||||||
{
|
|
||||||
module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,77 +4,219 @@ use poulpy_hal::{
|
|||||||
VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
|
VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
|
||||||
VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
||||||
VecZnxSubInplace, VecZnxSwitchRing,
|
VecZnxSubInplace, VecZnxSwitchRing,
|
||||||
|
ScratchTakeBasic
|
||||||
},
|
},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero},
|
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero},
|
||||||
source::Source,
|
oep::{ScratchAvailableImpl, VecZnxFillUniformImpl},
|
||||||
|
source::Source
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{
|
layouts::{
|
||||||
GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank,
|
GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, GLWEToLWESwitchingKeyToMut,
|
||||||
prepared::GLWESecretPrepared,
|
prepared::GLWESecretPrepared,
|
||||||
},
|
},
|
||||||
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl GLWEToLWESwitchingKey<Vec<u8>> {
|
impl GLWEToLWESwitchingKey<Vec<u8>> {
|
||||||
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes,
|
M: GLWEToLWESwitchingKeyEncrypt<BE>,
|
||||||
{
|
{
|
||||||
GLWESecretPrepared::bytes_of(module, infos.rank_in())
|
module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
||||||
+ (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(module, infos.rank_in()))
|
|
||||||
}
|
}
|
||||||
|
// pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
||||||
|
// where
|
||||||
|
// A: GGLWEInfos,
|
||||||
|
// Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes,
|
||||||
|
// {
|
||||||
|
// GLWESecretPrepared::bytes_of(module, infos.rank_in())
|
||||||
|
// + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(module, infos.rank_in()))
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: DataMut> GLWEToLWESwitchingKey<D> {
|
impl<D: DataMut> GLWEToLWESwitchingKey<D> {
|
||||||
#[allow(clippy::too_many_arguments)]
|
pub fn encrypt_sk<M, DLwe, DGlwe, BE: Backend>(
|
||||||
pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
|
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<B>,
|
module: &M,
|
||||||
sk_lwe: &LWESecret<DLwe>,
|
sk_lwe: &LWESecret<DLwe>,
|
||||||
sk_glwe: &GLWESecret<DGlwe>,
|
sk_glwe: &GLWESecret<DGlwe>,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<B>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
|
M: GLWEToLWESwitchingKeyEncrypt<BE>,
|
||||||
DLwe: DataRef,
|
DLwe: DataRef,
|
||||||
DGlwe: DataRef,
|
DGlwe: DataRef,
|
||||||
Module<B>: ModuleN
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
+ VecZnxAutomorphismInplace<B>
|
{
|
||||||
+ VecZnxAddScalarInplace
|
module.glwe_to_lwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch);
|
||||||
+ VecZnxDftBytesOf
|
}
|
||||||
+ VecZnxBigNormalize<B>
|
// #[allow(clippy::too_many_arguments)]
|
||||||
+ VecZnxDftApply<B>
|
// pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
|
||||||
+ SvpApplyDftToDftInplace<B>
|
// &mut self,
|
||||||
+ VecZnxIdftApplyConsume<B>
|
// module: &Module<B>,
|
||||||
|
// sk_lwe: &LWESecret<DLwe>,
|
||||||
|
// sk_glwe: &GLWESecret<DGlwe>,
|
||||||
|
// source_xa: &mut Source,
|
||||||
|
// source_xe: &mut Source,
|
||||||
|
// scratch: &mut Scratch<B>,
|
||||||
|
// ) where
|
||||||
|
// DLwe: DataRef,
|
||||||
|
// DGlwe: DataRef,
|
||||||
|
// Module<B>: ModuleN
|
||||||
|
// + VecZnxAutomorphismInplace<B>
|
||||||
|
// + VecZnxAddScalarInplace
|
||||||
|
// + VecZnxDftBytesOf
|
||||||
|
// + VecZnxBigNormalize<B>
|
||||||
|
// + VecZnxDftApply<B>
|
||||||
|
// + SvpApplyDftToDftInplace<B>
|
||||||
|
// + VecZnxIdftApplyConsume<B>
|
||||||
|
// + VecZnxNormalizeTmpBytes
|
||||||
|
// + VecZnxFillUniform
|
||||||
|
// + VecZnxSubInplace
|
||||||
|
// + VecZnxAddInplace
|
||||||
|
// + VecZnxNormalizeInplace<B>
|
||||||
|
// + VecZnxAddNormal
|
||||||
|
// + VecZnxNormalize<B>
|
||||||
|
// + VecZnxSub
|
||||||
|
// + SvpPrepare<B>
|
||||||
|
// + VecZnxSwitchRing
|
||||||
|
// + SvpPPolBytesOf
|
||||||
|
// + SvpPPolAlloc<B>,
|
||||||
|
// Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
|
||||||
|
// {
|
||||||
|
// #[cfg(debug_assertions)]
|
||||||
|
// {
|
||||||
|
// assert!(sk_lwe.n().0 <= module.n() as u32);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, 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));
|
||||||
|
// module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
|
||||||
|
|
||||||
|
// self.0.encrypt_sk(
|
||||||
|
// module,
|
||||||
|
// sk_glwe,
|
||||||
|
// &sk_lwe_as_glwe,
|
||||||
|
// source_xa,
|
||||||
|
// source_xe,
|
||||||
|
// scratch_1,
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait GLWEToLWESwitchingKeyEncrypt<BE: Backend>
|
||||||
|
where
|
||||||
|
Self: Sized
|
||||||
|
+ ModuleN
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ SvpPPolAlloc<BE>
|
||||||
+ VecZnxNormalizeTmpBytes
|
+ VecZnxNormalizeTmpBytes
|
||||||
+ VecZnxFillUniform
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxAutomorphismInplace<BE>
|
||||||
|
+ VecZnxAddScalarInplace
|
||||||
|
+ VecZnxBigNormalize<BE>
|
||||||
|
+ VecZnxDftApply<BE>
|
||||||
|
+ SvpApplyDftToDftInplace<BE>
|
||||||
|
+ VecZnxIdftApplyConsume<BE>
|
||||||
|
+ VecZnxNormalize<BE>
|
||||||
|
+ VecZnxNormalizeInplace<BE>
|
||||||
|
+ VecZnxAddNormal
|
||||||
|
+ VecZnxSub
|
||||||
+ VecZnxSubInplace
|
+ VecZnxSubInplace
|
||||||
+ VecZnxAddInplace
|
+ VecZnxAddInplace
|
||||||
+ VecZnxNormalizeInplace<B>
|
+ SvpPrepare<BE>
|
||||||
+ VecZnxAddNormal
|
|
||||||
+ VecZnxNormalize<B>
|
|
||||||
+ VecZnxSub
|
|
||||||
+ SvpPrepare<B>
|
|
||||||
+ VecZnxSwitchRing
|
+ VecZnxSwitchRing
|
||||||
|
+ ScratchAvailable
|
||||||
|
+ ScratchAvailableImpl<BE>
|
||||||
|
+ VecZnxFillUniform
|
||||||
|
+ VecZnxFillUniformImpl<BE>
|
||||||
|
{
|
||||||
|
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos;
|
||||||
|
|
||||||
|
fn glwe_to_lwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk_lwe: &LWESecret<DLwe>,
|
||||||
|
sk_glwe: &GLWESecret<DataGlwe>,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
R: GLWEToLWESwitchingKeyToMut,
|
||||||
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE> where
|
||||||
|
Module<BE>: ModuleN
|
||||||
+ SvpPPolBytesOf
|
+ SvpPPolBytesOf
|
||||||
+ SvpPPolAlloc<B>,
|
+ SvpPPolAlloc<BE>
|
||||||
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxAutomorphismInplace<BE>
|
||||||
|
+ VecZnxAddScalarInplace
|
||||||
|
+ VecZnxBigNormalize<BE>
|
||||||
|
+ VecZnxDftApply<BE>
|
||||||
|
+ SvpApplyDftToDftInplace<BE>
|
||||||
|
+ VecZnxIdftApplyConsume<BE>
|
||||||
|
+ VecZnxNormalize<BE>
|
||||||
|
+ VecZnxNormalizeInplace<BE>
|
||||||
|
+ VecZnxAddNormal
|
||||||
|
+ VecZnxSub
|
||||||
|
+ VecZnxSubInplace
|
||||||
|
+ VecZnxAddInplace
|
||||||
|
+ SvpPrepare<BE>
|
||||||
|
+ VecZnxSwitchRing
|
||||||
|
+ ScratchAvailable
|
||||||
|
+ ScratchAvailableImpl<BE>
|
||||||
|
+ ScratchTakeBasic
|
||||||
|
+ ScratchTakeCore<BE>
|
||||||
|
+ VecZnxFillUniform
|
||||||
|
+ VecZnxFillUniformImpl<BE>
|
||||||
|
+ GLWESwitchingKeyEncryptSk<BE>
|
||||||
|
{
|
||||||
|
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos
|
||||||
{
|
{
|
||||||
#[cfg(debug_assertions)]
|
GLWESecretPrepared::bytes_of(self, infos.rank_in())
|
||||||
{
|
+ (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in()))
|
||||||
assert!(sk_lwe.n().0 <= module.n() as u32);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1));
|
fn glwe_to_lwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk_lwe: &LWESecret<DLwe>,
|
||||||
|
sk_glwe: &GLWESecret<DataGlwe>,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
R: GLWEToLWESwitchingKeyToMut,
|
||||||
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
|
{
|
||||||
|
|
||||||
|
let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
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.zero();
|
||||||
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
|
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
|
||||||
module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
|
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
|
||||||
|
|
||||||
self.0.encrypt_sk(
|
res.0.encrypt_sk(
|
||||||
module,
|
self,
|
||||||
sk_glwe,
|
sk_glwe,
|
||||||
&sk_lwe_as_glwe,
|
&sk_lwe_as_glwe,
|
||||||
source_xa,
|
source_xa,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ use crate::{
|
|||||||
GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank,
|
GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank,
|
||||||
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
|
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
|
||||||
},
|
},
|
||||||
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -27,30 +28,6 @@ impl LWESwitchingKey<Vec<u8>> {
|
|||||||
module.lwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
module.lwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
|
||||||
// where
|
|
||||||
// A: GGLWEInfos,
|
|
||||||
// Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes,
|
|
||||||
// {
|
|
||||||
// debug_assert_eq!(
|
|
||||||
// infos.dsize().0,
|
|
||||||
// 1,
|
|
||||||
// "dsize > 1 is not supported for LWESwitchingKey"
|
|
||||||
// );
|
|
||||||
// debug_assert_eq!(
|
|
||||||
// infos.rank_in().0,
|
|
||||||
// 1,
|
|
||||||
// "rank_in > 1 is not supported for LWESwitchingKey"
|
|
||||||
// );
|
|
||||||
// debug_assert_eq!(
|
|
||||||
// infos.rank_out().0,
|
|
||||||
// 1,
|
|
||||||
// "rank_out > 1 is not supported for LWESwitchingKey"
|
|
||||||
// );
|
|
||||||
// GLWESecret::bytes_of(module, Rank(1))
|
|
||||||
// + GLWESecretPrepared::bytes_of(module, Rank(1))
|
|
||||||
// + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: DataMut> LWESwitchingKey<D> {
|
impl<D: DataMut> LWESwitchingKey<D> {
|
||||||
@@ -171,6 +148,8 @@ impl<BE:Backend> LWESwitchingKeyEncrypt<BE> for Module<BE> where
|
|||||||
+ VecZnxSwitchRing
|
+ VecZnxSwitchRing
|
||||||
+ GLWESecretAlloc
|
+ GLWESecretAlloc
|
||||||
+ GLWESecretPreparedAlloc<BE>
|
+ GLWESecretPreparedAlloc<BE>
|
||||||
|
+ GLWESwitchingKeyEncryptSk<BE>
|
||||||
|
,
|
||||||
{
|
{
|
||||||
fn lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -4,81 +4,227 @@ use poulpy_hal::{
|
|||||||
VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
|
VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
|
||||||
VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
||||||
VecZnxSubInplace, VecZnxSwitchRing,
|
VecZnxSubInplace, VecZnxSwitchRing,
|
||||||
|
ScratchTakeBasic,
|
||||||
},
|
},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut},
|
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut},
|
||||||
|
oep::{ScratchAvailableImpl},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank},
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank},
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl LWEToGLWESwitchingKey<Vec<u8>> {
|
impl LWEToGLWESwitchingKey<Vec<u8>> {
|
||||||
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
Module<B>: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc<B>,
|
M: LWEToGLWESwitchingKeyEncrypt<BE>,
|
||||||
|
{
|
||||||
|
module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
||||||
|
// where
|
||||||
|
// A: GGLWEInfos,
|
||||||
|
// Module<B>: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc<B>,
|
||||||
|
// {
|
||||||
|
// debug_assert_eq!(
|
||||||
|
// infos.rank_in(),
|
||||||
|
// Rank(1),
|
||||||
|
// "rank_in != 1 is not supported for LWEToGLWESwitchingKey"
|
||||||
|
// );
|
||||||
|
// GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos)
|
||||||
|
// + GLWESecret::bytes_of(module, infos.rank_in())
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D: DataMut> LWEToGLWESwitchingKey<D> {
|
||||||
|
pub fn encrypt_sk<M, DLwe, DGlwe, BE: Backend>(
|
||||||
|
&mut self,
|
||||||
|
module: &M,
|
||||||
|
sk_lwe: &LWESecret<DLwe>,
|
||||||
|
sk_glwe: &GLWESecret<DGlwe>,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
M: LWEToGLWESwitchingKeyEncrypt<BE>,
|
||||||
|
DLwe: DataRef,
|
||||||
|
DGlwe: DataRef,
|
||||||
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
|
{
|
||||||
|
module.lwe_to_glwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch);
|
||||||
|
}
|
||||||
|
// #[allow(clippy::too_many_arguments)]
|
||||||
|
// pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
|
||||||
|
// &mut self,
|
||||||
|
// module: &Module<B>,
|
||||||
|
// sk_lwe: &LWESecret<DLwe>,
|
||||||
|
// sk_glwe: &GLWESecret<DGlwe>,
|
||||||
|
// source_xa: &mut Source,
|
||||||
|
// source_xe: &mut Source,
|
||||||
|
// scratch: &mut Scratch<B>,
|
||||||
|
// ) where
|
||||||
|
// DLwe: DataRef,
|
||||||
|
// DGlwe: DataRef,
|
||||||
|
// Module<B>: ModuleN
|
||||||
|
// + VecZnxAutomorphismInplace<B>
|
||||||
|
// + VecZnxAddScalarInplace
|
||||||
|
// + VecZnxDftBytesOf
|
||||||
|
// + VecZnxBigNormalize<B>
|
||||||
|
// + VecZnxDftApply<B>
|
||||||
|
// + SvpApplyDftToDftInplace<B>
|
||||||
|
// + VecZnxIdftApplyConsume<B>
|
||||||
|
// + VecZnxNormalizeTmpBytes
|
||||||
|
// + VecZnxFillUniform
|
||||||
|
// + VecZnxSubInplace
|
||||||
|
// + VecZnxAddInplace
|
||||||
|
// + VecZnxNormalizeInplace<B>
|
||||||
|
// + VecZnxAddNormal
|
||||||
|
// + VecZnxNormalize<B>
|
||||||
|
// + VecZnxSub
|
||||||
|
// + SvpPrepare<B>
|
||||||
|
// + VecZnxSwitchRing
|
||||||
|
// + SvpPPolBytesOf
|
||||||
|
// + SvpPPolAlloc<B>,
|
||||||
|
// Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
|
||||||
|
// {
|
||||||
|
// #[cfg(debug_assertions)]
|
||||||
|
// {
|
||||||
|
// use crate::layouts::LWEInfos;
|
||||||
|
|
||||||
|
// assert!(sk_lwe.n().0 <= module.n() as u32);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1));
|
||||||
|
// sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
|
||||||
|
// sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0);
|
||||||
|
// module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
|
||||||
|
|
||||||
|
// self.0.encrypt_sk(
|
||||||
|
// module,
|
||||||
|
// &sk_lwe_as_glwe,
|
||||||
|
// sk_glwe,
|
||||||
|
// source_xa,
|
||||||
|
// source_xe,
|
||||||
|
// scratch_1,
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait LWEToGLWESwitchingKeyEncrypt<BE: Backend>
|
||||||
|
where
|
||||||
|
Self: Sized
|
||||||
|
+ ModuleN
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ SvpPPolAlloc<BE>
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxAutomorphismInplace<BE>
|
||||||
|
+ VecZnxAddScalarInplace
|
||||||
|
+ VecZnxBigNormalize<BE>
|
||||||
|
+ VecZnxDftApply<BE>
|
||||||
|
+ SvpApplyDftToDftInplace<BE>
|
||||||
|
+ VecZnxIdftApplyConsume<BE>
|
||||||
|
+ VecZnxNormalize<BE>
|
||||||
|
+ VecZnxNormalizeInplace<BE>
|
||||||
|
+ VecZnxAddNormal
|
||||||
|
+ VecZnxSub
|
||||||
|
+ VecZnxSubInplace
|
||||||
|
+ VecZnxAddInplace
|
||||||
|
+ SvpPrepare<BE>
|
||||||
|
+ VecZnxSwitchRing
|
||||||
|
+ ScratchAvailable
|
||||||
|
+ ScratchTakeBasic
|
||||||
|
+ ScratchAvailableImpl<BE>
|
||||||
|
{
|
||||||
|
fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos;
|
||||||
|
|
||||||
|
fn lwe_to_glwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk_lwe: &LWESecret<DLwe>,
|
||||||
|
sk_glwe: &GLWESecret<DataGlwe>,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
R: LWEToGLWESwitchingKeyToMut,
|
||||||
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE> where
|
||||||
|
Module<BE>: ModuleN
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ SvpPPolAlloc<BE>
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxAutomorphismInplace<BE>
|
||||||
|
+ VecZnxAddScalarInplace
|
||||||
|
+ VecZnxBigNormalize<BE>
|
||||||
|
+ VecZnxDftApply<BE>
|
||||||
|
+ SvpApplyDftToDftInplace<BE>
|
||||||
|
+ VecZnxIdftApplyConsume<BE>
|
||||||
|
+ VecZnxNormalize<BE>
|
||||||
|
+ VecZnxNormalizeInplace<BE>
|
||||||
|
+ VecZnxAddNormal
|
||||||
|
+ VecZnxSub
|
||||||
|
+ VecZnxSubInplace
|
||||||
|
+ VecZnxAddInplace
|
||||||
|
+ SvpPrepare<BE>
|
||||||
|
+ VecZnxSwitchRing
|
||||||
|
+ ScratchAvailable
|
||||||
|
+ ScratchAvailableImpl<BE>
|
||||||
|
+ ScratchTakeBasic
|
||||||
|
+ ScratchTakeCore<BE>
|
||||||
|
+ GLWESwitchingKeyEncryptSk<BE>
|
||||||
|
{
|
||||||
|
fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
|
where
|
||||||
|
A: GGLWEInfos
|
||||||
{
|
{
|
||||||
debug_assert_eq!(
|
debug_assert_eq!(
|
||||||
infos.rank_in(),
|
infos.rank_in(),
|
||||||
Rank(1),
|
Rank(1),
|
||||||
"rank_in != 1 is not supported for LWEToGLWESwitchingKey"
|
"rank_in != 1 is not supported for LWEToGLWESwitchingKey"
|
||||||
);
|
);
|
||||||
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos)
|
GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
|
||||||
+ GLWESecret::bytes_of(module, infos.rank_in())
|
+ GLWESecret::bytes_of(self, infos.rank_in())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<D: DataMut> LWEToGLWESwitchingKey<D> {
|
fn lwe_to_glwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
|
||||||
#[allow(clippy::too_many_arguments)]
|
&self,
|
||||||
pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
|
res: &mut R,
|
||||||
&mut self,
|
|
||||||
module: &Module<B>,
|
|
||||||
sk_lwe: &LWESecret<DLwe>,
|
sk_lwe: &LWESecret<DLwe>,
|
||||||
sk_glwe: &GLWESecret<DGlwe>,
|
sk_glwe: &GLWESecret<DataGlwe>,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<B>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
DLwe: DataRef,
|
R: LWEToGLWESwitchingKeyToMut,
|
||||||
DGlwe: DataRef,
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
Module<B>: ModuleN
|
|
||||||
+ VecZnxAutomorphismInplace<B>
|
|
||||||
+ VecZnxAddScalarInplace
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxBigNormalize<B>
|
|
||||||
+ VecZnxDftApply<B>
|
|
||||||
+ SvpApplyDftToDftInplace<B>
|
|
||||||
+ VecZnxIdftApplyConsume<B>
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxFillUniform
|
|
||||||
+ VecZnxSubInplace
|
|
||||||
+ VecZnxAddInplace
|
|
||||||
+ VecZnxNormalizeInplace<B>
|
|
||||||
+ VecZnxAddNormal
|
|
||||||
+ VecZnxNormalize<B>
|
|
||||||
+ VecZnxSub
|
|
||||||
+ SvpPrepare<B>
|
|
||||||
+ VecZnxSwitchRing
|
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ SvpPPolAlloc<B>,
|
|
||||||
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
|
|
||||||
{
|
{
|
||||||
|
let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
use crate::layouts::LWEInfos;
|
use crate::layouts::LWEInfos;
|
||||||
|
|
||||||
assert!(sk_lwe.n().0 <= module.n() as u32);
|
assert!(sk_lwe.n().0 <= self.n() as u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1));
|
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1));
|
||||||
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
|
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
|
||||||
sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0);
|
sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0);
|
||||||
module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
|
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
|
||||||
|
|
||||||
self.0.encrypt_sk(
|
res.0.encrypt_sk(
|
||||||
module,
|
self,
|
||||||
&sk_lwe_as_glwe,
|
&sk_lwe_as_glwe,
|
||||||
sk_glwe,
|
sk_glwe,
|
||||||
source_xa,
|
source_xa,
|
||||||
|
|||||||
Reference in New Issue
Block a user