poulpy_core/encryption/
glwe_to_lwe_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        ZnxZero,
8    },
9    layouts::{Backend, DataMut, DataRef, Module, Scratch},
10    source::Source,
11};
12
13use crate::{
14    TakeGLWESecret, TakeGLWESecretPrepared,
15    layouts::{GGLWESwitchingKey, GLWESecret, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretPrepared},
16};
17
18impl GLWEToLWESwitchingKey<Vec<u8>> {
19    pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_in: usize) -> usize
20    where
21        Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
22    {
23        GLWESecretPrepared::bytes_of(module, n, rank_in)
24            + (GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_in, 1) | GLWESecret::bytes_of(n, rank_in))
25    }
26}
27
28impl<D: DataMut> GLWEToLWESwitchingKey<D> {
29    #[allow(clippy::too_many_arguments)]
30    pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
31        &mut self,
32        module: &Module<B>,
33        sk_lwe: &LWESecret<DLwe>,
34        sk_glwe: &GLWESecret<DGlwe>,
35        source_xa: &mut Source,
36        source_xe: &mut Source,
37        sigma: f64,
38        scratch: &mut Scratch<B>,
39    ) where
40        DLwe: DataRef,
41        DGlwe: DataRef,
42        Module<B>: VecZnxAutomorphismInplace
43            + VecZnxAddScalarInplace
44            + VecZnxDftAllocBytes
45            + VecZnxBigNormalize<B>
46            + VecZnxDftFromVecZnx<B>
47            + SvpApplyInplace<B>
48            + VecZnxDftToVecZnxBigConsume<B>
49            + VecZnxNormalizeTmpBytes
50            + VecZnxFillUniform
51            + VecZnxSubABInplace
52            + VecZnxAddInplace
53            + VecZnxNormalizeInplace<B>
54            + VecZnxAddNormal
55            + VecZnxNormalize<B>
56            + VecZnxSub
57            + SvpPrepare<B>
58            + VecZnxSwithcDegree
59            + SvpPPolAllocBytes,
60        Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared<B>,
61    {
62        #[cfg(debug_assertions)]
63        {
64            assert!(sk_lwe.n() <= module.n());
65        }
66
67        let (mut sk_lwe_as_glwe, scratch1) = scratch.take_glwe_secret(sk_glwe.n(), 1);
68        sk_lwe_as_glwe.data.zero();
69        sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n()].copy_from_slice(sk_lwe.data.at(0, 0));
70        module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0);
71
72        self.0.encrypt_sk(
73            module,
74            sk_glwe,
75            &sk_lwe_as_glwe,
76            source_xa,
77            source_xe,
78            sigma,
79            scratch1,
80        );
81    }
82}