poulpy/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs
poulpy-core/src/layouts/glwe_to_lwe_ksk.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, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
8 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 [GLWECiphertext] to [LWECiphertext].
61#[derive(PartialEq, Eq, Clone)]
62pub struct GLWEToLWESwitchingKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
63
64impl<D: Data> LWEInfos for GLWEToLWESwitchingKey<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 GLWEToLWESwitchingKey<D> {
83 fn rank(&self) -> Rank {
84 self.rank_out()
85 }
86}
87impl<D: Data> GGLWEInfos for GLWEToLWESwitchingKey<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 GLWEToLWESwitchingKey<D> {
106 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107 write!(f, "{self}")
108 }
109}
110
111impl<D: DataMut> FillUniform for GLWEToLWESwitchingKey<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 GLWEToLWESwitchingKey<D> {
118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119 write!(f, "(GLWEToLWESwitchingKey) {}", self.0)
120 }
121}
122
123impl<D: DataMut> ReaderFrom for GLWEToLWESwitchingKey<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 GLWEToLWESwitchingKey<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 GLWEToLWESwitchingKey<Vec<u8>> {
136 pub fn alloc<A>(infos: &A) -> Self
137 where
138 A: GGLWEInfos,
139 {
140 debug_assert_eq!(
141 infos.rank_out().0,
142 1,
143 "rank_out > 1 is not supported for GLWEToLWESwitchingKey"
144 );
145 debug_assert_eq!(
146 infos.dsize().0,
147 1,
148 "dsize > 1 is not supported for GLWEToLWESwitchingKey"
149 );
150 Self(GLWESwitchingKey::alloc(infos))
151 }
152
153 pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
154 Self(GLWESwitchingKey::alloc_with(
155 n,
156 base2k,
157 k,
158 rank_in,
159 Rank(1),
160 dnum,
161 Dsize(1),
162 ))
163 }
164
165 pub fn alloc_bytes<A>(infos: &A) -> usize
166 where
167 A: GGLWEInfos,
168 {
169 debug_assert_eq!(
170 infos.rank_out().0,
171 1,
172 "rank_out > 1 is not supported for GLWEToLWESwitchingKey"
173 );
174 debug_assert_eq!(
175 infos.dsize().0,
176 1,
177 "dsize > 1 is not supported for GLWEToLWESwitchingKey"
178 );
179 GLWESwitchingKey::alloc_bytes(infos)
180 }
181
182 pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
183 GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1))
184 }
185}
186
187pub trait GLWEToLWESwitchingKeyToRef {
188 fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>;
189}
190
191impl<D: DataRef> GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey<D>
192where
193 GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
194{
195 fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> {
196 GLWEToLWESwitchingKey(self.0.to_ref())
197 }
198}
199
200pub trait GLWEToLWESwitchingKeyToMut {
201 fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>;
202}
203
204impl<D: DataMut> GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey<D>
205where
206 GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
207{
208 fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> {
209 GLWEToLWESwitchingKey(self.0.to_mut())
210 }
211}