poulpy/poulpy-core/src/layouts/compressed/lwe_to_glwe_key.rs
poulpy-core/src/layouts/compressed/lwe_to_glwe_key.rsRaw
1use poulpy_hal::{
2 layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
3 source::Source,
4};
5
6use crate::layouts::{
7 Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos,
8 GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWEToGLWEKey, Rank, TorusPrecision,
9 compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
10};
11use std::fmt;
12
13#[derive(PartialEq, Eq, Clone)]
14pub struct LWEToGLWEKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
15
16impl<D: Data> LWEInfos for LWEToGLWEKeyCompressed<D> {
17 fn n(&self) -> Degree {
18 self.0.n()
19 }
20
21 fn base2k(&self) -> Base2K {
22 self.0.base2k()
23 }
24
25 fn k(&self) -> TorusPrecision {
26 self.0.k()
27 }
28 fn size(&self) -> usize {
29 self.0.size()
30 }
31}
32impl<D: Data> GLWEInfos for LWEToGLWEKeyCompressed<D> {
33 fn rank(&self) -> Rank {
34 self.rank_out()
35 }
36}
37
38impl<D: Data> GGLWEInfos for LWEToGLWEKeyCompressed<D> {
39 fn dsize(&self) -> Dsize {
40 self.0.dsize()
41 }
42
43 fn rank_in(&self) -> Rank {
44 self.0.rank_in()
45 }
46
47 fn rank_out(&self) -> Rank {
48 self.0.rank_out()
49 }
50
51 fn dnum(&self) -> Dnum {
52 self.0.dnum()
53 }
54}
55
56impl<D: DataRef> fmt::Debug for LWEToGLWEKeyCompressed<D> {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 write!(f, "{self}")
59 }
60}
61
62impl<D: DataMut> FillUniform for LWEToGLWEKeyCompressed<D> {
63 fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
64 self.0.fill_uniform(log_bound, source);
65 }
66}
67
68impl<D: DataRef> fmt::Display for LWEToGLWEKeyCompressed<D> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "(LWEToGLWESwitchingKeyCompressed) {}", self.0)
71 }
72}
73
74impl<D: DataMut> ReaderFrom for LWEToGLWEKeyCompressed<D> {
75 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
76 self.0.read_from(reader)
77 }
78}
79
80impl<D: DataRef> WriterTo for LWEToGLWEKeyCompressed<D> {
81 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
82 self.0.write_to(writer)
83 }
84}
85
86impl LWEToGLWEKeyCompressed<Vec<u8>> {
87 pub fn alloc_from_infos<A>(infos: &A) -> Self
88 where
89 A: GGLWEInfos,
90 {
91 assert_eq!(
92 infos.dsize().0,
93 1,
94 "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
95 );
96 assert_eq!(
97 infos.rank_in().0,
98 1,
99 "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
100 );
101 Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
102 }
103
104 pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
105 LWEToGLWEKeyCompressed(GLWESwitchingKeyCompressed::alloc(
106 n,
107 base2k,
108 k,
109 Rank(1),
110 rank_out,
111 dnum,
112 Dsize(1),
113 ))
114 }
115
116 pub fn bytes_of_from_infos<A>(infos: &A) -> usize
117 where
118 A: GGLWEInfos,
119 {
120 assert_eq!(
121 infos.dsize().0,
122 1,
123 "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
124 );
125 assert_eq!(
126 infos.rank_in().0,
127 1,
128 "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
129 );
130 GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
131 }
132
133 pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
134 GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1))
135 }
136}
137
138pub trait LWEToGLWEKeyDecompress
139where
140 Self: GLWESwitchingKeyDecompress,
141{
142 fn decompress_lwe_to_glwe_key<R, O>(&self, res: &mut R, other: &O)
143 where
144 R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
145 O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
146 {
147 self.decompress_glwe_switching_key(res, other);
148 }
149}
150
151impl<B: Backend> LWEToGLWEKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
152
153impl<D: DataMut> LWEToGLWEKey<D> {
154 pub fn decompress<O, M>(&mut self, module: &M, other: &O)
155 where
156 O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
157 M: LWEToGLWEKeyDecompress,
158 {
159 module.decompress_lwe_to_glwe_key(self, other);
160 }
161}
162
163impl<D: DataRef> GGLWECompressedToRef for LWEToGLWEKeyCompressed<D> {
164 fn to_ref(&self) -> GGLWECompressed<&[u8]> {
165 self.0.to_ref()
166 }
167}
168
169impl<D: DataMut> GGLWECompressedToMut for LWEToGLWEKeyCompressed<D> {
170 fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
171 self.0.to_mut()
172 }
173}