arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs
poulpy-core/src/encryption/lwe_to_glwe_ksk.rsRaw
 1use poulpy_hal::{
 2    api::{
 3        ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
 4        VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
 5        VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
 6        VecZnxSubInplace, VecZnxSwitchRing,
 7    },
 8    layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut},
 9    source::Source,
10};
11
12use crate::layouts::{Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank};
13
14impl LWEToGLWESwitchingKey<Vec<u8>> {
15    pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
16    where
17        A: GGLWEInfos,
18        Module<B>: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes,
19    {
20        debug_assert_eq!(
21            infos.rank_in(),
22            Rank(1),
23            "rank_in != 1 is not supported for LWEToGLWESwitchingKey"
24        );
25        GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(Degree(module.n() as u32), infos.rank_in())
26    }
27}
28
29impl<D: DataMut> LWEToGLWESwitchingKey<D> {
30    #[allow(clippy::too_many_arguments)]
31    pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
32        &mut self,
33        module: &Module<B>,
34        sk_lwe: &LWESecret<DLwe>,
35        sk_glwe: &GLWESecret<DGlwe>,
36        source_xa: &mut Source,
37        source_xe: &mut Source,
38        scratch: &mut Scratch<B>,
39    ) where
40        DLwe: DataRef,
41        DGlwe: DataRef,
42        Module<B>: VecZnxAutomorphismInplace<B>
43            + VecZnxAddScalarInplace
44            + VecZnxDftBytesOf
45            + VecZnxBigNormalize<B>
46            + VecZnxDftApply<B>
47            + SvpApplyDftToDftInplace<B>
48            + VecZnxIdftApplyConsume<B>
49            + VecZnxNormalizeTmpBytes
50            + VecZnxFillUniform
51            + VecZnxSubInplace
52            + VecZnxAddInplace
53            + VecZnxNormalizeInplace<B>
54            + VecZnxAddNormal
55            + VecZnxNormalize<B>
56            + VecZnxSub
57            + SvpPrepare<B>
58            + VecZnxSwitchRing
59            + SvpPPolBytesOf,
60        Scratch<B>: ScratchAvailable,
61    {
62        #[cfg(debug_assertions)]
63        {
64            use crate::layouts::LWEInfos;
65
66            assert!(sk_lwe.n().0 <= module.n() as u32);
67        }
68
69        let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(sk_glwe.n(), Rank(1));
70        sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
71        sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0);
72        module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
73
74        self.0.encrypt_sk(
75            module,
76            &sk_lwe_as_glwe,
77            sk_glwe,
78            source_xa,
79            source_xe,
80            scratch_1,
81        );
82    }
83}