poulpy_core/encryption/
lwe_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, Infos, LWESecret, LWESwitchingKey, prepared::GLWESecretPrepared},
15};
16
17impl LWESwitchingKey<Vec<u8>> {
18    pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize
19    where
20        Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
21    {
22        GLWESecret::bytes_of(module.n(), 1)
23            + GLWESecretPrepared::bytes_of(module, 1)
24            + GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, 1, 1)
25    }
26}
27
28impl<D: DataMut> LWESwitchingKey<D> {
29    #[allow(clippy::too_many_arguments)]
30    pub fn encrypt_sk<DIn, DOut, B: Backend>(
31        &mut self,
32        module: &Module<B>,
33        sk_lwe_in: &LWESecret<DIn>,
34        sk_lwe_out: &LWESecret<DOut>,
35        source_xa: &mut Source,
36        source_xe: &mut Source,
37        scratch: &mut Scratch<B>,
38    ) where
39        DIn: DataRef,
40        DOut: DataRef,
41        Module<B>: VecZnxAutomorphismInplace<B>
42            + VecZnxAddScalarInplace
43            + VecZnxDftAllocBytes
44            + VecZnxBigNormalize<B>
45            + VecZnxDftApply<B>
46            + SvpApplyDftToDftInplace<B>
47            + VecZnxIdftApplyConsume<B>
48            + VecZnxNormalizeTmpBytes
49            + VecZnxFillUniform
50            + VecZnxSubABInplace
51            + VecZnxAddInplace
52            + VecZnxNormalizeInplace<B>
53            + VecZnxAddNormal
54            + VecZnxNormalize<B>
55            + VecZnxSub
56            + SvpPrepare<B>
57            + VecZnxSwitchRing
58            + SvpPPolAllocBytes,
59        Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared<B>,
60    {
61        #[cfg(debug_assertions)]
62        {
63            assert!(sk_lwe_in.n() <= self.n());
64            assert!(sk_lwe_out.n() <= self.n());
65            assert!(self.n() <= module.n());
66        }
67
68        let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self.n(), 1);
69        let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n(), 1);
70
71        sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n()].copy_from_slice(sk_lwe_out.data.at(0, 0));
72        sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n()..].fill(0);
73        module.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2);
74
75        sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n()].copy_from_slice(sk_lwe_in.data.at(0, 0));
76        sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n()..].fill(0);
77        module.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2);
78
79        self.0.encrypt_sk(
80            module,
81            &sk_in_glwe,
82            &sk_out_glwe,
83            source_xa,
84            source_xe,
85            scratch_2,
86        );
87    }
88}