arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/backend/src/hal/delegates/vec_znx.rs
backend/src/hal/delegates/vec_znx.rsRaw
  1use sampling::source::Source;
  2
  3use crate::hal::{
  4    api::{
  5        VecZnxAdd, VecZnxAddDistF64, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAlloc, VecZnxAllocBytes,
  6        VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxCopy, VecZnxDecodeCoeffsi64, VecZnxDecodeVecFloat,
  7        VecZnxDecodeVeci64, VecZnxEncodeCoeffsi64, VecZnxEncodeVeci64, VecZnxFillDistF64, VecZnxFillNormal, VecZnxFillUniform,
  8        VecZnxFromBytes, VecZnxLshInplace, VecZnxMerge, VecZnxMulXpMinusOne, VecZnxMulXpMinusOneInplace, VecZnxNegate,
  9        VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace,
 10        VecZnxRshInplace, VecZnxSplit, VecZnxStd, VecZnxSub, VecZnxSubABInplace, VecZnxSubBAInplace, VecZnxSubScalarInplace,
 11        VecZnxSwithcDegree,
 12    },
 13    layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxOwned, VecZnxToMut, VecZnxToRef},
 14    oep::{
 15        VecZnxAddDistF64Impl, VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarInplaceImpl,
 16        VecZnxAllocBytesImpl, VecZnxAllocImpl, VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl,
 17        VecZnxDecodeCoeffsi64Impl, VecZnxDecodeVecFloatImpl, VecZnxDecodeVeci64Impl, VecZnxEncodeCoeffsi64Impl,
 18        VecZnxEncodeVeci64Impl, VecZnxFillDistF64Impl, VecZnxFillNormalImpl, VecZnxFillUniformImpl, VecZnxFromBytesImpl,
 19        VecZnxLshInplaceImpl, VecZnxMergeImpl, VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl, VecZnxNegateImpl,
 20        VecZnxNegateInplaceImpl, VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, VecZnxRotateImpl,
 21        VecZnxRotateInplaceImpl, VecZnxRshInplaceImpl, VecZnxSplitImpl, VecZnxStdImpl, VecZnxSubABInplaceImpl,
 22        VecZnxSubBAInplaceImpl, VecZnxSubImpl, VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl,
 23    },
 24};
 25
 26impl<B> VecZnxAlloc for Module<B>
 27where
 28    B: Backend + VecZnxAllocImpl<B>,
 29{
 30    fn vec_znx_alloc(&self, cols: usize, size: usize) -> VecZnxOwned {
 31        B::vec_znx_alloc_impl(self.n(), cols, size)
 32    }
 33}
 34
 35impl<B> VecZnxFromBytes for Module<B>
 36where
 37    B: Backend + VecZnxFromBytesImpl<B>,
 38{
 39    fn vec_znx_from_bytes(&self, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxOwned {
 40        B::vec_znx_from_bytes_impl(self.n(), cols, size, bytes)
 41    }
 42}
 43
 44impl<B> VecZnxAllocBytes for Module<B>
 45where
 46    B: Backend + VecZnxAllocBytesImpl<B>,
 47{
 48    fn vec_znx_alloc_bytes(&self, cols: usize, size: usize) -> usize {
 49        B::vec_znx_alloc_bytes_impl(self.n(), cols, size)
 50    }
 51}
 52
 53impl<B> VecZnxNormalizeTmpBytes for Module<B>
 54where
 55    B: Backend + VecZnxNormalizeTmpBytesImpl<B>,
 56{
 57    fn vec_znx_normalize_tmp_bytes(&self, n: usize) -> usize {
 58        B::vec_znx_normalize_tmp_bytes_impl(self, n)
 59    }
 60}
 61
 62impl<B> VecZnxNormalize<B> for Module<B>
 63where
 64    B: Backend + VecZnxNormalizeImpl<B>,
 65{
 66    fn vec_znx_normalize<R, A>(&self, basek: usize, res: &mut R, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
 67    where
 68        R: VecZnxToMut,
 69        A: VecZnxToRef,
 70    {
 71        B::vec_znx_normalize_impl(self, basek, res, res_col, a, a_col, scratch)
 72    }
 73}
 74
 75impl<B> VecZnxNormalizeInplace<B> for Module<B>
 76where
 77    B: Backend + VecZnxNormalizeInplaceImpl<B>,
 78{
 79    fn vec_znx_normalize_inplace<A>(&self, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
 80    where
 81        A: VecZnxToMut,
 82    {
 83        B::vec_znx_normalize_inplace_impl(self, basek, a, a_col, scratch)
 84    }
 85}
 86
 87impl<B> VecZnxAdd for Module<B>
 88where
 89    B: Backend + VecZnxAddImpl<B>,
 90{
 91    fn vec_znx_add<R, A, C>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize)
 92    where
 93        R: VecZnxToMut,
 94        A: VecZnxToRef,
 95        C: VecZnxToRef,
 96    {
 97        B::vec_znx_add_impl(self, res, res_col, a, a_col, b, b_col)
 98    }
 99}
100
101impl<B> VecZnxAddInplace for Module<B>
102where
103    B: Backend + VecZnxAddInplaceImpl<B>,
104{
105    fn vec_znx_add_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
106    where
107        R: VecZnxToMut,
108        A: VecZnxToRef,
109    {
110        B::vec_znx_add_inplace_impl(self, res, res_col, a, a_col)
111    }
112}
113
114impl<B> VecZnxAddScalarInplace for Module<B>
115where
116    B: Backend + VecZnxAddScalarInplaceImpl<B>,
117{
118    fn vec_znx_add_scalar_inplace<R, A>(&self, res: &mut R, res_col: usize, res_limb: usize, a: &A, a_col: usize)
119    where
120        R: VecZnxToMut,
121        A: ScalarZnxToRef,
122    {
123        B::vec_znx_add_scalar_inplace_impl(self, res, res_col, res_limb, a, a_col)
124    }
125}
126
127impl<B> VecZnxSub for Module<B>
128where
129    B: Backend + VecZnxSubImpl<B>,
130{
131    fn vec_znx_sub<R, A, C>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize)
132    where
133        R: VecZnxToMut,
134        A: VecZnxToRef,
135        C: VecZnxToRef,
136    {
137        B::vec_znx_sub_impl(self, res, res_col, a, a_col, b, b_col)
138    }
139}
140
141impl<B> VecZnxSubABInplace for Module<B>
142where
143    B: Backend + VecZnxSubABInplaceImpl<B>,
144{
145    fn vec_znx_sub_ab_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
146    where
147        R: VecZnxToMut,
148        A: VecZnxToRef,
149    {
150        B::vec_znx_sub_ab_inplace_impl(self, res, res_col, a, a_col)
151    }
152}
153
154impl<B> VecZnxSubBAInplace for Module<B>
155where
156    B: Backend + VecZnxSubBAInplaceImpl<B>,
157{
158    fn vec_znx_sub_ba_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
159    where
160        R: VecZnxToMut,
161        A: VecZnxToRef,
162    {
163        B::vec_znx_sub_ba_inplace_impl(self, res, res_col, a, a_col)
164    }
165}
166
167impl<B> VecZnxSubScalarInplace for Module<B>
168where
169    B: Backend + VecZnxSubScalarInplaceImpl<B>,
170{
171    fn vec_znx_sub_scalar_inplace<R, A>(&self, res: &mut R, res_col: usize, res_limb: usize, a: &A, a_col: usize)
172    where
173        R: VecZnxToMut,
174        A: ScalarZnxToRef,
175    {
176        B::vec_znx_sub_scalar_inplace_impl(self, res, res_col, res_limb, a, a_col)
177    }
178}
179
180impl<B> VecZnxNegate for Module<B>
181where
182    B: Backend + VecZnxNegateImpl<B>,
183{
184    fn vec_znx_negate<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
185    where
186        R: VecZnxToMut,
187        A: VecZnxToRef,
188    {
189        B::vec_znx_negate_impl(self, res, res_col, a, a_col)
190    }
191}
192
193impl<B> VecZnxNegateInplace for Module<B>
194where
195    B: Backend + VecZnxNegateInplaceImpl<B>,
196{
197    fn vec_znx_negate_inplace<A>(&self, a: &mut A, a_col: usize)
198    where
199        A: VecZnxToMut,
200    {
201        B::vec_znx_negate_inplace_impl(self, a, a_col)
202    }
203}
204
205impl<B> VecZnxLshInplace for Module<B>
206where
207    B: Backend + VecZnxLshInplaceImpl<B>,
208{
209    fn vec_znx_lsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A)
210    where
211        A: VecZnxToMut,
212    {
213        B::vec_znx_lsh_inplace_impl(self, basek, k, a)
214    }
215}
216
217impl<B> VecZnxRshInplace for Module<B>
218where
219    B: Backend + VecZnxRshInplaceImpl<B>,
220{
221    fn vec_znx_rsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A)
222    where
223        A: VecZnxToMut,
224    {
225        B::vec_znx_rsh_inplace_impl(self, basek, k, a)
226    }
227}
228
229impl<B> VecZnxRotate for Module<B>
230where
231    B: Backend + VecZnxRotateImpl<B>,
232{
233    fn vec_znx_rotate<R, A>(&self, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
234    where
235        R: VecZnxToMut,
236        A: VecZnxToRef,
237    {
238        B::vec_znx_rotate_impl(self, k, res, res_col, a, a_col)
239    }
240}
241
242impl<B> VecZnxRotateInplace for Module<B>
243where
244    B: Backend + VecZnxRotateInplaceImpl<B>,
245{
246    fn vec_znx_rotate_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
247    where
248        A: VecZnxToMut,
249    {
250        B::vec_znx_rotate_inplace_impl(self, k, a, a_col)
251    }
252}
253
254impl<B> VecZnxAutomorphism for Module<B>
255where
256    B: Backend + VecZnxAutomorphismImpl<B>,
257{
258    fn vec_znx_automorphism<R, A>(&self, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
259    where
260        R: VecZnxToMut,
261        A: VecZnxToRef,
262    {
263        B::vec_znx_automorphism_impl(self, k, res, res_col, a, a_col)
264    }
265}
266
267impl<B> VecZnxAutomorphismInplace for Module<B>
268where
269    B: Backend + VecZnxAutomorphismInplaceImpl<B>,
270{
271    fn vec_znx_automorphism_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
272    where
273        A: VecZnxToMut,
274    {
275        B::vec_znx_automorphism_inplace_impl(self, k, a, a_col)
276    }
277}
278
279impl<B> VecZnxMulXpMinusOne for Module<B>
280where
281    B: Backend + VecZnxMulXpMinusOneImpl<B>,
282{
283    fn vec_znx_mul_xp_minus_one<R, A>(&self, p: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
284    where
285        R: VecZnxToMut,
286        A: VecZnxToRef,
287    {
288        B::vec_znx_mul_xp_minus_one_impl(self, p, res, res_col, a, a_col);
289    }
290}
291
292impl<B> VecZnxMulXpMinusOneInplace for Module<B>
293where
294    B: Backend + VecZnxMulXpMinusOneInplaceImpl<B>,
295{
296    fn vec_znx_mul_xp_minus_one_inplace<R>(&self, p: i64, res: &mut R, res_col: usize)
297    where
298        R: VecZnxToMut,
299    {
300        B::vec_znx_mul_xp_minus_one_inplace_impl(self, p, res, res_col);
301    }
302}
303
304impl<B> VecZnxSplit<B> for Module<B>
305where
306    B: Backend + VecZnxSplitImpl<B>,
307{
308    fn vec_znx_split<R, A>(&self, res: &mut Vec<R>, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
309    where
310        R: VecZnxToMut,
311        A: VecZnxToRef,
312    {
313        B::vec_znx_split_impl(self, res, res_col, a, a_col, scratch)
314    }
315}
316
317impl<B> VecZnxMerge for Module<B>
318where
319    B: Backend + VecZnxMergeImpl<B>,
320{
321    fn vec_znx_merge<R, A>(&self, res: &mut R, res_col: usize, a: Vec<A>, a_col: usize)
322    where
323        R: VecZnxToMut,
324        A: VecZnxToRef,
325    {
326        B::vec_znx_merge_impl(self, res, res_col, a, a_col)
327    }
328}
329
330impl<B> VecZnxSwithcDegree for Module<B>
331where
332    B: Backend + VecZnxSwithcDegreeImpl<B>,
333{
334    fn vec_znx_switch_degree<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
335    where
336        R: VecZnxToMut,
337        A: VecZnxToRef,
338    {
339        B::vec_znx_switch_degree_impl(self, res, res_col, a, a_col)
340    }
341}
342
343impl<B> VecZnxCopy for Module<B>
344where
345    B: Backend + VecZnxCopyImpl<B>,
346{
347    fn vec_znx_copy<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
348    where
349        R: VecZnxToMut,
350        A: VecZnxToRef,
351    {
352        B::vec_znx_copy_impl(self, res, res_col, a, a_col)
353    }
354}
355
356impl<B> VecZnxStd for Module<B>
357where
358    B: Backend + VecZnxStdImpl<B>,
359{
360    fn vec_znx_std<A>(&self, basek: usize, a: &A, a_col: usize) -> f64
361    where
362        A: VecZnxToRef,
363    {
364        B::vec_znx_std_impl(self, basek, a, a_col)
365    }
366}
367
368impl<B> VecZnxFillUniform for Module<B>
369where
370    B: Backend + VecZnxFillUniformImpl<B>,
371{
372    fn vec_znx_fill_uniform<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source)
373    where
374        R: VecZnxToMut,
375    {
376        B::vec_znx_fill_uniform_impl(self, basek, res, res_col, k, source);
377    }
378}
379
380impl<B> VecZnxFillDistF64 for Module<B>
381where
382    B: Backend + VecZnxFillDistF64Impl<B>,
383{
384    fn vec_znx_fill_dist_f64<R, D: rand::prelude::Distribution<f64>>(
385        &self,
386        basek: usize,
387        res: &mut R,
388        res_col: usize,
389        k: usize,
390        source: &mut Source,
391        dist: D,
392        bound: f64,
393    ) where
394        R: VecZnxToMut,
395    {
396        B::vec_znx_fill_dist_f64_impl(self, basek, res, res_col, k, source, dist, bound);
397    }
398}
399
400impl<B> VecZnxAddDistF64 for Module<B>
401where
402    B: Backend + VecZnxAddDistF64Impl<B>,
403{
404    fn vec_znx_add_dist_f64<R, D: rand::prelude::Distribution<f64>>(
405        &self,
406        basek: usize,
407        res: &mut R,
408        res_col: usize,
409        k: usize,
410        source: &mut Source,
411        dist: D,
412        bound: f64,
413    ) where
414        R: VecZnxToMut,
415    {
416        B::vec_znx_add_dist_f64_impl(self, basek, res, res_col, k, source, dist, bound);
417    }
418}
419
420impl<B> VecZnxFillNormal for Module<B>
421where
422    B: Backend + VecZnxFillNormalImpl<B>,
423{
424    fn vec_znx_fill_normal<R>(
425        &self,
426        basek: usize,
427        res: &mut R,
428        res_col: usize,
429        k: usize,
430        source: &mut Source,
431        sigma: f64,
432        bound: f64,
433    ) where
434        R: VecZnxToMut,
435    {
436        B::vec_znx_fill_normal_impl(self, basek, res, res_col, k, source, sigma, bound);
437    }
438}
439
440impl<B> VecZnxAddNormal for Module<B>
441where
442    B: Backend + VecZnxAddNormalImpl<B>,
443{
444    fn vec_znx_add_normal<R>(
445        &self,
446        basek: usize,
447        res: &mut R,
448        res_col: usize,
449        k: usize,
450        source: &mut Source,
451        sigma: f64,
452        bound: f64,
453    ) where
454        R: VecZnxToMut,
455    {
456        B::vec_znx_add_normal_impl(self, basek, res, res_col, k, source, sigma, bound);
457    }
458}
459
460impl<B> VecZnxEncodeVeci64 for Module<B>
461where
462    B: Backend + VecZnxEncodeVeci64Impl<B>,
463{
464    fn encode_vec_i64<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, data: &[i64], log_max: usize)
465    where
466        R: VecZnxToMut,
467    {
468        B::encode_vec_i64_impl(self, basek, res, res_col, k, data, log_max);
469    }
470}
471
472impl<B> VecZnxEncodeCoeffsi64 for Module<B>
473where
474    B: Backend + VecZnxEncodeCoeffsi64Impl<B>,
475{
476    fn encode_coeff_i64<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, i: usize, data: i64, log_max: usize)
477    where
478        R: VecZnxToMut,
479    {
480        B::encode_coeff_i64_impl(self, basek, res, res_col, k, i, data, log_max);
481    }
482}
483
484impl<B> VecZnxDecodeVeci64 for Module<B>
485where
486    B: Backend + VecZnxDecodeVeci64Impl<B>,
487{
488    fn decode_vec_i64<R>(&self, basek: usize, res: &R, res_col: usize, k: usize, data: &mut [i64])
489    where
490        R: VecZnxToRef,
491    {
492        B::decode_vec_i64_impl(self, basek, res, res_col, k, data);
493    }
494}
495
496impl<B> VecZnxDecodeCoeffsi64 for Module<B>
497where
498    B: Backend + VecZnxDecodeCoeffsi64Impl<B>,
499{
500    fn decode_coeff_i64<R>(&self, basek: usize, res: &R, res_col: usize, k: usize, i: usize) -> i64
501    where
502        R: VecZnxToRef,
503    {
504        B::decode_coeff_i64_impl(self, basek, res, res_col, k, i)
505    }
506}
507
508impl<B> VecZnxDecodeVecFloat for Module<B>
509where
510    B: Backend + VecZnxDecodeVecFloatImpl<B>,
511{
512    fn decode_vec_float<R>(&self, basek: usize, res: &R, col_i: usize, data: &mut [rug::Float])
513    where
514        R: VecZnxToRef,
515    {
516        B::decode_vec_float_impl(self, basek, res, col_i, data);
517    }
518}