arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/compressed/ggsw_ct.rs
poulpy-core/src/encryption/compressed/ggsw_ct.rsRaw
  1use poulpy_hal::{
  2    api::{
  3        DFT, IDFTConsume, ScratchAvailable, SvpApplyInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal,
  4        VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxFillUniform, VecZnxNormalize,
  5        VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace,
  6    },
  7    layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, ZnxZero},
  8    source::Source,
  9};
 10
 11use crate::{
 12    TakeGLWEPt,
 13    encryption::{SIGMA, glwe_encrypt_sk_internal},
 14    layouts::{GGSWCiphertext, Infos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretPrepared},
 15};
 16
 17impl GGSWCiphertextCompressed<Vec<u8>> {
 18    pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize
 19    where
 20        Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
 21    {
 22        GGSWCiphertext::encrypt_sk_scratch_space(module, basek, k, rank)
 23    }
 24}
 25
 26impl<DataSelf: DataMut> GGSWCiphertextCompressed<DataSelf> {
 27    #[allow(clippy::too_many_arguments)]
 28    pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
 29        &mut self,
 30        module: &Module<B>,
 31        pt: &ScalarZnx<DataPt>,
 32        sk: &GLWESecretPrepared<DataSk, B>,
 33        seed_xa: [u8; 32],
 34        source_xe: &mut Source,
 35        scratch: &mut Scratch<B>,
 36    ) where
 37        Module<B>: VecZnxAddScalarInplace
 38            + VecZnxDftAllocBytes
 39            + VecZnxBigNormalize<B>
 40            + DFT<B>
 41            + SvpApplyInplace<B>
 42            + IDFTConsume<B>
 43            + VecZnxNormalizeTmpBytes
 44            + VecZnxFillUniform
 45            + VecZnxSubABInplace
 46            + VecZnxAddInplace
 47            + VecZnxNormalizeInplace<B>
 48            + VecZnxAddNormal
 49            + VecZnxNormalize<B>
 50            + VecZnxSub,
 51        Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
 52    {
 53        #[cfg(debug_assertions)]
 54        {
 55            use poulpy_hal::layouts::ZnxInfos;
 56
 57            assert_eq!(self.rank(), sk.rank());
 58            assert_eq!(self.n(), sk.n());
 59            assert_eq!(pt.n(), sk.n());
 60        }
 61
 62        let basek: usize = self.basek();
 63        let k: usize = self.k();
 64        let rank: usize = self.rank();
 65        let cols: usize = rank + 1;
 66        let digits: usize = self.digits();
 67
 68        let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self.n(), basek, k);
 69
 70        let mut source = Source::new(seed_xa);
 71
 72        self.seed = vec![[0u8; 32]; self.rows() * cols];
 73
 74        (0..self.rows()).for_each(|row_i| {
 75            tmp_pt.data.zero();
 76
 77            // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
 78            module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (digits - 1) + row_i * digits, pt, 0);
 79            module.vec_znx_normalize_inplace(basek, &mut tmp_pt.data, 0, scratch_1);
 80
 81            (0..rank + 1).for_each(|col_j| {
 82                // rlwe encrypt of vec_znx_pt into vec_znx_ct
 83
 84                let (seed, mut source_xa_tmp) = source.branch();
 85
 86                self.seed[row_i * cols + col_j] = seed;
 87
 88                glwe_encrypt_sk_internal(
 89                    module,
 90                    self.basek(),
 91                    self.k(),
 92                    &mut self.at_mut(row_i, col_j).data,
 93                    cols,
 94                    true,
 95                    Some((&tmp_pt, col_j)),
 96                    sk,
 97                    &mut source_xa_tmp,
 98                    source_xe,
 99                    SIGMA,
100                    scratch_1,
101                );
102            });
103        });
104    }
105}