Removed traits on structs not dependent on Module API

This commit is contained in:
Pro7ech
2025-10-20 10:32:02 +02:00
parent 60fbd3e625
commit 53bc78f421
50 changed files with 858 additions and 2010 deletions

View File

@@ -1,31 +1,21 @@
use poulpy_hal::{
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace},
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
};
use crate::decryption::GLWEDecrypt;
use crate::layouts::{
GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
};
use crate::layouts::{GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::GLWESecretPreparedToRef};
impl<D: DataRef> GGLWE<D> {
pub fn assert_noise<M, DataSk, DataWant, BE: Backend>(
&self,
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
pt_want: &ScalarZnx<DataWant>,
max_noise: f64,
) where
DataSk: DataRef,
DataWant: DataRef,
pub fn assert_noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64)
where
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
M: GGLWENoise<BE>,
Scratch<BE>: ScratchTakeBasic,
{
module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
}
}
pub trait GGLWENoise<BE: Backend> {
@@ -34,7 +24,7 @@ pub trait GGLWENoise<BE: Backend> {
R: GGLWEToRef,
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
Scratch<BE>: ScratchTakeBasic;
Scratch<BE>: ScratchTakeBasic;
}
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
@@ -49,7 +39,7 @@ where
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
{
let res: &GGLWE<&[u8]> = &res.to_ref();
@@ -57,7 +47,7 @@ where
let base2k: usize = res.base2k().into();
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
(0..res.rank_in().into()).for_each(|col_i| {
(0..res.dnum().into()).for_each(|row_i| {

View File

@@ -1,12 +1,9 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace,
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize,
VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAlloc,
VecZnxBigNormalize, VecZnxDftAlloc, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
},
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
};
use crate::decryption::GLWEDecrypt;
@@ -14,32 +11,21 @@ use crate::layouts::prepared::GLWESecretPreparedToRef;
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
impl<D: DataRef> GGSW<D> {
pub fn assert_noise<M, BE, DataSk, DataScalar, F>(
&self,
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
pt_want: &ScalarZnx<DataScalar>,
max_noise: F,
) where
DataSk: DataRef,
DataScalar: DataRef,
pub fn assert_noise<M, BE: Backend, P, S, F>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: F)
where
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
M: GGSWNoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64,
{
module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise);
}
pub fn print_noise<M, BE, DataSk, DataScalar>(
&self,
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
pt_want: &ScalarZnx<DataScalar>,
) where
DataSk: DataRef,
DataScalar: DataRef,
pub fn print_noise<M, BE: Backend, P, S>(&self, module: &M, sk_prepared: &S, pt_want: &P)
where
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
M: GGSWNoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{
module.ggsw_print_noise(self, sk_prepared, pt_want);
}
@@ -51,43 +37,31 @@ pub trait GGSWNoise<BE: Backend> {
R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64;
fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P)
where
R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>;
P: ScalarZnxToRef;
}
impl<BE: Backend> GGSWNoise<BE> for Module<BE>
where
Module<BE>: VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddInplace<BE>
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxBigAlloc<BE>
Module<BE>: GLWEDecrypt<BE>
+ VecZnxDftAlloc<BE>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<BE>
+ VecZnxBigAlloc<BE>
+ VecZnxAddScalarInplace
+ VecZnxSubInplace
+ GLWEDecrypt<BE>,
+ VecZnxIdftApplyTmpA<BE>
+ VecZnxSubInplace,
Scratch<BE>: ScratchTakeBasic,
ScratchOwned<BE>: ScratchOwnedBorrow<BE> + ScratchOwnedAlloc<BE>,
{
fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F)
where
R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64,
{
let res: &GGSW<&[u8]> = &res.to_ref();
@@ -96,8 +70,8 @@ where
let base2k: usize = res.base2k().into();
let dsize: usize = res.dsize().into();
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
let mut pt_dft: VecZnxDft<Vec<u8>, BE> = self.vec_znx_dft_alloc(1, res.size());
let mut pt_big: VecZnxBig<Vec<u8>, BE> = self.vec_znx_big_alloc(1, res.size());
@@ -147,7 +121,6 @@ where
R: GGSWToRef,
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{
let res: &GGSW<&[u8]> = &res.to_ref();
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
@@ -155,16 +128,16 @@ where
let base2k: usize = res.base2k().into();
let dsize: usize = res.dsize().into();
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res);
let mut pt_dft: VecZnxDft<Vec<u8>, BE> = self.vec_znx_dft_alloc(1, res.size());
let mut pt_big: VecZnxBig<Vec<u8>, BE> = self.vec_znx_big_alloc(1, res.size());
let mut scratch: ScratchOwned<BE> =
ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes());
(0..(res.rank() + 1).into()).for_each(|col_j| {
(0..res.dnum().into()).for_each(|row_i| {
for col_j in 0..(res.rank() + 1).into() {
for row_i in 0..res.dnum().into() {
self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0);
// mul with sk[col_j-1]
@@ -194,7 +167,7 @@ where
let std_pt: f64 = pt_have.data.std(base2k, 0).log2();
println!("col: {col_j} row: {row_i}: {std_pt}");
pt.data.zero();
});
});
}
}
}
}

View File

@@ -1,19 +1,12 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace,
VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
},
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubInplace},
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
};
use crate::{
ScratchTakeCore,
decryption::GLWEDecrypt,
layouts::{
GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
},
layouts::{GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef},
};
impl<D: DataRef> GLWE<D> {
@@ -25,75 +18,15 @@ impl<D: DataRef> GLWE<D> {
{
module.glwe_noise(self, sk_prepared, pt_want, scratch)
}
// pub fn noise<B, DataSk, DataPt>(
// &self,
// module: &Module<B>,
// sk_prepared: &GLWESecretPrepared<DataSk, B>,
// pt_want: &GLWEPlaintext<DataPt>,
// scratch: &mut Scratch<B>,
// ) -> f64
// where
// DataSk: DataRef,
// DataPt: DataRef,
// B: Backend,
// Module<B>: VecZnxDftApply<B>
// + VecZnxSubInplace
// + VecZnxNormalizeInplace<B>
// + SvpApplyDftToDftInplace<B>
// + VecZnxIdftApplyConsume<B>
// + VecZnxBigAddInplace<B>
// + VecZnxBigAddSmallInplace<B>
// + VecZnxBigNormalize<B>,
// Scratch<B>:,
// {
// let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
// self.decrypt(module, &mut pt_have, sk_prepared, scratch);
// module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
// module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch);
// pt_have.data.std(self.base2k().into(), 0).log2()
// }
pub fn assert_noise<M, BE, DataSk, DataPt>(
&self,
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
pt_want: &GLWEPlaintext<DataPt>,
max_noise: f64,
) where
DataSk: DataRef,
DataPt: DataRef,
pub fn assert_noise<M, BE: Backend, S, P>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64)
where
S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef,
M: GLWENoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{
module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise);
}
// pub fn assert_noise<B, DataSk, DataPt>(
// &self,
// module: &Module<B>,
// sk_prepared: &GLWESecretPrepared<DataSk, B>,
// pt_want: &GLWEPlaintext<DataPt>,
// max_noise: f64,
// ) where
// DataSk: DataRef,
// DataPt: DataRef,
// Module<B>: VecZnxDftBytesOf
// + VecZnxBigBytesOf
// + VecZnxDftApply<B>
// + SvpApplyDftToDftInplace<B>
// + VecZnxIdftApplyConsume<B>
// + VecZnxBigAddInplace<B>
// + VecZnxBigAddSmallInplace<B>
// + VecZnxBigNormalize<B>
// + VecZnxNormalizeTmpBytes
// + VecZnxSubInplace
// + VecZnxNormalizeInplace<B>,
// B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
// {
// let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self));
// let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow());
// assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
// }
}
pub trait GLWENoise<BE: Backend> {
@@ -107,29 +40,14 @@ pub trait GLWENoise<BE: Backend> {
where
R: GLWEToRef,
S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>;
P: GLWEPlaintextToRef;
}
impl<BE: Backend> GLWENoise<BE> for Module<BE>
where
Module<BE>: VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddInplace<BE>
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxSubInplace
+ VecZnxNormalizeInplace<BE>
+ GLWEDecrypt<BE>,
Scratch<BE>: ScratchTakeBasic
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>,
Module<BE>: GLWEDecrypt<BE> + VecZnxSubInplace + VecZnxNormalizeInplace<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
where
@@ -141,7 +59,7 @@ where
let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.to_ref();
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res_ref);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(res_ref);
self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch);
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
self.vec_znx_normalize_inplace(res_ref.base2k().into(), &mut pt_have.data, 0, scratch);
@@ -153,7 +71,6 @@ where
R: GLWEToRef,
S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{
let res: &GLWE<&[u8]> = &res.to_ref();
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));