poulpy/poulpy-core/src/layouts/compressed/gglwe_to_ggsw_key.rs
poulpy-core/src/layouts/compressed/gglwe_to_ggsw_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, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos,
8 GGLWEToGGSWKey, GGLWEToGGSWKeyToMut, GLWEInfos, LWEInfos, Rank, TorusPrecision,
9};
10use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
11
12use std::fmt;
13
14#[derive(PartialEq, Eq, Clone)]
15pub struct GGLWEToGGSWKeyCompressed<D: Data> {
16 pub(crate) keys: Vec<GGLWECompressed<D>>,
17}
18
19impl<D: Data> LWEInfos for GGLWEToGGSWKeyCompressed<D> {
20 fn n(&self) -> Degree {
21 self.keys[0].n()
22 }
23
24 fn base2k(&self) -> Base2K {
25 self.keys[0].base2k()
26 }
27
28 fn k(&self) -> TorusPrecision {
29 self.keys[0].k()
30 }
31
32 fn size(&self) -> usize {
33 self.keys[0].size()
34 }
35}
36
37impl<D: Data> GLWEInfos for GGLWEToGGSWKeyCompressed<D> {
38 fn rank(&self) -> Rank {
39 self.keys[0].rank_out()
40 }
41}
42
43impl<D: Data> GGLWEInfos for GGLWEToGGSWKeyCompressed<D> {
44 fn rank_in(&self) -> Rank {
45 self.rank_out()
46 }
47
48 fn rank_out(&self) -> Rank {
49 self.keys[0].rank_out()
50 }
51
52 fn dsize(&self) -> Dsize {
53 self.keys[0].dsize()
54 }
55
56 fn dnum(&self) -> Dnum {
57 self.keys[0].dnum()
58 }
59}
60
61impl<D: DataRef> fmt::Debug for GGLWEToGGSWKeyCompressed<D> {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 write!(f, "{self}")
64 }
65}
66
67impl<D: DataMut> FillUniform for GGLWEToGGSWKeyCompressed<D> {
68 fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
69 self.keys
70 .iter_mut()
71 .for_each(|key: &mut GGLWECompressed<D>| key.fill_uniform(log_bound, source))
72 }
73}
74
75impl<D: DataRef> fmt::Display for GGLWEToGGSWKeyCompressed<D> {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 writeln!(f, "(GGLWEToGGSWKeyCompressed)",)?;
78 for (i, key) in self.keys.iter().enumerate() {
79 write!(f, "{i}: {key}")?;
80 }
81 Ok(())
82 }
83}
84
85impl GGLWEToGGSWKeyCompressed<Vec<u8>> {
86 pub fn alloc_from_infos<A>(infos: &A) -> Self
87 where
88 A: GGLWEInfos,
89 {
90 assert_eq!(
91 infos.rank_in(),
92 infos.rank_out(),
93 "rank_in != rank_out is not supported for GGLWEToGGSWKeyCompressed"
94 );
95 Self::alloc(
96 infos.n(),
97 infos.base2k(),
98 infos.k(),
99 infos.rank(),
100 infos.dnum(),
101 infos.dsize(),
102 )
103 }
104
105 pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
106 GGLWEToGGSWKeyCompressed {
107 keys: (0..rank.as_usize())
108 .map(|_| GGLWECompressed::alloc(n, base2k, k, rank, rank, dnum, dsize))
109 .collect(),
110 }
111 }
112
113 pub fn bytes_of_from_infos<A>(infos: &A) -> usize
114 where
115 A: GGLWEInfos,
116 {
117 assert_eq!(
118 infos.rank_in(),
119 infos.rank_out(),
120 "rank_in != rank_out is not supported for GGLWEToGGSWKeyCompressed"
121 );
122 Self::bytes_of(
123 infos.n(),
124 infos.base2k(),
125 infos.k(),
126 infos.rank(),
127 infos.dnum(),
128 infos.dsize(),
129 )
130 }
131
132 pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
133 rank.as_usize() * GGLWECompressed::bytes_of(n, base2k, k, rank, dnum, dsize)
134 }
135}
136
137impl<D: DataMut> GGLWEToGGSWKeyCompressed<D> {
138 // Returns a mutable reference to GGLWE_{s}([s[i]*s[0], s[i]*s[1], ..., s[i]*s[rank]])
139 pub fn at_mut(&mut self, i: usize) -> &mut GGLWECompressed<D> {
140 assert!((i as u32) < self.rank());
141 &mut self.keys[i]
142 }
143}
144
145impl<D: DataRef> GGLWEToGGSWKeyCompressed<D> {
146 // Returns a reference to GGLWE_{s}(s[i] * s[j])
147 pub fn at(&self, i: usize) -> &GGLWECompressed<D> {
148 assert!((i as u32) < self.rank());
149 &self.keys[i]
150 }
151}
152
153impl<D: DataMut> ReaderFrom for GGLWEToGGSWKeyCompressed<D> {
154 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
155 let len: usize = reader.read_u64::<LittleEndian>()? as usize;
156 if self.keys.len() != len {
157 return Err(std::io::Error::new(
158 std::io::ErrorKind::InvalidData,
159 format!("self.keys.len()={} != read len={}", self.keys.len(), len),
160 ));
161 }
162 for key in &mut self.keys {
163 key.read_from(reader)?;
164 }
165 Ok(())
166 }
167}
168
169impl<D: DataRef> WriterTo for GGLWEToGGSWKeyCompressed<D> {
170 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
171 writer.write_u64::<LittleEndian>(self.keys.len() as u64)?;
172 for key in &self.keys {
173 key.write_to(writer)?;
174 }
175 Ok(())
176 }
177}
178
179pub trait GGLWEToGGSWKeyDecompress
180where
181 Self: GGLWEDecompress,
182{
183 fn decompress_gglwe_to_ggsw_key<R, O>(&self, res: &mut R, other: &O)
184 where
185 R: GGLWEToGGSWKeyToMut,
186 O: GGLWEToGGSWKeyCompressedToRef,
187 {
188 let res: &mut GGLWEToGGSWKey<&mut [u8]> = &mut res.to_mut();
189 let other: &GGLWEToGGSWKeyCompressed<&[u8]> = &other.to_ref();
190
191 assert_eq!(res.keys.len(), other.keys.len());
192
193 for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
194 self.decompress_gglwe(a, b);
195 }
196 }
197}
198
199impl<D: DataMut> GGLWEToGGSWKey<D> {
200 pub fn decompress<O, M>(&mut self, module: &M, other: &O)
201 where
202 M: GGLWEToGGSWKeyDecompress,
203 O: GGLWEToGGSWKeyCompressedToRef,
204 {
205 module.decompress_gglwe_to_ggsw_key(self, other);
206 }
207}
208
209pub trait GGLWEToGGSWKeyCompressedToRef {
210 fn to_ref(&self) -> GGLWEToGGSWKeyCompressed<&[u8]>;
211}
212
213impl<D: DataRef> GGLWEToGGSWKeyCompressedToRef for GGLWEToGGSWKeyCompressed<D>
214where
215 GGLWECompressed<D>: GGLWECompressedToRef,
216{
217 fn to_ref(&self) -> GGLWEToGGSWKeyCompressed<&[u8]> {
218 GGLWEToGGSWKeyCompressed {
219 keys: self.keys.iter().map(|c| c.to_ref()).collect(),
220 }
221 }
222}
223
224pub trait GGLWEToGGSWKeyCompressedToMut {
225 fn to_mut(&mut self) -> GGLWEToGGSWKeyCompressed<&mut [u8]>;
226}
227
228impl<D: DataMut> GGLWEToGGSWKeyCompressedToMut for GGLWEToGGSWKeyCompressed<D>
229where
230 GGLWECompressed<D>: GGLWECompressedToMut,
231{
232 fn to_mut(&mut self) -> GGLWEToGGSWKeyCompressed<&mut [u8]> {
233 GGLWEToGGSWKeyCompressed {
234 keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
235 }
236 }
237}