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::{GLWETensorKeyPrepared, GLWETensorKeyPreparedToRef},
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: GLWETensorKeyPreparedToRef<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>
42where
43 Self: GGSWExpandRows<BE> + GLWECopy,
44{
45 fn ggsw_from_gglwe_tmp_bytes<R, A>(&self, res_infos: &R, tsk_infos: &A) -> usize
46 where
47 R: GGSWInfos,
48 A: GGLWEInfos,
49 {
50 self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos)
51 }
52
53 fn ggsw_from_gglwe<R, A, T>(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch<BE>)
54 where
55 R: GGSWToMut,
56 A: GGLWEToRef,
57 T: GLWETensorKeyPreparedToRef<BE>,
58 Scratch<BE>: ScratchTakeCore<BE>,
59 {
60 let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
61 let a: &GGLWE<&[u8]> = &a.to_ref();
62 let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref();
63
64 assert_eq!(res.rank(), a.rank_out());
65 assert_eq!(res.dnum(), a.dnum());
66 assert_eq!(res.n(), self.n() as u32);
67 assert_eq!(a.n(), self.n() as u32);
68 assert_eq!(tsk.n(), self.n() as u32);
69
70 for row in 0..res.dnum().into() {
71 self.glwe_copy(&mut res.at_mut(row, 0), &a.at(row, 0));
72 }
73
74 self.ggsw_expand_row(res, tsk, scratch);
75 }
76}
77
78pub trait GGSWFromGGLWE<BE: Backend> {
79 fn ggsw_from_gglwe_tmp_bytes<R, A>(&self, res_infos: &R, tsk_infos: &A) -> usize
80 where
81 R: GGSWInfos,
82 A: GGLWEInfos;
83
84 fn ggsw_from_gglwe<R, A, T>(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch<BE>)
85 where
86 R: GGSWToMut,
87 A: GGLWEToRef,
88 T: GLWETensorKeyPreparedToRef<BE>,
89 Scratch<BE>: ScratchTakeCore<BE>;
90}
91
92impl<BE: Backend> GGSWExpandRows<BE> for Module<BE> where
93 Self: Sized
94 + ModuleN
95 + VecZnxDftBytesOf
96 + VmpApplyDftToDftTmpBytes
97 + VecZnxBigBytesOf
98 + VecZnxNormalizeTmpBytes
99 + VecZnxDftBytesOf
100 + VmpApplyDftToDftTmpBytes
101 + VecZnxBigBytesOf
102 + VecZnxNormalizeTmpBytes
103 + VecZnxDftApply<BE>
104 + VecZnxDftCopy<BE>
105 + VmpApplyDftToDft<BE>
106 + VmpApplyDftToDftAdd<BE>
107 + VecZnxDftAddInplace<BE>
108 + VecZnxBigNormalize<BE>
109 + VecZnxIdftApplyTmpA<BE>
110 + VecZnxNormalize<BE>
111{
112}
113
114pub trait GGSWExpandRows<BE: Backend>
115where
116 Self: Sized
117 + ModuleN
118 + VecZnxDftBytesOf
119 + VmpApplyDftToDftTmpBytes
120 + VecZnxBigBytesOf
121 + VecZnxNormalizeTmpBytes
122 + VecZnxDftApply<BE>
123 + VecZnxDftCopy<BE>
124 + VmpApplyDftToDft<BE>
125 + VmpApplyDftToDftAdd<BE>
126 + VecZnxDftAddInplace<BE>
127 + VecZnxBigNormalize<BE>
128 + VecZnxIdftApplyTmpA<BE>
129 + VecZnxNormalize<BE>,
130{
131 fn ggsw_expand_rows_tmp_bytes<R, A>(&self, res_infos: &R, tsk_infos: &A) -> usize
132 where
133 R: GGSWInfos,
134 A: GGLWEInfos,
135 {
136 let tsk_size: usize = tsk_infos.k().div_ceil(tsk_infos.base2k()) as usize;
137 let size_in: usize = res_infos
138 .k()
139 .div_ceil(tsk_infos.base2k())
140 .div_ceil(tsk_infos.dsize().into()) as usize;
141
142 let tmp_dft_i: usize = self.bytes_of_vec_znx_dft((tsk_infos.rank_out() + 1).into(), tsk_size);
143 let tmp_a: usize = self.bytes_of_vec_znx_dft(1, size_in);
144 let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes(
145 tsk_size,
146 size_in,
147 size_in,
148 (tsk_infos.rank_in()).into(), (tsk_infos.rank_out()).into(), tsk_size,
151 );
152 let tmp_idft: usize = self.bytes_of_vec_znx_big(1, tsk_size);
153 let norm: usize = self.vec_znx_normalize_tmp_bytes();
154
155 tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm))
156 }
157
158 fn ggsw_expand_row<R, T>(&self, res: &mut R, tsk: &T, scratch: &mut Scratch<BE>)
159 where
160 R: GGSWToMut,
161 T: GLWETensorKeyPreparedToRef<BE>,
162 Scratch<BE>: ScratchTakeCore<BE>,
163 {
164 let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
165 let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref();
166
167 let basek_in: usize = res.base2k().into();
168 let basek_tsk: usize = tsk.base2k().into();
169
170 assert!(scratch.available() >= self.ggsw_expand_rows_tmp_bytes(res, tsk));
171
172 let rank: usize = res.rank().into();
173 let cols: usize = rank + 1;
174
175 let a_size: usize = (res.size() * basek_in).div_ceil(basek_tsk);
176
177 for row_i in 0..res.dnum().into() {
179 let a = &res.at(row_i, 0).data;
180
181 let (mut ci_dft, scratch_1) = scratch.take_vec_znx_dft(self, cols, a_size);
183
184 if basek_in == basek_tsk {
185 for i in 0..cols {
186 self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, a, i);
187 }
188 } else {
189 let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self.n(), 1, a_size);
190 for i in 0..cols {
191 self.vec_znx_normalize(basek_tsk, &mut a_conv, 0, basek_in, a, i, scratch_2);
192 self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, &a_conv, 0);
193 }
194 }
195
196 for col_j in 1..cols {
197 let dsize: usize = tsk.dsize().into();
218
219 let (mut tmp_dft_i, scratch_2) = scratch_1.take_vec_znx_dft(self, cols, tsk.size());
220 let (mut tmp_a, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, ci_dft.size().div_ceil(dsize));
221
222 {
223 for col_i in 1..cols {
235 let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).data; for di in 0..dsize {
239 tmp_a.set_size((ci_dft.size() + di) / dsize);
240
241 tmp_dft_i.set_size(tsk.size() - ((dsize - di) as isize - 2).max(0) as usize);
249
250 self.vec_znx_dft_copy(dsize, dsize - 1 - di, &mut tmp_a, 0, &ci_dft, col_i);
251 if di == 0 && col_i == 1 {
252 self.vmp_apply_dft_to_dft(&mut tmp_dft_i, &tmp_a, pmat, scratch_3);
253 } else {
254 self.vmp_apply_dft_to_dft_add(&mut tmp_dft_i, &tmp_a, pmat, di, scratch_3);
255 }
256 }
257 }
258 }
259
260 self.vec_znx_dft_add_inplace(&mut tmp_dft_i, col_j, &ci_dft, 0);
270 let (mut tmp_idft, scratch_3) = scratch_2.take_vec_znx_big(self, 1, tsk.size());
271 for i in 0..cols {
272 self.vec_znx_idft_apply_tmpa(&mut tmp_idft, 0, &mut tmp_dft_i, i);
273 self.vec_znx_big_normalize(
274 basek_in,
275 &mut res.at_mut(row_i, col_j).data,
276 i,
277 basek_tsk,
278 &tmp_idft,
279 0,
280 scratch_3,
281 );
282 }
283 }
284 }
285 }
286}