wip all of Encryption

This commit is contained in:
Rasoul Akhavan Mahdavi
2025-10-15 21:31:48 -04:00
parent 2f2c7aef00
commit 9f35a92a33
16 changed files with 316 additions and 231 deletions

View File

@@ -1,16 +1,18 @@
use poulpy_hal::{
api::{
SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes,
},
oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl},
layouts::{Backend, DataMut, DataRef, Module, Scratch},
source::Source,
};
use crate::{
ScratchTakeCore,
encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey,
GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey,
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
},
};
@@ -19,7 +21,7 @@ impl TensorKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf,
Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf,
{
TensorKey::encrypt_sk_tmp_bytes(module, infos)
}
@@ -35,18 +37,25 @@ pub trait GGLWETensorKeyCompressedEncryptSk<B: Backend> {
scratch: &mut Scratch<B>,
) where
R: TensorKeyCompressedToMut,
S: GLWESecretToRef;
S: GLWESecretToRef + GetDist;
}
impl<B: Backend> GGLWETensorKeyCompressedEncryptSk<B> for Module<B>
where
Module<B>: GGLWEKeyCompressedEncryptSk<B>
Module<B>: ModuleN
+ GGLWEKeyCompressedEncryptSk<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDft<B>
+ VecZnxIdftApplyTmpA<B>
+ VecZnxBigNormalize<B>
+ SvpPrepare<B>,
Scratch<B>:,
+ SvpPrepare<B>
+ SvpPPolAllocBytesImpl<B>
+ SvpPPolBytesOf
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ VecZnxDftBytesOf
+ VecZnxBigBytesOf,
Scratch<B>: ScratchTakeBasic + ScratchTakeCore<B>,
{
fn gglwe_tensor_key_encrypt_sk<R, S>(
&self,
@@ -57,9 +66,13 @@ where
scratch: &mut Scratch<B>,
) where
R: TensorKeyCompressedToMut,
S: GLWESecretToRef,
S: GLWESecretToRef + GetDist,
{
let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut();
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out());
sk_dft_prep.prepare(self, sk);
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
#[cfg(debug_assertions)]
@@ -68,21 +81,18 @@ where
assert_eq!(res.n(), sk.n());
}
let n: usize = sk.n().into();
// let n: usize = sk.n().into();
let rank: usize = res.rank_out().into();
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(sk.n(), res.rank_out());
sk_dft_prep.prepare(self, sk, scratch_1);
let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(n, rank, 1);
let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, rank, 1);
for i in 0..rank {
self.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
}
let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(n, 1, 1);
let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(sk.n(), Rank(1));
let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(n, 1, 1);
let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(self, 1, 1);
let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self, Rank(1));
let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(self, 1, 1);
let mut source_xa: Source = Source::new(seed_xa);
@@ -125,6 +135,7 @@ impl<DataSelf: DataMut> TensorKeyCompressed<DataSelf> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
GLWESecret<DataSk>: GetDist,
Module<B>: GGLWETensorKeyCompressedEncryptSk<B>,
{
module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch);