poulpy/poulpy-core/src/layouts/glwe_to_lwe_key.rs
poulpy-core/src/layouts/glwe_to_lwe_key.rsRaw
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!(infos.rank_out().0, 1, "rank_out > 1 is not supported for GLWEToLWEKey");
141 assert_eq!(infos.dsize().0, 1, "dsize > 1 is not supported for GLWEToLWEKey");
142 Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
143 }
144
145 pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
146 GLWEToLWEKey(GLWESwitchingKey::alloc(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
147 }
148
149 pub fn bytes_of_from_infos<A>(infos: &A) -> usize
150 where
151 A: GGLWEInfos,
152 {
153 assert_eq!(infos.rank_out().0, 1, "rank_out > 1 is not supported for GLWEToLWEKey");
154 assert_eq!(infos.dsize().0, 1, "dsize > 1 is not supported for GLWEToLWEKey");
155 Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
156 }
157
158 pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
159 GLWESwitchingKey::bytes_of(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1))
160 }
161}
162
163impl<D: DataRef> GGLWEToRef for GLWEToLWEKey<D> {
164 fn to_ref(&self) -> GGLWE<&[u8]> {
165 self.0.to_ref()
166 }
167}
168
169impl<D: DataMut> GGLWEToMut for GLWEToLWEKey<D> {
170 fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
171 self.0.to_mut()
172 }
173}
174
175impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWEToLWEKey<D> {
176 fn input_degree(&mut self) -> &mut Degree {
177 &mut self.0.input_degree
178 }
179
180 fn output_degree(&mut self) -> &mut Degree {
181 &mut self.0.output_degree
182 }
183}
184
185impl<D: DataRef> GLWESwitchingKeyDegrees for GLWEToLWEKey<D> {
186 fn input_degree(&self) -> &Degree {
187 &self.0.input_degree
188 }
189
190 fn output_degree(&self) -> &Degree {
191 &self.0.output_degree
192 }
193}