poulpy/backend/src/hal/api/vec_znx.rs
backend/src/hal/api/vec_znx.rsRaw
1use rand_distr::Distribution;
2use rug::Float;
3use sampling::source::Source;
4
5use crate::hal::layouts::{Backend, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef};
6
7pub trait VecZnxNormalizeTmpBytes {
8 /// Returns the minimum number of bytes necessary for normalization.
9 fn vec_znx_normalize_tmp_bytes(&self, n: usize) -> usize;
10}
11
12pub trait VecZnxNormalize<B: Backend> {
13 /// Normalizes the selected column of `a` and stores the result into the selected column of `res`.
14 fn vec_znx_normalize<R, A>(&self, basek: usize, res: &mut R, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
15 where
16 R: VecZnxToMut,
17 A: VecZnxToRef;
18}
19
20pub trait VecZnxNormalizeInplace<B: Backend> {
21 /// Normalizes the selected column of `a`.
22 fn vec_znx_normalize_inplace<A>(&self, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
23 where
24 A: VecZnxToMut;
25}
26
27pub trait VecZnxAdd {
28 /// Adds the selected column of `a` to the selected column of `b` and writes the result on the selected column of `res`.
29 fn vec_znx_add<R, A, B>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize)
30 where
31 R: VecZnxToMut,
32 A: VecZnxToRef,
33 B: VecZnxToRef;
34}
35
36pub trait VecZnxAddInplace {
37 /// Adds the selected column of `a` to the selected column of `res` and writes the result on the selected column of `res`.
38 fn vec_znx_add_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
39 where
40 R: VecZnxToMut,
41 A: VecZnxToRef;
42}
43
44pub trait VecZnxAddScalarInplace {
45 /// Adds the selected column of `a` on the selected column and limb of `res`.
46 fn vec_znx_add_scalar_inplace<R, A>(&self, res: &mut R, res_col: usize, res_limb: usize, a: &A, a_col: usize)
47 where
48 R: VecZnxToMut,
49 A: ScalarZnxToRef;
50}
51
52pub trait VecZnxSub {
53 /// Subtracts the selected column of `b` from the selected column of `a` and writes the result on the selected column of `res`.
54 fn vec_znx_sub<R, A, B>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize)
55 where
56 R: VecZnxToMut,
57 A: VecZnxToRef,
58 B: VecZnxToRef;
59}
60
61pub trait VecZnxSubABInplace {
62 /// Subtracts the selected column of `a` from the selected column of `res` inplace.
63 ///
64 /// res\[res_col\] -= a\[a_col\]
65 fn vec_znx_sub_ab_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
66 where
67 R: VecZnxToMut,
68 A: VecZnxToRef;
69}
70
71pub trait VecZnxSubBAInplace {
72 /// Subtracts the selected column of `res` from the selected column of `a` and inplace mutates `res`
73 ///
74 /// res\[res_col\] = a\[a_col\] - res\[res_col\]
75 fn vec_znx_sub_ba_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
76 where
77 R: VecZnxToMut,
78 A: VecZnxToRef;
79}
80
81pub trait VecZnxSubScalarInplace {
82 /// Subtracts the selected column of `a` on the selected column and limb of `res`.
83 fn vec_znx_sub_scalar_inplace<R, A>(&self, res: &mut R, res_col: usize, res_limb: usize, a: &A, a_col: usize)
84 where
85 R: VecZnxToMut,
86 A: ScalarZnxToRef;
87}
88
89pub trait VecZnxNegate {
90 // Negates the selected column of `a` and stores the result in `res_col` of `res`.
91 fn vec_znx_negate<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
92 where
93 R: VecZnxToMut,
94 A: VecZnxToRef;
95}
96
97pub trait VecZnxNegateInplace {
98 /// Negates the selected column of `a`.
99 fn vec_znx_negate_inplace<A>(&self, a: &mut A, a_col: usize)
100 where
101 A: VecZnxToMut;
102}
103
104pub trait VecZnxLshInplace {
105 /// Left shift by k bits all columns of `a`.
106 fn vec_znx_lsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A)
107 where
108 A: VecZnxToMut;
109}
110
111pub trait VecZnxRshInplace {
112 /// Right shift by k bits all columns of `a`.
113 fn vec_znx_rsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A)
114 where
115 A: VecZnxToMut;
116}
117
118pub trait VecZnxRotate {
119 /// Multiplies the selected column of `a` by X^k and stores the result in `res_col` of `res`.
120 fn vec_znx_rotate<R, A>(&self, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
121 where
122 R: VecZnxToMut,
123 A: VecZnxToRef;
124}
125
126pub trait VecZnxRotateInplace {
127 /// Multiplies the selected column of `a` by X^k.
128 fn vec_znx_rotate_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
129 where
130 A: VecZnxToMut;
131}
132
133pub trait VecZnxAutomorphism {
134 /// Applies the automorphism X^i -> X^ik on the selected column of `a` and stores the result in `res_col` column of `res`.
135 fn vec_znx_automorphism<R, A>(&self, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
136 where
137 R: VecZnxToMut,
138 A: VecZnxToRef;
139}
140
141pub trait VecZnxAutomorphismInplace {
142 /// Applies the automorphism X^i -> X^ik on the selected column of `a`.
143 fn vec_znx_automorphism_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
144 where
145 A: VecZnxToMut;
146}
147
148pub trait VecZnxMulXpMinusOne {
149 fn vec_znx_mul_xp_minus_one<R, A>(&self, p: i64, r: &mut R, r_col: usize, a: &A, a_col: usize)
150 where
151 R: VecZnxToMut,
152 A: VecZnxToRef;
153}
154
155pub trait VecZnxMulXpMinusOneInplace {
156 fn vec_znx_mul_xp_minus_one_inplace<R>(&self, p: i64, r: &mut R, r_col: usize)
157 where
158 R: VecZnxToMut;
159}
160
161pub trait VecZnxSplit<B: Backend> {
162 /// Splits the selected columns of `b` into subrings and copies them them into the selected column of `res`.
163 ///
164 /// # Panics
165 ///
166 /// This method requires that all [crate::hal::layouts::VecZnx] of b have the same ring degree
167 /// and that b.n() * b.len() <= a.n()
168 fn vec_znx_split<R, A>(&self, res: &mut Vec<R>, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
169 where
170 R: VecZnxToMut,
171 A: VecZnxToRef;
172}
173
174pub trait VecZnxMerge {
175 /// Merges the subrings of the selected column of `a` into the selected column of `res`.
176 ///
177 /// # Panics
178 ///
179 /// This method requires that all [crate::hal::layouts::VecZnx] of a have the same ring degree
180 /// and that a.n() * a.len() <= b.n()
181 fn vec_znx_merge<R, A>(&self, res: &mut R, res_col: usize, a: Vec<A>, a_col: usize)
182 where
183 R: VecZnxToMut,
184 A: VecZnxToRef;
185}
186
187pub trait VecZnxSwithcDegree {
188 fn vec_znx_switch_degree<R, A>(&self, res: &mut R, res_col: usize, a: &A, col_a: usize)
189 where
190 R: VecZnxToMut,
191 A: VecZnxToRef;
192}
193
194pub trait VecZnxCopy {
195 fn vec_znx_copy<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
196 where
197 R: VecZnxToMut,
198 A: VecZnxToRef;
199}
200
201pub trait VecZnxStd {
202 /// Returns the standard devaition of the i-th polynomial.
203 fn vec_znx_std<A>(&self, basek: usize, a: &A, a_col: usize) -> f64
204 where
205 A: VecZnxToRef;
206}
207
208pub trait VecZnxFillUniform {
209 /// Fills the first `size` size with uniform values in \[-2^{basek-1}, 2^{basek-1}\]
210 fn vec_znx_fill_uniform<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source)
211 where
212 R: VecZnxToMut;
213}
214
215pub trait VecZnxFillDistF64 {
216 fn vec_znx_fill_dist_f64<R, D: Distribution<f64>>(
217 &self,
218 basek: usize,
219 res: &mut R,
220 res_col: usize,
221 k: usize,
222 source: &mut Source,
223 dist: D,
224 bound: f64,
225 ) where
226 R: VecZnxToMut;
227}
228
229pub trait VecZnxAddDistF64 {
230 /// Adds vector sampled according to the provided distribution, scaled by 2^{-k} and bounded to \[-bound, bound\].
231 fn vec_znx_add_dist_f64<R, D: Distribution<f64>>(
232 &self,
233 basek: usize,
234 res: &mut R,
235 res_col: usize,
236 k: usize,
237 source: &mut Source,
238 dist: D,
239 bound: f64,
240 ) where
241 R: VecZnxToMut;
242}
243
244pub trait VecZnxFillNormal {
245 fn vec_znx_fill_normal<R>(
246 &self,
247 basek: usize,
248 res: &mut R,
249 res_col: usize,
250 k: usize,
251 source: &mut Source,
252 sigma: f64,
253 bound: f64,
254 ) where
255 R: VecZnxToMut;
256}
257
258pub trait VecZnxAddNormal {
259 /// Adds a discrete normal vector scaled by 2^{-k} with the provided standard deviation and bounded to \[-bound, bound\].
260 fn vec_znx_add_normal<R>(
261 &self,
262 basek: usize,
263 res: &mut R,
264 res_col: usize,
265 k: usize,
266 source: &mut Source,
267 sigma: f64,
268 bound: f64,
269 ) where
270 R: VecZnxToMut;
271}
272
273pub trait VecZnxEncodeVeci64 {
274 /// encode a vector of i64 on the receiver.
275 ///
276 /// # Arguments
277 ///
278 /// * `col_i`: the index of the poly where to encode the data.
279 /// * `basek`: base two negative logarithm decomposition of the receiver.
280 /// * `k`: base two negative logarithm of the scaling of the data.
281 /// * `data`: data to encode on the receiver.
282 /// * `log_max`: base two logarithm of the infinity norm of the input data.
283 fn encode_vec_i64<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, data: &[i64], log_max: usize)
284 where
285 R: VecZnxToMut;
286}
287
288pub trait VecZnxEncodeCoeffsi64 {
289 /// encodes a single i64 on the receiver at the given index.
290 ///
291 /// # Arguments
292 ///
293 /// * `res_col`: the index of the poly where to encode the data.
294 /// * `basek`: base two negative logarithm decomposition of the receiver.
295 /// * `k`: base two negative logarithm of the scaling of the data.
296 /// * `i`: index of the coefficient on which to encode the data.
297 /// * `data`: data to encode on the receiver.
298 /// * `log_max`: base two logarithm of the infinity norm of the input data.
299 fn encode_coeff_i64<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, i: usize, data: i64, log_max: usize)
300 where
301 R: VecZnxToMut;
302}
303
304pub trait VecZnxDecodeVeci64 {
305 /// decode a vector of i64 from the receiver.
306 ///
307 /// # Arguments
308 ///
309 /// * `res_col`: the index of the poly where to encode the data.
310 /// * `basek`: base two negative logarithm decomposition of the receiver.
311 /// * `k`: base two logarithm of the scaling of the data.
312 /// * `data`: data to decode from the receiver.
313 fn decode_vec_i64<R>(&self, basek: usize, res: &R, res_col: usize, k: usize, data: &mut [i64])
314 where
315 R: VecZnxToRef;
316}
317
318pub trait VecZnxDecodeCoeffsi64 {
319 /// decode a single of i64 from the receiver at the given index.
320 ///
321 /// # Arguments
322 ///
323 /// * `res_col`: the index of the poly where to encode the data.
324 /// * `basek`: base two negative logarithm decomposition of the receiver.
325 /// * `k`: base two negative logarithm of the scaling of the data.
326 /// * `i`: index of the coefficient to decode.
327 /// * `data`: data to decode from the receiver.
328 fn decode_coeff_i64<R>(&self, basek: usize, res: &R, res_col: usize, k: usize, i: usize) -> i64
329 where
330 R: VecZnxToRef;
331}
332
333pub trait VecZnxDecodeVecFloat {
334 /// decode a vector of Float from the receiver.
335 ///
336 /// # Arguments
337 /// * `col_i`: the index of the poly where to encode the data.
338 /// * `basek`: base two negative logarithm decomposition of the receiver.
339 /// * `data`: data to decode from the receiver.
340 fn decode_vec_float<R>(&self, basek: usize, res: &R, col_i: usize, data: &mut [Float])
341 where
342 R: VecZnxToRef;
343}