poulpy/poulpy-core/src/layouts/prepared/gglwe_ksk.rs
poulpy-core/src/layouts/prepared/gglwe_ksk.rsRaw
1use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
2
3use crate::layouts::{
4 Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut,
5 LWEInfos, Rank, TorusPrecision,
6 prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef},
7};
8
9#[derive(PartialEq, Eq)]
10pub struct GLWESwitchingKeyPrepared<D: Data, B: Backend> {
11 pub(crate) key: GGLWEPrepared<D, B>,
12 pub(crate) input_degree: Degree, // Degree of sk_in
13 pub(crate) output_degree: Degree, // Degree of sk_out
14}
15
16impl<D: DataRef, BE: Backend> GLWESwitchingKeyDegrees for GLWESwitchingKeyPrepared<D, BE> {
17 fn output_degree(&self) -> &Degree {
18 &self.output_degree
19 }
20
21 fn input_degree(&self) -> &Degree {
22 &self.input_degree
23 }
24}
25
26impl<D: DataMut, BE: Backend> GLWESwitchingKeyDegreesMut for GLWESwitchingKeyPrepared<D, BE> {
27 fn output_degree(&mut self) -> &mut Degree {
28 &mut self.output_degree
29 }
30
31 fn input_degree(&mut self) -> &mut Degree {
32 &mut self.input_degree
33 }
34}
35
36impl<D: Data, B: Backend> LWEInfos for GLWESwitchingKeyPrepared<D, B> {
37 fn n(&self) -> Degree {
38 self.key.n()
39 }
40
41 fn base2k(&self) -> Base2K {
42 self.key.base2k()
43 }
44
45 fn k(&self) -> TorusPrecision {
46 self.key.k()
47 }
48
49 fn size(&self) -> usize {
50 self.key.size()
51 }
52}
53
54impl<D: Data, B: Backend> GLWEInfos for GLWESwitchingKeyPrepared<D, B> {
55 fn rank(&self) -> Rank {
56 self.rank_out()
57 }
58}
59
60impl<D: Data, B: Backend> GGLWEInfos for GLWESwitchingKeyPrepared<D, B> {
61 fn rank_in(&self) -> Rank {
62 self.key.rank_in()
63 }
64
65 fn rank_out(&self) -> Rank {
66 self.key.rank_out()
67 }
68
69 fn dsize(&self) -> Dsize {
70 self.key.dsize()
71 }
72
73 fn dnum(&self) -> Dnum {
74 self.key.dnum()
75 }
76}
77
78pub trait GLWESwitchingKeyPreparedAlloc<B: Backend>
79where
80 Self: GGLWEPreparedAlloc<B>,
81{
82 fn alloc_glwe_switching_key_prepared(
83 &self,
84 base2k: Base2K,
85 k: TorusPrecision,
86 rank_in: Rank,
87 rank_out: Rank,
88 dnum: Dnum,
89 dsize: Dsize,
90 ) -> GLWESwitchingKeyPrepared<Vec<u8>, B> {
91 GLWESwitchingKeyPrepared::<Vec<u8>, B> {
92 key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize),
93 input_degree: Degree(0),
94 output_degree: Degree(0),
95 }
96 }
97
98 fn alloc_glwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> GLWESwitchingKeyPrepared<Vec<u8>, B>
99 where
100 A: GGLWEInfos,
101 {
102 self.alloc_glwe_switching_key_prepared(
103 infos.base2k(),
104 infos.k(),
105 infos.rank_in(),
106 infos.rank_out(),
107 infos.dnum(),
108 infos.dsize(),
109 )
110 }
111
112 fn bytes_of_glwe_switching_key_prepared(
113 &self,
114 base2k: Base2K,
115 k: TorusPrecision,
116 rank_in: Rank,
117 rank_out: Rank,
118 dnum: Dnum,
119 dsize: Dsize,
120 ) -> usize {
121 self.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
122 }
123
124 fn bytes_of_glwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> usize
125 where
126 A: GGLWEInfos,
127 {
128 self.bytes_of_glwe_switching_key_prepared(
129 infos.base2k(),
130 infos.k(),
131 infos.rank_in(),
132 infos.rank_out(),
133 infos.dnum(),
134 infos.dsize(),
135 )
136 }
137}
138
139impl<B: Backend> GLWESwitchingKeyPreparedAlloc<B> for Module<B> where Self: GGLWEPreparedAlloc<B> {}
140
141impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
142 pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
143 where
144 A: GGLWEInfos,
145 M: GLWESwitchingKeyPreparedAlloc<B>,
146 {
147 module.alloc_glwe_switching_key_prepared_from_infos(infos)
148 }
149
150 pub fn alloc<M>(
151 module: &M,
152 base2k: Base2K,
153 k: TorusPrecision,
154 rank_in: Rank,
155 rank_out: Rank,
156 dnum: Dnum,
157 dsize: Dsize,
158 ) -> Self
159 where
160 M: GLWESwitchingKeyPreparedAlloc<B>,
161 {
162 module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
163 }
164
165 pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
166 where
167 A: GGLWEInfos,
168 M: GLWESwitchingKeyPreparedAlloc<B>,
169 {
170 module.bytes_of_glwe_switching_key_prepared_from_infos(infos)
171 }
172
173 pub fn bytes_of<M>(
174 module: &M,
175 base2k: Base2K,
176 k: TorusPrecision,
177 rank_in: Rank,
178 rank_out: Rank,
179 dnum: Dnum,
180 dsize: Dsize,
181 ) -> usize
182 where
183 M: GLWESwitchingKeyPreparedAlloc<B>,
184 {
185 module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
186 }
187}
188
189pub trait GLWESwitchingKeyPrepare<B: Backend>
190where
191 Self: GGLWEPrepare<B>,
192{
193 fn prepare_glwe_switching_key_tmp_bytes<A>(&self, infos: &A) -> usize
194 where
195 A: GGLWEInfos,
196 {
197 self.prepare_gglwe_tmp_bytes(infos)
198 }
199
200 fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
201 where
202 R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
203 O: GGLWEToRef + GLWESwitchingKeyDegrees,
204 {
205 self.prepare_gglwe(res, other, scratch);
206 *res.input_degree() = *other.input_degree();
207 *res.output_degree() = *other.output_degree();
208 }
209}
210
211impl<B: Backend> GLWESwitchingKeyPrepare<B> for Module<B> where Self: GGLWEPrepare<B> {}
212
213impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
214 pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
215 where
216 O: GGLWEToRef + GLWESwitchingKeyDegrees,
217 M: GLWESwitchingKeyPrepare<B>,
218 {
219 module.prepare_glwe_switching(self, other, scratch);
220 }
221}
222
223impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
224 pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
225 where
226 M: GLWESwitchingKeyPrepare<B>,
227 {
228 module.prepare_glwe_switching_key_tmp_bytes(self)
229 }
230}
231
232impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for GLWESwitchingKeyPrepared<D, BE>
233where
234 GGLWEPrepared<D, BE>: GGLWEPreparedToRef<BE>,
235{
236 fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
237 self.key.to_ref()
238 }
239}
240
241impl<D: DataRef, BE: Backend> GGLWEPreparedToMut<BE> for GLWESwitchingKeyPrepared<D, BE>
242where
243 GGLWEPrepared<D, BE>: GGLWEPreparedToMut<BE>,
244{
245 fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], BE> {
246 self.key.to_mut()
247 }
248}