poulpy/backend/src/hal/api/vec_znx.rs
backend/src/hal/api/vec_znx.rsRaw
1use rand_distr::Distribution;
2use sampling::source::Source;
3
4use crate::hal::layouts::{Backend, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef};
5
6pub trait VecZnxNormalizeTmpBytes {
7 /// Returns the minimum number of bytes necessary for normalization.
8 fn vec_znx_normalize_tmp_bytes(&self, n: usize) -> usize;
9}
10
11pub trait VecZnxNormalize<B: Backend> {
12 /// Normalizes the selected column of `a` and stores the result into the selected column of `res`.
13 fn vec_znx_normalize<R, A>(&self, basek: usize, res: &mut R, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
14 where
15 R: VecZnxToMut,
16 A: VecZnxToRef;
17}
18
19pub trait VecZnxNormalizeInplace<B: Backend> {
20 /// Normalizes the selected column of `a`.
21 fn vec_znx_normalize_inplace<A>(&self, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
22 where
23 A: VecZnxToMut;
24}
25
26pub trait VecZnxAdd {
27 /// Adds the selected column of `a` to the selected column of `b` and writes the result on the selected column of `res`.
28 fn vec_znx_add<R, A, B>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize)
29 where
30 R: VecZnxToMut,
31 A: VecZnxToRef,
32 B: VecZnxToRef;
33}
34
35pub trait VecZnxAddInplace {
36 /// Adds the selected column of `a` to the selected column of `res` and writes the result on the selected column of `res`.
37 fn vec_znx_add_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
38 where
39 R: VecZnxToMut,
40 A: VecZnxToRef;
41}
42
43pub trait VecZnxAddScalarInplace {
44 /// Adds the selected column of `a` on the selected column and limb of `res`.
45 fn vec_znx_add_scalar_inplace<R, A>(&self, res: &mut R, res_col: usize, res_limb: usize, a: &A, a_col: usize)
46 where
47 R: VecZnxToMut,
48 A: ScalarZnxToRef;
49}
50
51pub trait VecZnxSub {
52 /// Subtracts the selected column of `b` from the selected column of `a` and writes the result on the selected column of `res`.
53 fn vec_znx_sub<R, A, B>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &B, b_col: usize)
54 where
55 R: VecZnxToMut,
56 A: VecZnxToRef,
57 B: VecZnxToRef;
58}
59
60pub trait VecZnxSubABInplace {
61 /// Subtracts the selected column of `a` from the selected column of `res` inplace.
62 ///
63 /// res\[res_col\] -= a\[a_col\]
64 fn vec_znx_sub_ab_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 VecZnxSubBAInplace {
71 /// Subtracts the selected column of `res` from the selected column of `a` and inplace mutates `res`
72 ///
73 /// res\[res_col\] = a\[a_col\] - res\[res_col\]
74 fn vec_znx_sub_ba_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
75 where
76 R: VecZnxToMut,
77 A: VecZnxToRef;
78}
79
80pub trait VecZnxSubScalarInplace {
81 /// Subtracts the selected column of `a` on the selected column and limb of `res`.
82 fn vec_znx_sub_scalar_inplace<R, A>(&self, res: &mut R, res_col: usize, res_limb: usize, a: &A, a_col: usize)
83 where
84 R: VecZnxToMut,
85 A: ScalarZnxToRef;
86}
87
88pub trait VecZnxNegate {
89 // Negates the selected column of `a` and stores the result in `res_col` of `res`.
90 fn vec_znx_negate<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 VecZnxNegateInplace {
97 /// Negates the selected column of `a`.
98 fn vec_znx_negate_inplace<A>(&self, a: &mut A, a_col: usize)
99 where
100 A: VecZnxToMut;
101}
102
103pub trait VecZnxLshInplace {
104 /// Left shift by k bits all columns of `a`.
105 fn vec_znx_lsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A)
106 where
107 A: VecZnxToMut;
108}
109
110pub trait VecZnxRshInplace {
111 /// Right shift by k bits all columns of `a`.
112 fn vec_znx_rsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A)
113 where
114 A: VecZnxToMut;
115}
116
117pub trait VecZnxRotate {
118 /// Multiplies the selected column of `a` by X^k and stores the result in `res_col` of `res`.
119 fn vec_znx_rotate<R, A>(&self, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
120 where
121 R: VecZnxToMut,
122 A: VecZnxToRef;
123}
124
125pub trait VecZnxRotateInplace {
126 /// Multiplies the selected column of `a` by X^k.
127 fn vec_znx_rotate_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
128 where
129 A: VecZnxToMut;
130}
131
132pub trait VecZnxAutomorphism {
133 /// Applies the automorphism X^i -> X^ik on the selected column of `a` and stores the result in `res_col` column of `res`.
134 fn vec_znx_automorphism<R, A>(&self, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
135 where
136 R: VecZnxToMut,
137 A: VecZnxToRef;
138}
139
140pub trait VecZnxAutomorphismInplace {
141 /// Applies the automorphism X^i -> X^ik on the selected column of `a`.
142 fn vec_znx_automorphism_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
143 where
144 A: VecZnxToMut;
145}
146
147pub trait VecZnxMulXpMinusOne {
148 fn vec_znx_mul_xp_minus_one<R, A>(&self, p: i64, r: &mut R, r_col: usize, a: &A, a_col: usize)
149 where
150 R: VecZnxToMut,
151 A: VecZnxToRef;
152}
153
154pub trait VecZnxMulXpMinusOneInplace {
155 fn vec_znx_mul_xp_minus_one_inplace<R>(&self, p: i64, r: &mut R, r_col: usize)
156 where
157 R: VecZnxToMut;
158}
159
160pub trait VecZnxSplit<B: Backend> {
161 /// Splits the selected columns of `b` into subrings and copies them them into the selected column of `res`.
162 ///
163 /// # Panics
164 ///
165 /// This method requires that all [crate::hal::layouts::VecZnx] of b have the same ring degree
166 /// and that b.n() * b.len() <= a.n()
167 fn vec_znx_split<R, A>(&self, res: &mut Vec<R>, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
168 where
169 R: VecZnxToMut,
170 A: VecZnxToRef;
171}
172
173pub trait VecZnxMerge {
174 /// Merges the subrings of the selected column of `a` into the selected column of `res`.
175 ///
176 /// # Panics
177 ///
178 /// This method requires that all [crate::hal::layouts::VecZnx] of a have the same ring degree
179 /// and that a.n() * a.len() <= b.n()
180 fn vec_znx_merge<R, A>(&self, res: &mut R, res_col: usize, a: Vec<A>, a_col: usize)
181 where
182 R: VecZnxToMut,
183 A: VecZnxToRef;
184}
185
186pub trait VecZnxSwithcDegree {
187 fn vec_znx_switch_degree<R, A>(&self, res: &mut R, res_col: usize, a: &A, col_a: usize)
188 where
189 R: VecZnxToMut,
190 A: VecZnxToRef;
191}
192
193pub trait VecZnxCopy {
194 fn vec_znx_copy<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
195 where
196 R: VecZnxToMut,
197 A: VecZnxToRef;
198}
199
200pub trait VecZnxFillUniform {
201 /// Fills the first `size` size with uniform values in \[-2^{basek-1}, 2^{basek-1}\]
202 fn vec_znx_fill_uniform<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source)
203 where
204 R: VecZnxToMut;
205}
206
207pub trait VecZnxFillDistF64 {
208 fn vec_znx_fill_dist_f64<R, D: Distribution<f64>>(
209 &self,
210 basek: usize,
211 res: &mut R,
212 res_col: usize,
213 k: usize,
214 source: &mut Source,
215 dist: D,
216 bound: f64,
217 ) where
218 R: VecZnxToMut;
219}
220
221pub trait VecZnxAddDistF64 {
222 /// Adds vector sampled according to the provided distribution, scaled by 2^{-k} and bounded to \[-bound, bound\].
223 fn vec_znx_add_dist_f64<R, D: Distribution<f64>>(
224 &self,
225 basek: usize,
226 res: &mut R,
227 res_col: usize,
228 k: usize,
229 source: &mut Source,
230 dist: D,
231 bound: f64,
232 ) where
233 R: VecZnxToMut;
234}
235
236pub trait VecZnxFillNormal {
237 fn vec_znx_fill_normal<R>(
238 &self,
239 basek: usize,
240 res: &mut R,
241 res_col: usize,
242 k: usize,
243 source: &mut Source,
244 sigma: f64,
245 bound: f64,
246 ) where
247 R: VecZnxToMut;
248}
249
250pub trait VecZnxAddNormal {
251 /// Adds a discrete normal vector scaled by 2^{-k} with the provided standard deviation and bounded to \[-bound, bound\].
252 fn vec_znx_add_normal<R>(
253 &self,
254 basek: usize,
255 res: &mut R,
256 res_col: usize,
257 k: usize,
258 source: &mut Source,
259 sigma: f64,
260 bound: f64,
261 ) where
262 R: VecZnxToMut;
263}