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