poulpy_core/encryption/
glwe_to_lwe_ksk.rs1use 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, ZnxZero},
9 source::Source,
10};
11
12use crate::{
13 TakeGLWESecret, TakeGLWESecretPrepared,
14 layouts::{GGLWESwitchingKey, GLWESecret, GLWEToLWESwitchingKey, LWESecret, prepared::GLWESecretPrepared},
15};
16
17impl GLWEToLWESwitchingKey<Vec<u8>> {
18 pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize, rank_in: usize) -> usize
19 where
20 Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
21 {
22 GLWESecretPrepared::bytes_of(module, rank_in)
23 + (GGLWESwitchingKey::encrypt_sk_scratch_space(module, basek, k, rank_in, 1)
24 | GLWESecret::bytes_of(module.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 scratch: &mut Scratch<B>,
38 ) where
39 DLwe: DataRef,
40 DGlwe: 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.n() <= module.n());
64 }
65
66 let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(sk_glwe.n(), 1);
67 sk_lwe_as_glwe.data.zero();
68 sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n()].copy_from_slice(sk_lwe.data.at(0, 0));
69 module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
70
71 self.0.encrypt_sk(
72 module,
73 sk_glwe,
74 &sk_lwe_as_glwe,
75 source_xa,
76 source_xe,
77 scratch_1,
78 );
79 }
80}