poulpy/base2k/src/svp.rs
base2k/src/svp.rsRaw
1use crate::ffi::svp::{self, svp_ppol_t};
2use crate::ffi::vec_znx_dft::vec_znx_dft_t;
3use crate::{BACKEND, Module, VecZnx, VecZnxDft, assert_alignement};
4
5use crate::{Infos, alloc_aligned, cast_mut};
6use rand::seq::SliceRandom;
7use rand_core::RngCore;
8use rand_distr::{Distribution, weighted::WeightedIndex};
9use sampling::source::Source;
10
11pub struct Scalar {
12 pub n: usize,
13 pub data: Vec<i64>,
14 pub ptr: *mut i64,
15}
16
17impl Module {
18 pub fn new_scalar(&self) -> Scalar {
19 Scalar::new(self.n())
20 }
21}
22
23impl Scalar {
24 pub fn new(n: usize) -> Self {
25 let mut data: Vec<i64> = alloc_aligned::<i64>(n);
26 let ptr: *mut i64 = data.as_mut_ptr();
27 Self {
28 n: n,
29 data: data,
30 ptr: ptr,
31 }
32 }
33
34 pub fn n(&self) -> usize {
35 self.n
36 }
37
38 pub fn bytes_of(n: usize) -> usize {
39 n * std::mem::size_of::<i64>()
40 }
41
42 pub fn from_bytes(n: usize, bytes: &mut [u8]) -> Self {
43 let size: usize = Self::bytes_of(n);
44 debug_assert!(
45 bytes.len() == size,
46 "invalid buffer: bytes.len()={} < self.bytes_of(n={})={}",
47 bytes.len(),
48 n,
49 size
50 );
51 #[cfg(debug_assertions)]
52 {
53 assert_alignement(bytes.as_ptr())
54 }
55 unsafe {
56 let bytes_i64: &mut [i64] = cast_mut::<u8, i64>(bytes);
57 let ptr: *mut i64 = bytes_i64.as_mut_ptr();
58 Self {
59 n: n,
60 data: Vec::from_raw_parts(bytes_i64.as_mut_ptr(), bytes.len(), bytes.len()),
61 ptr: ptr,
62 }
63 }
64 }
65
66 pub fn from_bytes_borrow(n: usize, bytes: &mut [u8]) -> Self {
67 let size: usize = Self::bytes_of(n);
68 debug_assert!(
69 bytes.len() == size,
70 "invalid buffer: bytes.len()={} < self.bytes_of(n={})={}",
71 bytes.len(),
72 n,
73 size
74 );
75 #[cfg(debug_assertions)]
76 {
77 assert_alignement(bytes.as_ptr())
78 }
79 let bytes_i64: &mut [i64] = cast_mut::<u8, i64>(bytes);
80 let ptr: *mut i64 = bytes_i64.as_mut_ptr();
81 Self {
82 n: n,
83 data: Vec::new(),
84 ptr: ptr,
85 }
86 }
87
88 pub fn as_ptr(&self) -> *const i64 {
89 self.ptr
90 }
91
92 pub fn raw(&self) -> &[i64] {
93 unsafe { std::slice::from_raw_parts(self.ptr, self.n) }
94 }
95
96 pub fn raw_mut(&self) -> &mut [i64] {
97 unsafe { std::slice::from_raw_parts_mut(self.ptr, self.n) }
98 }
99
100 pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) {
101 let choices: [i64; 3] = [-1, 0, 1];
102 let weights: [f64; 3] = [prob / 2.0, 1.0 - prob, prob / 2.0];
103 let dist: WeightedIndex<f64> = WeightedIndex::new(&weights).unwrap();
104 self.data
105 .iter_mut()
106 .for_each(|x: &mut i64| *x = choices[dist.sample(source)]);
107 }
108
109 pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) {
110 assert!(hw <= self.n());
111 self.data[..hw]
112 .iter_mut()
113 .for_each(|x: &mut i64| *x = (((source.next_u32() & 1) as i64) << 1) - 1);
114 self.data.shuffle(source);
115 }
116
117 pub fn as_vec_znx(&self) -> VecZnx {
118 VecZnx {
119 n: self.n,
120 size: 1, // TODO REVIEW IF NEED TO ADD size TO SCALAR
121 cols: 1,
122 data: Vec::new(),
123 ptr: self.ptr,
124 }
125 }
126}
127
128pub trait ScalarOps {
129 fn bytes_of_scalar(&self) -> usize;
130 fn new_scalar(&self) -> Scalar;
131 fn new_scalar_from_bytes(&self, bytes: &mut [u8]) -> Scalar;
132 fn new_scalar_from_bytes_borrow(&self, tmp_bytes: &mut [u8]) -> Scalar;
133}
134impl ScalarOps for Module {
135 fn bytes_of_scalar(&self) -> usize {
136 Scalar::bytes_of(self.n())
137 }
138 fn new_scalar(&self) -> Scalar {
139 Scalar::new(self.n())
140 }
141 fn new_scalar_from_bytes(&self, bytes: &mut [u8]) -> Scalar {
142 Scalar::from_bytes(self.n(), bytes)
143 }
144 fn new_scalar_from_bytes_borrow(&self, tmp_bytes: &mut [u8]) -> Scalar {
145 Scalar::from_bytes_borrow(self.n(), tmp_bytes)
146 }
147}
148
149pub struct SvpPPol {
150 pub n: usize,
151 pub data: Vec<u8>,
152 pub ptr: *mut u8,
153 pub backend: BACKEND,
154}
155
156/// A prepared [crate::Scalar] for [SvpPPolOps::svp_apply_dft].
157/// An [SvpPPol] an be seen as a [VecZnxDft] of one limb.
158impl SvpPPol {
159 pub fn new(module: &Module) -> Self {
160 module.new_svp_ppol()
161 }
162
163 /// Returns the ring degree of the [SvpPPol].
164 pub fn n(&self) -> usize {
165 self.n
166 }
167
168 pub fn bytes_of(module: &Module) -> usize {
169 module.bytes_of_svp_ppol()
170 }
171
172 pub fn from_bytes(module: &Module, bytes: &mut [u8]) -> SvpPPol {
173 #[cfg(debug_assertions)]
174 {
175 assert_alignement(bytes.as_ptr());
176 assert_eq!(bytes.len(), module.bytes_of_svp_ppol());
177 }
178 unsafe {
179 Self {
180 n: module.n(),
181 data: Vec::from_raw_parts(bytes.as_mut_ptr(), bytes.len(), bytes.len()),
182 ptr: bytes.as_mut_ptr(),
183 backend: module.backend(),
184 }
185 }
186 }
187
188 pub fn from_bytes_borrow(module: &Module, tmp_bytes: &mut [u8]) -> SvpPPol {
189 #[cfg(debug_assertions)]
190 {
191 assert_alignement(tmp_bytes.as_ptr());
192 assert_eq!(tmp_bytes.len(), module.bytes_of_svp_ppol());
193 }
194 Self {
195 n: module.n(),
196 data: Vec::new(),
197 ptr: tmp_bytes.as_mut_ptr(),
198 backend: module.backend(),
199 }
200 }
201
202 /// Returns the number of cols of the [SvpPPol], which is always 1.
203 pub fn cols(&self) -> usize {
204 1
205 }
206}
207
208pub trait SvpPPolOps {
209 /// Allocates a new [SvpPPol].
210 fn new_svp_ppol(&self) -> SvpPPol;
211
212 /// Returns the minimum number of bytes necessary to allocate
213 /// a new [SvpPPol] through [SvpPPol::from_bytes] ro.
214 fn bytes_of_svp_ppol(&self) -> usize;
215
216 /// Allocates a new [SvpPPol] from an array of bytes.
217 /// The array of bytes is owned by the [SvpPPol].
218 /// The method will panic if bytes.len() < [SvpPPolOps::bytes_of_svp_ppol]
219 fn new_svp_ppol_from_bytes(&self, bytes: &mut [u8]) -> SvpPPol;
220
221 /// Allocates a new [SvpPPol] from an array of bytes.
222 /// The array of bytes is borrowed by the [SvpPPol].
223 /// The method will panic if bytes.len() < [SvpPPolOps::bytes_of_svp_ppol]
224 fn new_svp_ppol_from_bytes_borrow(&self, tmp_bytes: &mut [u8]) -> SvpPPol;
225
226 /// Prepares a [crate::Scalar] for a [SvpPPolOps::svp_apply_dft].
227 fn svp_prepare(&self, svp_ppol: &mut SvpPPol, a: &Scalar);
228
229 /// Applies the [SvpPPol] x [VecZnxDft] product, where each limb of
230 /// the [VecZnxDft] is multiplied with [SvpPPol].
231 fn svp_apply_dft(&self, c: &mut VecZnxDft, a: &SvpPPol, b: &VecZnx);
232}
233
234impl SvpPPolOps for Module {
235 fn new_svp_ppol(&self) -> SvpPPol {
236 let mut data: Vec<u8> = alloc_aligned::<u8>(self.bytes_of_svp_ppol());
237 let ptr: *mut u8 = data.as_mut_ptr();
238 SvpPPol {
239 data: data,
240 ptr: ptr,
241 n: self.n(),
242 backend: self.backend(),
243 }
244 }
245
246 fn bytes_of_svp_ppol(&self) -> usize {
247 unsafe { svp::bytes_of_svp_ppol(self.ptr) as usize }
248 }
249
250 fn new_svp_ppol_from_bytes(&self, bytes: &mut [u8]) -> SvpPPol {
251 SvpPPol::from_bytes(self, bytes)
252 }
253
254 fn new_svp_ppol_from_bytes_borrow(&self, tmp_bytes: &mut [u8]) -> SvpPPol {
255 SvpPPol::from_bytes_borrow(self, tmp_bytes)
256 }
257
258 fn svp_prepare(&self, svp_ppol: &mut SvpPPol, a: &Scalar) {
259 unsafe { svp::svp_prepare(self.ptr, svp_ppol.ptr as *mut svp_ppol_t, a.as_ptr()) }
260 }
261
262 fn svp_apply_dft(&self, c: &mut VecZnxDft, a: &SvpPPol, b: &VecZnx) {
263 unsafe {
264 svp::svp_apply_dft(
265 self.ptr,
266 c.ptr as *mut vec_znx_dft_t,
267 c.cols() as u64,
268 a.ptr as *const svp_ppol_t,
269 b.as_ptr(),
270 b.cols() as u64,
271 b.n() as u64,
272 )
273 }
274 }
275}