poulpy/poulpy-core/src/layouts/lwe_switching_key.rs
poulpy-core/src/layouts/lwe_switching_key.rsRaw
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!(infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey");
110 assert_eq!(infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey");
111 assert_eq!(infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey");
112 Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum())
113 }
114
115 pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
116 LWESwitchingKey(GLWESwitchingKey::alloc(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)))
117 }
118
119 pub fn bytes_of_from_infos<A>(infos: &A) -> usize
120 where
121 A: GGLWEInfos,
122 {
123 assert_eq!(infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey");
124 assert_eq!(infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey");
125 assert_eq!(infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey");
126 Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.dnum())
127 }
128
129 pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
130 GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
131 }
132}
133
134impl<D: DataRef> fmt::Debug for LWESwitchingKey<D> {
135 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136 write!(f, "{self}")
137 }
138}
139
140impl<D: DataMut> FillUniform for LWESwitchingKey<D> {
141 fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
142 self.0.fill_uniform(log_bound, source);
143 }
144}
145
146impl<D: DataRef> fmt::Display for LWESwitchingKey<D> {
147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148 write!(f, "(LWESwitchingKey) {}", self.0)
149 }
150}
151
152impl<D: DataMut> ReaderFrom for LWESwitchingKey<D> {
153 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
154 self.0.read_from(reader)
155 }
156}
157
158impl<D: DataRef> WriterTo for LWESwitchingKey<D> {
159 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
160 self.0.write_to(writer)
161 }
162}
163
164impl<D: DataRef> GGLWEToRef for LWESwitchingKey<D> {
165 fn to_ref(&self) -> GGLWE<&[u8]> {
166 self.0.to_ref()
167 }
168}
169
170impl<D: DataMut> GGLWEToMut for LWESwitchingKey<D> {
171 fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
172 self.0.to_mut()
173 }
174}
175
176impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWESwitchingKey<D> {
177 fn input_degree(&mut self) -> &mut Degree {
178 &mut self.0.input_degree
179 }
180
181 fn output_degree(&mut self) -> &mut Degree {
182 &mut self.0.output_degree
183 }
184}
185
186impl<D: DataRef> GLWESwitchingKeyDegrees for LWESwitchingKey<D> {
187 fn input_degree(&self) -> &Degree {
188 &self.0.input_degree
189 }
190
191 fn output_degree(&self) -> &Degree {
192 &self.0.output_degree
193 }
194}