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