poulpy/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs
poulpy-core/src/encryption/glwe_to_lwe_ksk.rsRaw
1use poulpy_hal::{
2 api::{
3 ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
4 VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
5 VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
6 VecZnxSubInplace, VecZnxSwitchRing,
7 ScratchTakeBasic
8 },
9 layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero},
10 oep::{ScratchAvailableImpl, VecZnxFillUniformImpl},
11 source::Source
12};
13
14use crate::{
15 ScratchTakeCore,
16 layouts::{
17 GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, GLWEToLWESwitchingKeyToMut,
18 prepared::GLWESecretPrepared,
19 },
20 encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
21};
22
23impl GLWEToLWESwitchingKey<Vec<u8>> {
24 pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
25 where
26 A: GGLWEInfos,
27 M: GLWEToLWESwitchingKeyEncrypt<BE>,
28 {
29 module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos)
30 }
31 // pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
32 // where
33 // A: GGLWEInfos,
34 // Module<B>: ModuleN + SvpPPolBytesOf + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes,
35 // {
36 // GLWESecretPrepared::bytes_of(module, infos.rank_in())
37 // + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(module, infos.rank_in()))
38 // }
39}
40
41impl<D: DataMut> GLWEToLWESwitchingKey<D> {
42 pub fn encrypt_sk<M, DLwe, DGlwe, BE: Backend>(
43 &mut self,
44 module: &M,
45 sk_lwe: &LWESecret<DLwe>,
46 sk_glwe: &GLWESecret<DGlwe>,
47 source_xa: &mut Source,
48 source_xe: &mut Source,
49 scratch: &mut Scratch<BE>,
50 ) where
51 M: GLWEToLWESwitchingKeyEncrypt<BE>,
52 DLwe: DataRef,
53 DGlwe: DataRef,
54 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
55 {
56 module.glwe_to_lwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch);
57 }
58 // #[allow(clippy::too_many_arguments)]
59 // pub fn encrypt_sk<DLwe, DGlwe, B: Backend>(
60 // &mut self,
61 // module: &Module<B>,
62 // sk_lwe: &LWESecret<DLwe>,
63 // sk_glwe: &GLWESecret<DGlwe>,
64 // source_xa: &mut Source,
65 // source_xe: &mut Source,
66 // scratch: &mut Scratch<B>,
67 // ) where
68 // DLwe: DataRef,
69 // DGlwe: DataRef,
70 // Module<B>: ModuleN
71 // + VecZnxAutomorphismInplace<B>
72 // + VecZnxAddScalarInplace
73 // + VecZnxDftBytesOf
74 // + VecZnxBigNormalize<B>
75 // + VecZnxDftApply<B>
76 // + SvpApplyDftToDftInplace<B>
77 // + VecZnxIdftApplyConsume<B>
78 // + VecZnxNormalizeTmpBytes
79 // + VecZnxFillUniform
80 // + VecZnxSubInplace
81 // + VecZnxAddInplace
82 // + VecZnxNormalizeInplace<B>
83 // + VecZnxAddNormal
84 // + VecZnxNormalize<B>
85 // + VecZnxSub
86 // + SvpPrepare<B>
87 // + VecZnxSwitchRing
88 // + SvpPPolBytesOf
89 // + SvpPPolAlloc<B>,
90 // Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
91 // {
92 // #[cfg(debug_assertions)]
93 // {
94 // assert!(sk_lwe.n().0 <= module.n() as u32);
95 // }
96
97 // let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1));
98 // sk_lwe_as_glwe.data.zero();
99 // sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
100 // module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
101
102 // self.0.encrypt_sk(
103 // module,
104 // sk_glwe,
105 // &sk_lwe_as_glwe,
106 // source_xa,
107 // source_xe,
108 // scratch_1,
109 // );
110 // }
111}
112
113pub trait GLWEToLWESwitchingKeyEncrypt<BE: Backend>
114where
115 Self: Sized
116 + ModuleN
117 + SvpPPolBytesOf
118 + SvpPPolAlloc<BE>
119 + VecZnxNormalizeTmpBytes
120 + VecZnxDftBytesOf
121 + VecZnxAutomorphismInplace<BE>
122 + VecZnxAddScalarInplace
123 + VecZnxBigNormalize<BE>
124 + VecZnxDftApply<BE>
125 + SvpApplyDftToDftInplace<BE>
126 + VecZnxIdftApplyConsume<BE>
127 + VecZnxNormalize<BE>
128 + VecZnxNormalizeInplace<BE>
129 + VecZnxAddNormal
130 + VecZnxSub
131 + VecZnxSubInplace
132 + VecZnxAddInplace
133 + SvpPrepare<BE>
134 + VecZnxSwitchRing
135 + ScratchAvailable
136 + ScratchAvailableImpl<BE>
137 + VecZnxFillUniform
138 + VecZnxFillUniformImpl<BE>
139{
140 fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
141 where
142 A: GGLWEInfos;
143
144 fn glwe_to_lwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
145 &self,
146 res: &mut R,
147 sk_lwe: &LWESecret<DLwe>,
148 sk_glwe: &GLWESecret<DataGlwe>,
149 source_xa: &mut Source,
150 source_xe: &mut Source,
151 scratch: &mut Scratch<BE>,
152 ) where
153 R: GLWEToLWESwitchingKeyToMut,
154 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>;
155}
156
157impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE> where
158 Module<BE>: ModuleN
159 + SvpPPolBytesOf
160 + SvpPPolAlloc<BE>
161 + VecZnxNormalizeTmpBytes
162 + VecZnxDftBytesOf
163 + VecZnxAutomorphismInplace<BE>
164 + VecZnxAddScalarInplace
165 + VecZnxBigNormalize<BE>
166 + VecZnxDftApply<BE>
167 + SvpApplyDftToDftInplace<BE>
168 + VecZnxIdftApplyConsume<BE>
169 + VecZnxNormalize<BE>
170 + VecZnxNormalizeInplace<BE>
171 + VecZnxAddNormal
172 + VecZnxSub
173 + VecZnxSubInplace
174 + VecZnxAddInplace
175 + SvpPrepare<BE>
176 + VecZnxSwitchRing
177 + ScratchAvailable
178 + ScratchAvailableImpl<BE>
179 + ScratchTakeBasic
180 + ScratchTakeCore<BE>
181 + VecZnxFillUniform
182 + VecZnxFillUniformImpl<BE>
183 + GLWESwitchingKeyEncryptSk<BE>
184{
185 fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
186 where
187 A: GGLWEInfos
188 {
189 GLWESecretPrepared::bytes_of(self, infos.rank_in())
190 + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in()))
191 }
192
193 fn glwe_to_lwe_switching_key_encrypt_sk<R, DLwe: DataRef, DataGlwe: DataRef>(
194 &self,
195 res: &mut R,
196 sk_lwe: &LWESecret<DLwe>,
197 sk_glwe: &GLWESecret<DataGlwe>,
198 source_xa: &mut Source,
199 source_xe: &mut Source,
200 scratch: &mut Scratch<BE>,
201 ) where
202 R: GLWEToLWESwitchingKeyToMut,
203 Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
204 {
205
206 let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut();
207
208 #[cfg(debug_assertions)]
209 {
210 assert!(sk_lwe.n().0 <= self.n() as u32);
211 }
212
213 let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1));
214 sk_lwe_as_glwe.data.zero();
215 sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
216 self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);
217
218 res.0.encrypt_sk(
219 self,
220 sk_glwe,
221 &sk_lwe_as_glwe,
222 source_xa,
223 source_xe,
224 scratch_1,
225 );
226 }
227}