arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/ggsw.rs
poulpy-core/src/encryption/ggsw.rsRaw
  1use poulpy_hal::{
  2    api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes},
  3    layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero},
  4    source::Source,
  5};
  6
  7use crate::{
  8    GLWEEncryptSk, GLWEEncryptSkInternal, SIGMA, ScratchTakeCore,
  9    layouts::{
 10        GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintext, LWEInfos,
 11        prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
 12    },
 13};
 14
 15impl GGSW<Vec<u8>> {
 16    pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
 17    where
 18        A: GGSWInfos,
 19        M: GGSWEncryptSk<BE>,
 20    {
 21        module.ggsw_encrypt_sk_tmp_bytes(infos)
 22    }
 23}
 24
 25impl<D: DataMut> GGSW<D> {
 26    #[allow(clippy::too_many_arguments)]
 27    pub fn encrypt_sk<P, S, M, BE: Backend>(
 28        &mut self,
 29        module: &M,
 30        pt: &P,
 31        sk: &S,
 32        source_xa: &mut Source,
 33        source_xe: &mut Source,
 34        scratch: &mut Scratch<BE>,
 35    ) where
 36        P: ScalarZnxToRef,
 37        S: GLWESecretPreparedToRef<BE>,
 38        M: GGSWEncryptSk<BE>,
 39        Scratch<BE>: ScratchTakeCore<BE>,
 40    {
 41        module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
 42    }
 43}
 44
 45pub trait GGSWEncryptSk<BE: Backend> {
 46    fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
 47    where
 48        A: GGSWInfos;
 49
 50    fn ggsw_encrypt_sk<R, P, S>(
 51        &self,
 52        res: &mut R,
 53        pt: &P,
 54        sk: &S,
 55        source_xa: &mut Source,
 56        source_xe: &mut Source,
 57        scratch: &mut Scratch<BE>,
 58    ) where
 59        R: GGSWToMut,
 60        P: ScalarZnxToRef,
 61        S: GLWESecretPreparedToRef<BE>;
 62}
 63
 64impl<BE: Backend> GGSWEncryptSk<BE> for Module<BE>
 65where
 66    Self: ModuleN
 67        + GLWEEncryptSkInternal<BE>
 68        + GLWEEncryptSk<BE>
 69        + VecZnxDftBytesOf
 70        + VecZnxNormalizeInplace<BE>
 71        + VecZnxAddScalarInplace
 72        + VecZnxNormalizeTmpBytes,
 73    Scratch<BE>: ScratchTakeCore<BE>,
 74{
 75    fn ggsw_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
 76    where
 77        A: GGSWInfos,
 78    {
 79        self.glwe_encrypt_sk_tmp_bytes(infos).max(self.vec_znx_normalize_tmp_bytes()) + GLWEPlaintext::bytes_of_from_infos(infos)
 80    }
 81
 82    fn ggsw_encrypt_sk<R, P, S>(
 83        &self,
 84        res: &mut R,
 85        pt: &P,
 86        sk: &S,
 87        source_xa: &mut Source,
 88        source_xe: &mut Source,
 89        scratch: &mut Scratch<BE>,
 90    ) where
 91        R: GGSWToMut,
 92        P: ScalarZnxToRef,
 93        S: GLWESecretPreparedToRef<BE>,
 94    {
 95        let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
 96        let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
 97        let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
 98
 99        assert_eq!(res.rank(), sk.rank());
100        assert_eq!(res.n(), self.n() as u32);
101        assert_eq!(pt.n(), self.n());
102        assert_eq!(sk.n(), self.n() as u32);
103
104        let k: usize = res.k().into();
105        let base2k: usize = res.base2k().into();
106        let rank: usize = res.rank().into();
107        let dsize: usize = res.dsize().into();
108        let cols: usize = rank + 1;
109
110        let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(res);
111
112        for row_i in 0..res.dnum().into() {
113            tmp_pt.data.zero();
114            // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
115            self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0);
116            self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1);
117            for col_j in 0..rank + 1 {
118                self.glwe_encrypt_sk_internal(
119                    base2k,
120                    k,
121                    res.at_mut(row_i, col_j).data_mut(),
122                    cols,
123                    false,
124                    Some((&tmp_pt, col_j)),
125                    sk,
126                    source_xa,
127                    source_xe,
128                    SIGMA,
129                    scratch_1,
130                );
131            }
132        }
133    }
134}