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