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, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GLWESwitchingKeyCompressed,
10};
11
12#[derive(PartialEq, Eq, Clone)]
13pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
14
15impl<D: Data> LWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
16 fn base2k(&self) -> Base2K {
17 self.0.base2k()
18 }
19
20 fn k(&self) -> TorusPrecision {
21 self.0.k()
22 }
23
24 fn n(&self) -> Degree {
25 self.0.n()
26 }
27 fn size(&self) -> usize {
28 self.0.size()
29 }
30}
31
32impl<D: Data> GLWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
33 fn rank(&self) -> Rank {
34 self.rank_out()
35 }
36}
37
38impl<D: Data> GGLWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
39 fn rank_in(&self) -> Rank {
40 self.0.rank_in()
41 }
42
43 fn dsize(&self) -> Dsize {
44 self.0.dsize()
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 GLWEToLWESwitchingKeyCompressed<D> {
57 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 write!(f, "{self}")
59 }
60}
61
62impl<D: DataMut> FillUniform for GLWEToLWESwitchingKeyCompressed<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 GLWEToLWESwitchingKeyCompressed<D> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "(GLWEToLWESwitchingKeyCompressed) {}", self.0)
71 }
72}
73
74impl<D: DataMut> ReaderFrom for GLWEToLWESwitchingKeyCompressed<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 GLWEToLWESwitchingKeyCompressed<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 GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
87 pub fn alloc<A>(infos: &A) -> Self
88 where
89 A: GGLWEInfos,
90 {
91 debug_assert_eq!(
92 infos.rank_out().0,
93 1,
94 "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
95 );
96 debug_assert_eq!(
97 infos.dsize().0,
98 1,
99 "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
100 );
101 Self(GLWESwitchingKeyCompressed::alloc(infos))
102 }
103
104 pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
105 Self(GLWESwitchingKeyCompressed::alloc_with(
106 n,
107 base2k,
108 k,
109 rank_in,
110 Rank(1),
111 dnum,
112 Dsize(1),
113 ))
114 }
115
116 pub fn alloc_bytes<A>(infos: &A) -> usize
117 where
118 A: GGLWEInfos,
119 {
120 debug_assert_eq!(
121 infos.rank_out().0,
122 1,
123 "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
124 );
125 debug_assert_eq!(
126 infos.dsize().0,
127 1,
128 "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
129 );
130 GLWESwitchingKeyCompressed::alloc_bytes(infos)
131 }
132
133 pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize {
134 GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1))
135 }
136}