poulpy_core/layouts/
glwe_to_lwe_key.rs

1use poulpy_hal::{
2    layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
3    source::Source,
4};
5
6use crate::layouts::{
7    Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees,
8    GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
9};
10
11use std::fmt;
12
13#[derive(PartialEq, Eq, Copy, Clone, Debug)]
14pub struct GLWEToLWEKeyLayout {
15    pub n: Degree,
16    pub base2k: Base2K,
17    pub k: TorusPrecision,
18    pub rank_in: Rank,
19    pub dnum: Dnum,
20}
21
22impl LWEInfos for GLWEToLWEKeyLayout {
23    fn n(&self) -> Degree {
24        self.n
25    }
26
27    fn base2k(&self) -> Base2K {
28        self.base2k
29    }
30
31    fn k(&self) -> TorusPrecision {
32        self.k
33    }
34}
35
36impl GLWEInfos for GLWEToLWEKeyLayout {
37    fn rank(&self) -> Rank {
38        self.rank_out()
39    }
40}
41
42impl GGLWEInfos for GLWEToLWEKeyLayout {
43    fn rank_in(&self) -> Rank {
44        self.rank_in
45    }
46
47    fn dsize(&self) -> Dsize {
48        Dsize(1)
49    }
50
51    fn rank_out(&self) -> Rank {
52        Rank(1)
53    }
54
55    fn dnum(&self) -> Dnum {
56        self.dnum
57    }
58}
59
60/// A special [GLWESwitchingKey] required to for the conversion from [GLWE] to [LWE].
61#[derive(PartialEq, Eq, Clone)]
62pub struct GLWEToLWEKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
63
64impl<D: Data> LWEInfos for GLWEToLWEKey<D> {
65    fn base2k(&self) -> Base2K {
66        self.0.base2k()
67    }
68
69    fn k(&self) -> TorusPrecision {
70        self.0.k()
71    }
72
73    fn n(&self) -> Degree {
74        self.0.n()
75    }
76
77    fn size(&self) -> usize {
78        self.0.size()
79    }
80}
81
82impl<D: Data> GLWEInfos for GLWEToLWEKey<D> {
83    fn rank(&self) -> Rank {
84        self.rank_out()
85    }
86}
87impl<D: Data> GGLWEInfos for GLWEToLWEKey<D> {
88    fn rank_in(&self) -> Rank {
89        self.0.rank_in()
90    }
91
92    fn dsize(&self) -> Dsize {
93        self.0.dsize()
94    }
95
96    fn rank_out(&self) -> Rank {
97        self.0.rank_out()
98    }
99
100    fn dnum(&self) -> Dnum {
101        self.0.dnum()
102    }
103}
104
105impl<D: DataRef> fmt::Debug for GLWEToLWEKey<D> {
106    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107        write!(f, "{self}")
108    }
109}
110
111impl<D: DataMut> FillUniform for GLWEToLWEKey<D> {
112    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
113        self.0.fill_uniform(log_bound, source);
114    }
115}
116
117impl<D: DataRef> fmt::Display for GLWEToLWEKey<D> {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        write!(f, "(GLWEToLWEKey) {}", self.0)
120    }
121}
122
123impl<D: DataMut> ReaderFrom for GLWEToLWEKey<D> {
124    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
125        self.0.read_from(reader)
126    }
127}
128
129impl<D: DataRef> WriterTo for GLWEToLWEKey<D> {
130    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
131        self.0.write_to(writer)
132    }
133}
134
135impl GLWEToLWEKey<Vec<u8>> {
136    pub fn alloc_from_infos<A>(infos: &A) -> Self
137    where
138        A: GGLWEInfos,
139    {
140        assert_eq!(
141            infos.rank_out().0,
142            1,
143            "rank_out > 1 is not supported for GLWEToLWEKey"
144        );
145        assert_eq!(
146            infos.dsize().0,
147            1,
148            "dsize > 1 is not supported for GLWEToLWEKey"
149        );
150        Self::alloc(
151            infos.n(),
152            infos.base2k(),
153            infos.k(),
154            infos.rank_in(),
155            infos.dnum(),
156        )
157    }
158
159    pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
160        GLWEToLWEKey(GLWESwitchingKey::alloc(
161            n,
162            base2k,
163            k,
164            rank_in,
165            Rank(1),
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_out().0,
177            1,
178            "rank_out > 1 is not supported for GLWEToLWEKey"
179        );
180        assert_eq!(
181            infos.dsize().0,
182            1,
183            "dsize > 1 is not supported for GLWEToLWEKey"
184        );
185        Self::bytes_of(
186            infos.n(),
187            infos.base2k(),
188            infos.k(),
189            infos.rank_in(),
190            infos.dnum(),
191        )
192    }
193
194    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
195        GLWESwitchingKey::bytes_of(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1))
196    }
197}
198
199impl<D: DataRef> GGLWEToRef for GLWEToLWEKey<D> {
200    fn to_ref(&self) -> GGLWE<&[u8]> {
201        self.0.to_ref()
202    }
203}
204
205impl<D: DataMut> GGLWEToMut for GLWEToLWEKey<D> {
206    fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
207        self.0.to_mut()
208    }
209}
210
211impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWEToLWEKey<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 GLWEToLWEKey<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}