poulpy/backend/src/hal/api/vec_znx_big.rs
backend/src/hal/api/vec_znx_big.rsRaw
1use rand_distr::Distribution;
2use sampling::source::Source;
3
4use crate::hal::layouts::{Backend, Scratch, VecZnxBigOwned, VecZnxBigToMut, VecZnxBigToRef, VecZnxToMut, VecZnxToRef};
5
6/// Allocates as [crate::hal::layouts::VecZnxBig].
7pub trait VecZnxBigAlloc<B: Backend> {
8 fn vec_znx_big_alloc(&self, cols: usize, size: usize) -> VecZnxBigOwned<B>;
9}
10
11/// Returns the size in bytes to allocate a [crate::hal::layouts::VecZnxBig].
12pub trait VecZnxBigAllocBytes {
13 fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize;
14}
15
16/// Consume a vector of bytes into a [crate::hal::layouts::VecZnxBig].
17/// User must ensure that bytes is memory aligned and that it length is equal to [VecZnxBigAllocBytes].
18pub trait VecZnxBigFromBytes<B: Backend> {
19 fn vec_znx_big_from_bytes(&self, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxBigOwned<B>;
20}
21
22/// Add a discrete normal distribution on res.
23///
24/// # Arguments
25/// * `basek`: base two logarithm of the bivariate representation
26/// * `res`: receiver.
27/// * `res_col`: column of the receiver on which the operation is performed/stored.
28/// * `k`:
29/// * `source`: random coin source.
30/// * `sigma`: standard deviation of the discrete normal distribution.
31/// * `bound`: rejection sampling bound.
32pub trait VecZnxBigAddNormal<B: Backend> {
33 fn vec_znx_big_add_normal<R: VecZnxBigToMut<B>>(
34 &self,
35 basek: usize,
36 res: &mut R,
37 res_col: usize,
38 k: usize,
39 source: &mut Source,
40 sigma: f64,
41 bound: f64,
42 );
43}
44
45pub trait VecZnxBigFillNormal<B: Backend> {
46 fn vec_znx_big_fill_normal<R: VecZnxBigToMut<B>>(
47 &self,
48 basek: usize,
49 res: &mut R,
50 res_col: usize,
51 k: usize,
52 source: &mut Source,
53 sigma: f64,
54 bound: f64,
55 );
56}
57
58pub trait VecZnxBigFillDistF64<B: Backend> {
59 fn vec_znx_big_fill_dist_f64<R: VecZnxBigToMut<B>, D: Distribution<f64>>(
60 &self,
61 basek: usize,
62 res: &mut R,
63 res_col: usize,
64 k: usize,
65 source: &mut Source,
66 dist: D,
67 bound: f64,
68 );
69}
70
71pub trait VecZnxBigAddDistF64<B: Backend> {
72 fn vec_znx_big_add_dist_f64<R: VecZnxBigToMut<B>, D: Distribution<f64>>(
73 &self,
74 basek: usize,
75 res: &mut R,
76 res_col: usize,
77 k: usize,
78 source: &mut Source,
79 dist: D,
80 bound: f64,
81 );
82}
83
84pub trait VecZnxBigAdd<B: Backend> {
85 /// Adds `a` to `b` and stores the result on `c`.
86 fn vec_znx_big_add<R, A, C>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize)
87 where
88 R: VecZnxBigToMut<B>,
89 A: VecZnxBigToRef<B>,
90 C: VecZnxBigToRef<B>;
91}
92
93pub trait VecZnxBigAddInplace<B: Backend> {
94 /// Adds `a` to `b` and stores the result on `b`.
95 fn vec_znx_big_add_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
96 where
97 R: VecZnxBigToMut<B>,
98 A: VecZnxBigToRef<B>;
99}
100
101pub trait VecZnxBigAddSmall<B: Backend> {
102 /// Adds `a` to `b` and stores the result on `c`.
103 fn vec_znx_big_add_small<R, A, C>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize)
104 where
105 R: VecZnxBigToMut<B>,
106 A: VecZnxBigToRef<B>,
107 C: VecZnxToRef;
108}
109
110pub trait VecZnxBigAddSmallInplace<B: Backend> {
111 /// Adds `a` to `b` and stores the result on `b`.
112 fn vec_znx_big_add_small_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
113 where
114 R: VecZnxBigToMut<B>,
115 A: VecZnxToRef;
116}
117
118pub trait VecZnxBigSub<B: Backend> {
119 /// Subtracts `a` to `b` and stores the result on `c`.
120 fn vec_znx_big_sub<R, A, C>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize)
121 where
122 R: VecZnxBigToMut<B>,
123 A: VecZnxBigToRef<B>,
124 C: VecZnxBigToRef<B>;
125}
126
127pub trait VecZnxBigSubABInplace<B: Backend> {
128 /// Subtracts `a` from `b` and stores the result on `b`.
129 fn vec_znx_big_sub_ab_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
130 where
131 R: VecZnxBigToMut<B>,
132 A: VecZnxBigToRef<B>;
133}
134
135pub trait VecZnxBigSubBAInplace<B: Backend> {
136 /// Subtracts `b` from `a` and stores the result on `b`.
137 fn vec_znx_big_sub_ba_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
138 where
139 R: VecZnxBigToMut<B>,
140 A: VecZnxBigToRef<B>;
141}
142
143pub trait VecZnxBigSubSmallA<B: Backend> {
144 /// Subtracts `b` from `a` and stores the result on `c`.
145 fn vec_znx_big_sub_small_a<R, A, C>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize)
146 where
147 R: VecZnxBigToMut<B>,
148 A: VecZnxToRef,
149 C: VecZnxBigToRef<B>;
150}
151
152pub trait VecZnxBigSubSmallAInplace<B: Backend> {
153 /// Subtracts `a` from `res` and stores the result on `res`.
154 fn vec_znx_big_sub_small_a_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
155 where
156 R: VecZnxBigToMut<B>,
157 A: VecZnxToRef;
158}
159
160pub trait VecZnxBigSubSmallB<B: Backend> {
161 /// Subtracts `b` from `a` and stores the result on `c`.
162 fn vec_znx_big_sub_small_b<R, A, C>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize)
163 where
164 R: VecZnxBigToMut<B>,
165 A: VecZnxBigToRef<B>,
166 C: VecZnxToRef;
167}
168
169pub trait VecZnxBigSubSmallBInplace<B: Backend> {
170 /// Subtracts `res` from `a` and stores the result on `res`.
171 fn vec_znx_big_sub_small_b_inplace<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
172 where
173 R: VecZnxBigToMut<B>,
174 A: VecZnxToRef;
175}
176
177pub trait VecZnxBigNegateInplace<B: Backend> {
178 fn vec_znx_big_negate_inplace<A>(&self, a: &mut A, a_col: usize)
179 where
180 A: VecZnxBigToMut<B>;
181}
182
183pub trait VecZnxBigNormalizeTmpBytes {
184 fn vec_znx_big_normalize_tmp_bytes(&self, n: usize) -> usize;
185}
186
187pub trait VecZnxBigNormalize<B: Backend> {
188 fn vec_znx_big_normalize<R, A>(
189 &self,
190 basek: usize,
191 res: &mut R,
192 res_col: usize,
193 a: &A,
194 a_col: usize,
195 scratch: &mut Scratch<B>,
196 ) where
197 R: VecZnxToMut,
198 A: VecZnxBigToRef<B>;
199}
200
201pub trait VecZnxBigAutomorphism<B: Backend> {
202 /// Applies the automorphism X^i -> X^ik on `a` and stores the result on `b`.
203 fn vec_znx_big_automorphism<R, A>(&self, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize)
204 where
205 R: VecZnxBigToMut<B>,
206 A: VecZnxBigToRef<B>;
207}
208
209pub trait VecZnxBigAutomorphismInplace<B: Backend> {
210 /// Applies the automorphism X^i -> X^ik on `a` and stores the result on `a`.
211 fn vec_znx_big_automorphism_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
212 where
213 A: VecZnxBigToMut<B>;
214}