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