Skip to main content

poulpy_hal/api/
vec_znx_big.rs

1use crate::{
2    layouts::{
3        Backend, NoiseInfos, ScalarZnxBackendRef, ScratchArena, VecZnxBackendMut, VecZnxBackendRef, VecZnxBigBackendMut,
4        VecZnxBigBackendRef, VecZnxBigOwned,
5    },
6    source::Source,
7};
8
9/// Converts a coefficient-domain [`VecZnx`](crate::layouts::VecZnx) column
10/// into a [`VecZnxBig`](crate::layouts::VecZnxBig) column.
11pub trait VecZnxBigFromSmallBackend<B: Backend> {
12    fn vec_znx_big_from_small_backend(
13        &self,
14        res: &mut VecZnxBigBackendMut<'_, B>,
15        res_col: usize,
16        a: &VecZnxBackendRef<'_, B>,
17        a_col: usize,
18    );
19}
20
21/// Allocates as [crate::layouts::VecZnxBig].
22pub trait VecZnxBigAlloc<B: Backend> {
23    fn vec_znx_big_alloc(&self, cols: usize, size: usize) -> VecZnxBigOwned<B>;
24
25    fn vec_znx_big_alloc_n(&self, n: usize, cols: usize, size: usize) -> VecZnxBigOwned<B>;
26}
27
28/// Returns the size in bytes to allocate a [crate::layouts::VecZnxBig].
29pub trait VecZnxBigBytesOf {
30    fn bytes_of_vec_znx_big(&self, cols: usize, size: usize) -> usize;
31
32    fn bytes_of_vec_znx_big_n(&self, n: usize, cols: usize, size: usize) -> usize;
33}
34
35/// Consume a vector of bytes into a [crate::layouts::VecZnxBig].
36/// User must ensure that bytes is memory aligned and that its length is equal to [VecZnxBigBytesOf::bytes_of_vec_znx_big].
37pub trait VecZnxBigFromBytes<B: Backend> {
38    fn vec_znx_big_from_bytes(&self, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxBigOwned<B>;
39
40    fn vec_znx_big_from_bytes_n(&self, n: usize, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxBigOwned<B>;
41}
42
43#[allow(clippy::too_many_arguments)]
44/// Add a discrete normal distribution on res.
45///
46/// # Arguments
47/// * `base2k`: base two logarithm of the bivariate representation
48/// * `res`: receiver.
49/// * `res_col`: column of the receiver on which the operation is performed/stored.
50/// * `k`:
51/// * `source`: random coin source.
52/// * `sigma`: standard deviation of the discrete normal distribution.
53/// * `bound`: rejection sampling bound.
54pub trait VecZnxBigAddNormal<B: Backend> {
55    fn vec_znx_big_add_normal(
56        &self,
57        base2k: usize,
58        res: &mut VecZnxBigBackendMut<'_, B>,
59        res_col: usize,
60        noise_infos: NoiseInfos,
61        source: &mut Source,
62    );
63}
64
65#[allow(clippy::too_many_arguments)]
66pub trait VecZnxBigAddNormalBackend<B: Backend> {
67    fn vec_znx_big_add_normal_backend(
68        &self,
69        base2k: usize,
70        res: &mut VecZnxBigBackendMut<'_, B>,
71        res_col: usize,
72        noise_infos: NoiseInfos,
73        seed: [u8; 32],
74    );
75}
76
77pub trait VecZnxBigAddInto<B: Backend> {
78    /// Adds `a` to `b` and stores the result on `c`.
79    fn vec_znx_big_add_into(
80        &self,
81        res: &mut VecZnxBigBackendMut<'_, B>,
82        res_col: usize,
83        a: &VecZnxBigBackendRef<'_, B>,
84        a_col: usize,
85        b: &VecZnxBigBackendRef<'_, B>,
86        b_col: usize,
87    );
88}
89
90pub trait VecZnxBigAddAssign<B: Backend> {
91    /// Adds `a` to `b` and stores the result on `b`.
92    fn vec_znx_big_add_assign(
93        &self,
94        res: &mut VecZnxBigBackendMut<'_, B>,
95        res_col: usize,
96        a: &VecZnxBigBackendRef<'_, B>,
97        a_col: usize,
98    );
99}
100
101pub trait VecZnxBigAddSmallIntoBackend<B: Backend> {
102    /// Adds `a` to `b` and stores the result on `c`.
103    fn vec_znx_big_add_small_into_backend(
104        &self,
105        res: &mut VecZnxBigBackendMut<'_, B>,
106        res_col: usize,
107        a: &VecZnxBigBackendRef<'_, B>,
108        a_col: usize,
109        b: &VecZnxBackendRef<'_, B>,
110        b_col: usize,
111    );
112}
113
114pub trait VecZnxBigAddSmallAssign<B: Backend> {
115    /// Adds `a` to `b` and stores the result on `b`.
116    fn vec_znx_big_add_small_assign<'r, 'a>(
117        &self,
118        res: &mut VecZnxBigBackendMut<'r, B>,
119        res_col: usize,
120        a: &VecZnxBackendRef<'a, B>,
121        a_col: usize,
122    );
123}
124
125pub trait VecZnxBigSub<B: Backend> {
126    /// Subtracts `a` to `b` and stores the result on `c`.
127    fn vec_znx_big_sub(
128        &self,
129        res: &mut VecZnxBigBackendMut<'_, B>,
130        res_col: usize,
131        a: &VecZnxBigBackendRef<'_, B>,
132        a_col: usize,
133        b: &VecZnxBigBackendRef<'_, B>,
134        b_col: usize,
135    );
136}
137
138pub trait VecZnxBigSubAssign<B: Backend> {
139    /// Subtracts `a` from `b` and stores the result on `b`.
140    fn vec_znx_big_sub_assign(
141        &self,
142        res: &mut VecZnxBigBackendMut<'_, B>,
143        res_col: usize,
144        a: &VecZnxBigBackendRef<'_, B>,
145        a_col: usize,
146    );
147}
148
149pub trait VecZnxBigSubNegateAssign<B: Backend> {
150    /// Subtracts `b` from `a` and stores the result on `b`.
151    fn vec_znx_big_sub_negate_assign(
152        &self,
153        res: &mut VecZnxBigBackendMut<'_, B>,
154        res_col: usize,
155        a: &VecZnxBigBackendRef<'_, B>,
156        a_col: usize,
157    );
158}
159
160pub trait VecZnxBigSubSmallABackend<B: Backend> {
161    /// Subtracts `b` from `a` and stores the result on `c`.
162    fn vec_znx_big_sub_small_a_backend(
163        &self,
164        res: &mut VecZnxBigBackendMut<'_, B>,
165        res_col: usize,
166        a: &VecZnxBackendRef<'_, B>,
167        a_col: usize,
168        b: &VecZnxBigBackendRef<'_, B>,
169        b_col: usize,
170    );
171}
172
173pub trait VecZnxBigSubSmallAssign<B: Backend> {
174    /// Subtracts `a` from `res` and stores the result on `res`.
175    fn vec_znx_big_sub_small_assign<'r, 'a>(
176        &self,
177        res: &mut VecZnxBigBackendMut<'r, B>,
178        res_col: usize,
179        a: &VecZnxBackendRef<'a, B>,
180        a_col: usize,
181    );
182}
183
184pub trait VecZnxBigSubSmallBBackend<B: Backend> {
185    /// Subtracts `b` from `a` and stores the result on `c`.
186    fn vec_znx_big_sub_small_b_backend(
187        &self,
188        res: &mut VecZnxBigBackendMut<'_, B>,
189        res_col: usize,
190        a: &VecZnxBigBackendRef<'_, B>,
191        a_col: usize,
192        b: &VecZnxBackendRef<'_, B>,
193        b_col: usize,
194    );
195}
196
197pub trait VecZnxBigSubSmallNegateAssign<B: Backend> {
198    /// Subtracts `res` from `a` and stores the result on `res`.
199    fn vec_znx_big_sub_small_negate_assign<'r, 'a>(
200        &self,
201        res: &mut VecZnxBigBackendMut<'r, B>,
202        res_col: usize,
203        a: &VecZnxBackendRef<'a, B>,
204        a_col: usize,
205    );
206}
207
208/// Sums coefficients from a selected [`VecZnxBig`](crate::layouts::VecZnxBig)
209/// column and stores each limb's result in one destination coefficient.
210pub trait VecZnxBigInnerSumBackend<B: Backend> {
211    fn vec_znx_big_inner_sum_backend<'r, 'a>(
212        &self,
213        res: &mut VecZnxBigBackendMut<'r, B>,
214        res_col: usize,
215        res_coeff: usize,
216        a: &VecZnxBigBackendRef<'a, B>,
217        a_col: usize,
218    );
219}
220
221/// Computes the element-wise (Hadamard) product `res[k] = a[k] * b[k]` for all `k`
222/// and stores each product as a [`ScalarBig`](Backend::ScalarBig) value in `res`.
223/// Use [`VecZnxBigInnerSumBackend`] afterwards to reduce to a single scalar.
224pub trait VecZnxScalarProduct<B: Backend> {
225    fn vec_znx_scalar_product<'r, 'a, 'b>(
226        &self,
227        res: &mut VecZnxBigBackendMut<'r, B>,
228        res_col: usize,
229        a: &VecZnxBackendRef<'a, B>,
230        a_col: usize,
231        b: &ScalarZnxBackendRef<'b, B>,
232        b_col: usize,
233    );
234}
235
236/// Negates the selected column of `a` and stores the result in `res`.
237pub trait VecZnxBigNegate<B: Backend> {
238    fn vec_znx_big_negate(
239        &self,
240        res: &mut VecZnxBigBackendMut<'_, B>,
241        res_col: usize,
242        a: &VecZnxBigBackendRef<'_, B>,
243        a_col: usize,
244    );
245}
246
247/// Negates the selected column of `res` in-place.
248pub trait VecZnxBigNegateAssign<B: Backend> {
249    fn vec_znx_big_negate_assign(&self, res: &mut VecZnxBigBackendMut<'_, B>, res_col: usize);
250}
251
252/// Returns scratch bytes required for [`VecZnxBigNormalize`].
253pub trait VecZnxBigNormalizeTmpBytes {
254    fn vec_znx_big_normalize_tmp_bytes(&self) -> usize;
255}
256
257#[allow(clippy::too_many_arguments)]
258/// Normalizes a [`VecZnxBig`](crate::layouts::VecZnxBig) into a coefficient-domain
259/// [`VecZnx`](crate::layouts::VecZnx) with the target base and offset.
260pub trait VecZnxBigNormalize<B: Backend> {
261    fn vec_znx_big_normalize(
262        &self,
263        res: &mut VecZnxBackendMut<'_, B>,
264        res_base2k: usize,
265        res_offset: i64,
266        res_col: usize,
267        a: &VecZnxBigBackendRef<'_, B>,
268        a_base2k: usize,
269        a_col: usize,
270        scratch: &mut ScratchArena<'_, B>,
271    );
272}
273
274/// Returns scratch bytes required for in-place automorphism on [`VecZnxBig`](crate::layouts::VecZnxBig).
275pub trait VecZnxBigAutomorphismAssignTmpBytes {
276    fn vec_znx_big_automorphism_assign_tmp_bytes(&self) -> usize;
277}
278
279pub trait VecZnxBigAutomorphism<B: Backend> {
280    /// Applies the automorphism X^i -> X^ik on `a` and stores the result on `b`.
281    fn vec_znx_big_automorphism(
282        &self,
283        p: i64,
284        res: &mut VecZnxBigBackendMut<'_, B>,
285        res_col: usize,
286        a: &VecZnxBigBackendRef<'_, B>,
287        a_col: usize,
288    );
289}
290
291pub trait VecZnxBigAutomorphismAssign<B: Backend> {
292    /// Applies the automorphism X^i -> X^ik on `a` and stores the result on `a`.
293    fn vec_znx_big_automorphism_assign(
294        &self,
295        p: i64,
296        res: &mut VecZnxBigBackendMut<'_, B>,
297        res_col: usize,
298        scratch: &mut ScratchArena<'_, B>,
299    );
300}