poulpy_hal/api/
vec_znx.rs

1use crate::{
2    layouts::{Backend, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef},
3    source::Source,
4};
5
6pub trait VecZnxNormalizeTmpBytes {
7    /// Returns the minimum number of bytes necessary for normalization.
8    fn vec_znx_normalize_tmp_bytes(&self) -> usize;
9}
10
11pub trait VecZnxNormalize<B: Backend> {
12    #[allow(clippy::too_many_arguments)]
13    /// Normalizes the selected column of `a` and stores the result into the selected column of `res`.
14    fn vec_znx_normalize<R, A>(
15        &self,
16        res_basek: usize,
17        res: &mut R,
18        res_col: usize,
19        a_basek: usize,
20        a: &A,
21        a_col: usize,
22        scratch: &mut Scratch<B>,
23    ) where
24        R: VecZnxToMut,
25        A: VecZnxToRef;
26}
27
28pub trait VecZnxNormalizeInplace<B: Backend> {
29    /// Normalizes the selected column of `a`.
30    fn vec_znx_normalize_inplace<A>(&self, base2k: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
31    where
32        A: VecZnxToMut;
33}
34
35pub trait VecZnxAdd {
36    /// Adds the selected column of `a` to the selected column of `b` and writes the result on the selected column of `res`.
37    fn vec_znx_add<R, A, B>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize)
38    where
39        R: VecZnxToMut,
40        A: VecZnxToRef,
41        B: VecZnxToRef;
42}
43
44pub trait VecZnxAddInplace {
45    /// Adds the selected column of `a` to the selected column of `res` and writes the result on the selected column of `res`.
46    fn vec_znx_add_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
47    where
48        R: VecZnxToMut,
49        A: VecZnxToRef;
50}
51
52pub trait VecZnxAddScalar {
53    /// Adds the selected column of `a` on the selected column and limb of `b` and writes the result on the selected column of `res`.
54    #[allow(clippy::too_many_arguments)]
55    fn vec_znx_add_scalar<R, A, B>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize, b_limb: usize)
56    where
57        R: VecZnxToMut,
58        A: ScalarZnxToRef,
59        B: VecZnxToRef;
60}
61
62pub trait VecZnxAddScalarInplace {
63    /// Adds the selected column of `a` on the selected column and limb of `res`.
64    fn vec_znx_add_scalar_inplace<R, A>(&self, res: &mut R, res_col: usize, res_limb: usize, a: &A, a_col: usize)
65    where
66        R: VecZnxToMut,
67        A: ScalarZnxToRef;
68}
69
70pub trait VecZnxSub {
71    /// Subtracts the selected column of `b` from the selected column of `a` and writes the result on the selected column of `res`.
72    fn vec_znx_sub<R, A, B>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize)
73    where
74        R: VecZnxToMut,
75        A: VecZnxToRef,
76        B: VecZnxToRef;
77}
78
79pub trait VecZnxSubInplace {
80    /// Subtracts the selected column of `a` from the selected column of `res` inplace.
81    ///
82    /// res\[res_col\] -= a\[a_col\]
83    fn vec_znx_sub_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
84    where
85        R: VecZnxToMut,
86        A: VecZnxToRef;
87}
88
89pub trait VecZnxSubNegateInplace {
90    /// Subtracts the selected column of `res` from the selected column of `a` and inplace mutates `res`
91    ///
92    /// res\[res_col\] = a\[a_col\] - res\[res_col\]
93    fn vec_znx_sub_negate_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
94    where
95        R: VecZnxToMut,
96        A: VecZnxToRef;
97}
98
99pub trait VecZnxSubScalar {
100    /// Subtracts the selected column of `a` on the selected column and limb of `b` and writes the result on the selected column of `res`.
101    #[allow(clippy::too_many_arguments)]
102    fn vec_znx_sub_scalar<R, A, B>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize, b_limb: usize)
103    where
104        R: VecZnxToMut,
105        A: ScalarZnxToRef,
106        B: VecZnxToRef;
107}
108
109pub trait VecZnxSubScalarInplace {
110    /// Subtracts the selected column of `a` on the selected column and limb of `res`.
111    fn vec_znx_sub_scalar_inplace<R, A>(&self, res: &mut R, res_col: usize, res_limb: usize, a: &A, a_col: usize)
112    where
113        R: VecZnxToMut,
114        A: ScalarZnxToRef;
115}
116
117pub trait VecZnxNegate {
118    // Negates the selected column of `a` and stores the result in `res_col` of `res`.
119    fn vec_znx_negate<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
120    where
121        R: VecZnxToMut,
122        A: VecZnxToRef;
123}
124
125pub trait VecZnxNegateInplace {
126    /// Negates the selected column of `a`.
127    fn vec_znx_negate_inplace<A>(&self, a: &mut A, a_col: usize)
128    where
129        A: VecZnxToMut;
130}
131
132pub trait VecZnxLshTmpBytes {
133    fn vec_znx_lsh_tmp_bytes(&self) -> usize;
134}
135
136pub trait VecZnxLsh<B: Backend> {
137    /// Left shift by k bits all columns of `a`.
138    #[allow(clippy::too_many_arguments)]
139    fn vec_znx_lsh<R, A>(
140        &self,
141        base2k: usize,
142        k: usize,
143        r: &mut R,
144        res_col: usize,
145        a: &A,
146        a_col: usize,
147        scratch: &mut Scratch<B>,
148    ) where
149        R: VecZnxToMut,
150        A: VecZnxToRef;
151}
152
153pub trait VecZnxRshTmpBytes {
154    fn vec_znx_rsh_tmp_bytes(&self) -> usize;
155}
156
157pub trait VecZnxRsh<B: Backend> {
158    /// Right shift by k bits all columns of `a`.
159    #[allow(clippy::too_many_arguments)]
160    fn vec_znx_rsh<R, A>(
161        &self,
162        base2k: usize,
163        k: usize,
164        r: &mut R,
165        res_col: usize,
166        a: &A,
167        a_col: usize,
168        scratch: &mut Scratch<B>,
169    ) where
170        R: VecZnxToMut,
171        A: VecZnxToRef;
172}
173
174pub trait VecZnxLshInplace<B: Backend> {
175    /// Left shift by k bits all columns of `a`.
176    fn vec_znx_lsh_inplace<A>(&self, base2k: usize, k: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
177    where
178        A: VecZnxToMut;
179}
180
181pub trait VecZnxRshInplace<B: Backend> {
182    /// Right shift by k bits all columns of `a`.
183    fn vec_znx_rsh_inplace<A>(&self, base2k: usize, k: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
184    where
185        A: VecZnxToMut;
186}
187
188pub trait VecZnxRotate {
189    /// Multiplies the selected column of `a` by X^k and stores the result in `res_col` of `res`.
190    fn vec_znx_rotate<R, A>(&self, p: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
191    where
192        R: VecZnxToMut,
193        A: VecZnxToRef;
194}
195
196pub trait VecZnxRotateInplaceTmpBytes {
197    fn vec_znx_rotate_inplace_tmp_bytes(&self) -> usize;
198}
199
200pub trait VecZnxRotateInplace<B: Backend> {
201    /// Multiplies the selected column of `a` by X^k.
202    fn vec_znx_rotate_inplace<A>(&self, p: i64, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
203    where
204        A: VecZnxToMut;
205}
206
207pub trait VecZnxAutomorphism {
208    /// Applies the automorphism X^i -> X^ik on the selected column of `a` and stores the result in `res_col` column of `res`.
209    fn vec_znx_automorphism<R, A>(&self, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
210    where
211        R: VecZnxToMut,
212        A: VecZnxToRef;
213}
214
215pub trait VecZnxAutomorphismInplaceTmpBytes {
216    fn vec_znx_automorphism_inplace_tmp_bytes(&self) -> usize;
217}
218
219pub trait VecZnxAutomorphismInplace<B: Backend> {
220    /// Applies the automorphism X^i -> X^ik on the selected column of `a`.
221    fn vec_znx_automorphism_inplace<R>(&self, k: i64, res: &mut R, res_col: usize, scratch: &mut Scratch<B>)
222    where
223        R: VecZnxToMut;
224}
225
226pub trait VecZnxMulXpMinusOne {
227    fn vec_znx_mul_xp_minus_one<R, A>(&self, p: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
228    where
229        R: VecZnxToMut,
230        A: VecZnxToRef;
231}
232
233pub trait VecZnxMulXpMinusOneInplaceTmpBytes {
234    fn vec_znx_mul_xp_minus_one_inplace_tmp_bytes(&self) -> usize;
235}
236
237pub trait VecZnxMulXpMinusOneInplace<B: Backend> {
238    fn vec_znx_mul_xp_minus_one_inplace<R>(&self, p: i64, res: &mut R, res_col: usize, scratch: &mut Scratch<B>)
239    where
240        R: VecZnxToMut;
241}
242
243pub trait VecZnxSplitRingTmpBytes {
244    fn vec_znx_split_ring_tmp_bytes(&self) -> usize;
245}
246
247pub trait VecZnxSplitRing<B: Backend> {
248    /// Splits the selected columns of `b` into subrings and copies them them into the selected column of `res`.
249    ///
250    /// # Panics
251    ///
252    /// This method requires that all [crate::layouts::VecZnx] of b have the same ring degree
253    /// and that b.n() * b.len() <= a.n()
254    fn vec_znx_split_ring<R, A>(&self, res: &mut [R], res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
255    where
256        R: VecZnxToMut,
257        A: VecZnxToRef;
258}
259
260pub trait VecZnxMergeRingsTmpBytes {
261    fn vec_znx_merge_rings_tmp_bytes(&self) -> usize;
262}
263
264pub trait VecZnxMergeRings<B: Backend> {
265    /// Merges the subrings of the selected column of `a` into the selected column of `res`.
266    ///
267    /// # Panics
268    ///
269    /// This method requires that all [crate::layouts::VecZnx] of a have the same ring degree
270    /// and that a.n() * a.len() <= b.n()
271    fn vec_znx_merge_rings<R, A>(&self, res: &mut R, res_col: usize, a: &[A], a_col: usize, scratch: &mut Scratch<B>)
272    where
273        R: VecZnxToMut,
274        A: VecZnxToRef;
275}
276
277pub trait VecZnxSwitchRing {
278    fn vec_znx_switch_ring<R, A>(&self, res: &mut R, res_col: usize, a: &A, col_a: usize)
279    where
280        R: VecZnxToMut,
281        A: VecZnxToRef;
282}
283
284pub trait VecZnxCopy {
285    fn vec_znx_copy<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
286    where
287        R: VecZnxToMut,
288        A: VecZnxToRef;
289}
290
291pub trait VecZnxFillUniform {
292    /// Fills the first `size` size with uniform values in \[-2^{base2k-1}, 2^{base2k-1}\]
293    fn vec_znx_fill_uniform<R>(&self, base2k: usize, res: &mut R, res_col: usize, source: &mut Source)
294    where
295        R: VecZnxToMut;
296}
297
298#[allow(clippy::too_many_arguments)]
299pub trait VecZnxFillNormal {
300    fn vec_znx_fill_normal<R>(
301        &self,
302        base2k: usize,
303        res: &mut R,
304        res_col: usize,
305        k: usize,
306        source: &mut Source,
307        sigma: f64,
308        bound: f64,
309    ) where
310        R: VecZnxToMut;
311}
312
313#[allow(clippy::too_many_arguments)]
314pub trait VecZnxAddNormal {
315    /// Adds a discrete normal vector scaled by 2^{-k} with the provided standard deviation and bounded to \[-bound, bound\].
316    fn vec_znx_add_normal<R>(
317        &self,
318        base2k: usize,
319        res: &mut R,
320        res_col: usize,
321        k: usize,
322        source: &mut Source,
323        sigma: f64,
324        bound: f64,
325    ) where
326        R: VecZnxToMut;
327}