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