fix remaining issues before fixing tests

This commit is contained in:
Pro7ech
2025-10-18 11:59:50 +02:00
parent a282e88126
commit 0b8dcb1f16
34 changed files with 224 additions and 160 deletions

View File

@@ -1,19 +1,16 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace,
},
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ScalarZnx, ScalarZnxToRef, ZnxZero},
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace},
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl},
};
use crate::decryption::GLWEDecryption;
use crate::layouts::{
GGLWE, GGLWEToRef, GGLWEInfos, GLWEPlaintext, LWEInfos,
GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
};
use crate::decryption::GLWEDecryption;
impl<D: DataRef> GGLWE<D> {
pub fn assert_noise<M, BE, DataSk, DataWant>(
&self,
module: &M,
@@ -24,12 +21,15 @@ impl<D: DataRef> GGLWE<D> {
DataSk: DataRef,
DataWant: DataRef,
M: GGLWENoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
BE: Backend
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>
+ VecZnxSubScalarInplaceImpl<BE>,
{
module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
}
// pub fn assert_noise<B, DataSk, DataWant>(
// &self,
// module: &Module<B>,
@@ -79,7 +79,6 @@ impl<D: DataRef> GGLWE<D> {
// }
}
pub trait GGLWENoise<BE: Backend> {
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
where
@@ -89,10 +88,14 @@ pub trait GGLWENoise<BE: Backend> {
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>;
}
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
where
Module<BE>: GLWEDecryption<BE>,
Scratch<BE>: ScratchTakeBasic + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeBasic
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>,
{
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
where
@@ -101,9 +104,8 @@ where
P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
{
let res: &GGLWE<&[u8]> = &res.to_ref();
let dsize: usize = res.dsize().into();
let base2k: usize = res.base2k().into();
@@ -112,7 +114,12 @@ where
(0..res.rank_in().into()).for_each(|col_i| {
(0..res.dnum().into()).for_each(|row_i| {
self.glwe_decrypt(&res.at(row_i, col_i), &mut pt, sk_prepared, scratch.borrow());
self.glwe_decrypt(
&res.at(row_i, col_i),
&mut pt,
sk_prepared,
scratch.borrow(),
);
self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
@@ -129,4 +136,4 @@ where
});
});
}
}
}

View File

@@ -1,18 +1,17 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAddInplace,
VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes,
VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes,
VecZnxSubInplace,
ScratchTakeBasic,
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace,
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize,
VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
},
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
};
use crate::layouts::{GGSW, GGSWInfos, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, GGSWToRef};
use crate::layouts::prepared::GLWESecretPreparedToRef;
use crate::decryption::GLWEDecryption;
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>(
@@ -20,7 +19,7 @@ impl<D: DataRef> GGSW<D> {
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
pt_want: &ScalarZnx<DataScalar>,
max_noise: F
max_noise: F,
) where
DataSk: DataRef,
DataScalar: DataRef,
@@ -91,7 +90,6 @@ where
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64,
{
let res: &GGSW<&[u8]> = &res.to_ref();
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
@@ -126,7 +124,12 @@ where
);
}
self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow());
self.glwe_decrypt(
&res.at(row_i, col_j),
&mut pt_have,
sk_prepared,
scratch.borrow(),
);
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
@@ -136,7 +139,7 @@ where
pt.data.zero();
});
});
});
}
fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P)
@@ -180,7 +183,12 @@ where
);
}
self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow());
self.glwe_decrypt(
&res.at(row_i, col_j),
&mut pt_have,
sk_prepared,
scratch.borrow(),
);
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
let std_pt: f64 = pt_have.data.std(base2k, 0).log2();
@@ -188,5 +196,5 @@ where
pt.data.zero();
});
});
}
}
}
}

View File

@@ -1,8 +1,8 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace,
VecZnxNormalizeTmpBytes, VecZnxSubInplace, ScratchTakeBasic,
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace,
VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
},
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
@@ -96,7 +96,6 @@ impl<D: DataRef> GLWE<D> {
// }
}
pub trait GLWENoise<BE: Backend> {
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
where
@@ -126,7 +125,11 @@ where
+ VecZnxSubInplace
+ VecZnxNormalizeInplace<BE>
+ GLWEDecryption<BE>,
Scratch<BE>: ScratchTakeBasic + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeBasic
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>,
{
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
where
@@ -157,4 +160,4 @@ where
let noise_have: f64 = self.glwe_noise(res, sk_prepared, pt_want, scratch.borrow());
assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
}
}
}