poulpy_core/layouts/
lwe_to_glwe_key.rs

1use std::fmt;
2
3use poulpy_hal::{
4    layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
5    source::Source,
6};
7
8use crate::layouts::{
9    Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees,
10    GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
11};
12
13#[derive(PartialEq, Eq, Copy, Clone, Debug)]
14pub struct LWEToGLWEKeyLayout {
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 LWEToGLWEKeyLayout {
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 LWEToGLWEKeyLayout {
37    fn rank(&self) -> Rank {
38        self.rank_out()
39    }
40}
41
42impl GGLWEInfos for LWEToGLWEKeyLayout {
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 LWEToGLWEKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
62
63impl<D: Data> LWEInfos for LWEToGLWEKey<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 LWEToGLWEKey<D> {
82    fn rank(&self) -> Rank {
83        self.rank_out()
84    }
85}
86impl<D: Data> GGLWEInfos for LWEToGLWEKey<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 LWEToGLWEKey<D> {
105    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106        write!(f, "{self}")
107    }
108}
109
110impl<D: DataMut> FillUniform for LWEToGLWEKey<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 LWEToGLWEKey<D> {
117    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118        write!(f, "(LWEToGLWEKey) {}", self.0)
119    }
120}
121
122impl<D: DataMut> ReaderFrom for LWEToGLWEKey<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 LWEToGLWEKey<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
134impl LWEToGLWEKey<Vec<u8>> {
135    pub fn alloc_from_infos<A>(infos: &A) -> Self
136    where
137        A: GGLWEInfos,
138    {
139        assert_eq!(
140            infos.rank_in().0,
141            1,
142            "rank_in > 1 is not supported for LWEToGLWEKey"
143        );
144        assert_eq!(
145            infos.dsize().0,
146            1,
147            "dsize > 1 is not supported for LWEToGLWEKey"
148        );
149
150        Self::alloc(
151            infos.n(),
152            infos.base2k(),
153            infos.k(),
154            infos.rank_out(),
155            infos.dnum(),
156        )
157    }
158
159    pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
160        LWEToGLWEKey(GLWESwitchingKey::alloc(
161            n,
162            base2k,
163            k,
164            Rank(1),
165            rank_out,
166            dnum,
167            Dsize(1),
168        ))
169    }
170
171    pub fn bytes_of_from_infos<A>(infos: &A) -> usize
172    where
173        A: GGLWEInfos,
174    {
175        assert_eq!(
176            infos.rank_in().0,
177            1,
178            "rank_in > 1 is not supported for LWEToGLWEKey"
179        );
180        assert_eq!(
181            infos.dsize().0,
182            1,
183            "dsize > 1 is not supported for LWEToGLWEKey"
184        );
185        Self::bytes_of(
186            infos.n(),
187            infos.base2k(),
188            infos.k(),
189            infos.rank_out(),
190            infos.dnum(),
191        )
192    }
193
194    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
195        GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1))
196    }
197}
198
199impl<D: DataRef> GGLWEToRef for LWEToGLWEKey<D> {
200    fn to_ref(&self) -> GGLWE<&[u8]> {
201        self.0.to_ref()
202    }
203}
204
205impl<D: DataMut> GGLWEToMut for LWEToGLWEKey<D> {
206    fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
207        self.0.to_mut()
208    }
209}
210
211impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWEToGLWEKey<D> {
212    fn input_degree(&mut self) -> &mut Degree {
213        &mut self.0.input_degree
214    }
215
216    fn output_degree(&mut self) -> &mut Degree {
217        &mut self.0.output_degree
218    }
219}
220
221impl<D: DataRef> GLWESwitchingKeyDegrees for LWEToGLWEKey<D> {
222    fn input_degree(&self) -> &Degree {
223        &self.0.input_degree
224    }
225
226    fn output_degree(&self) -> &Degree {
227        &self.0.output_degree
228    }
229}