arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/noise/gglwe_ct.rs
poulpy-core/src/noise/gglwe_ct.rsRaw
  1use poulpy_hal::{
  2    api::{
  3        ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace,
  4    },
  5    layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ScalarZnx, ScalarZnxToRef, ZnxZero},
  6    oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl},
  7};
  8
  9use crate::layouts::{
 10    GGLWE, GGLWEToRef, GGLWEInfos, GLWEPlaintext, LWEInfos,
 11    prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
 12};
 13use crate::decryption::GLWEDecryption;
 14
 15impl<D: DataRef> GGLWE<D> {
 16
 17    pub fn assert_noise<M, BE, DataSk, DataWant>(
 18        &self,
 19        module: &M,
 20        sk_prepared: &GLWESecretPrepared<DataSk, BE>,
 21        pt_want: &ScalarZnx<DataWant>,
 22        max_noise: f64,
 23    ) where
 24        DataSk: DataRef,
 25        DataWant: DataRef,
 26        M: GGLWENoise<BE>,
 27        BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
 28    {
 29        module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
 30    }
 31
 32
 33    // pub fn assert_noise<B, DataSk, DataWant>(
 34    //     &self,
 35    //     module: &Module<B>,
 36    //     sk: &GLWESecretPrepared<DataSk, B>,
 37    //     pt_want: &ScalarZnx<DataWant>,
 38    //     max_noise: f64,
 39    // ) where
 40    //     DataSk: DataRef,
 41    //     DataWant: DataRef,
 42    //     Module<B>: VecZnxDftBytesOf
 43    //         + VecZnxBigBytesOf
 44    //         + VecZnxDftApply<B>
 45    //         + SvpApplyDftToDftInplace<B>
 46    //         + VecZnxIdftApplyConsume<B>
 47    //         + VecZnxBigAddInplace<B>
 48    //         + VecZnxBigAddSmallInplace<B>
 49    //         + VecZnxBigNormalize<B>
 50    //         + VecZnxNormalizeTmpBytes
 51    //         + VecZnxSubScalarInplace,
 52    //     B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
 53    // {
 54    //     let dsize: usize = self.dsize().into();
 55    //     let base2k: usize = self.base2k().into();
 56
 57    //     let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self));
 58    //     let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
 59
 60    //     (0..self.rank_in().into()).for_each(|col_i| {
 61    //         (0..self.dnum().into()).for_each(|row_i| {
 62    //             self.at(row_i, col_i)
 63    //                 .decrypt(module, &mut pt, sk, scratch.borrow());
 64
 65    //             module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
 66
 67    //             let noise_have: f64 = pt.data.std(base2k, 0).log2();
 68
 69    //             println!("noise_have: {noise_have}");
 70
 71    //             assert!(
 72    //                 noise_have <= max_noise,
 73    //                 "noise_have: {noise_have} > max_noise: {max_noise}"
 74    //             );
 75
 76    //             pt.data.zero();
 77    //         });
 78    //     });
 79    // }
 80}
 81
 82
 83pub trait GGLWENoise<BE: Backend> {
 84    fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
 85    where
 86        R: GGLWEToRef,
 87        S: GLWESecretPreparedToRef<BE>,
 88        P: ScalarZnxToRef,
 89        BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>;
 90}
 91
 92impl<BE: Backend> GGLWENoise<BE> for Module<BE> 
 93where
 94    Module<BE>: GLWEDecryption<BE>,
 95    Scratch<BE>: ScratchTakeBasic + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
 96{
 97    fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
 98    where
 99        R: GGLWEToRef,
100        S: GLWESecretPreparedToRef<BE>,
101        P: ScalarZnxToRef,
102        BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
103    {
104
105        let res: &GGLWE<&[u8]> = &res.to_ref();
106        
107        let dsize: usize = res.dsize().into();
108        let base2k: usize = res.base2k().into();
109
110        let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));
111        let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res);
112
113        (0..res.rank_in().into()).for_each(|col_i| {
114            (0..res.dnum().into()).for_each(|row_i| {
115                self.glwe_decrypt(&res.at(row_i, col_i), &mut pt, sk_prepared, scratch.borrow());
116
117                self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
118
119                let noise_have: f64 = pt.data.std(base2k, 0).log2();
120
121                println!("noise_have: {noise_have}");
122
123                assert!(
124                    noise_have <= max_noise,
125                    "noise_have: {noise_have} > max_noise: {max_noise}"
126                );
127
128                pt.data.zero();
129            });
130        });
131    }
132}