mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
review of encryption
This commit is contained in:
@@ -1,17 +1,14 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{
|
||||||
ModuleN, ScratchTakeBasic,
|
ModuleN, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf,
|
||||||
SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxBigBytesOf,
|
VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes,
|
||||||
VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes,
|
|
||||||
},
|
},
|
||||||
layouts::{Backend, DataMut, DataViewMut, Module, Scratch},
|
layouts::{Backend, DataMut, DataViewMut, Module, Scratch},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::layouts::{
|
||||||
layouts::{
|
GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToMut, GLWEToMut, LWEInfos,
|
||||||
GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, GLWEToMut, GLWEPlaintextToMut,
|
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
|
||||||
prepared::{GLWESecretPreparedToRef, GLWESecretPrepared},
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
impl GLWE<Vec<u8>> {
|
impl GLWE<Vec<u8>> {
|
||||||
@@ -25,12 +22,12 @@ impl GLWE<Vec<u8>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<DataSelf: DataMut> GLWE<DataSelf> {
|
impl<DataSelf: DataMut> GLWE<DataSelf> {
|
||||||
pub fn decrypt<P, S, M, BE: Backend>(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch<BE>)
|
pub fn decrypt<P, S, M, BE: Backend>(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch<BE>)
|
||||||
where
|
where
|
||||||
P: GLWEPlaintextToMut,
|
P: GLWEPlaintextToMut,
|
||||||
S: GLWESecretPreparedToRef<BE>,
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
M: GLWEDecryption<BE>,
|
M: GLWEDecryption<BE>,
|
||||||
Scratch<BE>: ScratchTakeBasic,
|
Scratch<BE>: ScratchTakeBasic,
|
||||||
{
|
{
|
||||||
module.glwe_decrypt(self, pt, sk, scratch);
|
module.glwe_decrypt(self, pt, sk, scratch);
|
||||||
}
|
}
|
||||||
@@ -48,29 +45,23 @@ where
|
|||||||
+ VecZnxIdftApplyConsume<BE>
|
+ VecZnxIdftApplyConsume<BE>
|
||||||
+ VecZnxBigAddInplace<BE>
|
+ VecZnxBigAddInplace<BE>
|
||||||
+ VecZnxBigAddSmallInplace<BE>
|
+ VecZnxBigAddSmallInplace<BE>
|
||||||
+ VecZnxBigNormalize<BE>
|
+ VecZnxBigNormalize<BE>,
|
||||||
{
|
{
|
||||||
fn glwe_decrypt_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn glwe_decrypt_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GLWEInfos
|
A: GLWEInfos,
|
||||||
{
|
{
|
||||||
let size: usize = infos.size();
|
let size: usize = infos.size();
|
||||||
(self.vec_znx_normalize_tmp_bytes() | self.bytes_of_vec_znx_dft(1, size)) + self.bytes_of_vec_znx_dft(1, size)
|
(self.vec_znx_normalize_tmp_bytes() | self.bytes_of_vec_znx_dft(1, size)) + self.bytes_of_vec_znx_dft(1, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn glwe_decrypt<R, P, S>(
|
fn glwe_decrypt<R, P, S>(&self, res: &mut R, pt: &mut P, sk: &S, scratch: &mut Scratch<BE>)
|
||||||
&self,
|
where
|
||||||
res: &mut R,
|
|
||||||
pt: &mut P,
|
|
||||||
sk: &S,
|
|
||||||
scratch: &mut Scratch<BE>,
|
|
||||||
) where
|
|
||||||
R: GLWEToMut,
|
R: GLWEToMut,
|
||||||
P: GLWEPlaintextToMut,
|
P: GLWEPlaintextToMut,
|
||||||
S: GLWESecretPreparedToRef<BE>,
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
Scratch<BE>: ScratchTakeBasic,
|
Scratch<BE>: ScratchTakeBasic,
|
||||||
{
|
{
|
||||||
|
|
||||||
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
||||||
let pt: &mut GLWEPlaintext<&mut [u8]> = &mut pt.to_ref();
|
let pt: &mut GLWEPlaintext<&mut [u8]> = &mut pt.to_ref();
|
||||||
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
||||||
@@ -117,10 +108,9 @@ where
|
|||||||
pt.base2k = res.base2k();
|
pt.base2k = res.base2k();
|
||||||
pt.k = pt.k().min(res.k());
|
pt.k = pt.k().min(res.k());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <BE: Backend> GLWEDecryption<BE> for Module<BE> where
|
impl<BE: Backend> GLWEDecryption<BE> for Module<BE> where
|
||||||
Self: ModuleN
|
Self: ModuleN
|
||||||
+ VecZnxDftBytesOf
|
+ VecZnxDftBytesOf
|
||||||
+ VecZnxNormalizeTmpBytes
|
+ VecZnxNormalizeTmpBytes
|
||||||
@@ -132,4 +122,4 @@ impl <BE: Backend> GLWEDecryption<BE> for Module<BE> where
|
|||||||
+ VecZnxBigAddSmallInplace<BE>
|
+ VecZnxBigAddSmallInplace<BE>
|
||||||
+ VecZnxBigNormalize<BE>
|
+ VecZnxBigNormalize<BE>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,9 @@ use poulpy_hal::{
|
|||||||
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
|
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToMut, LWESecretToRef};
|
use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToMut, LWESecret, LWESecretToRef, LWEToMut};
|
||||||
|
|
||||||
impl<DataSelf: DataRef + DataMut> LWE<DataSelf>
|
impl<DataSelf: DataRef + DataMut> LWE<DataSelf> {
|
||||||
{
|
|
||||||
pub fn decrypt<P, S, M, B>(&mut self, module: &M, pt: &mut P, sk: S)
|
pub fn decrypt<P, S, M, B>(&mut self, module: &M, pt: &mut P, sk: S)
|
||||||
where
|
where
|
||||||
P: LWEPlaintextToMut,
|
P: LWEPlaintextToMut,
|
||||||
@@ -21,16 +20,15 @@ impl<DataSelf: DataRef + DataMut> LWE<DataSelf>
|
|||||||
|
|
||||||
pub trait LWEDecrypt<BE: Backend>
|
pub trait LWEDecrypt<BE: Backend>
|
||||||
where
|
where
|
||||||
Self: Sized + ZnNormalizeInplace<BE>
|
Self: Sized + ZnNormalizeInplace<BE>,
|
||||||
{
|
{
|
||||||
fn lwe_decrypt<R, P, S>(&self, res: &mut R, pt: &mut P, sk: S)
|
fn lwe_decrypt<R, P, S>(&self, res: &mut R, pt: &mut P, sk: S)
|
||||||
where
|
where
|
||||||
R: LWEToMut,
|
R: LWEToMut,
|
||||||
P: LWEPlaintextToMut,
|
P: LWEPlaintextToMut,
|
||||||
S: LWESecretToRef,
|
S: LWESecretToRef,
|
||||||
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
|
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
|
||||||
{
|
{
|
||||||
|
|
||||||
let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
|
let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
|
||||||
let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut();
|
let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut();
|
||||||
let sk: LWESecret<&[u8]> = sk.to_ref();
|
let sk: LWESecret<&[u8]> = sk.to_ref();
|
||||||
@@ -60,8 +58,4 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> LWEDecrypt<BE> for Module<BE> where
|
impl<BE: Backend> LWEDecrypt<BE> for Module<BE> where Self: Sized + ZnNormalizeInplace<BE> {}
|
||||||
Self: Sized + ZnNormalizeInplace<BE>
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes},
|
api::{
|
||||||
|
ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf,
|
||||||
|
VecZnxNormalizeTmpBytes,
|
||||||
|
},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, Scratch},
|
layouts::{Backend, DataMut, DataRef, Module, Scratch},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
@@ -17,7 +20,8 @@ impl AutomorphismKeyCompressed<Vec<u8>> {
|
|||||||
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
Module<B>: ModuleN + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf,
|
Module<B>:
|
||||||
|
ModuleN + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf,
|
||||||
{
|
{
|
||||||
assert_eq!(module.n() as u32, infos.n());
|
assert_eq!(module.n() as u32, infos.n());
|
||||||
GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(module, infos.rank_out())
|
GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(module, infos.rank_out())
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing},
|
api::{
|
||||||
|
ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf,
|
||||||
|
VecZnxNormalizeTmpBytes, VecZnxSwitchRing,
|
||||||
|
},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
|
layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
@@ -18,7 +21,8 @@ impl GLWESwitchingKeyCompressed<Vec<u8>> {
|
|||||||
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
Module<B>: ModuleN + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf,
|
Module<B>:
|
||||||
|
ModuleN + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf,
|
||||||
{
|
{
|
||||||
(GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1))
|
(GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1))
|
||||||
+ ScalarZnx::bytes_of(module.n(), infos.rank_in().into())
|
+ ScalarZnx::bytes_of(module.n(), infos.rank_in().into())
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{
|
||||||
ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
|
ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf,
|
||||||
VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes,
|
VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes,
|
||||||
},
|
},
|
||||||
oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl},
|
|
||||||
layouts::{Backend, DataMut, DataRef, Module, Scratch},
|
layouts::{Backend, DataMut, DataRef, Module, Scratch},
|
||||||
|
oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ use crate::{
|
|||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk,
|
encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk,
|
||||||
layouts::{
|
layouts::{
|
||||||
GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey,
|
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey,
|
||||||
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
|
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -21,7 +21,13 @@ impl TensorKeyCompressed<Vec<u8>> {
|
|||||||
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf,
|
Module<B>: ModuleN
|
||||||
|
+ SvpPPolBytesOf
|
||||||
|
+ SvpPPolAlloc<B>
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxNormalizeTmpBytes
|
||||||
|
+ VecZnxBigBytesOf,
|
||||||
{
|
{
|
||||||
TensorKey::encrypt_sk_tmp_bytes(module, infos)
|
TensorKey::encrypt_sk_tmp_bytes(module, infos)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +1,30 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{ScratchAvailable, VecZnxAutomorphism},
|
||||||
ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
|
|
||||||
VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
|
|
||||||
VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
|
|
||||||
VecZnxSwitchRing,
|
|
||||||
},
|
|
||||||
layouts::{Backend, DataMut, GaloisElement, Module, Scratch},
|
layouts::{Backend, DataMut, GaloisElement, Module, Scratch},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey,
|
layouts::{AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos},
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
impl AutomorphismKey<Vec<u8>> {
|
impl AutomorphismKey<Vec<u8>> {
|
||||||
|
|
||||||
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
M: GGLWEAutomorphismKeyEncryptSk<BE>
|
M: AutomorphismKeyEncryptSk<BE>,
|
||||||
{
|
{
|
||||||
module.gglwe_automorphism_key_encrypt_sk_tmp_bytes(infos)
|
module.automorphism_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: GGLWEAutomorphismKeyEncryptPk<BE>
|
M: GGLWEAutomorphismKeyEncryptPk<BE>,
|
||||||
{
|
{
|
||||||
module.gglwe_automorphism_key_encrypt_pk_tmp_bytes(infos)
|
module.automorphism_key_encrypt_pk_tmp_bytes(infos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,9 +32,9 @@ impl<DM: DataMut> AutomorphismKey<DM>
|
|||||||
where
|
where
|
||||||
Self: AutomorphismKeyToMut,
|
Self: AutomorphismKeyToMut,
|
||||||
{
|
{
|
||||||
pub fn encrypt_sk<S, BE: Backend>(
|
pub fn encrypt_sk<S, M, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<BE>,
|
module: &M,
|
||||||
p: i64,
|
p: i64,
|
||||||
sk: &S,
|
sk: &S,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
@@ -49,57 +42,36 @@ where
|
|||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
S: GLWESecretToRef,
|
S: GLWESecretToRef,
|
||||||
Module<BE>: GGLWEAutomorphismKeyEncryptSk<BE>,
|
M: AutomorphismKeyEncryptSk<BE>,
|
||||||
{
|
{
|
||||||
module.gglwe_automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch);
|
module.automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait GGLWEAutomorphismKeyEncryptSk<BE: Backend> {
|
pub trait AutomorphismKeyEncryptSk<BE: Backend> {
|
||||||
fn gglwe_automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
fn gglwe_automorphism_key_encrypt_sk<A, B>(
|
fn automorphism_key_encrypt_sk<R, S>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut A,
|
res: &mut R,
|
||||||
p: i64,
|
p: i64,
|
||||||
sk: &B,
|
sk: &S,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
A: AutomorphismKeyToMut,
|
R: AutomorphismKeyToMut,
|
||||||
B: GLWESecretToRef;
|
S: GLWESecretToRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GGLWEAutomorphismKeyEncryptSk<BE> for Module<BE>
|
impl<BE: Backend> AutomorphismKeyEncryptSk<BE> for Module<BE>
|
||||||
where
|
where
|
||||||
Module<BE>: ModuleN
|
Module<BE>: GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphism + GaloisElement,
|
||||||
+ VecZnxAddScalarInplace
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxBigNormalize<BE>
|
|
||||||
+ VecZnxDftApply<BE>
|
|
||||||
+ SvpApplyDftToDftInplace<BE>
|
|
||||||
+ VecZnxIdftApplyConsume<BE>
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxFillUniform
|
|
||||||
+ VecZnxSubInplace
|
|
||||||
+ VecZnxAddInplace
|
|
||||||
+ VecZnxNormalizeInplace<BE>
|
|
||||||
+ VecZnxAddNormal
|
|
||||||
+ VecZnxNormalize<BE>
|
|
||||||
+ VecZnxSub
|
|
||||||
+ SvpPrepare<BE>
|
|
||||||
+ VecZnxSwitchRing
|
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ VecZnxAutomorphism
|
|
||||||
+ SvpPPolAlloc<BE>
|
|
||||||
+ GaloisElement,
|
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
|
||||||
{
|
{
|
||||||
|
fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
fn gglwe_automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
{
|
{
|
||||||
@@ -108,38 +80,33 @@ where
|
|||||||
infos.rank_out(),
|
infos.rank_out(),
|
||||||
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
|
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
|
||||||
);
|
);
|
||||||
GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of_from_infos(self, &infos.glwe_layout())
|
self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(self, infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gglwe_automorphism_key_encrypt_sk<A, B>(
|
fn automorphism_key_encrypt_sk<R, S>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut A,
|
res: &mut R,
|
||||||
p: i64,
|
p: i64,
|
||||||
sk: &B,
|
sk: &S,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
A: AutomorphismKeyToMut,
|
R: AutomorphismKeyToMut,
|
||||||
B: GLWESecretToRef,
|
S: GLWESecretToRef,
|
||||||
{
|
{
|
||||||
let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
|
let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut();
|
||||||
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
|
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert_eq!(res.n(), sk.n());
|
||||||
{
|
assert_eq!(res.rank_out(), res.rank_in());
|
||||||
use crate::layouts::{GLWEInfos, LWEInfos};
|
assert_eq!(sk.rank(), res.rank_out());
|
||||||
|
assert!(
|
||||||
assert_eq!(res.n(), sk.n());
|
scratch.available() >= self.automorphism_key_encrypt_sk_tmp_bytes(res),
|
||||||
assert_eq!(res.rank_out(), res.rank_in());
|
"scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}",
|
||||||
assert_eq!(sk.rank(), res.rank_out());
|
scratch.available(),
|
||||||
assert!(
|
self.automorphism_key_encrypt_sk_tmp_bytes(res)
|
||||||
scratch.available() >= AutomorphismKey::encrypt_sk_tmp_bytes(self, res),
|
);
|
||||||
"scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}",
|
|
||||||
scratch.available(),
|
|
||||||
AutomorphismKey::encrypt_sk_tmp_bytes(self, res)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank());
|
let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank());
|
||||||
|
|
||||||
@@ -162,45 +129,21 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub trait GGLWEAutomorphismKeyEncryptPk<BE: Backend> {
|
pub trait GGLWEAutomorphismKeyEncryptPk<BE: Backend> {
|
||||||
fn gglwe_automorphism_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn automorphism_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GGLWEAutomorphismKeyEncryptPk<BE> for Module<BE>
|
impl<BE: Backend> GGLWEAutomorphismKeyEncryptPk<BE> for Module<BE>
|
||||||
where
|
where
|
||||||
Module<BE>: ModuleN
|
Module<BE>:,
|
||||||
+ VecZnxAddScalarInplace
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
+ 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 automorphism_key_encrypt_pk_tmp_bytes<A>(&self, _infos: &A) -> usize
|
||||||
fn gglwe_automorphism_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
{
|
{
|
||||||
assert_eq!(
|
unimplemented!()
|
||||||
infos.rank_in(),
|
|
||||||
infos.rank_out(),
|
|
||||||
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
|
|
||||||
);
|
|
||||||
GLWESwitchingKey::encrypt_pk_tmp_bytes(self, infos)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes},
|
api::{ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes},
|
||||||
// oep::SvpPPolAllocBytesImpl,
|
layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero},
|
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
encryption::glwe_ct::GLWEEncryptSk, layouts::GLWEInfos, ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
|
encryption::glwe_ct::GLWEEncryptSk,
|
||||||
|
layouts::GLWEInfos,
|
||||||
layouts::{
|
layouts::{
|
||||||
GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos,
|
GGLWE, GGLWEInfos, GGLWEToMut, GLWEPlaintext, LWEInfos,
|
||||||
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
|
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl GGLWE<Vec<u8>> {
|
impl GGLWE<Vec<u8>> {
|
||||||
|
|
||||||
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
@@ -32,16 +32,31 @@ impl GGLWE<Vec<u8>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait GGLWEEncryptSk<B: Backend> {
|
impl<D: DataMut> GGLWE<D> {
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn encrypt_sk<P, S, M, BE: Backend>(
|
||||||
|
&mut self,
|
||||||
|
module: &M,
|
||||||
|
pt: &P,
|
||||||
|
sk: &S,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
P: ScalarZnxToRef,
|
||||||
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
|
M: GGLWEEncryptSk<BE>,
|
||||||
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
|
{
|
||||||
|
module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait GGLWEEncryptSk<BE: Backend> {
|
||||||
fn gglwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn gglwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
fn gglwe_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
|
||||||
where
|
|
||||||
A: GGLWEInfos;
|
|
||||||
|
|
||||||
fn gglwe_encrypt_sk<R, P, S>(
|
fn gglwe_encrypt_sk<R, P, S>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
@@ -49,40 +64,29 @@ pub trait GGLWEEncryptSk<B: Backend> {
|
|||||||
sk: &S,
|
sk: &S,
|
||||||
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
|
||||||
R: GGLWEToMut,
|
R: GGLWEToMut,
|
||||||
P: ScalarZnxToRef,
|
P: ScalarZnxToRef,
|
||||||
S: GLWESecretPreparedToRef<B>;
|
S: GLWESecretPreparedToRef<BE>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<B: Backend> GGLWEEncryptSk<B> for Module<B>
|
impl<BE: Backend> GGLWEEncryptSk<BE> for Module<BE>
|
||||||
where
|
where
|
||||||
Module<B>: ModuleN
|
Module<BE>: ModuleN
|
||||||
+ GLWEEncryptSk<B>
|
+ GLWEEncryptSk<BE>
|
||||||
+ VecZnxNormalizeTmpBytes
|
+ VecZnxNormalizeTmpBytes
|
||||||
+ VecZnxDftBytesOf
|
+ VecZnxDftBytesOf
|
||||||
+ VecZnxAddScalarInplace
|
+ VecZnxAddScalarInplace
|
||||||
+ VecZnxNormalizeInplace<B>,
|
+ VecZnxNormalizeInplace<BE>,
|
||||||
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
fn gglwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn gglwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
{
|
{
|
||||||
GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout())
|
self.glwe_encrypt_sk_tmp_bytes(infos)
|
||||||
+ (GLWEPlaintext::bytes_of_from_infos(self, &infos.glwe_layout()) | self.vec_znx_normalize_tmp_bytes())
|
+ GLWEPlaintext::bytes_of_from_infos(self, infos).max(self.vec_znx_normalize_tmp_bytes())
|
||||||
}
|
|
||||||
|
|
||||||
fn gglwe_encrypt_pk_tmp_bytes<A>(&self, _infos: &A) -> usize
|
|
||||||
where
|
|
||||||
A: GGLWEInfos,
|
|
||||||
{
|
|
||||||
unimplemented!()
|
|
||||||
// TODO: Is this correct?
|
|
||||||
// GLWE::encrypt_pk_tmp_bytes(self, &infos.glwe_layout())
|
|
||||||
// + (GLWEPlaintext::bytes_of_from_infos(self, &infos.glwe_layout()) | self.vec_znx_normalize_tmp_bytes())
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gglwe_encrypt_sk<R, P, S>(
|
fn gglwe_encrypt_sk<R, P, S>(
|
||||||
@@ -92,54 +96,49 @@ where
|
|||||||
sk: &S,
|
sk: &S,
|
||||||
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
|
||||||
R: GGLWEToMut,
|
R: GGLWEToMut,
|
||||||
P: ScalarZnxToRef,
|
P: ScalarZnxToRef,
|
||||||
S: GLWESecretPreparedToRef<B>,
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
{
|
{
|
||||||
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
|
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
|
||||||
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
|
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
|
||||||
|
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert_eq!(
|
||||||
{
|
res.rank_in(),
|
||||||
use poulpy_hal::layouts::ZnxInfos;
|
pt.cols() as u32,
|
||||||
let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref();
|
"res.rank_in(): {} != pt.cols(): {}",
|
||||||
|
res.rank_in(),
|
||||||
assert_eq!(
|
pt.cols()
|
||||||
res.rank_in(),
|
);
|
||||||
pt.cols() as u32,
|
assert_eq!(
|
||||||
"res.rank_in(): {} != pt.cols(): {}",
|
res.rank_out(),
|
||||||
res.rank_in(),
|
sk.rank(),
|
||||||
pt.cols()
|
"res.rank_out(): {} != sk.rank(): {}",
|
||||||
);
|
res.rank_out(),
|
||||||
assert_eq!(
|
sk.rank()
|
||||||
res.rank_out(),
|
);
|
||||||
sk.rank(),
|
assert_eq!(res.n(), sk.n());
|
||||||
"res.rank_out(): {} != sk.rank(): {}",
|
assert_eq!(pt.n() as u32, sk.n());
|
||||||
res.rank_out(),
|
assert!(
|
||||||
sk.rank()
|
scratch.available() >= self.gglwe_encrypt_sk_tmp_bytes(res),
|
||||||
);
|
"scratch.available: {} < GGLWE::encrypt_sk_tmp_bytes(self, res.rank()={}, res.size()={}): {}",
|
||||||
assert_eq!(res.n(), sk.n());
|
scratch.available(),
|
||||||
assert_eq!(pt.n() as u32, sk.n());
|
res.rank_out(),
|
||||||
assert!(
|
res.size(),
|
||||||
scratch.available() >= GGLWE::encrypt_sk_tmp_bytes(self, res),
|
self.gglwe_encrypt_sk_tmp_bytes(res)
|
||||||
"scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes(self, res.rank()={}, res.size()={}): {}",
|
);
|
||||||
scratch.available(),
|
assert!(
|
||||||
res.rank_out(),
|
res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0,
|
||||||
res.size(),
|
"res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}",
|
||||||
GGLWE::encrypt_sk_tmp_bytes(self, res)
|
res.dnum(),
|
||||||
);
|
res.dsize(),
|
||||||
assert!(
|
res.base2k(),
|
||||||
res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0,
|
res.dnum().0 * res.dsize().0 * res.base2k().0,
|
||||||
"res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}",
|
res.k()
|
||||||
res.dnum(),
|
);
|
||||||
res.dsize(),
|
|
||||||
res.base2k(),
|
|
||||||
res.dnum().0 * res.dsize().0 * res.base2k().0,
|
|
||||||
res.k()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let dnum: usize = res.dnum().into();
|
let dnum: usize = res.dnum().into();
|
||||||
let dsize: usize = res.dsize().into();
|
let dsize: usize = res.dsize().into();
|
||||||
@@ -147,7 +146,7 @@ where
|
|||||||
let rank_in: usize = res.rank_in().into();
|
let rank_in: usize = res.rank_in().into();
|
||||||
|
|
||||||
let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, &res.glwe_layout());
|
let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, &res.glwe_layout());
|
||||||
// For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns
|
// For each input column (i.e. rank) produces a GGLWE of rank_out+1 columns
|
||||||
//
|
//
|
||||||
// Example for ksk rank 2 to rank 3:
|
// Example for ksk rank 2 to rank 3:
|
||||||
//
|
//
|
||||||
@@ -177,20 +176,3 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<DataSelf: DataMut> GGLWE<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>: GGLWEEncryptSk<B>,
|
|
||||||
{
|
|
||||||
module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPrepare, VecZnxSwitchRing},
|
||||||
ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
|
layouts::{Backend, DataMut, Module, ScalarZnx, Scratch},
|
||||||
VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume,
|
|
||||||
VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing,
|
|
||||||
ScratchTakeBasic,
|
|
||||||
},
|
|
||||||
layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
|
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{
|
|
||||||
GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, prepared::GLWESecretPrepared,
|
|
||||||
},
|
|
||||||
encryption::gglwe_ct::GGLWEEncryptSk,
|
encryption::gglwe_ct::GGLWEEncryptSk,
|
||||||
|
layouts::{
|
||||||
|
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos,
|
||||||
|
prepared::GLWESecretPreparedAlloc,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl GLWESwitchingKey<Vec<u8>> {
|
impl GLWESwitchingKey<Vec<u8>> {
|
||||||
@@ -36,209 +32,102 @@ impl GLWESwitchingKey<Vec<u8>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
|
impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
|
||||||
|
pub fn encrypt_sk<M, S1, S2, BE: Backend>(
|
||||||
pub fn encrypt_sk<M, DataSkIn: DataRef, DataSkOut: DataRef, BE: Backend>(
|
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &M,
|
module: &M,
|
||||||
sk_in: &GLWESecret<DataSkIn>,
|
sk_in: &S1,
|
||||||
sk_out: &GLWESecret<DataSkOut>,
|
sk_out: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
|
S1: GLWESecretToRef,
|
||||||
|
S2: GLWESecretToRef,
|
||||||
M: GLWESwitchingKeyEncryptSk<BE>,
|
M: GLWESwitchingKeyEncryptSk<BE>,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
module.glwe_switching_key_encrypt_sk(self, sk_in, sk_out, source_xa, source_xe, scratch);
|
module.glwe_switching_key_encrypt_sk(self, sk_in, sk_out, source_xa, source_xe, scratch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// #[allow(clippy::too_many_arguments)]
|
|
||||||
// pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
|
|
||||||
// &mut self,
|
|
||||||
// module: &Module<B>,
|
|
||||||
// sk_in: &GLWESecret<DataSkIn>,
|
|
||||||
// sk_out: &GLWESecret<DataSkOut>,
|
|
||||||
// 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 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());
|
|
||||||
// (0..sk_in.rank().into()).for_each(|i| {
|
|
||||||
// module.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(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();
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait GLWESwitchingKeyEncryptSk<BE: Backend> {
|
||||||
pub trait GLWESwitchingKeyEncryptSk<BE: Backend>
|
|
||||||
where
|
|
||||||
Self: Sized
|
|
||||||
+ ModuleN
|
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ SvpPPolAlloc<BE>
|
|
||||||
+ GGLWEEncryptSk<BE>,
|
|
||||||
{
|
|
||||||
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
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
fn glwe_switching_key_encrypt_sk<R, DataSkIn, DataSkOut>(
|
fn glwe_switching_key_encrypt_sk<R, S1, S2>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
sk_in: &GLWESecret<DataSkIn>,
|
sk_in: &S1,
|
||||||
sk_out: &GLWESecret<DataSkOut>,
|
sk_out: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
)
|
) where
|
||||||
where
|
|
||||||
R: GLWESwitchingKeyToMut,
|
R: GLWESwitchingKeyToMut,
|
||||||
DataSkIn: DataRef,
|
S1: GLWESecretToRef,
|
||||||
DataSkOut: DataRef,
|
S2: GLWESecretToRef,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>;
|
Scratch<BE>: ScratchTakeCore<BE>;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE> where
|
impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE>
|
||||||
Self: ModuleN
|
where
|
||||||
+ SvpPPolBytesOf
|
Self: ModuleN + GGLWEEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + VecZnxSwitchRing + SvpPrepare<BE>,
|
||||||
+ VecZnxDftBytesOf
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ SvpPPolAlloc<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
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
{
|
{
|
||||||
(GGLWE::encrypt_sk_tmp_bytes(self, infos) | ScalarZnx::bytes_of(self.n(), 1))
|
self.gglwe_encrypt_sk_tmp_bytes(infos)
|
||||||
|
.max(ScalarZnx::bytes_of(self.n(), 1))
|
||||||
+ ScalarZnx::bytes_of(self.n(), infos.rank_in().into())
|
+ ScalarZnx::bytes_of(self.n(), infos.rank_in().into())
|
||||||
+ GLWESecretPrepared::bytes_of_from_infos(self, &infos.glwe_layout())
|
+ self.bytes_of_glwe_secret_prepared_from_infos(infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn glwe_switching_key_encrypt_sk<R, DataSkIn, DataSkOut>(
|
fn glwe_switching_key_encrypt_sk<R, S1, S2>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
sk_in: &GLWESecret<DataSkIn>,
|
sk_in: &S1,
|
||||||
sk_out: &GLWESecret<DataSkOut>,
|
sk_out: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
)
|
) where
|
||||||
where
|
|
||||||
R: GLWESwitchingKeyToMut,
|
R: GLWESwitchingKeyToMut,
|
||||||
DataSkIn: DataRef,
|
S1: GLWESecretToRef,
|
||||||
DataSkOut: DataRef,
|
S2: GLWESecretToRef,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
|
||||||
{
|
{
|
||||||
|
|
||||||
let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
|
let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
|
||||||
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
|
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
|
||||||
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
|
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert!(sk_in.n().0 <= self.n() as u32);
|
||||||
{
|
assert!(sk_out.n().0 <= self.n() as u32);
|
||||||
assert!(sk_in.n().0 <= self.n() as u32);
|
assert!(
|
||||||
assert!(sk_out.n().0 <= self.n() as u32);
|
scratch.available() >= self.glwe_switching_key_encrypt_sk_tmp_bytes(res),
|
||||||
assert!(
|
"scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
|
||||||
scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res),
|
scratch.available(),
|
||||||
"scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
|
self.glwe_switching_key_encrypt_sk_tmp_bytes(res)
|
||||||
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());
|
let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into());
|
||||||
(0..sk_in.rank().into()).for_each(|i| {
|
for i in 0..sk_in.rank().into() {
|
||||||
self.vec_znx_switch_ring(
|
self.vec_znx_switch_ring(
|
||||||
&mut sk_in_tmp.as_vec_znx_mut(),
|
&mut sk_in_tmp.as_vec_znx_mut(),
|
||||||
i,
|
i,
|
||||||
&sk_in.data.as_vec_znx(),
|
&sk_in.data.as_vec_znx(),
|
||||||
i,
|
i,
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
|
||||||
let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank());
|
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);
|
let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1);
|
||||||
(0..sk_out.rank().into()).for_each(|i| {
|
for i in 0..sk_out.rank().into() {
|
||||||
self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), 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);
|
self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.key.encrypt_sk(
|
res.key.encrypt_sk(
|
||||||
@@ -251,39 +140,20 @@ impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE> where
|
|||||||
);
|
);
|
||||||
res.sk_in_n = sk_in.n().into();
|
res.sk_in_n = sk_in.n().into();
|
||||||
res.sk_out_n = sk_out.n().into();
|
res.sk_out_n = sk_out.n().into();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait GLWESwitchingKeyEncryptPk<BE: Backend>
|
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
|
fn glwe_switching_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GLWESwitchingKeyEncryptPk<BE> for Module<BE> where
|
impl<BE: Backend> GLWESwitchingKeyEncryptPk<BE> for Module<BE> {
|
||||||
Self: ModuleN
|
fn glwe_switching_key_encrypt_pk_tmp_bytes<A>(&self, _infos: &A) -> usize
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ SvpPPolAlloc<BE>
|
|
||||||
+ GGLWEEncryptSk<BE>,
|
|
||||||
{
|
|
||||||
|
|
||||||
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)
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,156 +1,54 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{
|
||||||
ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
|
ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply,
|
||||||
VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
|
VecZnxDftBytesOf, VecZnxIdftApplyTmpA,
|
||||||
VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
|
||||||
VecZnxSubInplace, VecZnxSwitchRing,
|
|
||||||
},
|
},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, Scratch},
|
layouts::{Backend, DataMut, Module, Scratch},
|
||||||
oep::{VecZnxAddScalarInplaceImpl, VecZnxBigAllocBytesImpl, VecZnxDftApplyImpl, SvpApplyDftToDftImpl, VecZnxIdftApplyTmpAImpl, VecZnxBigNormalizeImpl},
|
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{
|
|
||||||
GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut,
|
|
||||||
prepared::GLWESecretPrepared,
|
|
||||||
},
|
|
||||||
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
|
layouts::{
|
||||||
|
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GetDist, LWEInfos, Rank, TensorKey, TensorKeyToMut,
|
||||||
|
prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl TensorKey<Vec<u8>> {
|
impl TensorKey<Vec<u8>> {
|
||||||
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
M: GGLWETensorKeyEncryptSk<BE>
|
M: TensorKeyEncryptSk<BE>,
|
||||||
{
|
{
|
||||||
module.gglwe_tensor_key_encrypt_sk_tmp_bytes(infos)
|
module.tensor_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 + 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<M, DataSk: DataRef, BE: Backend>(
|
pub fn encrypt_sk<M, S, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &M,
|
module: &M,
|
||||||
sk: &GLWESecret<DataSk>,
|
sk: &S,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
M: GGLWETensorKeyEncryptSk<BE>,
|
M: TensorKeyEncryptSk<BE>,
|
||||||
GLWESecret<DataSk>: GetDist,
|
S: GLWESecretToRef + GetDist,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
module.gglwe_tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch);
|
module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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>
|
pub trait TensorKeyEncryptSk<BE: Backend> {
|
||||||
where
|
fn tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
Self: Sized
|
|
||||||
+ ModuleN
|
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ SvpPPolAlloc<BE>
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxBigBytesOf,
|
|
||||||
{
|
|
||||||
fn gglwe_tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
fn gglwe_tensor_key_encrypt_sk<R, S>(
|
fn tensor_key_encrypt_sk<R, S>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
sk: &S,
|
sk: &S,
|
||||||
@@ -162,41 +60,33 @@ where
|
|||||||
S: GLWESecretToRef + GetDist;
|
S: GLWESecretToRef + GetDist;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GGLWETensorKeyEncryptSk<BE> for Module<BE> where
|
impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE>
|
||||||
|
where
|
||||||
Module<BE>: ModuleN
|
Module<BE>: ModuleN
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ SvpPPolAlloc<BE>
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxBigBytesOf
|
|
||||||
+ VecZnxAddScalarInplaceImpl<BE>
|
|
||||||
+ VecZnxDftApply<BE>
|
|
||||||
+ VecZnxDftApplyImpl<BE>
|
|
||||||
+ SvpApplyDftToDftImpl<BE>
|
|
||||||
+ GLWESwitchingKeyEncryptSk<BE>
|
+ GLWESwitchingKeyEncryptSk<BE>
|
||||||
|
+ VecZnxDftBytesOf
|
||||||
|
+ VecZnxBigBytesOf
|
||||||
|
+ GLWESecretPreparedAlloc<BE>
|
||||||
|
+ GLWESecretPrepare<BE>
|
||||||
|
+ VecZnxDftApply<BE>
|
||||||
+ SvpApplyDftToDft<BE>
|
+ SvpApplyDftToDft<BE>
|
||||||
+ VecZnxIdftApplyTmpAImpl<BE>
|
|
||||||
+ VecZnxBigNormalizeImpl<BE>
|
|
||||||
+ VecZnxIdftApplyTmpA<BE>
|
+ VecZnxIdftApplyTmpA<BE>
|
||||||
+ VecZnxBigNormalize<BE>
|
+ VecZnxBigNormalize<BE>,
|
||||||
+ VecZnxAddScalarInplaceImpl<BE>
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
+ SvpPrepare<BE>,
|
|
||||||
Scratch<BE>: ScratchTakeBasic + ScratchTakeCore<BE>,
|
|
||||||
{
|
{
|
||||||
fn gglwe_tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos,
|
A: GGLWEInfos,
|
||||||
{
|
{
|
||||||
GLWESecretPrepared::bytes_of(self, infos.rank_out())
|
GLWESecretPrepared::bytes_of(self, infos.rank_out())
|
||||||
+ self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1)
|
+ self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1)
|
||||||
+ self.bytes_of_vec_znx_big(1, 1)
|
+ self.bytes_of_vec_znx_big(1, 1)
|
||||||
+ self.bytes_of_vec_znx_dft(1, 1)
|
+ self.bytes_of_vec_znx_dft(1, 1)
|
||||||
+ GLWESecret::bytes_of(self, Rank(1))
|
+ GLWESecret::bytes_of(self, Rank(1))
|
||||||
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
|
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gglwe_tensor_key_encrypt_sk<R, S>(
|
fn tensor_key_encrypt_sk<R, S>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
sk: &S,
|
sk: &S,
|
||||||
@@ -217,11 +107,8 @@ impl<BE: Backend> GGLWETensorKeyEncryptSk<BE> for Module<BE> where
|
|||||||
|
|
||||||
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
|
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert_eq!(res.rank_out(), sk.rank());
|
||||||
{
|
assert_eq!(res.n(), sk.n());
|
||||||
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);
|
let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, rank.into(), 1);
|
||||||
|
|
||||||
@@ -251,6 +138,6 @@ impl<BE: Backend> GGLWETensorKeyEncryptSk<BE> for Module<BE> where
|
|||||||
res.at_mut(i, j)
|
res.at_mut(i, j)
|
||||||
.encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5);
|
.encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes},
|
api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxZero},
|
layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxInfos, ZnxZero},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
SIGMA, ScratchTakeCore,
|
SIGMA, ScratchTakeCore,
|
||||||
encryption::glwe_ct::GLWEEncryptSkInternal,
|
encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
|
||||||
layouts::{
|
layouts::{
|
||||||
GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos,
|
GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos,
|
||||||
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
|
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -21,31 +21,23 @@ impl GGSW<Vec<u8>> {
|
|||||||
{
|
{
|
||||||
module.ggsw_encrypt_sk_tmp_bytes(infos)
|
module.ggsw_encrypt_sk_tmp_bytes(infos)
|
||||||
}
|
}
|
||||||
// pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
|
||||||
// where
|
|
||||||
// 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> {
|
impl<D: DataMut> GGSW<D> {
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
|
pub fn encrypt_sk<P, S, M, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<B>,
|
module: &M,
|
||||||
pt: &ScalarZnx<DataPt>,
|
pt: &P,
|
||||||
sk: &GLWESecretPrepared<DataSk, B>,
|
sk: &S,
|
||||||
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>: GGSWEncryptSk<B>,
|
P: ScalarZnxToRef,
|
||||||
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
|
M: GGSWEncryptSk<BE>,
|
||||||
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
|
module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
|
||||||
}
|
}
|
||||||
@@ -55,7 +47,7 @@ pub trait GGSWEncryptSk<B: Backend> {
|
|||||||
fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGSWInfos;
|
A: GGSWInfos;
|
||||||
|
|
||||||
fn ggsw_encrypt_sk<R, P, S>(
|
fn ggsw_encrypt_sk<R, P, S>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
@@ -74,19 +66,18 @@ impl<B: Backend> GGSWEncryptSk<B> for Module<B>
|
|||||||
where
|
where
|
||||||
Module<B>: ModuleN
|
Module<B>: ModuleN
|
||||||
+ GLWEEncryptSkInternal<B>
|
+ GLWEEncryptSkInternal<B>
|
||||||
+ VecZnxAddScalarInplace
|
+ GLWEEncryptSk<B>
|
||||||
+ VecZnxNormalizeInplace<B>
|
|
||||||
+ VecZnxDftBytesOf
|
+ VecZnxDftBytesOf
|
||||||
+ VecZnxNormalizeTmpBytes,
|
+ VecZnxNormalizeInplace<B>
|
||||||
|
+ VecZnxAddScalarInplace,
|
||||||
Scratch<B>: ScratchTakeCore<B>,
|
Scratch<B>: ScratchTakeCore<B>,
|
||||||
{
|
{
|
||||||
|
|
||||||
fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGSWInfos,
|
A: GGSWInfos,
|
||||||
{
|
{
|
||||||
let size = infos.size();
|
let size = infos.size();
|
||||||
GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout())
|
self.glwe_encrypt_sk_tmp_bytes(infos)
|
||||||
+ VecZnx::bytes_of(self.n(), (infos.rank() + 1).into(), size)
|
+ VecZnx::bytes_of(self.n(), (infos.rank() + 1).into(), size)
|
||||||
+ VecZnx::bytes_of(self.n(), 1, size)
|
+ VecZnx::bytes_of(self.n(), 1, size)
|
||||||
+ self.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size)
|
+ self.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size)
|
||||||
@@ -109,15 +100,10 @@ where
|
|||||||
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
|
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
|
||||||
let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref();
|
let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert_eq!(res.rank(), sk.rank());
|
||||||
{
|
assert_eq!(res.n(), self.n() as u32);
|
||||||
use poulpy_hal::layouts::ZnxInfos;
|
assert_eq!(pt.n(), self.n());
|
||||||
|
assert_eq!(sk.n(), self.n() as u32);
|
||||||
assert_eq!(res.rank(), sk.rank());
|
|
||||||
assert_eq!(res.n(), self.n() as u32);
|
|
||||||
assert_eq!(pt.n(), self.n());
|
|
||||||
assert_eq!(sk.n(), self.n() as u32);
|
|
||||||
}
|
|
||||||
|
|
||||||
let k: usize = res.k().into();
|
let k: usize = res.k().into();
|
||||||
let base2k: usize = res.base2k().into();
|
let base2k: usize = res.base2k().into();
|
||||||
@@ -149,4 +135,4 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{
|
||||||
ModuleN, ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace,
|
ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare,
|
||||||
VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply,
|
VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize,
|
||||||
VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace,
|
VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace,
|
||||||
VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, ScratchTakeBasic,
|
VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
|
||||||
},
|
},
|
||||||
layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero},
|
layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
ScratchTakeCore,
|
||||||
dist::Distribution,
|
dist::Distribution,
|
||||||
encryption::{SIGMA, SIGMA_BOUND},
|
encryption::{SIGMA, SIGMA_BOUND},
|
||||||
layouts::{
|
layouts::{
|
||||||
@@ -19,7 +20,6 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
impl GLWE<Vec<u8>> {
|
impl GLWE<Vec<u8>> {
|
||||||
|
|
||||||
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GLWEInfos,
|
A: GLWEInfos,
|
||||||
@@ -31,16 +31,16 @@ impl GLWE<Vec<u8>> {
|
|||||||
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: GLWEInfos,
|
A: GLWEInfos,
|
||||||
M: GLWEEncryptPk<BE>
|
M: GLWEEncryptPk<BE>,
|
||||||
{
|
{
|
||||||
module.glwe_encrypt_pk_tmp_bytes(infos)
|
module.glwe_encrypt_pk_tmp_bytes(infos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: DataMut> GLWE<D> {
|
impl<D: DataMut> GLWE<D> {
|
||||||
pub fn encrypt_sk<R, P, S, BE: Backend>(
|
pub fn encrypt_sk<R, P, S, M, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<BE>,
|
module: &M,
|
||||||
pt: &P,
|
pt: &P,
|
||||||
sk: &S,
|
sk: &S,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
@@ -49,28 +49,30 @@ impl<D: DataMut> GLWE<D> {
|
|||||||
) where
|
) where
|
||||||
P: GLWEPlaintextToRef,
|
P: GLWEPlaintextToRef,
|
||||||
S: GLWESecretPreparedToRef<BE>,
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
Module<BE>: GLWEEncryptSk<BE>,
|
M: GLWEEncryptSk<BE>,
|
||||||
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
module.glwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
|
module.glwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_zero_sk<S, BE: Backend>(
|
pub fn encrypt_zero_sk<S, M, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<BE>,
|
module: &M,
|
||||||
sk: &S,
|
sk: &S,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
S: GLWESecretPreparedToRef<BE>,
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
Module<BE>: GLWEEncryptSk<BE>,
|
M: GLWEEncryptSk<BE>,
|
||||||
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch);
|
module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_pk<P, K, BE: Backend>(
|
pub fn encrypt_pk<P, K, M, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<BE>,
|
module: &M,
|
||||||
pt: &P,
|
pt: &P,
|
||||||
pk: &K,
|
pk: &K,
|
||||||
source_xu: &mut Source,
|
source_xu: &mut Source,
|
||||||
@@ -79,31 +81,28 @@ impl<D: DataMut> GLWE<D> {
|
|||||||
) where
|
) where
|
||||||
P: GLWEPlaintextToRef,
|
P: GLWEPlaintextToRef,
|
||||||
K: GLWEPublicKeyPreparedToRef<BE>,
|
K: GLWEPublicKeyPreparedToRef<BE>,
|
||||||
Module<BE>: GLWEEncryptPk<BE>,
|
M: GLWEEncryptPk<BE>,
|
||||||
{
|
{
|
||||||
module.glwe_encrypt_pk(self, pt, pk, source_xu, source_xe, scratch);
|
module.glwe_encrypt_pk(self, pt, pk, source_xu, source_xe, scratch);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt_zero_pk<K, BE: Backend>(
|
pub fn encrypt_zero_pk<K, M, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<BE>,
|
module: &M,
|
||||||
pk: &K,
|
pk: &K,
|
||||||
source_xu: &mut Source,
|
source_xu: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
K: GLWEPublicKeyPreparedToRef<BE>,
|
K: GLWEPublicKeyPreparedToRef<BE>,
|
||||||
Module<BE>: GLWEEncryptPk<BE>,
|
M: GLWEEncryptPk<BE>,
|
||||||
{
|
{
|
||||||
module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch);
|
module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait GLWEEncryptSk<BE: Backend> {
|
pub trait GLWEEncryptSk<BE: Backend> {
|
||||||
fn glwe_encrypt_sk_tmp_bytes<A>(
|
fn glwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
&self,
|
|
||||||
infos: &A,
|
|
||||||
) -> usize
|
|
||||||
where
|
where
|
||||||
A: GLWEInfos;
|
A: GLWEInfos;
|
||||||
|
|
||||||
@@ -129,22 +128,15 @@ pub trait GLWEEncryptSk<BE: Backend> {
|
|||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
R: GLWEToMut,
|
R: GLWEToMut,
|
||||||
S: GLWESecretPreparedToRef<BE>;
|
S: GLWESecretPreparedToRef<BE>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GLWEEncryptSk<BE> for Module<BE>
|
impl<BE: Backend> GLWEEncryptSk<BE> for Module<BE>
|
||||||
where
|
where
|
||||||
Module<BE>: Sized
|
Module<BE>: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal<BE>,
|
||||||
+ ModuleN
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ GLWEEncryptSkInternal<BE>,
|
|
||||||
Scratch<BE>: ScratchAvailable,
|
Scratch<BE>: ScratchAvailable,
|
||||||
{
|
{
|
||||||
fn glwe_encrypt_sk_tmp_bytes<A>(
|
fn glwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
&self,
|
|
||||||
infos: &A,
|
|
||||||
) -> usize
|
|
||||||
where
|
where
|
||||||
A: GLWEInfos,
|
A: GLWEInfos,
|
||||||
{
|
{
|
||||||
@@ -166,23 +158,20 @@ where
|
|||||||
P: GLWEPlaintextToRef,
|
P: GLWEPlaintextToRef,
|
||||||
S: GLWESecretPreparedToRef<BE>,
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
{
|
{
|
||||||
let mut res: GLWE<&mut [u8]> = res.to_mut();
|
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
||||||
let pt: GLWEPlaintext<&[u8]> = pt.to_ref();
|
let pt: &GLWEPlaintext<&[u8]> = &pt.to_ref();
|
||||||
|
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert_eq!(res.rank(), sk.rank());
|
||||||
{
|
assert_eq!(res.n(), self.n() as u32);
|
||||||
let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref();
|
assert_eq!(sk.n(), self.n() as u32);
|
||||||
assert_eq!(res.rank(), sk.rank());
|
assert_eq!(pt.n(), self.n() as u32);
|
||||||
assert_eq!(res.n(), self.n() as u32);
|
assert!(
|
||||||
assert_eq!(sk.n(), self.n() as u32);
|
scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(res),
|
||||||
assert_eq!(pt.n(), self.n() as u32);
|
"scratch.available(): {} < GLWE::encrypt_sk_tmp_bytes: {}",
|
||||||
assert!(
|
scratch.available(),
|
||||||
scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res),
|
self.glwe_encrypt_sk_tmp_bytes(res)
|
||||||
"scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}",
|
);
|
||||||
scratch.available(),
|
|
||||||
GLWE::encrypt_sk_tmp_bytes(self, &res)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let cols: usize = (res.rank() + 1).into();
|
let cols: usize = (res.rank() + 1).into();
|
||||||
self.glwe_encrypt_sk_internal(
|
self.glwe_encrypt_sk_internal(
|
||||||
@@ -191,7 +180,7 @@ where
|
|||||||
res.data_mut(),
|
res.data_mut(),
|
||||||
cols,
|
cols,
|
||||||
false,
|
false,
|
||||||
Some((&pt, 0)),
|
Some((pt, 0)),
|
||||||
sk,
|
sk,
|
||||||
source_xa,
|
source_xa,
|
||||||
source_xe,
|
source_xe,
|
||||||
@@ -211,21 +200,18 @@ where
|
|||||||
R: GLWEToMut,
|
R: GLWEToMut,
|
||||||
S: GLWESecretPreparedToRef<BE>,
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
{
|
{
|
||||||
let mut res: GLWE<&mut [u8]> = res.to_mut();
|
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
||||||
|
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert_eq!(res.rank(), sk.rank());
|
||||||
{
|
assert_eq!(res.n(), self.n() as u32);
|
||||||
let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref();
|
assert_eq!(sk.n(), self.n() as u32);
|
||||||
assert_eq!(res.rank(), sk.rank());
|
assert!(
|
||||||
assert_eq!(res.n(), self.n() as u32);
|
scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(res),
|
||||||
assert_eq!(sk.n(), self.n() as u32);
|
"scratch.available(): {} < GLWE::encrypt_sk_tmp_bytes: {}",
|
||||||
assert!(
|
scratch.available(),
|
||||||
scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(&res),
|
self.glwe_encrypt_sk_tmp_bytes(res)
|
||||||
"scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}",
|
);
|
||||||
scratch.available(),
|
|
||||||
self.glwe_encrypt_sk_tmp_bytes(&res)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let cols: usize = (res.rank() + 1).into();
|
let cols: usize = (res.rank() + 1).into();
|
||||||
self.glwe_encrypt_sk_internal(
|
self.glwe_encrypt_sk_internal(
|
||||||
@@ -241,14 +227,11 @@ where
|
|||||||
SIGMA,
|
SIGMA,
|
||||||
scratch,
|
scratch,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait GLWEEncryptPk<BE: Backend> {
|
pub trait GLWEEncryptPk<BE: Backend> {
|
||||||
fn glwe_encrypt_pk_tmp_bytes<A>(
|
fn glwe_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
&self,
|
|
||||||
infos: &A,
|
|
||||||
) -> usize
|
|
||||||
where
|
where
|
||||||
A: GLWEInfos;
|
A: GLWEInfos;
|
||||||
|
|
||||||
@@ -274,29 +257,20 @@ pub trait GLWEEncryptPk<BE: Backend> {
|
|||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
R: GLWEToMut,
|
R: GLWEToMut,
|
||||||
K: GLWEPublicKeyPreparedToRef<BE>;
|
K: GLWEPublicKeyPreparedToRef<BE>;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GLWEEncryptPk<BE> for Module<BE>
|
impl<BE: Backend> GLWEEncryptPk<BE> for Module<BE>
|
||||||
where
|
where
|
||||||
Module<BE>: GLWEEncryptPkInternal<BE>
|
Module<BE>: GLWEEncryptPkInternal<BE> + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes,
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ VecZnxBigBytesOf
|
|
||||||
+ VecZnxNormalizeTmpBytes,
|
|
||||||
{
|
{
|
||||||
fn glwe_encrypt_pk_tmp_bytes<A>(
|
fn glwe_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
&self,
|
|
||||||
infos: &A,
|
|
||||||
) -> usize
|
|
||||||
where
|
where
|
||||||
A: GLWEInfos,
|
A: GLWEInfos,
|
||||||
{
|
{
|
||||||
let size: usize = infos.size();
|
let size: usize = infos.size();
|
||||||
assert_eq!(self.n() as u32, infos.n());
|
assert_eq!(self.n() as u32, infos.n());
|
||||||
((self.bytes_of_vec_znx_dft(1, size) + self.bytes_of_vec_znx_big(1, size)) | ScalarZnx::bytes_of(self.n(), 1))
|
((self.bytes_of_vec_znx_dft(1, size) + self.bytes_of_vec_znx_big(1, size)).max(ScalarZnx::bytes_of(self.n(), 1)))
|
||||||
+ self.bytes_of_svp_ppol(1)
|
+ self.bytes_of_svp_ppol(1)
|
||||||
+ self.vec_znx_normalize_tmp_bytes()
|
+ self.vec_znx_normalize_tmp_bytes()
|
||||||
}
|
}
|
||||||
@@ -383,15 +357,12 @@ where
|
|||||||
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
||||||
let pk: &GLWEPublicKeyPrepared<&[u8], BE> = &pk.to_ref();
|
let pk: &GLWEPublicKeyPrepared<&[u8], BE> = &pk.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert_eq!(res.base2k(), pk.base2k());
|
||||||
{
|
assert_eq!(res.n(), pk.n());
|
||||||
assert_eq!(res.base2k(), pk.base2k());
|
assert_eq!(res.rank(), pk.rank());
|
||||||
assert_eq!(res.n(), pk.n());
|
if let Some((pt, _)) = pt {
|
||||||
assert_eq!(res.rank(), pk.rank());
|
assert_eq!(pt.to_ref().base2k(), pk.base2k());
|
||||||
if let Some((pt, _)) = pt {
|
assert_eq!(pt.to_ref().n(), pk.n());
|
||||||
assert_eq!(pt.to_ref().base2k(), pk.base2k());
|
|
||||||
assert_eq!(pt.to_ref().n(), pk.n());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let base2k: usize = pk.base2k().into();
|
let base2k: usize = pk.base2k().into();
|
||||||
@@ -517,7 +488,7 @@ where
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
ct.cols(),
|
ct.cols(),
|
||||||
1,
|
1,
|
||||||
"invalid ciphertext: compressed tag=true but #cols={} != 1",
|
"invalid glwe: compressed tag=true but #cols={} != 1",
|
||||||
ct.cols()
|
ct.cols()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes},
|
api::{ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||||
layouts::{Backend, DataMut, DataRef, Module, ScratchOwned},
|
layouts::{Backend, DataMut, DataRef, Module, Scratch, ScratchOwned},
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
encryption::glwe_ct::{GLWEEncryptSk},
|
Distribution, ScratchTakeCore,
|
||||||
|
encryption::glwe_ct::GLWEEncryptSk,
|
||||||
layouts::{
|
layouts::{
|
||||||
GLWE, GLWEPublicKey, GLWEPublicKeyToMut,
|
GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos,
|
||||||
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
|
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -33,37 +34,33 @@ pub trait GLWEPublicKeyGenerate<B: Backend> {
|
|||||||
S: GLWESecretPreparedToRef<B>;
|
S: GLWESecretPreparedToRef<B>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<B: Backend> GLWEPublicKeyGenerate<B> for Module<B>
|
impl<BE: Backend> GLWEPublicKeyGenerate<BE> for Module<BE>
|
||||||
where
|
where
|
||||||
Module<B>: GLWEEncryptSk<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf,
|
Module<BE>: GLWEEncryptSk<BE>,
|
||||||
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
|
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||||
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
||||||
where
|
where
|
||||||
R: GLWEPublicKeyToMut,
|
R: GLWEPublicKeyToMut,
|
||||||
S: GLWESecretPreparedToRef<B>,
|
S: GLWESecretPreparedToRef<BE>,
|
||||||
{
|
{
|
||||||
let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut();
|
let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut();
|
||||||
let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref();
|
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert_eq!(res.n(), self.n() as u32);
|
||||||
{
|
assert_eq!(sk.n(), self.n() as u32);
|
||||||
use crate::{Distribution, layouts::LWEInfos};
|
|
||||||
|
|
||||||
assert_eq!(res.n(), self.n() as u32);
|
if sk.dist == Distribution::NONE {
|
||||||
assert_eq!(sk.n(), self.n() as u32);
|
panic!("invalid sk: SecretDistribution::NONE")
|
||||||
|
|
||||||
if sk.dist == Distribution::NONE {
|
|
||||||
panic!("invalid sk: SecretDistribution::NONE")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Its ok to allocate scratch space here since pk is usually generated only once.
|
// Its ok to allocate scratch space here since pk is usually generated only once.
|
||||||
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res));
|
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res));
|
||||||
|
|
||||||
let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(self, res);
|
let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(self, res);
|
||||||
|
|
||||||
tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow());
|
tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow());
|
||||||
res.dist = sk.dist;
|
res.dist = sk.dist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,17 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{ModuleN, VecZnxAutomorphismInplace},
|
||||||
ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
|
layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero},
|
||||||
VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
|
source::Source,
|
||||||
VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
|
||||||
VecZnxSubInplace, VecZnxSwitchRing,
|
|
||||||
ScratchTakeBasic
|
|
||||||
},
|
|
||||||
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero},
|
|
||||||
oep::{ScratchAvailableImpl, VecZnxFillUniformImpl},
|
|
||||||
source::Source
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ScratchTakeCore,
|
ScratchTakeCore,
|
||||||
layouts::{
|
|
||||||
GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, GLWEToLWESwitchingKeyToMut,
|
|
||||||
prepared::GLWESecretPrepared,
|
|
||||||
},
|
|
||||||
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
|
layouts::{
|
||||||
|
GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos,
|
||||||
|
LWESecret, LWESecretToRef, Rank,
|
||||||
|
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl GLWEToLWESwitchingKey<Vec<u8>> {
|
impl GLWEToLWESwitchingKey<Vec<u8>> {
|
||||||
@@ -28,187 +22,77 @@ impl GLWEToLWESwitchingKey<Vec<u8>> {
|
|||||||
{
|
{
|
||||||
module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
module.glwe_to_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,
|
|
||||||
// {
|
|
||||||
// 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> {
|
||||||
pub fn encrypt_sk<M, DLwe, DGlwe, BE: Backend>(
|
pub fn encrypt_sk<M, S1, S2, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &M,
|
module: &M,
|
||||||
sk_lwe: &LWESecret<DLwe>,
|
sk_lwe: &S1,
|
||||||
sk_glwe: &GLWESecret<DGlwe>,
|
sk_glwe: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
M: GLWEToLWESwitchingKeyEncrypt<BE>,
|
M: GLWEToLWESwitchingKeyEncrypt<BE>,
|
||||||
DLwe: DataRef,
|
S1: LWESecretToRef,
|
||||||
DGlwe: DataRef,
|
S2: GLWESecretToRef,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
{
|
{
|
||||||
module.glwe_to_lwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch);
|
module.glwe_to_lwe_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)]
|
|
||||||
// {
|
|
||||||
// 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>
|
pub trait GLWEToLWESwitchingKeyEncrypt<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
|
|
||||||
+ ScratchAvailableImpl<BE>
|
|
||||||
+ VecZnxFillUniform
|
|
||||||
+ VecZnxFillUniformImpl<BE>
|
|
||||||
{
|
|
||||||
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
fn glwe_to_lwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
|
fn glwe_to_lwe_switching_key_encrypt_sk<R, S1, S2>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
sk_lwe: &LWESecret<DLwe>,
|
sk_lwe: &S1,
|
||||||
sk_glwe: &GLWESecret<DataGlwe>,
|
sk_glwe: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
R: GLWEToLWESwitchingKeyToMut,
|
S1: LWESecretToRef,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>;
|
S2: GLWESecretToRef,
|
||||||
|
R: GLWEToLWESwitchingKeyToMut;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE> where
|
impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE>
|
||||||
Module<BE>: ModuleN
|
where
|
||||||
+ SvpPPolBytesOf
|
Module<BE>: ModuleN + GLWESwitchingKeyEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + VecZnxAutomorphismInplace<BE>,
|
||||||
+ SvpPPolAlloc<BE>
|
Scratch<BE>: ScratchTakeCore<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>
|
|
||||||
+ VecZnxFillUniform
|
|
||||||
+ VecZnxFillUniformImpl<BE>
|
|
||||||
+ GLWESwitchingKeyEncryptSk<BE>
|
|
||||||
{
|
{
|
||||||
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos
|
A: GGLWEInfos,
|
||||||
{
|
{
|
||||||
GLWESecretPrepared::bytes_of(self, infos.rank_in())
|
GLWESecretPrepared::bytes_of(self, infos.rank_in())
|
||||||
+ (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in()))
|
+ (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn glwe_to_lwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
|
fn glwe_to_lwe_switching_key_encrypt_sk<R, S1, S2>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
sk_lwe: &LWESecret<DLwe>,
|
sk_lwe: &S1,
|
||||||
sk_glwe: &GLWESecret<DataGlwe>,
|
sk_glwe: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
|
S1: LWESecretToRef,
|
||||||
|
S2: GLWESecretToRef,
|
||||||
R: GLWEToLWESwitchingKeyToMut,
|
R: GLWEToLWESwitchingKeyToMut,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
|
||||||
{
|
{
|
||||||
|
|
||||||
let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
|
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();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
assert!(sk_lwe.n().0 <= self.n() as u32);
|
||||||
{
|
|
||||||
assert!(sk_lwe.n().0 <= self.n() as u32);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1));
|
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();
|
||||||
@@ -222,6 +106,6 @@ impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE> where
|
|||||||
source_xa,
|
source_xa,
|
||||||
source_xe,
|
source_xe,
|
||||||
scratch_1,
|
scratch_1,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,10 @@ use poulpy_hal::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
encryption::{SIGMA, SIGMA_BOUND},
|
encryption::{SIGMA, SIGMA_BOUND},
|
||||||
layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToRef, LWESecretToRef},
|
layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToRef, LWESecret, LWESecretToRef, LWEToMut},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<DataSelf: DataMut> LWE<DataSelf> {
|
impl<DataSelf: DataMut> LWE<DataSelf> {
|
||||||
|
|
||||||
pub fn encrypt_sk<P, S, M, BE: Backend>(&mut self, module: &M, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
pub fn encrypt_sk<P, S, M, BE: Backend>(&mut self, module: &M, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
||||||
where
|
where
|
||||||
P: LWEPlaintextToRef,
|
P: LWEPlaintextToRef,
|
||||||
@@ -23,27 +22,24 @@ impl<DataSelf: DataMut> LWE<DataSelf> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait LWEEncryptSk<BE: Backend>
|
pub trait LWEEncryptSk<BE: Backend> {
|
||||||
where
|
|
||||||
Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>,
|
|
||||||
{
|
|
||||||
fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
||||||
where
|
where
|
||||||
R: LWEToMut,
|
R: LWEToMut,
|
||||||
P: LWEPlaintextToRef,
|
P: LWEPlaintextToRef,
|
||||||
S: LWESecretToRef,
|
S: LWESecretToRef;
|
||||||
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> LWEEncryptSk<BE> for Module<BE> where
|
impl<BE: Backend> LWEEncryptSk<BE> for Module<BE>
|
||||||
|
where
|
||||||
Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>,
|
Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>,
|
||||||
|
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
|
||||||
{
|
{
|
||||||
fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
||||||
where
|
where
|
||||||
R: LWEToMut,
|
R: LWEToMut,
|
||||||
P: LWEPlaintextToRef,
|
P: LWEPlaintextToRef,
|
||||||
S: LWESecretToRef,
|
S: LWESecretToRef,
|
||||||
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
|
|
||||||
{
|
{
|
||||||
let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
|
let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
|
||||||
let pt: &LWEPlaintext<&[u8]> = &pt.to_ref();
|
let pt: &LWEPlaintext<&[u8]> = &pt.to_ref();
|
||||||
@@ -101,6 +97,6 @@ impl<BE: Backend> LWEEncryptSk<BE> for Module<BE> where
|
|||||||
|
|
||||||
(0..res.size()).for_each(|i| {
|
(0..res.size()).for_each(|i| {
|
||||||
res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0];
|
res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{ModuleN, VecZnxAutomorphismInplace},
|
||||||
ModuleN, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace,
|
layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut},
|
||||||
VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
|
|
||||||
VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
|
|
||||||
VecZnxSwitchRing,
|
|
||||||
},
|
|
||||||
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut},
|
|
||||||
source::Source,
|
source::Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
ScratchTakeCore,
|
||||||
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
layouts::{
|
layouts::{
|
||||||
GLWESecretAlloc,
|
GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, LWESwitchingKeyToMut,
|
||||||
GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank,
|
Rank,
|
||||||
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
|
prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc},
|
||||||
},
|
},
|
||||||
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
|
||||||
ScratchTakeCore,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
impl LWESwitchingKey<Vec<u8>> {
|
impl LWESwitchingKey<Vec<u8>> {
|
||||||
@@ -27,145 +22,65 @@ impl LWESwitchingKey<Vec<u8>> {
|
|||||||
{
|
{
|
||||||
module.lwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
module.lwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: DataMut> LWESwitchingKey<D> {
|
impl<D: DataMut> LWESwitchingKey<D> {
|
||||||
#[allow(clippy::too_many_arguments)]
|
pub fn encrypt_sk<S1, S2, M, BE: Backend>(
|
||||||
pub fn encrypt_sk<DIn, DOut, B: Backend>(
|
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &Module<B>,
|
module: &M,
|
||||||
sk_lwe_in: &LWESecret<DIn>,
|
sk_lwe_in: &S1,
|
||||||
sk_lwe_out: &LWESecret<DOut>,
|
sk_lwe_out: &S2,
|
||||||
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
|
||||||
DIn: DataRef,
|
S1: LWESecretToRef,
|
||||||
DOut: DataRef,
|
S2: LWESecretToRef,
|
||||||
Module<B>: ModuleN
|
M: LWESwitchingKeyEncrypt<BE>,
|
||||||
+ 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>: ScratchTakeCore<B>,
|
|
||||||
{
|
{
|
||||||
#[cfg(debug_assertions)]
|
module.lwe_switching_key_encrypt_sk(self, sk_lwe_in, sk_lwe_out, source_xa, source_xe, scratch);
|
||||||
{
|
|
||||||
assert!(sk_lwe_in.n().0 <= self.n().0);
|
|
||||||
assert!(sk_lwe_out.n().0 <= self.n().0);
|
|
||||||
assert!(self.n().0 <= module.n() as u32);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1));
|
|
||||||
let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(module, Rank(1));
|
|
||||||
|
|
||||||
sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0));
|
|
||||||
sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0);
|
|
||||||
module.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2);
|
|
||||||
|
|
||||||
sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0));
|
|
||||||
sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0);
|
|
||||||
module.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2);
|
|
||||||
|
|
||||||
self.0.encrypt_sk(
|
|
||||||
module,
|
|
||||||
&sk_in_glwe,
|
|
||||||
&sk_out_glwe,
|
|
||||||
source_xa,
|
|
||||||
source_xe,
|
|
||||||
scratch_2,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait LWESwitchingKeyEncrypt<BE: Backend>
|
pub trait LWESwitchingKeyEncrypt<BE: Backend> {
|
||||||
where
|
|
||||||
Self: Sized
|
|
||||||
+ ModuleN
|
|
||||||
+ SvpPPolBytesOf
|
|
||||||
+ SvpPPolAlloc<BE>
|
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxAutomorphismInplace<BE>
|
|
||||||
+ VecZnxAddScalarInplace
|
|
||||||
+ VecZnxBigNormalize<BE>
|
|
||||||
+ VecZnxDftApply<BE>
|
|
||||||
+ SvpApplyDftToDftInplace<BE>
|
|
||||||
+ VecZnxIdftApplyConsume<BE>
|
|
||||||
+ VecZnxFillUniform
|
|
||||||
+ VecZnxSubInplace
|
|
||||||
+ VecZnxAddInplace
|
|
||||||
+ VecZnxNormalizeInplace<BE>
|
|
||||||
+ VecZnxAddNormal
|
|
||||||
+ VecZnxNormalize<BE>
|
|
||||||
+ VecZnxSub
|
|
||||||
+ SvpPrepare<BE>
|
|
||||||
+ VecZnxSwitchRing
|
|
||||||
+ GLWESecretAlloc
|
|
||||||
+ GLWESecretPreparedAlloc<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
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
|
fn lwe_switching_key_encrypt_sk<R, S1, S2>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk_lwe_in: &S1,
|
||||||
|
sk_lwe_out: &S2,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
R: LWESwitchingKeyToMut,
|
||||||
|
S1: LWESecretToRef,
|
||||||
|
S2: LWESecretToRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE:Backend> LWESwitchingKeyEncrypt<BE> for Module<BE> where
|
impl<BE: Backend> LWESwitchingKeyEncrypt<BE> for Module<BE>
|
||||||
Self: ModuleN
|
where
|
||||||
+ SvpPPolBytesOf
|
Self: ModuleN + GLWESwitchingKeyEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + VecZnxAutomorphismInplace<BE>,
|
||||||
+ SvpPPolAlloc<BE>
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
+ VecZnxNormalizeTmpBytes
|
|
||||||
+ VecZnxDftBytesOf
|
|
||||||
+ VecZnxAutomorphismInplace<BE>
|
|
||||||
+ VecZnxAddScalarInplace
|
|
||||||
+ VecZnxBigNormalize<BE>
|
|
||||||
+ VecZnxDftApply<BE>
|
|
||||||
+ SvpApplyDftToDftInplace<BE>
|
|
||||||
+ VecZnxIdftApplyConsume<BE>
|
|
||||||
+ VecZnxFillUniform
|
|
||||||
+ VecZnxSubInplace
|
|
||||||
+ VecZnxAddInplace
|
|
||||||
+ VecZnxNormalizeInplace<BE>
|
|
||||||
+ VecZnxAddNormal
|
|
||||||
+ VecZnxNormalize<BE>
|
|
||||||
+ VecZnxSub
|
|
||||||
+ SvpPrepare<BE>
|
|
||||||
+ VecZnxSwitchRing
|
|
||||||
+ GLWESecretAlloc
|
|
||||||
+ 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
|
||||||
A: GGLWEInfos
|
A: GGLWEInfos,
|
||||||
{
|
{
|
||||||
debug_assert_eq!(
|
assert_eq!(
|
||||||
infos.dsize().0,
|
infos.dsize().0,
|
||||||
1,
|
1,
|
||||||
"dsize > 1 is not supported for LWESwitchingKey"
|
"dsize > 1 is not supported for LWESwitchingKey"
|
||||||
);
|
);
|
||||||
debug_assert_eq!(
|
assert_eq!(
|
||||||
infos.rank_in().0,
|
infos.rank_in().0,
|
||||||
1,
|
1,
|
||||||
"rank_in > 1 is not supported for LWESwitchingKey"
|
"rank_in > 1 is not supported for LWESwitchingKey"
|
||||||
);
|
);
|
||||||
debug_assert_eq!(
|
assert_eq!(
|
||||||
infos.rank_out().0,
|
infos.rank_out().0,
|
||||||
1,
|
1,
|
||||||
"rank_out > 1 is not supported for LWESwitchingKey"
|
"rank_out > 1 is not supported for LWESwitchingKey"
|
||||||
@@ -174,4 +89,47 @@ impl<BE:Backend> LWESwitchingKeyEncrypt<BE> for Module<BE> where
|
|||||||
+ GLWESecretPrepared::bytes_of(self, Rank(1))
|
+ GLWESecretPrepared::bytes_of(self, Rank(1))
|
||||||
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
|
+ GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
fn lwe_switching_key_encrypt_sk<R, S1, S2>(
|
||||||
|
&self,
|
||||||
|
res: &mut R,
|
||||||
|
sk_lwe_in: &S1,
|
||||||
|
sk_lwe_out: &S2,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch<BE>,
|
||||||
|
) where
|
||||||
|
R: LWESwitchingKeyToMut,
|
||||||
|
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();
|
||||||
|
|
||||||
|
assert!(sk_lwe_in.n().0 <= res.n().0);
|
||||||
|
assert!(sk_lwe_out.n().0 <= res.n().0);
|
||||||
|
assert!(res.n() <= self.n() as u32);
|
||||||
|
|
||||||
|
let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1));
|
||||||
|
let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1));
|
||||||
|
|
||||||
|
sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0));
|
||||||
|
sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0);
|
||||||
|
self.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2);
|
||||||
|
|
||||||
|
sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0));
|
||||||
|
sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0);
|
||||||
|
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,
|
||||||
|
&sk_in_glwe,
|
||||||
|
&sk_out_glwe,
|
||||||
|
source_xa,
|
||||||
|
source_xe,
|
||||||
|
scratch_2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
use poulpy_hal::{
|
use poulpy_hal::{
|
||||||
api::{
|
api::{ModuleN, VecZnxAutomorphismInplace},
|
||||||
ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
|
layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut},
|
||||||
VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
|
|
||||||
VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
|
||||||
VecZnxSubInplace, VecZnxSwitchRing,
|
|
||||||
ScratchTakeBasic,
|
|
||||||
},
|
|
||||||
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,
|
||||||
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
|
||||||
|
layouts::{
|
||||||
|
GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey,
|
||||||
|
LWEToGLWESwitchingKeyToMut, Rank,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl LWEToGLWESwitchingKey<Vec<u8>> {
|
impl LWEToGLWESwitchingKey<Vec<u8>> {
|
||||||
@@ -25,198 +21,81 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
|
|||||||
{
|
{
|
||||||
module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos)
|
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> {
|
impl<D: DataMut> LWEToGLWESwitchingKey<D> {
|
||||||
pub fn encrypt_sk<M, DLwe, DGlwe, BE: Backend>(
|
pub fn encrypt_sk<S1, S2, M, BE: Backend>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module: &M,
|
module: &M,
|
||||||
sk_lwe: &LWESecret<DLwe>,
|
sk_lwe: &S1,
|
||||||
sk_glwe: &GLWESecret<DGlwe>,
|
sk_glwe: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
|
S1: LWESecretToRef,
|
||||||
|
S2: GLWESecretToRef,
|
||||||
M: LWEToGLWESwitchingKeyEncrypt<BE>,
|
M: LWEToGLWESwitchingKeyEncrypt<BE>,
|
||||||
DLwe: DataRef,
|
Scratch<BE>: ScratchTakeCore<BE>,
|
||||||
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);
|
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>
|
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
|
fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos;
|
A: GGLWEInfos;
|
||||||
|
|
||||||
fn lwe_to_glwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
|
fn lwe_to_glwe_switching_key_encrypt_sk<R, S1, S2>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
sk_lwe: &LWESecret<DLwe>,
|
sk_lwe: &S1,
|
||||||
sk_glwe: &GLWESecret<DataGlwe>,
|
sk_glwe: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
R: LWEToGLWESwitchingKeyToMut,
|
S1: LWESecretToRef,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>;
|
S2: GLWESecretToRef,
|
||||||
|
R: LWEToGLWESwitchingKeyToMut;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE> where
|
impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE>
|
||||||
Module<BE>: ModuleN
|
where
|
||||||
+ SvpPPolBytesOf
|
Self: ModuleN + GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphismInplace<BE>,
|
||||||
+ SvpPPolAlloc<BE>
|
Scratch<BE>: ScratchTakeCore<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
|
fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GGLWEInfos
|
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(self, infos)
|
GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self, infos.rank_in())
|
||||||
+ GLWESecret::bytes_of(self, infos.rank_in())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lwe_to_glwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
|
fn lwe_to_glwe_switching_key_encrypt_sk<R, S1, S2>(
|
||||||
&self,
|
&self,
|
||||||
res: &mut R,
|
res: &mut R,
|
||||||
sk_lwe: &LWESecret<DLwe>,
|
sk_lwe: &S1,
|
||||||
sk_glwe: &GLWESecret<DataGlwe>,
|
sk_glwe: &S2,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
scratch: &mut Scratch<BE>,
|
scratch: &mut Scratch<BE>,
|
||||||
) where
|
) where
|
||||||
|
S1: LWESecretToRef,
|
||||||
|
S2: GLWESecretToRef,
|
||||||
R: LWEToGLWESwitchingKeyToMut,
|
R: LWEToGLWESwitchingKeyToMut,
|
||||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
|
||||||
{
|
{
|
||||||
let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
|
let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
|
||||||
|
let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref();
|
||||||
#[cfg(debug_assertions)]
|
let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref();
|
||||||
{
|
|
||||||
use crate::layouts::LWEInfos;
|
|
||||||
|
|
||||||
assert!(sk_lwe.n().0 <= self.n() as u32);
|
assert!(sk_lwe.n().0 <= self.n() as u32);
|
||||||
}
|
|
||||||
|
|
||||||
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, 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));
|
||||||
@@ -232,4 +111,4 @@ impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE> where
|
|||||||
scratch_1,
|
scratch_1,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ where
|
|||||||
fn bytes_of_glwe_secret(&self, rank: Rank) -> usize {
|
fn bytes_of_glwe_secret(&self, rank: Rank) -> usize {
|
||||||
self.bytes_of_svp_ppol(rank.into())
|
self.bytes_of_svp_ppol(rank.into())
|
||||||
}
|
}
|
||||||
fn bytes_of_glwe_secret_from_infos<A>(&self, infos: &A) -> usize
|
fn bytes_of_glwe_secret_prepared_from_infos<A>(&self, infos: &A) -> usize
|
||||||
where
|
where
|
||||||
A: GLWEInfos,
|
A: GLWEInfos,
|
||||||
{
|
{
|
||||||
@@ -98,7 +98,7 @@ impl<B: Backend> GLWESecretPrepared<Vec<u8>, B> {
|
|||||||
A: GLWEInfos,
|
A: GLWEInfos,
|
||||||
M: GLWESecretPreparedAlloc<B>,
|
M: GLWESecretPreparedAlloc<B>,
|
||||||
{
|
{
|
||||||
module.bytes_of_glwe_secret_from_infos(infos)
|
module.bytes_of_glwe_secret_prepared_from_infos(infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bytes_of<M>(module: &M, rank: Rank) -> usize
|
pub fn bytes_of<M>(module: &M, rank: Rank) -> usize
|
||||||
|
|||||||
Reference in New Issue
Block a user