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