poulpy/poulpy-core/src/layouts/compressed/gglwe_atk.rs
poulpy-core/src/layouts/compressed/gglwe_atk.rsRaw
1use poulpy_hal::{
2 layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
3 source::Source,
4};
5
6use crate::layouts::{
7 AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
8 compressed::{
9 GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut,
10 GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
11 },
12 prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
13};
14use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
15use std::fmt;
16
17#[derive(PartialEq, Eq, Clone)]
18pub struct AutomorphismKeyCompressed<D: Data> {
19 pub(crate) key: GLWESwitchingKeyCompressed<D>,
20 pub(crate) p: i64,
21}
22
23impl<D: Data> LWEInfos for AutomorphismKeyCompressed<D> {
24 fn n(&self) -> Degree {
25 self.key.n()
26 }
27
28 fn base2k(&self) -> Base2K {
29 self.key.base2k()
30 }
31
32 fn k(&self) -> TorusPrecision {
33 self.key.k()
34 }
35
36 fn size(&self) -> usize {
37 self.key.size()
38 }
39}
40impl<D: Data> GLWEInfos for AutomorphismKeyCompressed<D> {
41 fn rank(&self) -> Rank {
42 self.rank_out()
43 }
44}
45
46impl<D: Data> GGLWEInfos for AutomorphismKeyCompressed<D> {
47 fn rank_in(&self) -> Rank {
48 self.key.rank_in()
49 }
50
51 fn rank_out(&self) -> Rank {
52 self.key.rank_out()
53 }
54
55 fn dsize(&self) -> Dsize {
56 self.key.dsize()
57 }
58
59 fn dnum(&self) -> Dnum {
60 self.key.dnum()
61 }
62}
63
64impl<D: DataRef> fmt::Debug for AutomorphismKeyCompressed<D> {
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 write!(f, "{self}")
67 }
68}
69
70impl<D: DataMut> FillUniform for AutomorphismKeyCompressed<D> {
71 fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
72 self.key.fill_uniform(log_bound, source);
73 }
74}
75
76impl<D: DataRef> fmt::Display for AutomorphismKeyCompressed<D> {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key)
79 }
80}
81
82impl<BE: Backend> AutomorphismKeyCompressedAlloc for Module<BE> where Self: GLWESwitchingKeyCompressedAlloc {}
83
84pub trait AutomorphismKeyCompressedAlloc
85where
86 Self: GLWESwitchingKeyCompressedAlloc,
87{
88 fn alloc_automorphism_key_compressed(
89 &self,
90 base2k: Base2K,
91 k: TorusPrecision,
92 rank: Rank,
93 dnum: Dnum,
94 dsize: Dsize,
95 ) -> AutomorphismKeyCompressed<Vec<u8>> {
96 AutomorphismKeyCompressed {
97 key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize),
98 p: 0,
99 }
100 }
101
102 fn alloc_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> AutomorphismKeyCompressed<Vec<u8>>
103 where
104 A: GGLWEInfos,
105 {
106 assert_eq!(infos.rank_in(), infos.rank_out());
107 self.alloc_automorphism_key_compressed(
108 infos.base2k(),
109 infos.k(),
110 infos.rank(),
111 infos.dnum(),
112 infos.dsize(),
113 )
114 }
115
116 fn bytes_of_automorphism_key_compressed(
117 &self,
118 base2k: Base2K,
119 k: TorusPrecision,
120 rank: Rank,
121 dnum: Dnum,
122 dsize: Dsize,
123 ) -> usize {
124 self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize)
125 }
126
127 fn bytes_of_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> usize
128 where
129 A: GGLWEInfos,
130 {
131 assert_eq!(infos.rank_in(), infos.rank_out());
132 self.bytes_of_automorphism_key_compressed(
133 infos.base2k(),
134 infos.k(),
135 infos.rank(),
136 infos.dnum(),
137 infos.dsize(),
138 )
139 }
140}
141
142impl AutomorphismKeyCompressed<Vec<u8>> {
143 pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
144 where
145 A: GGLWEInfos,
146 M: AutomorphismKeyCompressedAlloc,
147 {
148 module.alloc_automorphism_key_compressed_from_infos(infos)
149 }
150
151 pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
152 where
153 M: AutomorphismKeyCompressedAlloc,
154 {
155 module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize)
156 }
157
158 pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
159 where
160 A: GGLWEInfos,
161 M: AutomorphismKeyCompressedAlloc,
162 {
163 module.bytes_of_automorphism_key_compressed_from_infos(infos)
164 }
165
166 pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
167 where
168 M: AutomorphismKeyCompressedAlloc,
169 {
170 module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize)
171 }
172}
173
174impl<D: DataMut> ReaderFrom for AutomorphismKeyCompressed<D> {
175 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
176 self.p = reader.read_u64::<LittleEndian>()? as i64;
177 self.key.read_from(reader)
178 }
179}
180
181impl<D: DataRef> WriterTo for AutomorphismKeyCompressed<D> {
182 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
183 writer.write_u64::<LittleEndian>(self.p as u64)?;
184 self.key.write_to(writer)
185 }
186}
187
188pub trait AutomorphismKeyDecompress
189where
190 Self: GLWESwitchingKeyDecompress,
191{
192 fn decompress_automorphism_key<R, O>(&self, res: &mut R, other: &O)
193 where
194 R: AutomorphismKeyToMut + SetAutomorphismGaloisElement,
195 O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
196 {
197 self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key);
198 res.set_p(other.p());
199 }
200}
201
202impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: AutomorphismKeyDecompress {}
203
204impl<D: DataMut> AutomorphismKey<D>
205where
206 Self: SetAutomorphismGaloisElement,
207{
208 pub fn decompress<O, M>(&mut self, module: &M, other: &O)
209 where
210 O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
211 M: AutomorphismKeyDecompress,
212 {
213 module.decompress_automorphism_key(self, other);
214 }
215}
216
217pub trait AutomorphismKeyCompressedToRef {
218 fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>;
219}
220
221impl<D: DataRef> AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed<D>
222where
223 GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
224{
225 fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> {
226 AutomorphismKeyCompressed {
227 key: self.key.to_ref(),
228 p: self.p,
229 }
230 }
231}
232
233pub trait AutomorphismKeyCompressedToMut {
234 fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]>;
235}
236
237impl<D: DataMut> AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed<D>
238where
239 GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
240{
241 fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> {
242 AutomorphismKeyCompressed {
243 p: self.p,
244 key: self.key.to_mut(),
245 }
246 }
247}