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