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, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef,
5 GLWESwtichingKeyGetMetaData, 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) sk_in_n: usize, // Degree of sk_in
13 pub(crate) sk_out_n: usize, // Degree of sk_out
14}
15
16impl<D: DataMut, B: Backend> GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared<D, B> {
17 fn set_sk_in_n(&mut self, sk_in_n: usize) {
18 self.sk_in_n = sk_in_n
19 }
20
21 fn set_sk_out_n(&mut self, sk_out_n: usize) {
22 self.sk_out_n = sk_out_n
23 }
24}
25
26impl<D: DataRef, B: Backend> GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared<D, B> {
27 fn sk_in_n(&self) -> usize {
28 self.sk_in_n
29 }
30
31 fn sk_out_n(&self) -> usize {
32 self.sk_out_n
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 sk_in_n: 0,
94 sk_out_n: 0,
95 }
96 }
97
98 fn glwe_switching_key_prepared_alloc_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 glwe_switching_key_prepared_bytes_of_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 Module<B>: GGLWEPreparedAlloc<B> {}
140
141impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B>
142where
143 Module<B>: GLWESwitchingKeyPreparedAlloc<B>,
144{
145 pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self
146 where
147 A: GGLWEInfos,
148 {
149 module.glwe_switching_key_prepared_alloc_from_infos(infos)
150 }
151
152 pub fn alloc(
153 module: &Module<B>,
154 base2k: Base2K,
155 k: TorusPrecision,
156 rank_in: Rank,
157 rank_out: Rank,
158 dnum: Dnum,
159 dsize: Dsize,
160 ) -> Self {
161 module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
162 }
163
164 pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
165 where
166 A: GGLWEInfos,
167 {
168 module.glwe_switching_key_prepared_bytes_of_from_infos(infos)
169 }
170
171 pub fn bytes_of(
172 module: &Module<B>,
173 base2k: Base2K,
174 k: TorusPrecision,
175 rank_in: Rank,
176 rank_out: Rank,
177 dnum: Dnum,
178 dsize: Dsize,
179 ) -> usize {
180 module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
181 }
182}
183
184pub trait GLWESwitchingKeyPrepare<B: Backend>
185where
186 Self: GGLWEPrepare<B>,
187{
188 fn prepare_glwe_switching_key_tmp_bytes<A>(&self, infos: &A) -> usize
189 where
190 A: GGLWEInfos,
191 {
192 self.prepare_gglwe_tmp_bytes(infos)
193 }
194
195 fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
196 where
197 R: GLWESwitchingKeyPreparedToMut<B> + GLWESwitchingKeySetMetaData,
198 O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData,
199 {
200 self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch);
201 res.set_sk_in_n(other.sk_in_n());
202 res.set_sk_out_n(other.sk_out_n());
203 }
204}
205
206impl<B: Backend> GLWESwitchingKeyPrepare<B> for Module<B> where Self: GGLWEPrepare<B> {}
207
208impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
209 pub fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)
210 where
211 O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData,
212 Module<B>: GLWESwitchingKeyPrepare<B>,
213 {
214 module.prepare_glwe_switching(self, other, scratch);
215 }
216}
217
218impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B>
219where
220 Module<B>: GLWESwitchingKeyPrepare<B>,
221{
222 pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize {
223 module.prepare_gglwe_tmp_bytes(self)
224 }
225}
226
227pub trait GLWESwitchingKeyPreparedToMut<B: Backend> {
228 fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>;
229}
230
231impl<D: DataMut, B: Backend> GLWESwitchingKeyPreparedToMut<B> for GLWESwitchingKeyPrepared<D, B> {
232 fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> {
233 GLWESwitchingKeyPrepared {
234 sk_in_n: self.sk_in_n,
235 sk_out_n: self.sk_out_n,
236 key: self.key.to_mut(),
237 }
238 }
239}
240
241pub trait GLWESwitchingKeyPreparedToRef<B: Backend> {
242 fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>;
243}
244
245impl<D: DataRef, B: Backend> GLWESwitchingKeyPreparedToRef<B> for GLWESwitchingKeyPrepared<D, B> {
246 fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> {
247 GLWESwitchingKeyPrepared {
248 sk_in_n: self.sk_in_n,
249 sk_out_n: self.sk_out_n,
250 key: self.key.to_ref(),
251 }
252 }
253}