remaining in encryption + noise

This commit is contained in:
Rasoul Akhavan Mahdavi
2025-10-17 19:56:10 -04:00
parent 957345f9ea
commit a282e88126
9 changed files with 527 additions and 308 deletions

View File

@@ -1,49 +1,120 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes,
VecZnxSubScalarInplace,
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace,
},
layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ScalarZnx, ScalarZnxToRef, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl},
};
use crate::layouts::{GGLWE, GGLWEInfos, GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
use crate::layouts::{
GGLWE, GGLWEToRef, GGLWEInfos, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
};
use crate::decryption::GLWEDecryption;
impl<D: DataRef> GGLWE<D> {
pub fn assert_noise<B, DataSk, DataWant>(
pub fn assert_noise<M, BE, DataSk, DataWant>(
&self,
module: &Module<B>,
sk: &GLWESecretPrepared<DataSk, B>,
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
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>,
M: GGLWENoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
{
let dsize: usize = self.dsize().into();
let base2k: usize = self.base2k().into();
module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
}
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());
// 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();
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
// 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> {
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>;
}
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
where
Module<BE>: GLWEDecryption<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
R: GGLWEToRef,
S: GLWESecretPreparedToRef<BE>,
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();
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);
(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.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();
@@ -58,4 +129,4 @@ impl<D: DataRef> GGLWE<D> {
});
});
}
}
}

View File

@@ -4,62 +4,118 @@ use poulpy_hal::{
VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes,
VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes,
VecZnxSubInplace,
ScratchTakeBasic,
},
layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
};
use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
use crate::layouts::{GGSW, GGSWInfos, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, GGSWToRef};
use crate::layouts::prepared::GLWESecretPreparedToRef;
use crate::decryption::GLWEDecryption;
impl<D: DataRef> GGSW<D> {
pub fn assert_noise<B, DataSk, DataScalar, F>(
pub fn assert_noise<M, BE, DataSk, DataScalar, F>(
&self,
module: &Module<B>,
sk_prepared: &GLWESecretPrepared<DataSk, B>,
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
pt_want: &ScalarZnx<DataScalar>,
max_noise: F,
max_noise: F
) where
DataSk: DataRef,
DataScalar: DataRef,
Module<B>: VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigNormalize<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxBigAlloc<B>
+ VecZnxDftAlloc<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<B>
+ VecZnxAddScalarInplace
+ VecZnxSubInplace,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
M: GGSWNoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64,
{
let base2k: usize = self.base2k().into();
let dsize: usize = self.dsize().into();
module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise);
}
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(1, self.size());
let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(1, self.size());
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,
M: GGSWNoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{
module.ggsw_print_noise(self, sk_prepared, pt_want);
}
}
let mut scratch: ScratchOwned<B> =
ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self) | module.vec_znx_normalize_tmp_bytes());
pub trait GGSWNoise<BE: Backend> {
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;
(0..(self.rank() + 1).into()).for_each(|col_j| {
(0..self.dnum().into()).for_each(|row_i| {
module.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0);
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>;
}
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>
+ VecZnxDftAlloc<BE>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<BE>
+ VecZnxAddScalarInplace
+ VecZnxSubInplace
+ GLWEDecryption<BE>,
Scratch<BE>: ScratchTakeBasic,
{
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();
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
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_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| {
self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0);
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0);
module.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
module.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(
self.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0);
self.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
self.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0);
self.vec_znx_big_normalize(
base2k,
&mut pt.data,
0,
@@ -70,10 +126,9 @@ impl<D: DataRef> GGSW<D> {
);
}
self.at(row_i, col_j)
.decrypt(module, &mut pt_have, sk_prepared, scratch.borrow());
self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow());
module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
let std_pt: f64 = pt_have.data.std(base2k, 0).log2();
let noise: f64 = max_noise(col_j);
@@ -81,57 +136,40 @@ impl<D: DataRef> GGSW<D> {
pt.data.zero();
});
});
});
}
}
impl<D: DataRef> GGSW<D> {
pub fn print_noise<B, DataSk, DataScalar>(
&self,
module: &Module<B>,
sk_prepared: &GLWESecretPrepared<DataSk, B>,
pt_want: &ScalarZnx<DataScalar>,
) where
DataSk: DataRef,
DataScalar: DataRef,
Module<B>: VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigNormalize<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxBigAlloc<B>
+ VecZnxDftAlloc<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<B>
+ VecZnxAddScalarInplace
+ VecZnxSubInplace,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
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>,
{
let base2k: usize = self.base2k().into();
let dsize: usize = self.dsize().into();
let res: &GGSW<&[u8]> = &res.to_ref();
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(1, self.size());
let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(1, self.size());
let base2k: usize = res.base2k().into();
let dsize: usize = res.dsize().into();
let mut scratch: ScratchOwned<B> =
ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self) | module.vec_znx_normalize_tmp_bytes());
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_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());
(0..(self.rank() + 1).into()).for_each(|col_j| {
(0..self.dnum().into()).for_each(|row_i| {
module.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0);
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| {
self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0);
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0);
module.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
module.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(
self.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0);
self.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1);
self.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0);
self.vec_znx_big_normalize(
base2k,
&mut pt.data,
0,
@@ -142,15 +180,13 @@ impl<D: DataRef> GGSW<D> {
);
}
self.at(row_i, col_j)
.decrypt(module, &mut pt_have, sk_prepared, scratch.borrow());
module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
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();
println!("col: {col_j} row: {row_i}: {std_pt}");
pt.data.zero();
// println!(">>>>>>>>>>>>>>>>");
});
});
}
}
}
}

View File

@@ -2,67 +2,159 @@ use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace,
VecZnxNormalizeTmpBytes, VecZnxSubInplace,
VecZnxNormalizeTmpBytes, VecZnxSubInplace, ScratchTakeBasic,
},
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
};
use crate::layouts::{GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
use crate::{
decryption::GLWEDecryption,
layouts::{
GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
},
};
impl<D: DataRef> GLWE<D> {
pub fn noise<B, DataSk, DataPt>(
&self,
module: &Module<B>,
sk_prepared: &GLWESecretPrepared<DataSk, B>,
pt_want: &GLWEPlaintext<DataPt>,
scratch: &mut Scratch<B>,
) -> f64
pub fn noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> 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>:,
M: GLWENoise<BE>,
S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef,
{
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()
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<B, DataSk, DataPt>(
pub fn assert_noise<M, BE, DataSk, DataPt>(
&self,
module: &Module<B>,
sk_prepared: &GLWESecretPrepared<DataSk, B>,
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
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>,
M: GLWENoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
{
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());
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> {
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
where
R: GLWEToRef,
S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef;
fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
where
R: GLWEToRef,
S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>;
}
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>
+ GLWEDecryption<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
R: GLWEToRef,
S: GLWESecretPreparedToRef<BE>,
P: GLWEPlaintextToRef,
{
let res_ref: &GLWE<&[u8]> = &res.to_ref();
let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.to_ref();
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, 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);
pt_have.data.std(res_ref.base2k().into(), 0).log2()
}
fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
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));
let noise_have: f64 = self.glwe_noise(res, sk_prepared, pt_want, scratch.borrow());
assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
}
}
}