poulpy/poulpy-core/src/layouts/lwe_to_glwe_key.rs
poulpy-core/src/layouts/lwe_to_glwe_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 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!(infos.rank_in().0, 1, "rank_in > 1 is not supported for LWEToGLWEKey");
140 assert_eq!(infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWEKey");
141
142 Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
143 }
144
145 pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
146 LWEToGLWEKey(GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank_out, 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_in().0, 1, "rank_in > 1 is not supported for LWEToGLWEKey");
154 assert_eq!(infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWEKey");
155 Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
156 }
157
158 pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
159 GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1))
160 }
161}
162
163impl<D: DataRef> GGLWEToRef for LWEToGLWEKey<D> {
164 fn to_ref(&self) -> GGLWE<&[u8]> {
165 self.0.to_ref()
166 }
167}
168
169impl<D: DataMut> GGLWEToMut for LWEToGLWEKey<D> {
170 fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
171 self.0.to_mut()
172 }
173}
174
175impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWEToGLWEKey<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 LWEToGLWEKey<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}