poulpy/poulpy-core/src/conversion/gglwe_to_ggsw.rs
poulpy-core/src/conversion/gglwe_to_ggsw.rsRaw
1use poulpy_hal::{
2 api::{
3 ModuleN, ScratchAvailable, ScratchTakeBasic, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftAddInplace, VecZnxDftApply,
4 VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft,
5 VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes,
6 },
7 layouts::{Backend, DataMut, Module, Scratch, VmpPMat, ZnxInfos},
8};
9
10use crate::{
11 GLWECopy, ScratchTakeCore,
12 layouts::{
13 GGLWE, GGLWEInfos, GGLWEToRef, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos,
14 prepared::{TensorKeyPrepared, TensorKeyPreparedToRef},
15 },
16};
17
18impl GGLWE<Vec<u8>> {
19 pub fn from_gglw_tmp_bytes<R, A, M, BE: Backend>(module: &M, res_infos: &R, tsk_infos: &A) -> usize
20 where
21 M: GGSWFromGGLWE<BE>,
22 R: GGSWInfos,
23 A: GGLWEInfos,
24 {
25 module.ggsw_from_gglwe_tmp_bytes(res_infos, tsk_infos)
26 }
27}
28
29impl<D: DataMut> GGSW<D> {
30 pub fn from_gglwe<G, M, T, BE: Backend>(&mut self, module: &M, gglwe: &G, tsk: &T, scratch: &mut Scratch<BE>)
31 where
32 M: GGSWFromGGLWE<BE>,
33 G: GGLWEToRef,
34 T: TensorKeyPreparedToRef<BE>,
35 Scratch<BE>: ScratchTakeCore<BE>,
36 {
37 module.ggsw_from_gglwe(self, gglwe, tsk, scratch);
38 }
39}
40
41impl<BE: Backend> GGSWFromGGLWE<BE> for Module<BE> where Self: GGSWExpandRows<BE> + GLWECopy {}
42
43pub trait GGSWFromGGLWE<BE: Backend>
44where
45 Self: GGSWExpandRows<BE> + GLWECopy,
46{
47 fn ggsw_from_gglwe_tmp_bytes<R, A>(&self, res_infos: &R, tsk_infos: &A) -> usize
48 where
49 R: GGSWInfos,
50 A: GGLWEInfos,
51 {
52 self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos)
53 }
54
55 fn ggsw_from_gglwe<R, A, T>(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch<BE>)
56 where
57 R: GGSWToMut,
58 A: GGLWEToRef,
59 T: TensorKeyPreparedToRef<BE>,
60 Scratch<BE>: ScratchTakeCore<BE>,
61 {
62 let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
63 let a: &GGLWE<&[u8]> = &a.to_ref();
64 let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref();
65
66 assert_eq!(res.rank(), a.rank_out());
67 assert_eq!(res.dnum(), a.dnum());
68 assert_eq!(res.n(), self.n() as u32);
69 assert_eq!(a.n(), self.n() as u32);
70 assert_eq!(tsk.n(), self.n() as u32);
71
72 for row in 0..res.dnum().into() {
73 self.glwe_copy(&mut res.at_mut(row, 0), &a.at(row, 0));
74 }
75
76 self.ggsw_expand_row(res, tsk, scratch);
77 }
78}
79
80impl<BE: Backend> GGSWExpandRows<BE> for Module<BE> where
81 Self: Sized
82 + ModuleN
83 + VecZnxDftBytesOf
84 + VmpApplyDftToDftTmpBytes
85 + VecZnxBigBytesOf
86 + VecZnxNormalizeTmpBytes
87 + VecZnxDftBytesOf
88 + VmpApplyDftToDftTmpBytes
89 + VecZnxBigBytesOf
90 + VecZnxNormalizeTmpBytes
91 + VecZnxDftApply<BE>
92 + VecZnxDftCopy<BE>
93 + VmpApplyDftToDft<BE>
94 + VmpApplyDftToDftAdd<BE>
95 + VecZnxDftAddInplace<BE>
96 + VecZnxBigNormalize<BE>
97 + VecZnxIdftApplyTmpA<BE>
98 + VecZnxNormalize<BE>
99{
100}
101
102pub(crate) trait GGSWExpandRows<BE: Backend>
103where
104 Self: Sized
105 + ModuleN
106 + VecZnxDftBytesOf
107 + VmpApplyDftToDftTmpBytes
108 + VecZnxBigBytesOf
109 + VecZnxNormalizeTmpBytes
110 + VecZnxDftBytesOf
111 + VmpApplyDftToDftTmpBytes
112 + VecZnxBigBytesOf
113 + VecZnxNormalizeTmpBytes
114 + VecZnxDftApply<BE>
115 + VecZnxDftCopy<BE>
116 + VmpApplyDftToDft<BE>
117 + VmpApplyDftToDftAdd<BE>
118 + VecZnxDftAddInplace<BE>
119 + VecZnxBigNormalize<BE>
120 + VecZnxIdftApplyTmpA<BE>
121 + VecZnxNormalize<BE>,
122{
123 fn ggsw_expand_rows_tmp_bytes<R, A>(&self, res_infos: &R, tsk_infos: &A) -> usize
124 where
125 R: GGSWInfos,
126 A: GGLWEInfos,
127 {
128 let tsk_size: usize = tsk_infos.k().div_ceil(tsk_infos.base2k()) as usize;
129 let size_in: usize = res_infos
130 .k()
131 .div_ceil(tsk_infos.base2k())
132 .div_ceil(tsk_infos.dsize().into()) as usize;
133
134 let tmp_dft_i: usize = self.bytes_of_vec_znx_dft((tsk_infos.rank_out() + 1).into(), tsk_size);
135 let tmp_a: usize = self.bytes_of_vec_znx_dft(1, size_in);
136 let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes(
137 tsk_size,
138 size_in,
139 size_in,
140 (tsk_infos.rank_in()).into(), // Verify if rank+1
141 (tsk_infos.rank_out()).into(), // Verify if rank+1
142 tsk_size,
143 );
144 let tmp_idft: usize = self.bytes_of_vec_znx_big(1, tsk_size);
145 let norm: usize = self.vec_znx_normalize_tmp_bytes();
146
147 tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm))
148 }
149
150 fn ggsw_expand_row<R, T>(&self, res: &mut R, tsk: &T, scratch: &mut Scratch<BE>)
151 where
152 R: GGSWToMut,
153 T: TensorKeyPreparedToRef<BE>,
154 Scratch<BE>: ScratchTakeCore<BE>,
155 {
156 let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
157 let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref();
158
159 let basek_in: usize = res.base2k().into();
160 let basek_tsk: usize = tsk.base2k().into();
161
162 assert!(scratch.available() >= self.ggsw_expand_rows_tmp_bytes(res, tsk));
163
164 let rank: usize = res.rank().into();
165 let cols: usize = rank + 1;
166
167 let a_size: usize = (res.size() * basek_in).div_ceil(basek_tsk);
168
169 // Keyswitch the j-th row of the col 0
170 for row_i in 0..res.dnum().into() {
171 let a = &res.at(row_i, 0).data;
172
173 // Pre-compute DFT of (a0, a1, a2)
174 let (mut ci_dft, scratch_1) = scratch.take_vec_znx_dft(self, cols, a_size);
175
176 if basek_in == basek_tsk {
177 for i in 0..cols {
178 self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, a, i);
179 }
180 } else {
181 let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self, 1, a_size);
182 for i in 0..cols {
183 self.vec_znx_normalize(basek_tsk, &mut a_conv, 0, basek_in, a, i, scratch_2);
184 self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, &a_conv, 0);
185 }
186 }
187
188 for col_j in 1..cols {
189 // Example for rank 3:
190 //
191 // Note: M is a vector (m, Bm, B^2m, B^3m, ...), so each column is
192 // actually composed of that many dnum and we focus on a specific row here
193 // implicitely given ci_dft.
194 //
195 // # Input
196 //
197 // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 )
198 // col 1: (0, 0, 0, 0)
199 // col 2: (0, 0, 0, 0)
200 // col 3: (0, 0, 0, 0)
201 //
202 // # Output
203 //
204 // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 )
205 // col 1: (-(b0s0 + b1s1 + b2s2) , b0 + M[i], b1 , b2 )
206 // col 2: (-(c0s0 + c1s1 + c2s2) , c0 , c1 + M[i], c2 )
207 // col 3: (-(d0s0 + d1s1 + d2s2) , d0 , d1 , d2 + M[i])
208
209 let dsize: usize = tsk.dsize().into();
210
211 let (mut tmp_dft_i, scratch_2) = scratch_1.take_vec_znx_dft(self, cols, tsk.size());
212 let (mut tmp_a, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, ci_dft.size().div_ceil(dsize));
213
214 {
215 // Performs a key-switch for each combination of s[i]*s[j], i.e. for a0, a1, a2
216 //
217 // # Example for col=1
218 //
219 // a0 * (-(f0s0 + f1s1 + f1s2) + s0^2, f0, f1, f2) = (-(a0f0s0 + a0f1s1 + a0f1s2) + a0s0^2, a0f0, a0f1, a0f2)
220 // +
221 // a1 * (-(g0s0 + g1s1 + g1s2) + s0s1, g0, g1, g2) = (-(a1g0s0 + a1g1s1 + a1g1s2) + a1s0s1, a1g0, a1g1, a1g2)
222 // +
223 // a2 * (-(h0s0 + h1s1 + h1s2) + s0s2, h0, h1, h2) = (-(a2h0s0 + a2h1s1 + a2h1s2) + a2s0s2, a2h0, a2h1, a2h2)
224 // =
225 // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2)
226 for col_i in 1..cols {
227 let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j])
228
229 // Extracts a[i] and multipies with Enc(s[i]s[j])
230 for di in 0..dsize {
231 tmp_a.set_size((ci_dft.size() + di) / dsize);
232
233 // Small optimization for dsize > 2
234 // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then
235 // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}.
236 // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products.
237 // It is possible to further ignore the last dsize-1 limbs, but this introduce
238 // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same
239 // noise is kept with respect to the ideal functionality.
240 tmp_dft_i.set_size(tsk.size() - ((dsize - di) as isize - 2).max(0) as usize);
241
242 self.vec_znx_dft_copy(dsize, dsize - 1 - di, &mut tmp_a, 0, &ci_dft, col_i);
243 if di == 0 && col_i == 1 {
244 self.vmp_apply_dft_to_dft(&mut tmp_dft_i, &tmp_a, pmat, scratch_3);
245 } else {
246 self.vmp_apply_dft_to_dft_add(&mut tmp_dft_i, &tmp_a, pmat, di, scratch_3);
247 }
248 }
249 }
250 }
251
252 // Adds -(sum a[i] * s[i]) + m) on the i-th column of tmp_idft_i
253 //
254 // (-(x0s0 + x1s1 + x2s2) + a0s0s0 + a1s0s1 + a2s0s2, x0, x1, x2)
255 // +
256 // (0, -(a0s0 + a1s1 + a2s2) + M[i], 0, 0)
257 // =
258 // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0 -(a0s0 + a1s1 + a2s2) + M[i], x1, x2)
259 // =
260 // (-(x0s0 + x1s1 + x2s2), x0 + M[i], x1, x2)
261 self.vec_znx_dft_add_inplace(&mut tmp_dft_i, col_j, &ci_dft, 0);
262 let (mut tmp_idft, scratch_3) = scratch_2.take_vec_znx_big(self, 1, tsk.size());
263 for i in 0..cols {
264 self.vec_znx_idft_apply_tmpa(&mut tmp_idft, 0, &mut tmp_dft_i, i);
265 self.vec_znx_big_normalize(
266 basek_in,
267 &mut res.at_mut(row_i, col_j).data,
268 i,
269 basek_tsk,
270 &tmp_idft,
271 0,
272 scratch_3,
273 );
274 }
275 }
276 }
277 }
278}