arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/gglwe_tsk.rs
poulpy-core/src/encryption/gglwe_tsk.rsRaw
  1use poulpy_hal::{
  2    api::{
  3        ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx,
  4        TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAllocBytes,
  5        VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA,
  6        VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing,
  7    },
  8    layouts::{Backend, DataMut, DataRef, Module, Scratch},
  9    source::Source,
 10};
 11
 12use crate::{
 13    TakeGLWESecret, TakeGLWESecretPrepared,
 14    layouts::{
 15        Degree, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared,
 16    },
 17};
 18
 19impl TensorKey<Vec<u8>> {
 20    pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
 21    where
 22        A: GGLWEInfos,
 23        Module<B>:
 24            SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes,
 25    {
 26        GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out())
 27            + module.vec_znx_dft_alloc_bytes(infos.rank_out().into(), 1)
 28            + module.vec_znx_big_alloc_bytes(1, 1)
 29            + module.vec_znx_dft_alloc_bytes(1, 1)
 30            + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1))
 31            + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos)
 32    }
 33}
 34
 35impl<DataSelf: DataMut> TensorKey<DataSelf> {
 36    pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
 37        &mut self,
 38        module: &Module<B>,
 39        sk: &GLWESecret<DataSk>,
 40        source_xa: &mut Source,
 41        source_xe: &mut Source,
 42        scratch: &mut Scratch<B>,
 43    ) where
 44        Module<B>: SvpApplyDftToDft<B>
 45            + VecZnxIdftApplyTmpA<B>
 46            + VecZnxAddScalarInplace
 47            + VecZnxDftAllocBytes
 48            + VecZnxBigNormalize<B>
 49            + VecZnxDftApply<B>
 50            + SvpApplyDftToDftInplace<B>
 51            + VecZnxIdftApplyConsume<B>
 52            + VecZnxNormalizeTmpBytes
 53            + VecZnxFillUniform
 54            + VecZnxSubInplace
 55            + VecZnxAddInplace
 56            + VecZnxNormalizeInplace<B>
 57            + VecZnxAddNormal
 58            + VecZnxNormalize<B>
 59            + VecZnxSub
 60            + SvpPrepare<B>
 61            + VecZnxSwitchRing
 62            + SvpPPolAllocBytes,
 63        Scratch<B>:
 64            TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared<B> + TakeVecZnxBig<B>,
 65    {
 66        #[cfg(debug_assertions)]
 67        {
 68            assert_eq!(self.rank_out(), sk.rank());
 69            assert_eq!(self.n(), sk.n());
 70        }
 71
 72        let n: Degree = sk.n();
 73        let rank: Rank = self.rank_out();
 74
 75        let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(n, rank);
 76        sk_dft_prep.prepare(module, sk, scratch_1);
 77
 78        let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(n.into(), rank.into(), 1);
 79
 80        (0..rank.into()).for_each(|i| {
 81            module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
 82        });
 83
 84        let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(n.into(), 1, 1);
 85        let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(n, Rank(1));
 86        let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(n.into(), 1, 1);
 87
 88        (0..rank.into()).for_each(|i| {
 89            (i..rank.into()).for_each(|j| {
 90                module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i);
 91
 92                module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
 93                module.vec_znx_big_normalize(
 94                    self.base2k().into(),
 95                    &mut sk_ij.data.as_vec_znx_mut(),
 96                    0,
 97                    self.base2k().into(),
 98                    &sk_ij_big,
 99                    0,
100                    scratch_5,
101                );
102
103                self.at_mut(i, j)
104                    .encrypt_sk(module, &sk_ij, sk, source_xa, source_xe, scratch_5);
105            });
106        })
107    }
108}