poulpy_core/encryption/compressed/
gglwe_ksk.rs1use poulpy_hal::{
2 api::{
3 ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft,
4 VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply,
5 VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
6 VecZnxSubInplace, VecZnxSwitchRing,
7 },
8 layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
9 source::Source,
10};
11
12use crate::{
13 TakeGLWESecretPrepared,
14 layouts::{
15 Degree, GGLWECiphertext, GGLWELayoutInfos, GLWEInfos, GLWESecret, LWEInfos, compressed::GGLWESwitchingKeyCompressed,
16 prepared::GLWESecretPrepared,
17 },
18};
19
20impl GGLWESwitchingKeyCompressed<Vec<u8>> {
21 pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
22 where
23 A: GGLWELayoutInfos,
24 Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes,
25 {
26 (GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1))
27 + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into())
28 + GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out())
29 }
30}
31
32impl<DataSelf: DataMut> GGLWESwitchingKeyCompressed<DataSelf> {
33 #[allow(clippy::too_many_arguments)]
34 pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
35 &mut self,
36 module: &Module<B>,
37 sk_in: &GLWESecret<DataSkIn>,
38 sk_out: &GLWESecret<DataSkOut>,
39 seed_xa: [u8; 32],
40 source_xe: &mut Source,
41 scratch: &mut Scratch<B>,
42 ) where
43 Module<B>: SvpPrepare<B>
44 + SvpPPolAllocBytes
45 + VecZnxSwitchRing
46 + VecZnxDftAllocBytes
47 + VecZnxBigNormalize<B>
48 + VecZnxDftApply<B>
49 + SvpApplyDftToDftInplace<B>
50 + VecZnxIdftApplyConsume<B>
51 + VecZnxNormalizeTmpBytes
52 + VecZnxFillUniform
53 + VecZnxSubInplace
54 + VecZnxAddInplace
55 + VecZnxNormalizeInplace<B>
56 + VecZnxAddNormal
57 + VecZnxNormalize<B>
58 + VecZnxSub
59 + VecZnxAddScalarInplace,
60 Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared<B>,
61 {
62 #[cfg(debug_assertions)]
63 {
64 use crate::layouts::GGLWESwitchingKey;
65
66 assert!(sk_in.n().0 <= module.n() as u32);
67 assert!(sk_out.n().0 <= module.n() as u32);
68 assert!(
69 scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(module, self),
70 "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
71 scratch.available(),
72 GGLWESwitchingKey::encrypt_sk_scratch_space(module, self)
73 )
74 }
75
76 let n: usize = sk_in.n().max(sk_out.n()).into();
77
78 let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(n, sk_in.rank().into());
79 (0..sk_in.rank().into()).for_each(|i| {
80 module.vec_znx_switch_ring(
81 &mut sk_in_tmp.as_vec_znx_mut(),
82 i,
83 &sk_in.data.as_vec_znx(),
84 i,
85 );
86 });
87
88 let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(Degree(n as u32), sk_out.rank());
89 {
90 let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1);
91 (0..sk_out.rank().into()).for_each(|i| {
92 module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
93 module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
94 });
95 }
96
97 self.key.encrypt_sk(
98 module,
99 &sk_in_tmp,
100 &sk_out_tmp,
101 seed_xa,
102 source_xe,
103 scratch_2,
104 );
105 self.sk_in_n = sk_in.n().into();
106 self.sk_out_n = sk_out.n().into();
107 }
108}