poulpy_core/layouts/
lwe_switching_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 LWESwitchingKeyLayout {
15    pub n: Degree,
16    pub base2k: Base2K,
17    pub k: TorusPrecision,
18    pub dnum: Dnum,
19}
20
21impl LWEInfos for LWESwitchingKeyLayout {
22    fn n(&self) -> Degree {
23        self.n
24    }
25
26    fn base2k(&self) -> Base2K {
27        self.base2k
28    }
29
30    fn k(&self) -> TorusPrecision {
31        self.k
32    }
33}
34
35impl GLWEInfos for LWESwitchingKeyLayout {
36    fn rank(&self) -> Rank {
37        self.rank_out()
38    }
39}
40
41impl GGLWEInfos for LWESwitchingKeyLayout {
42    fn rank_in(&self) -> Rank {
43        Rank(1)
44    }
45
46    fn dsize(&self) -> Dsize {
47        Dsize(1)
48    }
49
50    fn rank_out(&self) -> Rank {
51        Rank(1)
52    }
53
54    fn dnum(&self) -> Dnum {
55        self.dnum
56    }
57}
58
59#[derive(PartialEq, Eq, Clone)]
60pub struct LWESwitchingKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
61
62impl<D: Data> LWEInfos for LWESwitchingKey<D> {
63    fn base2k(&self) -> Base2K {
64        self.0.base2k()
65    }
66
67    fn k(&self) -> TorusPrecision {
68        self.0.k()
69    }
70
71    fn n(&self) -> Degree {
72        self.0.n()
73    }
74
75    fn size(&self) -> usize {
76        self.0.size()
77    }
78}
79
80impl<D: Data> GLWEInfos for LWESwitchingKey<D> {
81    fn rank(&self) -> Rank {
82        self.rank_out()
83    }
84}
85
86impl<D: Data> GGLWEInfos for LWESwitchingKey<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 LWESwitchingKey<Vec<u8>> {
105    pub fn alloc_from_infos<A>(infos: &A) -> Self
106    where
107        A: GGLWEInfos,
108    {
109        assert_eq!(
110            infos.dsize().0,
111            1,
112            "dsize > 1 is not supported for LWESwitchingKey"
113        );
114        assert_eq!(
115            infos.rank_in().0,
116            1,
117            "rank_in > 1 is not supported for LWESwitchingKey"
118        );
119        assert_eq!(
120            infos.rank_out().0,
121            1,
122            "rank_out > 1 is not supported for LWESwitchingKey"
123        );
124        Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum())
125    }
126
127    pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
128        LWESwitchingKey(GLWESwitchingKey::alloc(
129            n,
130            base2k,
131            k,
132            Rank(1),
133            Rank(1),
134            dnum,
135            Dsize(1),
136        ))
137    }
138
139    pub fn bytes_of_from_infos<A>(infos: &A) -> usize
140    where
141        A: GGLWEInfos,
142    {
143        assert_eq!(
144            infos.dsize().0,
145            1,
146            "dsize > 1 is not supported for LWESwitchingKey"
147        );
148        assert_eq!(
149            infos.rank_in().0,
150            1,
151            "rank_in > 1 is not supported for LWESwitchingKey"
152        );
153        assert_eq!(
154            infos.rank_out().0,
155            1,
156            "rank_out > 1 is not supported for LWESwitchingKey"
157        );
158        Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.dnum())
159    }
160
161    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
162        GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
163    }
164}
165
166impl<D: DataRef> fmt::Debug for LWESwitchingKey<D> {
167    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
168        write!(f, "{self}")
169    }
170}
171
172impl<D: DataMut> FillUniform for LWESwitchingKey<D> {
173    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
174        self.0.fill_uniform(log_bound, source);
175    }
176}
177
178impl<D: DataRef> fmt::Display for LWESwitchingKey<D> {
179    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180        write!(f, "(LWESwitchingKey) {}", self.0)
181    }
182}
183
184impl<D: DataMut> ReaderFrom for LWESwitchingKey<D> {
185    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
186        self.0.read_from(reader)
187    }
188}
189
190impl<D: DataRef> WriterTo for LWESwitchingKey<D> {
191    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
192        self.0.write_to(writer)
193    }
194}
195
196impl<D: DataRef> GGLWEToRef for LWESwitchingKey<D> {
197    fn to_ref(&self) -> GGLWE<&[u8]> {
198        self.0.to_ref()
199    }
200}
201
202impl<D: DataMut> GGLWEToMut for LWESwitchingKey<D> {
203    fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
204        self.0.to_mut()
205    }
206}
207
208impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWESwitchingKey<D> {
209    fn input_degree(&mut self) -> &mut Degree {
210        &mut self.0.input_degree
211    }
212
213    fn output_degree(&mut self) -> &mut Degree {
214        &mut self.0.output_degree
215    }
216}
217
218impl<D: DataRef> GLWESwitchingKeyDegrees for LWESwitchingKey<D> {
219    fn input_degree(&self) -> &Degree {
220        &self.0.input_degree
221    }
222
223    fn output_degree(&self) -> &Degree {
224        &self.0.output_degree
225    }
226}