review of encryption

This commit is contained in:
Pro7ech
2025-10-17 10:51:14 +02:00
parent 0a7e0b0903
commit e0d3ca5cea
17 changed files with 513 additions and 1162 deletions

View File

@@ -1,17 +1,14 @@
use poulpy_hal::{
api::{
ModuleN, ScratchTakeBasic,
SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxBigBytesOf,
VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes,
ModuleN, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf,
VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes,
},
layouts::{Backend, DataMut, DataViewMut, Module, Scratch},
};
use crate::{
layouts::{
GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, GLWEToMut, GLWEPlaintextToMut,
prepared::{GLWESecretPreparedToRef, GLWESecretPrepared},
}
use crate::layouts::{
GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToMut, GLWEToMut, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
};
impl GLWE<Vec<u8>> {
@@ -25,12 +22,12 @@ impl GLWE<Vec<u8>> {
}
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
P: GLWEPlaintextToMut,
S: GLWESecretPreparedToRef<BE>,
M: GLWEDecryption<BE>,
Scratch<BE>: ScratchTakeBasic,
Scratch<BE>: ScratchTakeBasic,
{
module.glwe_decrypt(self, pt, sk, scratch);
}
@@ -48,29 +45,23 @@ where
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddInplace<BE>
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxBigNormalize<BE>,
{
fn glwe_decrypt_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GLWEInfos
A: GLWEInfos,
{
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)
}
fn glwe_decrypt<R, P, S>(
&self,
res: &mut R,
pt: &mut P,
sk: &S,
scratch: &mut Scratch<BE>,
) where
fn glwe_decrypt<R, P, S>(&self, res: &mut R, pt: &mut P, sk: &S, scratch: &mut Scratch<BE>)
where
R: GLWEToMut,
P: GLWEPlaintextToMut,
S: GLWESecretPreparedToRef<BE>,
Scratch<BE>: ScratchTakeBasic,
{
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let pt: &mut GLWEPlaintext<&mut [u8]> = &mut pt.to_ref();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
@@ -117,10 +108,9 @@ where
pt.base2k = res.base2k();
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
+ VecZnxDftBytesOf
+ VecZnxNormalizeTmpBytes
@@ -132,4 +122,4 @@ impl <BE: Backend> GLWEDecryption<BE> for Module<BE> where
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>
{
}
}

View File

@@ -4,10 +4,9 @@ use poulpy_hal::{
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)
where
P: LWEPlaintextToMut,
@@ -21,16 +20,15 @@ impl<DataSelf: DataRef + DataMut> LWE<DataSelf>
pub trait LWEDecrypt<BE: Backend>
where
Self: Sized + ZnNormalizeInplace<BE>
Self: Sized + ZnNormalizeInplace<BE>,
{
fn lwe_decrypt<R, P, S>(&self, res: &mut R, pt: &mut P, sk: S)
where
R: LWEToMut,
P: LWEPlaintextToMut,
S: LWESecretToRef,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
R: LWEToMut,
P: LWEPlaintextToMut,
S: LWESecretToRef,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
{
let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut();
let sk: LWESecret<&[u8]> = sk.to_ref();
@@ -60,8 +58,4 @@ where
}
}
impl<BE: Backend> LWEDecrypt<BE> for Module<BE> where
Self: Sized + ZnNormalizeInplace<BE>
{
}
impl<BE: Backend> LWEDecrypt<BE> for Module<BE> where Self: Sized + ZnNormalizeInplace<BE> {}