arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/encryption/lwe_ksk.rs
core/src/encryption/lwe_ksk.rsRaw
 1use backend::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};
10use sampling::source::Source;
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>, n: usize, basek: usize, k: usize) -> usize
19    where
20        Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
21    {
22        GLWESecret::bytes_of(n, 1)
23            + GLWESecretPrepared::bytes_of(module, n, 1)
24            + GGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, 1, 1)
25    }
26}
27
28impl<D: DataMut> LWESwitchingKey<D> {
29    pub fn encrypt_sk<DIn, DOut, B: Backend>(
30        &mut self,
31        module: &Module<B>,
32        sk_lwe_in: &LWESecret<DIn>,
33        sk_lwe_out: &LWESecret<DOut>,
34        source_xa: &mut Source,
35        source_xe: &mut Source,
36        sigma: f64,
37        scratch: &mut Scratch<B>,
38    ) where
39        DIn: DataRef,
40        DOut: DataRef,
41        Module<B>: VecZnxAutomorphismInplace
42            + VecZnxAddScalarInplace
43            + VecZnxDftAllocBytes
44            + VecZnxBigNormalize<B>
45            + VecZnxDftFromVecZnx<B>
46            + SvpApplyInplace<B>
47            + VecZnxDftToVecZnxBigConsume<B>
48            + VecZnxNormalizeTmpBytes
49            + VecZnxFillUniform
50            + VecZnxSubABInplace
51            + VecZnxAddInplace
52            + VecZnxNormalizeInplace<B>
53            + VecZnxAddNormal
54            + VecZnxNormalize<B>
55            + VecZnxSub
56            + SvpPrepare<B>
57            + VecZnxSwithcDegree
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, scratch1) = scratch.take_glwe_secret(self.n(), 1);
69        let (mut sk_out_glwe, scratch2) = scratch1.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);
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);
78
79        self.0.encrypt_sk(
80            module,
81            &sk_in_glwe,
82            &sk_out_glwe,
83            source_xa,
84            source_xe,
85            sigma,
86            scratch2,
87        );
88    }
89}