poulpy/poulpy-core/src/layouts/compressed/lwe_ksk.rs
poulpy-core/src/layouts/compressed/lwe_ksk.rsRaw
1use poulpy_hal::{
2 api::{VecZnxCopy, VecZnxFillUniform},
3 layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
4 source::Source,
5};
6
7use crate::layouts::{
8 Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision,
9 compressed::{Decompress, GLWESwitchingKeyCompressed},
10};
11use std::fmt;
12
13#[derive(PartialEq, Eq, Clone)]
14pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
15
16impl<D: Data> LWEInfos for LWESwitchingKeyCompressed<D> {
17 fn base2k(&self) -> Base2K {
18 self.0.base2k()
19 }
20
21 fn k(&self) -> TorusPrecision {
22 self.0.k()
23 }
24
25 fn n(&self) -> Degree {
26 self.0.n()
27 }
28 fn size(&self) -> usize {
29 self.0.size()
30 }
31}
32impl<D: Data> GLWEInfos for LWESwitchingKeyCompressed<D> {
33 fn rank(&self) -> Rank {
34 self.rank_out()
35 }
36}
37
38impl<D: Data> GGLWEInfos for LWESwitchingKeyCompressed<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 LWESwitchingKeyCompressed<D> {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 write!(f, "{self}")
59 }
60}
61
62impl<D: DataMut> FillUniform for LWESwitchingKeyCompressed<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 LWESwitchingKeyCompressed<D> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "(LWESwitchingKeyCompressed) {}", self.0)
71 }
72}
73
74impl<D: DataMut> ReaderFrom for LWESwitchingKeyCompressed<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 LWESwitchingKeyCompressed<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 LWESwitchingKeyCompressed<Vec<u8>> {
87 pub fn alloc<A>(infos: &A) -> Self
88 where
89 A: GGLWEInfos,
90 {
91 debug_assert_eq!(
92 infos.dsize().0,
93 1,
94 "dsize > 1 is not supported for LWESwitchingKeyCompressed"
95 );
96 debug_assert_eq!(
97 infos.rank_in().0,
98 1,
99 "rank_in > 1 is not supported for LWESwitchingKeyCompressed"
100 );
101 debug_assert_eq!(
102 infos.rank_out().0,
103 1,
104 "rank_out > 1 is not supported for LWESwitchingKeyCompressed"
105 );
106 Self(GLWESwitchingKeyCompressed::alloc(infos))
107 }
108
109 pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
110 Self(GLWESwitchingKeyCompressed::alloc_with(
111 n,
112 base2k,
113 k,
114 Rank(1),
115 Rank(1),
116 dnum,
117 Dsize(1),
118 ))
119 }
120
121 pub fn alloc_bytes<A>(infos: &A) -> usize
122 where
123 A: GGLWEInfos,
124 {
125 debug_assert_eq!(
126 infos.dsize().0,
127 1,
128 "dsize > 1 is not supported for LWESwitchingKey"
129 );
130 debug_assert_eq!(
131 infos.rank_in().0,
132 1,
133 "rank_in > 1 is not supported for LWESwitchingKey"
134 );
135 debug_assert_eq!(
136 infos.rank_out().0,
137 1,
138 "rank_out > 1 is not supported for LWESwitchingKey"
139 );
140 GLWESwitchingKeyCompressed::alloc_bytes(infos)
141 }
142
143 pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
144 GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1))
145 }
146}
147
148impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWESwitchingKeyCompressed<DR>> for LWESwitchingKey<D>
149where
150 Module<B>: VecZnxFillUniform + VecZnxCopy,
151{
152 fn decompress(&mut self, module: &Module<B>, other: &LWESwitchingKeyCompressed<DR>) {
153 self.0.decompress(module, &other.0);
154 }
155}