1use std::collections::HashMap;
2
3use poulpy_hal::layouts::{Backend, ScratchArena};
4
5use crate::layouts::{
6 GGLWEInfos, GGSWAtViewMut, GGSWAtViewRef, GGSWInfos, GGSWToBackendMut, GGSWToBackendRef, GLWEAutomorphismKeyHelper,
7 GLWEInfos, GLWEToBackendMut, GLWEToBackendRef, GetGaloisElement,
8 prepared::{GGLWEPreparedToBackendRef, GLWETensorKeyPreparedToBackendRef},
9};
10
11pub trait GLWETrace<BE: Backend> {
12 fn glwe_trace_galois_elements(&self) -> Vec<i64>;
13
14 fn glwe_trace_tmp_bytes<R, A, K>(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize
15 where
16 R: GLWEInfos,
17 A: GLWEInfos,
18 K: GGLWEInfos;
19
20 fn glwe_trace<R, A, K, H>(
21 &self,
22 res: &mut R,
23 skip: usize,
24 a: &A,
25 keys: &H,
26 key_size: usize,
27 scratch: &mut ScratchArena<'_, BE>,
28 ) where
29 R: GLWEToBackendMut<BE> + GLWEInfos,
30 A: GLWEToBackendRef<BE> + GLWEInfos,
31 K: GGLWEPreparedToBackendRef<BE> + GetGaloisElement + GGLWEInfos,
32 H: GLWEAutomorphismKeyHelper<K, BE>;
33
34 fn glwe_trace_assign<R, K, H>(&self, res: &mut R, skip: usize, keys: &H, key_size: usize, scratch: &mut ScratchArena<'_, BE>)
35 where
36 R: GLWEToBackendMut<BE> + GLWEInfos,
37 K: GGLWEPreparedToBackendRef<BE> + GetGaloisElement + GGLWEInfos,
38 H: GLWEAutomorphismKeyHelper<K, BE>;
39}
40
41pub trait GLWEPacking<BE: Backend> {
42 fn glwe_pack_galois_elements(&self) -> Vec<i64>;
43
44 fn glwe_pack_tmp_bytes<R, K>(&self, res: &R, key: &K) -> usize
45 where
46 R: GLWEInfos,
47 K: GGLWEInfos;
48
49 fn glwe_pack<R, A, K, H>(
50 &self,
51 res: &mut R,
52 a: HashMap<usize, &mut A>,
53 log_gap_out: usize,
54 keys: &H,
55 key_size: usize,
56 scratch: &mut ScratchArena<'_, BE>,
57 ) where
58 R: GLWEToBackendMut<BE> + GLWEInfos,
59 A: GLWEToBackendMut<BE> + GLWEInfos,
60 K: GGLWEPreparedToBackendRef<BE> + GetGaloisElement + GGLWEInfos,
61 H: GLWEAutomorphismKeyHelper<K, BE>;
62}
63
64pub trait GLWEMulConst<BE: Backend> {
65 fn glwe_mul_const_tmp_bytes<R, A, B>(&self, res: &R, a: &A, b: &B) -> usize
66 where
67 R: GLWEInfos,
68 A: GLWEInfos,
69 B: GLWEInfos;
70
71 fn glwe_mul_const<R, A, B>(
72 &self,
73 cnv_offset: usize,
74 res: &mut R,
75 a: &A,
76 b: &B,
77 b_coeff: usize,
78 scratch: &mut ScratchArena<'_, BE>,
79 ) where
80 R: GLWEToBackendMut<BE> + GLWEInfos,
81 A: GLWEToBackendRef<BE> + GLWEInfos,
82 B: GLWEToBackendRef<BE> + GLWEInfos;
83
84 fn glwe_mul_const_assign<R, B>(
85 &self,
86 cnv_offset: usize,
87 res: &mut R,
88 b: &B,
89 b_coeff: usize,
90 scratch: &mut ScratchArena<'_, BE>,
91 ) where
92 R: GLWEToBackendMut<BE> + GLWEInfos,
93 B: GLWEToBackendRef<BE> + GLWEInfos;
94}
95
96pub trait GLWEMulPlain<BE: Backend> {
97 fn glwe_mul_plain_tmp_bytes<R, A, B>(&self, res: &R, a: &A, b: &B) -> usize
98 where
99 R: GLWEInfos,
100 A: GLWEInfos,
101 B: GLWEInfos;
102
103 #[allow(clippy::too_many_arguments)]
104 fn glwe_mul_plain<R, A, B>(
105 &self,
106 cnv_offset: usize,
107 res: &mut R,
108 a: &A,
109 a_effective_k: usize,
110 b: &B,
111 b_effective_k: usize,
112 scratch: &mut ScratchArena<'_, BE>,
113 ) where
114 R: GLWEToBackendMut<BE> + GLWEInfos,
115 A: GLWEToBackendRef<BE> + GLWEInfos,
116 B: GLWEToBackendRef<BE> + GLWEInfos;
117
118 #[allow(clippy::too_many_arguments)]
119 fn glwe_mul_plain_assign<R, A>(
120 &self,
121 cnv_offset: usize,
122 res: &mut R,
123 res_effective_k: usize,
124 a: &A,
125 a_effective_k: usize,
126 scratch: &mut ScratchArena<'_, BE>,
127 ) where
128 R: GLWEToBackendMut<BE> + GLWEInfos,
129 A: GLWEToBackendRef<BE> + GLWEInfos;
130}
131
132pub trait GLWETensoring<BE: Backend> {
133 fn glwe_tensor_apply_tmp_bytes<R, A, B>(&self, res: &R, a: &A, b: &B) -> usize
134 where
135 R: GLWEInfos,
136 A: GLWEInfos,
137 B: GLWEInfos;
138
139 fn glwe_tensor_square_apply_tmp_bytes<R, A>(&self, res: &R, a: &A) -> usize
140 where
141 R: GLWEInfos,
142 A: GLWEInfos;
143
144 #[allow(clippy::too_many_arguments)]
145 fn glwe_tensor_apply<R, A, B>(
146 &self,
147 cnv_offset: usize,
148 res: &mut R,
149 a: &A,
150 a_effective_k: usize,
151 b: &B,
152 b_effective_k: usize,
153 scratch: &mut ScratchArena<'_, BE>,
154 ) where
155 R: GLWEToBackendMut<BE> + GLWEInfos,
156 A: GLWEToBackendRef<BE> + GLWEInfos,
157 B: GLWEToBackendRef<BE> + GLWEInfos;
158
159 #[allow(clippy::too_many_arguments)]
160 fn glwe_tensor_square_apply<R, A>(
161 &self,
162 cnv_offset: usize,
163 res: &mut R,
164 a: &A,
165 a_effective_k: usize,
166 scratch: &mut ScratchArena<'_, BE>,
167 ) where
168 R: GLWEToBackendMut<BE> + GLWEInfos,
169 A: GLWEToBackendRef<BE> + GLWEInfos;
170
171 fn glwe_tensor_relinearize<R, A, T>(&self, res: &mut R, a: &A, tsk: &T, tsk_size: usize, scratch: &mut ScratchArena<'_, BE>)
172 where
173 R: GLWEToBackendMut<BE> + GLWEInfos,
174 A: GLWEToBackendRef<BE> + GLWEInfos,
175 T: GGLWEInfos + GLWETensorKeyPreparedToBackendRef<BE>;
176
177 fn glwe_tensor_relinearize_tmp_bytes<R, A, B>(&self, res: &R, a: &A, tsk: &B) -> usize
178 where
179 R: GLWEInfos,
180 A: GLWEInfos,
181 B: GGLWEInfos;
182}
183
184pub trait GLWEAdd<BE: Backend> {
185 fn glwe_add_into<R, A, B>(&self, res: &mut R, a: &A, b: &B)
186 where
187 R: GLWEToBackendMut<BE>,
188 A: GLWEToBackendRef<BE>,
189 B: GLWEToBackendRef<BE>;
190
191 fn glwe_add_assign<R, A>(&self, res: &mut R, a: &A)
192 where
193 R: GLWEToBackendMut<BE>,
194 A: GLWEToBackendRef<BE>;
195}
196
197pub trait GLWENegate<BE: Backend> {
198 fn glwe_negate<R, A>(&self, res: &mut R, a: &A)
199 where
200 R: GLWEToBackendMut<BE>,
201 A: GLWEToBackendRef<BE>;
202
203 fn glwe_negate_assign<R>(&self, res: &mut R)
204 where
205 R: GLWEToBackendMut<BE>;
206}
207
208pub trait GLWESub<BE: Backend> {
209 fn glwe_sub<R, A, B>(&self, res: &mut R, a: &A, b: &B)
210 where
211 R: GLWEToBackendMut<BE>,
212 A: GLWEToBackendRef<BE>,
213 B: GLWEToBackendRef<BE>;
214
215 fn glwe_sub_assign<R, A>(&self, res: &mut R, a: &A)
216 where
217 R: GLWEToBackendMut<BE>,
218 A: GLWEToBackendRef<BE>;
219
220 fn glwe_sub_negate_assign<R, A>(&self, res: &mut R, a: &A)
221 where
222 R: GLWEToBackendMut<BE>,
223 A: GLWEToBackendRef<BE>;
224}
225
226pub trait GLWEZero<BE: Backend> {
227 fn glwe_zero<R>(&self, res: &mut R)
228 where
229 R: GLWEToBackendMut<BE>;
230}
231
232pub trait GLWERotate<BE: Backend> {
233 fn glwe_rotate_tmp_bytes(&self) -> usize;
234
235 fn glwe_rotate<R, A>(&self, k: i64, res: &mut R, a: &A)
236 where
237 R: GLWEToBackendMut<BE>,
238 A: GLWEToBackendRef<BE>;
239
240 fn glwe_rotate_assign<R>(&self, k: i64, res: &mut R, scratch: &mut ScratchArena<'_, BE>)
241 where
242 R: GLWEToBackendMut<BE>;
243}
244
245pub trait GGSWRotate<BE: Backend> {
246 fn ggsw_rotate_tmp_bytes(&self) -> usize;
247
248 fn ggsw_rotate<R, A>(&self, k: i64, res: &mut R, a: &A)
249 where
250 R: GGSWToBackendMut<BE> + GGSWAtViewMut<BE> + GGSWInfos,
251 A: GGSWToBackendRef<BE> + GGSWAtViewRef<BE> + GGSWInfos;
252
253 fn ggsw_rotate_assign<R>(&self, k: i64, res: &mut R, scratch: &mut ScratchArena<'_, BE>)
254 where
255 R: GGSWToBackendMut<BE> + GGSWInfos;
256}
257
258pub trait GLWEMulXpMinusOne<BE: Backend> {
259 fn glwe_mul_xp_minus_one<R, A>(&self, k: i64, res: &mut R, a: &A)
260 where
261 R: GLWEToBackendMut<BE>,
262 A: GLWEToBackendRef<BE>;
263
264 fn glwe_mul_xp_minus_one_assign<R>(&self, k: i64, res: &mut R, scratch: &mut ScratchArena<'_, BE>)
265 where
266 R: GLWEToBackendMut<BE>;
267}
268
269pub trait GLWECopy<BE: Backend> {
270 fn glwe_copy<R, A>(&self, res: &mut R, a: &A)
271 where
272 R: GLWEToBackendMut<BE>,
273 A: GLWEToBackendRef<BE>;
274}
275
276pub trait GLWEShift<BE: Backend> {
277 fn glwe_shift_tmp_bytes(&self) -> usize;
278
279 fn glwe_rsh<R>(&self, k: usize, res: &mut R, scratch: &mut ScratchArena<'_, BE>)
280 where
281 R: GLWEToBackendMut<BE>;
282
283 fn glwe_lsh_assign<R>(&self, res: &mut R, k: usize, scratch: &mut ScratchArena<'_, BE>)
284 where
285 R: GLWEToBackendMut<BE>;
286
287 fn glwe_lsh<R, A>(&self, res: &mut R, a: &A, k: usize, scratch: &mut ScratchArena<'_, BE>)
288 where
289 R: GLWEToBackendMut<BE>,
290 A: GLWEToBackendRef<BE>;
291
292 fn glwe_lsh_add<R, A>(&self, res: &mut R, a: &A, k: usize, scratch: &mut ScratchArena<'_, BE>)
293 where
294 R: GLWEToBackendMut<BE>,
295 A: GLWEToBackendRef<BE>;
296
297 fn glwe_lsh_sub<R, A>(&self, res: &mut R, a: &A, k: usize, scratch: &mut ScratchArena<'_, BE>)
298 where
299 R: GLWEToBackendMut<BE>,
300 A: GLWEToBackendRef<BE>;
301}
302
303pub trait GLWENormalize<BE: Backend> {
304 fn glwe_normalize_tmp_bytes(&self) -> usize;
305
306 fn glwe_normalize<R, A>(&self, res: &mut R, a: &A, scratch: &mut ScratchArena<'_, BE>)
307 where
308 R: GLWEToBackendMut<BE>,
309 A: GLWEToBackendRef<BE>;
310
311 fn glwe_normalize_assign<R>(&self, res: &mut R, scratch: &mut ScratchArena<'_, BE>)
312 where
313 R: GLWEToBackendMut<BE>;
314}