mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 21:26:41 +01:00
fixed many test noise check + update noise retrieval (not passing)
This commit is contained in:
@@ -1,74 +1,91 @@
|
||||
use poulpy_hal::{
|
||||
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace},
|
||||
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
|
||||
api::VecZnxAddScalarInplace,
|
||||
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, Stats, ZnxInfos, ZnxZero},
|
||||
};
|
||||
|
||||
use crate::decryption::GLWEDecrypt;
|
||||
use crate::layouts::{GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::GLWESecretPreparedToRef};
|
||||
use crate::{
|
||||
GLWENoise,
|
||||
layouts::{GGLWE, GGLWEInfos, GGLWEToRef, prepared::GLWESecretPreparedToRef},
|
||||
};
|
||||
use crate::{ScratchTakeCore, layouts::GLWEPlaintext};
|
||||
|
||||
impl<D: DataRef> GGLWE<D> {
|
||||
pub fn assert_noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
pub fn noise<M, S, P, BE: Backend>(
|
||||
&self,
|
||||
module: &M,
|
||||
row: usize,
|
||||
col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
M: GGLWENoise<BE>,
|
||||
Scratch<BE>: ScratchTakeBasic,
|
||||
{
|
||||
module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
|
||||
module.gglwe_noise(self, row, col, pt_want, sk_prepared, scratch)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GGLWENoise<BE: Backend> {
|
||||
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
fn gglwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos;
|
||||
|
||||
fn gglwe_noise<R, S, P>(
|
||||
&self,
|
||||
res: &R,
|
||||
res_row: usize,
|
||||
res_col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
R: GGLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
Scratch<BE>: ScratchTakeBasic;
|
||||
P: ScalarZnxToRef;
|
||||
}
|
||||
|
||||
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
|
||||
where
|
||||
Module<BE>: GLWEDecrypt<BE> + VecZnxFillUniform + VecZnxSubScalarInplace,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
|
||||
Module<BE>: VecZnxAddScalarInplace + GLWENoise<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
fn gglwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
GLWEPlaintext::bytes_of_from_infos(infos) + self.glwe_noise_tmp_bytes(infos)
|
||||
}
|
||||
|
||||
fn gglwe_noise<R, S, P>(
|
||||
&self,
|
||||
res: &R,
|
||||
res_row: usize,
|
||||
res_col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
R: GGLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
|
||||
{
|
||||
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(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.stats(base2k, 0).std().log2();
|
||||
|
||||
assert!(
|
||||
noise_have <= max_noise,
|
||||
"noise_have: {noise_have} > max_noise: {max_noise}"
|
||||
);
|
||||
|
||||
pt.data.zero();
|
||||
});
|
||||
});
|
||||
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(res);
|
||||
pt.data_mut().zero();
|
||||
println!("col: {res_col} {}", pt_want.to_ref().cols());
|
||||
self.vec_znx_add_scalar_inplace(
|
||||
&mut pt.data,
|
||||
0,
|
||||
(dsize - 1) + res_row * dsize,
|
||||
pt_want,
|
||||
res_col,
|
||||
);
|
||||
self.glwe_noise(&res.at(res_row, res_col), &pt, sk_prepared, scratch_1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,49 @@
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAlloc,
|
||||
VecZnxBigNormalize, VecZnxDftAlloc, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
|
||||
ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes,
|
||||
VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
|
||||
},
|
||||
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
|
||||
layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, Stats, ZnxZero},
|
||||
};
|
||||
|
||||
use crate::decryption::GLWEDecrypt;
|
||||
use crate::layouts::prepared::GLWESecretPreparedToRef;
|
||||
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
|
||||
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, LWEInfos, prepared::GLWESecretPrepared};
|
||||
use crate::{GLWENoise, layouts::prepared::GLWESecretPreparedToRef};
|
||||
use crate::{ScratchTakeCore, layouts::GLWEPlaintext};
|
||||
|
||||
impl<D: DataRef> GGSW<D> {
|
||||
pub fn assert_noise<M, BE: Backend, P, S, F>(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: &F)
|
||||
pub fn noise<M, BE: Backend, P, S>(
|
||||
&self,
|
||||
module: &M,
|
||||
row: usize,
|
||||
col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
M: GGSWNoise<BE>,
|
||||
F: Fn(usize) -> f64,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise);
|
||||
}
|
||||
|
||||
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>,
|
||||
{
|
||||
module.ggsw_print_noise(self, sk_prepared, pt_want);
|
||||
module.ggsw_noise(self, row, col, pt_want, sk_prepared, scratch)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
fn ggsw_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
R: GGSWToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
F: Fn(usize) -> f64;
|
||||
A: GGSWInfos;
|
||||
|
||||
fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P)
|
||||
fn ggsw_noise<R, S, P>(
|
||||
&self,
|
||||
res: &R,
|
||||
res_row: usize,
|
||||
res_col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
R: GGSWToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
@@ -48,79 +52,39 @@ pub trait GGSWNoise<BE: Backend> {
|
||||
|
||||
impl<BE: Backend> GGSWNoise<BE> for Module<BE>
|
||||
where
|
||||
Module<BE>: GLWEDecrypt<BE>
|
||||
+ VecZnxDftAlloc<BE>
|
||||
+ VecZnxBigAlloc<BE>
|
||||
+ VecZnxAddScalarInplace
|
||||
+ VecZnxIdftApplyTmpA<BE>
|
||||
+ VecZnxSubInplace,
|
||||
Scratch<BE>: ScratchTakeBasic,
|
||||
ScratchOwned<BE>: ScratchOwnedBorrow<BE> + ScratchOwnedAlloc<BE>,
|
||||
Module<BE>: VecZnxAddScalarInplace
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxDftBytesOf
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ GLWENoise<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: &F)
|
||||
fn ggsw_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
R: GGSWToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
F: Fn(usize) -> f64,
|
||||
A: GGSWInfos,
|
||||
{
|
||||
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(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| {
|
||||
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 {
|
||||
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,
|
||||
base2k,
|
||||
&pt_big,
|
||||
0,
|
||||
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.stats(base2k, 0).std().log2();
|
||||
let noise: f64 = max_noise(col_j);
|
||||
assert!(std_pt <= noise, "{std_pt} > {noise}");
|
||||
|
||||
pt.data.zero();
|
||||
});
|
||||
});
|
||||
GLWEPlaintext::bytes_of_from_infos(infos)
|
||||
+ (self.bytes_of_vec_znx_dft(1, infos.size()) + self.vec_znx_big_normalize_tmp_bytes())
|
||||
.max(self.glwe_noise_tmp_bytes(infos))
|
||||
}
|
||||
|
||||
fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P)
|
||||
fn ggsw_noise<R, S, P>(
|
||||
&self,
|
||||
res: &R,
|
||||
res_row: usize,
|
||||
res_col: usize,
|
||||
pt_want: &P,
|
||||
sk_prepared: &S,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Stats
|
||||
where
|
||||
R: GGSWToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: ScalarZnxToRef,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let res: &GGSW<&[u8]> = &res.to_ref();
|
||||
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
|
||||
@@ -128,47 +92,19 @@ where
|
||||
let base2k: usize = res.base2k().into();
|
||||
let dsize: usize = res.dsize().into();
|
||||
|
||||
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 pt, scratch_1) = scratch.take_glwe_plaintext(res);
|
||||
pt.data_mut().zero();
|
||||
self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + res_row * dsize, pt_want, 0);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> =
|
||||
ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes());
|
||||
|
||||
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]
|
||||
if col_j > 0 {
|
||||
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,
|
||||
base2k,
|
||||
&pt_big,
|
||||
0,
|
||||
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.stats(base2k, 0).std().log2();
|
||||
println!("col: {col_j} row: {row_i}: {std_pt}");
|
||||
pt.data.zero();
|
||||
}
|
||||
// mul with sk[col_j-1]
|
||||
if res_col > 0 {
|
||||
let (mut pt_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, 1, res.size());
|
||||
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, res_col - 1);
|
||||
let pt_big = self.vec_znx_idft_apply_consume(pt_dft);
|
||||
self.vec_znx_big_normalize(base2k, &mut pt.data, 0, base2k, &pt_big, 0, scratch_2);
|
||||
}
|
||||
|
||||
self.glwe_noise(&res.at(res_row, res_col), &pt, sk_prepared, scratch_1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,83 +1,61 @@
|
||||
use poulpy_hal::{
|
||||
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubInplace},
|
||||
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, Stats},
|
||||
};
|
||||
use poulpy_hal::layouts::{Backend, DataRef, Module, Scratch, Stats};
|
||||
|
||||
use crate::{
|
||||
ScratchTakeCore,
|
||||
GLWENormalize, GLWESub, ScratchTakeCore,
|
||||
decryption::GLWEDecrypt,
|
||||
layouts::{GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef},
|
||||
layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef},
|
||||
};
|
||||
|
||||
impl<D: DataRef> GLWE<D> {
|
||||
pub fn noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
pub fn noise<M, P, S, BE: Backend>(&self, module: &M, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
M: GLWENoise<BE>,
|
||||
P: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef,
|
||||
{
|
||||
module.glwe_noise(self, sk_prepared, pt_want, scratch)
|
||||
}
|
||||
|
||||
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>,
|
||||
{
|
||||
module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise);
|
||||
module.glwe_noise(self, pt_want, sk_prepared, scratch)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWENoise<BE: Backend> {
|
||||
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
fn glwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
R: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef;
|
||||
A: GLWEInfos;
|
||||
|
||||
fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
fn glwe_noise<R, P, S>(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
R: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef;
|
||||
R: GLWEToRef + GLWEInfos,
|
||||
P: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>;
|
||||
}
|
||||
|
||||
impl<BE: Backend> GLWENoise<BE> for Module<BE>
|
||||
where
|
||||
Module<BE>: GLWEDecrypt<BE> + VecZnxSubInplace + VecZnxNormalizeInplace<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Module<BE>: GLWEDecrypt<BE> + GLWESub + GLWENormalize<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
fn glwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
R: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef,
|
||||
A: GLWEInfos,
|
||||
{
|
||||
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(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.stats(res_ref.base2k().into(), 0)
|
||||
GLWEPlaintext::bytes_of_from_infos(infos)
|
||||
+ self
|
||||
.glwe_normalize_tmp_bytes()
|
||||
.max(self.glwe_decrypt_tmp_bytes(infos))
|
||||
}
|
||||
|
||||
fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
fn glwe_noise<R, P, S>(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
R: GLWEToRef,
|
||||
R: GLWEToRef + GLWEInfos,
|
||||
P: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
P: GLWEPlaintextToRef,
|
||||
{
|
||||
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())
|
||||
.std()
|
||||
.log2();
|
||||
assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
|
||||
let (mut pt_have, scratch_1) = scratch.take_glwe_plaintext(res);
|
||||
self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch_1);
|
||||
// println!("pt_have: {pt_have}");
|
||||
// println!("pt_want: {}", pt_want.to_ref());
|
||||
self.glwe_sub_inplace(&mut pt_have, pt_want);
|
||||
self.glwe_normalize_inplace(&mut pt_have, scratch_1);
|
||||
pt_have.data.stats(pt_have.base2k().into(), 0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user