Files
poulpy/poulpy-core/src/noise/glwe.rs
Jean-Philippe Bossuat 2559d8ea81 Fix #130 & #131 (#132)
* fix #131

* fix #130

* update CHANGELOG.md
2026-01-16 18:14:07 +01:00

59 lines
1.9 KiB
Rust

use poulpy_hal::layouts::{Backend, DataRef, Module, Scratch, Stats};
use crate::{
GLWENormalize, GLWESub, ScratchTakeCore,
decryption::GLWEDecrypt,
layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef},
};
impl<D: DataRef> GLWE<D> {
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> + GLWEInfos,
{
module.glwe_noise(self, pt_want, sk_prepared, scratch)
}
}
pub trait GLWENoise<BE: Backend> {
fn glwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GLWEInfos;
fn glwe_noise<R, P, S>(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
where
R: GLWEToRef + GLWEInfos,
P: GLWEToRef,
S: GLWESecretPreparedToRef<BE> + GLWEInfos;
}
impl<BE: Backend> GLWENoise<BE> for Module<BE>
where
Module<BE>: GLWEDecrypt<BE> + GLWESub + GLWENormalize<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn glwe_noise_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
GLWEPlaintext::bytes_of_from_infos(infos) + self.glwe_normalize_tmp_bytes().max(self.glwe_decrypt_tmp_bytes(infos))
}
fn glwe_noise<R, P, S>(&self, res: &R, pt_want: &P, sk_prepared: &S, scratch: &mut Scratch<BE>) -> Stats
where
R: GLWEToRef + GLWEInfos,
P: GLWEToRef,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
{
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)
}
}