poulpy/poulpy-core/src/layouts/prepared/gglwe_ct.rs
poulpy-core/src/layouts/prepared/gglwe_ct.rsRaw
1use poulpy_hal::{
2 api::{VmpPMatAlloc, VmpPMatBytesOf, VmpPrepare, VmpPrepareTmpBytes},
3 layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos},
4};
5
6use crate::layouts::{
7 Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision,
8};
9
10#[derive(PartialEq, Eq)]
11pub struct GGLWEPrepared<D: Data, B: Backend> {
12 pub(crate) data: VmpPMat<D, B>,
13 pub(crate) k: TorusPrecision,
14 pub(crate) base2k: Base2K,
15 pub(crate) dsize: Dsize,
16}
17
18impl<D: Data, B: Backend> LWEInfos for GGLWEPrepared<D, B> {
19 fn n(&self) -> Degree {
20 Degree(self.data.n() as u32)
21 }
22
23 fn base2k(&self) -> Base2K {
24 self.base2k
25 }
26
27 fn k(&self) -> TorusPrecision {
28 self.k
29 }
30
31 fn size(&self) -> usize {
32 self.data.size()
33 }
34}
35
36impl<D: Data, B: Backend> GLWEInfos for GGLWEPrepared<D, B> {
37 fn rank(&self) -> Rank {
38 self.rank_out()
39 }
40}
41
42impl<D: Data, B: Backend> GGLWEInfos for GGLWEPrepared<D, B> {
43 fn rank_in(&self) -> Rank {
44 Rank(self.data.cols_in() as u32)
45 }
46
47 fn rank_out(&self) -> Rank {
48 Rank(self.data.cols_out() as u32 - 1)
49 }
50
51 fn dsize(&self) -> Dsize {
52 self.dsize
53 }
54
55 fn dnum(&self) -> Dnum {
56 Dnum(self.data.rows() as u32)
57 }
58}
59
60pub trait GGLWEPreparedAlloc<B: Backend>
61where
62 Self: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf,
63{
64 fn alloc_gglwe_prepared(
65 &self,
66 base2k: Base2K,
67 k: TorusPrecision,
68 rank_in: Rank,
69 rank_out: Rank,
70 dnum: Dnum,
71 dsize: Dsize,
72 ) -> GGLWEPrepared<Vec<u8>, B> {
73 let size: usize = k.0.div_ceil(base2k.0) as usize;
74 debug_assert!(
75 size as u32 > dsize.0,
76 "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}",
77 dsize.0
78 );
79
80 assert!(
81 dnum.0 * dsize.0 <= size as u32,
82 "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}",
83 dnum.0,
84 dsize.0,
85 );
86
87 GGLWEPrepared {
88 data: self.vmp_pmat_alloc(dnum.into(), rank_in.into(), (rank_out + 1).into(), size),
89 k,
90 base2k,
91 dsize,
92 }
93 }
94
95 fn alloc_gglwe_prepared_from_infos<A>(&self, infos: &A) -> GGLWEPrepared<Vec<u8>, B>
96 where
97 A: GGLWEInfos,
98 {
99 assert_eq!(self.ring_degree(), infos.n());
100 self.alloc_gglwe_prepared(
101 infos.base2k(),
102 infos.k(),
103 infos.rank_in(),
104 infos.rank_out(),
105 infos.dnum(),
106 infos.dsize(),
107 )
108 }
109
110 fn bytes_of_gglwe_prepared(
111 &self,
112 base2k: Base2K,
113 k: TorusPrecision,
114 rank_in: Rank,
115 rank_out: Rank,
116 dnum: Dnum,
117 dsize: Dsize,
118 ) -> usize {
119 let size: usize = k.0.div_ceil(base2k.0) as usize;
120 debug_assert!(
121 size as u32 > dsize.0,
122 "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}",
123 dsize.0
124 );
125
126 assert!(
127 dnum.0 * dsize.0 <= size as u32,
128 "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}",
129 dnum.0,
130 dsize.0,
131 );
132
133 self.bytes_of_vmp_pmat(dnum.into(), rank_in.into(), (rank_out + 1).into(), size)
134 }
135
136 fn bytes_of_gglwe_prepared_from_infos<A>(&self, infos: &A) -> usize
137 where
138 A: GGLWEInfos,
139 {
140 assert_eq!(self.ring_degree(), infos.n());
141 self.bytes_of_gglwe_prepared(
142 infos.base2k(),
143 infos.k(),
144 infos.rank_in(),
145 infos.rank_out(),
146 infos.dnum(),
147 infos.dsize(),
148 )
149 }
150}
151
152impl<B: Backend> GGLWEPreparedAlloc<B> for Module<B> where Module<B>: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf {}
153
154impl<B: Backend> GGLWEPrepared<Vec<u8>, B> {
155 pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
156 where
157 A: GGLWEInfos,
158 M: GGLWEPreparedAlloc<B>,
159 {
160 module.alloc_gglwe_prepared_from_infos(infos)
161 }
162
163 pub fn alloc<M>(
164 module: &M,
165 base2k: Base2K,
166 k: TorusPrecision,
167 rank_in: Rank,
168 rank_out: Rank,
169 dnum: Dnum,
170 dsize: Dsize,
171 ) -> Self
172 where
173 M: GGLWEPreparedAlloc<B>,
174 {
175 module.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
176 }
177
178 pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
179 where
180 A: GGLWEInfos,
181 M: GGLWEPreparedAlloc<B>,
182 {
183 module.bytes_of_gglwe_prepared_from_infos(infos)
184 }
185
186 pub fn bytes_of<M>(
187 module: &M,
188 base2k: Base2K,
189 k: TorusPrecision,
190 rank_in: Rank,
191 rank_out: Rank,
192 dnum: Dnum,
193 dsize: Dsize,
194 ) -> usize
195 where
196 M: GGLWEPreparedAlloc<B>,
197 {
198 module.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
199 }
200}
201
202pub trait GGLWEPrepare<B: Backend>
203where
204 Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare<B>,
205{
206 fn prepare_gglwe_tmp_bytes<A>(&self, infos: &A) -> usize
207 where
208 A: GGLWEInfos,
209 {
210 self.vmp_prepare_tmp_bytes(
211 infos.dnum().into(),
212 infos.rank_in().into(),
213 (infos.rank() + 1).into(),
214 infos.size(),
215 )
216 }
217
218 fn prepare_gglwe<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
219 where
220 R: GGLWEPreparedToMut<B>,
221 O: GGLWEToRef,
222 {
223 let mut res: GGLWEPrepared<&mut [u8], B> = res.to_mut();
224 let other: GGLWE<&[u8]> = other.to_ref();
225
226 assert_eq!(res.n(), self.ring_degree());
227 assert_eq!(other.n(), self.ring_degree());
228 assert_eq!(res.base2k, other.base2k);
229 assert_eq!(res.k, other.k);
230 assert_eq!(res.dsize, other.dsize);
231
232 self.vmp_prepare(&mut res.data, &other.data, scratch);
233 }
234}
235
236impl<B: Backend> GGLWEPrepare<B> for Module<B> where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare<B> {}
237
238impl<D: DataMut, B: Backend> GGLWEPrepared<D, B> {
239 pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
240 where
241 O: GGLWEToRef,
242 M: GGLWEPrepare<B>,
243 {
244 module.prepare_gglwe(self, other, scratch);
245 }
246}
247
248impl<B: Backend> GGLWEPrepared<Vec<u8>, B> {
249 pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
250 where
251 M: GGLWEPrepare<B>,
252 {
253 module.prepare_gglwe_tmp_bytes(self)
254 }
255}
256
257pub trait GGLWEPreparedToMut<B: Backend> {
258 fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B>;
259}
260
261impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GGLWEPrepared<D, B> {
262 fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
263 GGLWEPrepared {
264 k: self.k,
265 base2k: self.base2k,
266 dsize: self.dsize,
267 data: self.data.to_mut(),
268 }
269 }
270}
271
272pub trait GGLWEPreparedToRef<B: Backend> {
273 fn to_ref(&self) -> GGLWEPrepared<&[u8], B>;
274}
275
276impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GGLWEPrepared<D, B> {
277 fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
278 GGLWEPrepared {
279 k: self.k,
280 base2k: self.base2k,
281 dsize: self.dsize,
282 data: self.data.to_ref(),
283 }
284 }
285}