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::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
5 source::Source,
6};
7
8use crate::layouts::{
9 Base2K, Degree, Digits, GGLWELayoutInfos, GLWEInfos, LWEInfos, Rank, Rows, TorusPrecision,
10 compressed::GGLWESwitchingKeyCompressed,
11};
12
13#[derive(PartialEq, Eq, Clone)]
14pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
15
16impl<D: Data> LWEInfos for GLWEToLWESwitchingKeyCompressed<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}
32
33impl<D: Data> GLWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
34 fn rank(&self) -> Rank {
35 self.rank_out()
36 }
37}
38
39impl<D: Data> GGLWELayoutInfos for GLWEToLWESwitchingKeyCompressed<D> {
40 fn rank_in(&self) -> Rank {
41 self.0.rank_in()
42 }
43
44 fn digits(&self) -> Digits {
45 self.0.digits()
46 }
47
48 fn rank_out(&self) -> Rank {
49 self.0.rank_out()
50 }
51
52 fn rows(&self) -> Rows {
53 self.0.rows()
54 }
55}
56
57impl<D: DataRef> fmt::Debug for GLWEToLWESwitchingKeyCompressed<D> {
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 write!(f, "{self}")
60 }
61}
62
63impl<D: DataMut> FillUniform for GLWEToLWESwitchingKeyCompressed<D> {
64 fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
65 self.0.fill_uniform(log_bound, source);
66 }
67}
68
69impl<D: DataRef> fmt::Display for GLWEToLWESwitchingKeyCompressed<D> {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 write!(f, "(GLWEToLWESwitchingKeyCompressed) {}", self.0)
72 }
73}
74
75impl<D: DataMut> ReaderFrom for GLWEToLWESwitchingKeyCompressed<D> {
76 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
77 self.0.read_from(reader)
78 }
79}
80
81impl<D: DataRef> WriterTo for GLWEToLWESwitchingKeyCompressed<D> {
82 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
83 self.0.write_to(writer)
84 }
85}
86
87impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
88 pub fn alloc<A>(infos: &A) -> Self
89 where
90 A: GGLWELayoutInfos,
91 {
92 debug_assert_eq!(
93 infos.rank_out().0,
94 1,
95 "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
96 );
97 debug_assert_eq!(
98 infos.digits().0,
99 1,
100 "digits > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
101 );
102 Self(GGLWESwitchingKeyCompressed::alloc(infos))
103 }
104
105 pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, rank_in: Rank) -> Self {
106 Self(GGLWESwitchingKeyCompressed::alloc_with(
107 n,
108 base2k,
109 k,
110 rows,
111 Digits(1),
112 rank_in,
113 Rank(1),
114 ))
115 }
116
117 pub fn alloc_bytes<A>(infos: &A) -> usize
118 where
119 A: GGLWELayoutInfos,
120 {
121 debug_assert_eq!(
122 infos.rank_out().0,
123 1,
124 "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
125 );
126 debug_assert_eq!(
127 infos.digits().0,
128 1,
129 "digits > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
130 );
131 GGLWESwitchingKeyCompressed::alloc_bytes(infos)
132 }
133
134 pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rows: Rows, rank_in: Rank) -> usize {
135 GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rows, Digits(1), rank_in, Rank(1))
136 }
137}