gglwe tests

This commit is contained in:
Rasoul Akhavan Mahdavi
2025-10-19 21:13:52 -04:00
parent a706b759d6
commit 1ba54b68c7
7 changed files with 113 additions and 215 deletions

View File

@@ -1,7 +1,6 @@
use poulpy_hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace},
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl},
};
use crate::decryption::GLWEDecrypt;
@@ -11,7 +10,7 @@ use crate::layouts::{
};
impl<D: DataRef> GGLWE<D> {
pub fn assert_noise<M, BE, DataSk, DataWant>(
pub fn assert_noise<M, DataSk, DataWant, BE: Backend>(
&self,
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
@@ -21,62 +20,12 @@ impl<D: DataRef> GGLWE<D> {
DataSk: DataRef,
DataWant: DataRef,
M: GGLWENoise<BE>,
BE: Backend
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>
+ VecZnxSubScalarInplaceImpl<BE>,
Scratch<BE>: ScratchTakeBasic,
{
module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
}
// pub fn assert_noise<B, DataSk, DataWant>(
// &self,
// module: &Module<B>,
// sk: &GLWESecretPrepared<DataSk, B>,
// pt_want: &ScalarZnx<DataWant>,
// max_noise: f64,
// ) where
// DataSk: DataRef,
// DataWant: DataRef,
// Module<B>: VecZnxDftBytesOf
// + VecZnxBigBytesOf
// + VecZnxDftApply<B>
// + SvpApplyDftToDftInplace<B>
// + VecZnxIdftApplyConsume<B>
// + VecZnxBigAddInplace<B>
// + VecZnxBigAddSmallInplace<B>
// + VecZnxBigNormalize<B>
// + VecZnxNormalizeTmpBytes
// + VecZnxSubScalarInplace,
// B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
// {
// let dsize: usize = self.dsize().into();
// let base2k: usize = self.base2k().into();
// let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self));
// let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
// (0..self.rank_in().into()).for_each(|col_i| {
// (0..self.dnum().into()).for_each(|row_i| {
// self.at(row_i, col_i)
// .decrypt(module, &mut pt, sk, scratch.borrow());
// module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
// let noise_have: f64 = pt.data.std(base2k, 0).log2();
// println!("noise_have: {noise_have}");
// assert!(
// noise_have <= max_noise,
// "noise_have: {noise_have} > max_noise: {max_noise}"
// );
// pt.data.zero();
// });
// });
// }
}
pub trait GGLWENoise<BE: Backend> {
@@ -85,24 +34,22 @@ pub trait GGLWENoise<BE: Backend> {
R: GGLWEToRef,
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>;
Scratch<BE>: ScratchTakeBasic;
}
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
where
Module<BE>: GLWEDecrypt<BE>,
Scratch<BE>: ScratchTakeBasic
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>,
Module<BE>: GLWEDecrypt<BE> + VecZnxFillUniform + VecZnxSubScalarInplace,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
{
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
where
R: GGLWEToRef,
S: GLWESecretPreparedToRef<BE>,
P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
{
let res: &GGLWE<&[u8]> = &res.to_ref();
@@ -125,7 +72,7 @@ where
let noise_have: f64 = pt.data.std(base2k, 0).log2();
println!("noise_have: {noise_have}");
// println!("noise_have: {noise_have}");
assert!(
noise_have <= max_noise,

View File

@@ -2,6 +2,10 @@ mod gglwe_ct;
mod ggsw_ct;
mod glwe_ct;
pub use gglwe_ct::*;
pub use ggsw_ct::*;
pub use glwe_ct::*;
#[allow(clippy::too_many_arguments)]
#[allow(dead_code)]
pub(crate) fn var_noise_gglwe_product(