mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
Improve noise stats functionality
This commit is contained in:
@@ -60,7 +60,7 @@ where
|
||||
|
||||
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();
|
||||
let noise_have: f64 = pt.data.stats(base2k, 0).std().log2();
|
||||
|
||||
println!("noise_have: {noise_have}");
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ where
|
||||
|
||||
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 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}");
|
||||
|
||||
@@ -165,7 +165,7 @@ where
|
||||
|
||||
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 std_pt: f64 = pt_have.data.stats(base2k, 0).std().log2();
|
||||
println!("col: {col_j} row: {row_i}: {std_pt}");
|
||||
pt.data.zero();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use poulpy_hal::{
|
||||
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubInplace},
|
||||
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},
|
||||
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, Stats},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -10,7 +10,7 @@ use crate::{
|
||||
};
|
||||
|
||||
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>) -> f64
|
||||
pub fn noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
M: GLWENoise<BE>,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
@@ -30,7 +30,7 @@ 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
|
||||
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
R: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
@@ -49,7 +49,7 @@ where
|
||||
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
|
||||
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> Stats
|
||||
where
|
||||
R: GLWEToRef,
|
||||
S: GLWESecretPreparedToRef<BE>,
|
||||
@@ -63,7 +63,7 @@ where
|
||||
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()
|
||||
pt_have.data.stats(res_ref.base2k().into(), 0)
|
||||
}
|
||||
|
||||
fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
|
||||
@@ -74,7 +74,10 @@ where
|
||||
{
|
||||
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());
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ where
|
||||
col_i,
|
||||
);
|
||||
|
||||
let noise_have: f64 = pt.data.std(base2k, 0).log2();
|
||||
let noise_have: f64 = pt.data.stats(base2k, 0).std().log2();
|
||||
let noise_want: f64 = log2_std_noise_gglwe_product(
|
||||
n as f64,
|
||||
base2k * di,
|
||||
@@ -306,7 +306,7 @@ where
|
||||
col_i,
|
||||
);
|
||||
|
||||
let noise_have: f64 = pt.data.std(base2k, 0).log2();
|
||||
let noise_have: f64 = pt.data.stats(base2k, 0).std().log2();
|
||||
let noise_want: f64 = log2_std_noise_gglwe_product(
|
||||
n as f64,
|
||||
base2k * di,
|
||||
|
||||
@@ -74,7 +74,7 @@ where
|
||||
|
||||
module.glwe_sub_inplace(&mut pt_want, &pt_have);
|
||||
|
||||
let noise_have: f64 = pt_want.data.std(base2k, 0) * (ct.k().as_u32() as f64).exp2();
|
||||
let noise_have: f64 = pt_want.data.stats(base2k, 0).std() * (ct.k().as_u32() as f64).exp2();
|
||||
let noise_want: f64 = SIGMA;
|
||||
|
||||
assert!(noise_have <= noise_want + 0.2);
|
||||
@@ -147,7 +147,7 @@ where
|
||||
|
||||
module.glwe_sub_inplace(&mut pt_want, &pt_have);
|
||||
|
||||
let noise_have: f64 = pt_want.data.std(base2k, 0) * (ct.k().as_u32() as f64).exp2();
|
||||
let noise_have: f64 = pt_want.data.stats(base2k, 0).std() * (ct.k().as_u32() as f64).exp2();
|
||||
let noise_want: f64 = SIGMA;
|
||||
|
||||
assert!(
|
||||
@@ -203,7 +203,7 @@ where
|
||||
);
|
||||
ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
|
||||
|
||||
assert!((SIGMA - pt.data.std(base2k, 0) * (k_ct as f64).exp2()) <= 0.2);
|
||||
assert!((SIGMA - pt.data.stats(base2k, 0).std() * (k_ct as f64).exp2()) <= 0.2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,7 +271,7 @@ where
|
||||
|
||||
module.glwe_sub_inplace(&mut pt_want, &pt_have);
|
||||
|
||||
let noise_have: f64 = pt_want.data.std(base2k, 0).log2();
|
||||
let noise_have: f64 = pt_want.data.stats(base2k, 0).std().log2();
|
||||
let noise_want: f64 = ((((rank as f64) + 1.0) * n as f64 * 0.5 * SIGMA * SIGMA).sqrt()).log2() - (k_ct as f64);
|
||||
|
||||
assert!(
|
||||
|
||||
@@ -150,7 +150,7 @@ where
|
||||
|
||||
module.glwe_sub_inplace(&mut pt, &pt_want);
|
||||
|
||||
let noise_have: f64 = pt.std().log2();
|
||||
let noise_have: f64 = pt.stats().std().log2();
|
||||
|
||||
assert!(
|
||||
noise_have < -((k_ct - base2k) as f64),
|
||||
|
||||
@@ -123,7 +123,7 @@ where
|
||||
module.vec_znx_sub_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
|
||||
module.vec_znx_normalize_inplace(base2k, &mut pt_want.data, 0, scratch.borrow());
|
||||
|
||||
let noise_have: f64 = pt_want.std().log2();
|
||||
let noise_have: f64 = pt_want.stats().std().log2();
|
||||
|
||||
let mut noise_want: f64 = var_noise_gglwe_product(
|
||||
n as f64,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::layouts::{GLWEPlaintext, LWEInfos, LWEPlaintext, TorusPrecision};
|
||||
use poulpy_hal::layouts::{DataMut, DataRef};
|
||||
use poulpy_hal::layouts::{DataMut, DataRef, Stats};
|
||||
use rug::Float;
|
||||
|
||||
impl<D: DataMut> GLWEPlaintext<D> {
|
||||
@@ -29,8 +29,8 @@ impl<D: DataRef> GLWEPlaintext<D> {
|
||||
self.data.decode_vec_float(self.base2k().into(), 0, data);
|
||||
}
|
||||
|
||||
pub fn std(&self) -> f64 {
|
||||
self.data.std(self.base2k().into(), 0)
|
||||
pub fn stats(&self) -> Stats {
|
||||
self.data.stats(self.base2k().into(), 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user