poulpy/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs
poulpy-core/src/layouts/lwe_to_glwe_ksk.rsRaw
1use std::fmt;
2
3use poulpy_hal::{
4 layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
5 source::Source,
6};
7
8use crate::layouts::{
9 Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut,
10 GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
11};
12
13#[derive(PartialEq, Eq, Copy, Clone, Debug)]
14pub struct LWEToGLWESwitchingKeyLayout {
15 pub n: Degree,
16 pub base2k: Base2K,
17 pub k: TorusPrecision,
18 pub rank_out: Rank,
19 pub dnum: Dnum,
20}
21
22impl LWEInfos for LWEToGLWESwitchingKeyLayout {
23 fn base2k(&self) -> Base2K {
24 self.base2k
25 }
26
27 fn k(&self) -> TorusPrecision {
28 self.k
29 }
30
31 fn n(&self) -> Degree {
32 self.n
33 }
34}
35
36impl GLWEInfos for LWEToGLWESwitchingKeyLayout {
37 fn rank(&self) -> Rank {
38 self.rank_out()
39 }
40}
41
42impl GGLWEInfos for LWEToGLWESwitchingKeyLayout {
43 fn rank_in(&self) -> Rank {
44 Rank(1)
45 }
46
47 fn dsize(&self) -> Dsize {
48 Dsize(1)
49 }
50
51 fn rank_out(&self) -> Rank {
52 self.rank_out
53 }
54
55 fn dnum(&self) -> Dnum {
56 self.dnum
57 }
58}
59
60#[derive(PartialEq, Eq, Clone)]
61pub struct LWEToGLWESwitchingKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
62
63impl<D: Data> LWEInfos for LWEToGLWESwitchingKey<D> {
64 fn base2k(&self) -> Base2K {
65 self.0.base2k()
66 }
67
68 fn k(&self) -> TorusPrecision {
69 self.0.k()
70 }
71
72 fn n(&self) -> Degree {
73 self.0.n()
74 }
75
76 fn size(&self) -> usize {
77 self.0.size()
78 }
79}
80
81impl<D: Data> GLWEInfos for LWEToGLWESwitchingKey<D> {
82 fn rank(&self) -> Rank {
83 self.rank_out()
84 }
85}
86impl<D: Data> GGLWEInfos for LWEToGLWESwitchingKey<D> {
87 fn dsize(&self) -> Dsize {
88 self.0.dsize()
89 }
90
91 fn rank_in(&self) -> Rank {
92 self.0.rank_in()
93 }
94
95 fn rank_out(&self) -> Rank {
96 self.0.rank_out()
97 }
98
99 fn dnum(&self) -> Dnum {
100 self.0.dnum()
101 }
102}
103
104impl<D: DataRef> fmt::Debug for LWEToGLWESwitchingKey<D> {
105 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106 write!(f, "{self}")
107 }
108}
109
110impl<D: DataMut> FillUniform for LWEToGLWESwitchingKey<D> {
111 fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
112 self.0.fill_uniform(log_bound, source);
113 }
114}
115
116impl<D: DataRef> fmt::Display for LWEToGLWESwitchingKey<D> {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 write!(f, "(LWEToGLWESwitchingKey) {}", self.0)
119 }
120}
121
122impl<D: DataMut> ReaderFrom for LWEToGLWESwitchingKey<D> {
123 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
124 self.0.read_from(reader)
125 }
126}
127
128impl<D: DataRef> WriterTo for LWEToGLWESwitchingKey<D> {
129 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
130 self.0.write_to(writer)
131 }
132}
133
134pub trait LWEToGLWESwitchingKeyAlloc
135where
136 Self: GLWESwitchingKeyAlloc,
137{
138 fn alloc_lwe_to_glwe_switching_key(
139 &self,
140 base2k: Base2K,
141 k: TorusPrecision,
142 rank_out: Rank,
143 dnum: Dnum,
144 ) -> LWEToGLWESwitchingKey<Vec<u8>> {
145 LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
146 }
147
148 fn alloc_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKey<Vec<u8>>
149 where
150 A: GGLWEInfos,
151 {
152 assert_eq!(
153 infos.rank_in().0,
154 1,
155 "rank_in > 1 is not supported for LWEToGLWESwitchingKey"
156 );
157 assert_eq!(
158 infos.dsize().0,
159 1,
160 "dsize > 1 is not supported for LWEToGLWESwitchingKey"
161 );
162
163 self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
164 }
165
166 fn bytes_of_lwe_to_glwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
167 self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))
168 }
169
170 fn bytes_of_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
171 where
172 A: GGLWEInfos,
173 {
174 assert_eq!(
175 infos.rank_in().0,
176 1,
177 "rank_in > 1 is not supported for LWEToGLWESwitchingKey"
178 );
179 assert_eq!(
180 infos.dsize().0,
181 1,
182 "dsize > 1 is not supported for LWEToGLWESwitchingKey"
183 );
184 self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
185 }
186}
187
188impl<B: Backend> LWEToGLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
189
190impl LWEToGLWESwitchingKey<Vec<u8>> {
191 pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
192 where
193 A: GGLWEInfos,
194 Module<B>: LWEToGLWESwitchingKeyAlloc,
195 {
196 module.alloc_lwe_to_glwe_switching_key_from_infos(infos)
197 }
198
199 pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self
200 where
201 Module<B>: LWEToGLWESwitchingKeyAlloc,
202 {
203 module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum)
204 }
205
206 pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
207 where
208 A: GGLWEInfos,
209 Module<B>: LWEToGLWESwitchingKeyAlloc,
210 {
211 module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos)
212 }
213
214 pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize
215 where
216 Module<B>: LWEToGLWESwitchingKeyAlloc,
217 {
218 module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum)
219 }
220}
221
222pub trait LWEToGLWESwitchingKeyToRef {
223 fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>;
224}
225
226impl<D: DataRef> LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey<D>
227where
228 GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
229{
230 fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> {
231 LWEToGLWESwitchingKey(self.0.to_ref())
232 }
233}
234
235pub trait LWEToGLWESwitchingKeyToMut {
236 fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>;
237}
238
239impl<D: DataMut> LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey<D>
240where
241 GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
242{
243 fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> {
244 LWEToGLWESwitchingKey(self.0.to_mut())
245 }
246}