arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/noise/glwe_ct.rs
poulpy-core/src/noise/glwe_ct.rsRaw
  1use poulpy_hal::{
  2    api::{
  3        ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
  4        VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace,
  5        VecZnxNormalizeTmpBytes, VecZnxSubInplace, ScratchTakeBasic,
  6    },
  7    layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},
  8    oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
  9};
 10
 11use crate::{
 12    decryption::GLWEDecryption,
 13    layouts::{
 14        GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos,
 15        prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
 16    },
 17};
 18
 19impl<D: DataRef> GLWE<D> {
 20    pub fn noise<M, S, P, BE: Backend>(&self, module: &M, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
 21    where
 22        M: GLWENoise<BE>,
 23        S: GLWESecretPreparedToRef<BE>,
 24        P: GLWEPlaintextToRef,
 25    {
 26        module.glwe_noise(self, sk_prepared, pt_want, scratch)
 27    }
 28    // pub fn noise<B, DataSk, DataPt>(
 29    //     &self,
 30    //     module: &Module<B>,
 31    //     sk_prepared: &GLWESecretPrepared<DataSk, B>,
 32    //     pt_want: &GLWEPlaintext<DataPt>,
 33    //     scratch: &mut Scratch<B>,
 34    // ) -> f64
 35    // where
 36    //     DataSk: DataRef,
 37    //     DataPt: DataRef,
 38    //     B: Backend,
 39    //     Module<B>: VecZnxDftApply<B>
 40    //         + VecZnxSubInplace
 41    //         + VecZnxNormalizeInplace<B>
 42    //         + SvpApplyDftToDftInplace<B>
 43    //         + VecZnxIdftApplyConsume<B>
 44    //         + VecZnxBigAddInplace<B>
 45    //         + VecZnxBigAddSmallInplace<B>
 46    //         + VecZnxBigNormalize<B>,
 47    //     Scratch<B>:,
 48    // {
 49    //     let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, self);
 50    //     self.decrypt(module, &mut pt_have, sk_prepared, scratch);
 51    //     module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
 52    //     module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch);
 53    //     pt_have.data.std(self.base2k().into(), 0).log2()
 54    // }
 55
 56    pub fn assert_noise<M, BE, DataSk, DataPt>(
 57        &self,
 58        module: &M,
 59        sk_prepared: &GLWESecretPrepared<DataSk, BE>,
 60        pt_want: &GLWEPlaintext<DataPt>,
 61        max_noise: f64,
 62    ) where
 63        DataSk: DataRef,
 64        DataPt: DataRef,
 65        M: GLWENoise<BE>,
 66        BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
 67    {
 68        module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise);
 69    }
 70
 71    // pub fn assert_noise<B, DataSk, DataPt>(
 72    //     &self,
 73    //     module: &Module<B>,
 74    //     sk_prepared: &GLWESecretPrepared<DataSk, B>,
 75    //     pt_want: &GLWEPlaintext<DataPt>,
 76    //     max_noise: f64,
 77    // ) where
 78    //     DataSk: DataRef,
 79    //     DataPt: DataRef,
 80    //     Module<B>: VecZnxDftBytesOf
 81    //         + VecZnxBigBytesOf
 82    //         + VecZnxDftApply<B>
 83    //         + SvpApplyDftToDftInplace<B>
 84    //         + VecZnxIdftApplyConsume<B>
 85    //         + VecZnxBigAddInplace<B>
 86    //         + VecZnxBigAddSmallInplace<B>
 87    //         + VecZnxBigNormalize<B>
 88    //         + VecZnxNormalizeTmpBytes
 89    //         + VecZnxSubInplace
 90    //         + VecZnxNormalizeInplace<B>,
 91    //     B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
 92    // {
 93    //     let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self));
 94    //     let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow());
 95    //     assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
 96    // }
 97}
 98
 99
100pub trait GLWENoise<BE: Backend> {
101    fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
102    where
103        R: GLWEToRef,
104        S: GLWESecretPreparedToRef<BE>,
105        P: GLWEPlaintextToRef;
106
107    fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
108    where
109        R: GLWEToRef,
110        S: GLWESecretPreparedToRef<BE>,
111        P: GLWEPlaintextToRef,
112        BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>;
113}
114
115impl<BE: Backend> GLWENoise<BE> for Module<BE>
116where
117    Module<BE>: VecZnxDftBytesOf
118        + VecZnxBigBytesOf
119        + VecZnxDftApply<BE>
120        + SvpApplyDftToDftInplace<BE>
121        + VecZnxIdftApplyConsume<BE>
122        + VecZnxBigAddInplace<BE>
123        + VecZnxBigAddSmallInplace<BE>
124        + VecZnxBigNormalize<BE>
125        + VecZnxNormalizeTmpBytes
126        + VecZnxSubInplace
127        + VecZnxNormalizeInplace<BE>
128        + GLWEDecryption<BE>,
129    Scratch<BE>: ScratchTakeBasic + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
130{
131    fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
132    where
133        R: GLWEToRef,
134        S: GLWESecretPreparedToRef<BE>,
135        P: GLWEPlaintextToRef,
136    {
137        let res_ref: &GLWE<&[u8]> = &res.to_ref();
138
139        let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.to_ref();
140
141        let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self, res_ref);
142        self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch);
143        self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
144        self.vec_znx_normalize_inplace(res_ref.base2k().into(), &mut pt_have.data, 0, scratch);
145        pt_have.data.std(res_ref.base2k().into(), 0).log2()
146    }
147
148    fn glwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
149    where
150        R: GLWEToRef,
151        S: GLWESecretPreparedToRef<BE>,
152        P: GLWEPlaintextToRef,
153        BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
154    {
155        let res: &GLWE<&[u8]> = &res.to_ref();
156        let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res));
157        let noise_have: f64 = self.glwe_noise(res, sk_prepared, pt_want, scratch.borrow());
158        assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
159    }
160}