arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/lwe_ct.rs
poulpy-core/src/encryption/lwe_ct.rsRaw
  1use poulpy_hal::{
  2    api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace},
  3    layouts::{Backend, DataMut, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut},
  4    oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
  5    source::Source,
  6};
  7
  8use crate::{
  9    encryption::{SIGMA, SIGMA_BOUND},
 10    layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToRef, LWESecretToRef},
 11};
 12
 13impl<DataSelf: DataMut> LWE<DataSelf> {
 14
 15    pub fn encrypt_sk<P, S, M, BE: Backend>(&mut self, module: &M, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
 16    where
 17        P: LWEPlaintextToRef,
 18        S: LWESecretToRef,
 19        M: LWEEncryptSk<BE>,
 20        BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
 21    {
 22        module.lwe_encrypt_sk(self, pt, sk, source_xa, source_xe);
 23    }
 24}
 25
 26
 27pub trait LWEEncryptSk<BE: Backend>
 28where
 29    Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>,
 30{
 31    fn lwe_encrypt_sk<R, P, S>(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
 32    where
 33        R: LWEToMut,
 34        P: LWEPlaintextToRef,
 35        S: LWESecretToRef,
 36        BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE>,
 37    {
 38        let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
 39        let pt: &LWEPlaintext<&[u8]> = &pt.to_ref();
 40        let sk: &LWESecret<&[u8]> = &sk.to_ref();
 41
 42        #[cfg(debug_assertions)]
 43        {
 44            assert_eq!(res.n(), sk.n())
 45        }
 46
 47        let base2k: usize = res.base2k().into();
 48        let k: usize = res.k().into();
 49
 50        self.zn_fill_uniform((res.n() + 1).into(), base2k, &mut res.data, 0, source_xa);
 51
 52        let mut tmp_znx: Zn<Vec<u8>> = Zn::alloc(1, 1, res.size());
 53
 54        let min_size = res.size().min(pt.size());
 55
 56        (0..min_size).for_each(|i| {
 57            tmp_znx.at_mut(0, i)[0] = pt.data.at(0, i)[0]
 58                - res.data.at(0, i)[1..]
 59                    .iter()
 60                    .zip(sk.data.at(0, 0))
 61                    .map(|(x, y)| x * y)
 62                    .sum::<i64>();
 63        });
 64
 65        (min_size..res.size()).for_each(|i| {
 66            tmp_znx.at_mut(0, i)[0] -= res.data.at(0, i)[1..]
 67                .iter()
 68                .zip(sk.data.at(0, 0))
 69                .map(|(x, y)| x * y)
 70                .sum::<i64>();
 71        });
 72
 73        self.zn_add_normal(
 74            1,
 75            base2k,
 76            &mut res.data,
 77            0,
 78            k,
 79            source_xe,
 80            SIGMA,
 81            SIGMA_BOUND,
 82        );
 83
 84        self.zn_normalize_inplace(
 85            1,
 86            base2k,
 87            &mut tmp_znx,
 88            0,
 89            ScratchOwned::alloc(size_of::<i64>()).borrow(),
 90        );
 91
 92        (0..res.size()).for_each(|i| {
 93            res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0];
 94        });        
 95    }
 96}
 97
 98impl<BE: Backend> LWEEncryptSk<BE> for Module<BE> where
 99    Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace<BE>,
100{
101}