poulpy_core/encryption/
lwe_to_glwe_ksk.rs

1use poulpy_hal::{
2    api::{
3        ScratchAvailable, SvpApplyInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft,
4        VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize,
5        VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize,
6        VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, ZnxView, ZnxViewMut,
7    },
8    layouts::{Backend, DataMut, DataRef, Module, Scratch},
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>, n: usize, 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, n, basek, k, 1, rank_out) + GLWESecret::bytes_of(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        sigma: f64,
36        scratch: &mut Scratch<B>,
37    ) where
38        DLwe: DataRef,
39        DGlwe: DataRef,
40        Module<B>: VecZnxAutomorphismInplace
41            + VecZnxAddScalarInplace
42            + VecZnxDftAllocBytes
43            + VecZnxBigNormalize<B>
44            + VecZnxDftFromVecZnx<B>
45            + SvpApplyInplace<B>
46            + VecZnxDftToVecZnxBigConsume<B>
47            + VecZnxNormalizeTmpBytes
48            + VecZnxFillUniform
49            + VecZnxSubABInplace
50            + VecZnxAddInplace
51            + VecZnxNormalizeInplace<B>
52            + VecZnxAddNormal
53            + VecZnxNormalize<B>
54            + VecZnxSub
55            + SvpPrepare<B>
56            + VecZnxSwithcDegree
57            + SvpPPolAllocBytes,
58        Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared<B>,
59    {
60        #[cfg(debug_assertions)]
61        {
62            assert!(sk_lwe.n() <= module.n());
63        }
64
65        let (mut sk_lwe_as_glwe, scratch1) = scratch.take_glwe_secret(sk_glwe.n(), 1);
66        sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n()].copy_from_slice(sk_lwe.data.at(0, 0));
67        sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n()..].fill(0);
68        module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0);
69
70        self.0.encrypt_sk(
71            module,
72            &sk_lwe_as_glwe,
73            sk_glwe,
74            source_xa,
75            source_xe,
76            sigma,
77            scratch1,
78        );
79    }
80}