poulpy_core/encryption/
lwe_to_glwe_ksk.rs

1use poulpy_hal::{
2    api::{
3        ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft,
4        VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize,
5        VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace,
6        VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace, VecZnxSwitchRing,
7    },
8    layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut},
9    source::Source,
10};
11
12use crate::{
13    TakeGLWESecret, TakeGLWESecretPrepared,
14    layouts::{GGLWESwitchingKey, GLWESecret, LWESecret, LWEToGLWESwitchingKey},
15};
16
17impl LWEToGLWESwitchingKey<Vec<u8>> {
18    pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank_out: usize) -> usize
19    where
20        Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
21    {
22        GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, 1, rank_out) + GLWESecret::bytes_of(module.n(), 1)
23    }
24}
25
26impl<D: DataMut> LWEToGLWESwitchingKey<D> {
27    #[allow(clippy::too_many_arguments)]
28    pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
29        &mut self,
30        module: &Module<B>,
31        sk_lwe: &LWESecret<DLwe>,
32        sk_glwe: &GLWESecret<DGlwe>,
33        source_xa: &mut Source,
34        source_xe: &mut Source,
35        scratch: &mut Scratch<B>,
36    ) where
37        DLwe: DataRef,
38        DGlwe: DataRef,
39        Module<B>: VecZnxAutomorphismInplace<B>
40            + VecZnxAddScalarInplace
41            + VecZnxDftAllocBytes
42            + VecZnxBigNormalize<B>
43            + VecZnxDftApply<B>
44            + SvpApplyDftToDftInplace<B>
45            + VecZnxIdftApplyConsume<B>
46            + VecZnxNormalizeTmpBytes
47            + VecZnxFillUniform
48            + VecZnxSubABInplace
49            + VecZnxAddInplace
50            + VecZnxNormalizeInplace<B>
51            + VecZnxAddNormal
52            + VecZnxNormalize<B>
53            + VecZnxSub
54            + SvpPrepare<B>
55            + VecZnxSwitchRing
56            + SvpPPolAllocBytes,
57        Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared<B>,
58    {
59        #[cfg(debug_assertions)]
60        {
61            assert!(sk_lwe.n() <= module.n());
62        }
63
64        let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(sk_glwe.n(), 1);
65        sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n()].copy_from_slice(sk_lwe.data.at(0, 0));
66        sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n()..].fill(0);
67        module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
68
69        self.0.encrypt_sk(
70            module,
71            &sk_lwe_as_glwe,
72            sk_glwe,
73            source_xa,
74            source_xe,
75            scratch_1,
76        );
77    }
78}