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