poulpy/poulpy-core/src/layouts/glwe_pk.rs
poulpy-core/src/layouts/glwe_pk.rsRaw
1use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos};
2
3use crate::{
4 dist::Distribution,
5 layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision},
6};
7use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
8
9#[derive(PartialEq, Eq)]
10pub struct GLWEPublicKey<D: Data> {
11 pub(crate) data: VecZnx<D>,
12 pub(crate) base2k: Base2K,
13 pub(crate) k: TorusPrecision,
14 pub(crate) dist: Distribution,
15}
16
17#[derive(PartialEq, Eq, Copy, Clone, Debug)]
18pub struct GLWEPublicKeyLayout {
19 pub n: Degree,
20 pub base2k: Base2K,
21 pub k: TorusPrecision,
22 pub rank: Rank,
23}
24
25pub trait GetDist {
26 fn get_dist(&self) -> Distribution;
27}
28
29impl<D: DataRef> GetDist for GLWEPublicKey<D> {
30 fn get_dist(&self) -> Distribution {
31 self.dist
32 }
33}
34
35impl<D: Data> LWEInfos for GLWEPublicKey<D> {
36 fn base2k(&self) -> Base2K {
37 self.base2k
38 }
39
40 fn k(&self) -> TorusPrecision {
41 self.k
42 }
43
44 fn n(&self) -> Degree {
45 Degree(self.data.n() as u32)
46 }
47
48 fn size(&self) -> usize {
49 self.data.size()
50 }
51}
52
53impl<D: Data> GLWEInfos for GLWEPublicKey<D> {
54 fn rank(&self) -> Rank {
55 Rank(self.data.cols() as u32 - 1)
56 }
57}
58
59impl LWEInfos for GLWEPublicKeyLayout {
60 fn base2k(&self) -> Base2K {
61 self.base2k
62 }
63
64 fn k(&self) -> TorusPrecision {
65 self.k
66 }
67
68 fn n(&self) -> Degree {
69 self.n
70 }
71
72 fn size(&self) -> usize {
73 self.k.0.div_ceil(self.base2k.0) as usize
74 }
75}
76
77impl GLWEInfos for GLWEPublicKeyLayout {
78 fn rank(&self) -> Rank {
79 self.rank
80 }
81}
82
83impl GLWEPublicKey<Vec<u8>> {
84 pub fn alloc<A>(infos: &A) -> Self
85 where
86 A: GLWEInfos,
87 {
88 Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank())
89 }
90
91 pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
92 Self {
93 data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
94 base2k,
95 k,
96 dist: Distribution::NONE,
97 }
98 }
99
100 pub fn alloc_bytes<A>(infos: &A) -> usize
101 where
102 A: GLWEInfos,
103 {
104 Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank())
105 }
106
107 pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
108 VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
109 }
110}
111
112impl<D: DataMut> ReaderFrom for GLWEPublicKey<D> {
113 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
114 self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
115 self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
116 match Distribution::read_from(reader) {
117 Ok(dist) => self.dist = dist,
118 Err(e) => return Err(e),
119 }
120 self.data.read_from(reader)
121 }
122}
123
124impl<D: DataRef> WriterTo for GLWEPublicKey<D> {
125 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
126 writer.write_u32::<LittleEndian>(self.k.0)?;
127 writer.write_u32::<LittleEndian>(self.base2k.0)?;
128 match self.dist.write_to(writer) {
129 Ok(()) => {}
130 Err(e) => return Err(e),
131 }
132 self.data.write_to(writer)
133 }
134}
135
136pub trait GLWEPublicKeyToRef {
137 fn to_ref(&self) -> GLWEPublicKey<&[u8]>;
138}
139
140impl<D: DataRef> GLWEPublicKeyToRef for GLWEPublicKey<D> {
141 fn to_ref(&self) -> GLWEPublicKey<&[u8]> {
142 GLWEPublicKey {
143 data: self.data.to_ref(),
144 base2k: self.base2k,
145 k: self.k,
146 dist: self.dist,
147 }
148 }
149}
150
151pub trait GLWEPublicKeyToMut {
152 fn to_mut(&mut self) -> GLWEPublicKey<&mut [u8]>;
153}
154
155impl<D: DataMut> GLWEPublicKeyToMut for GLWEPublicKey<D> {
156 fn to_mut(&mut self) -> GLWEPublicKey<&mut [u8]> {
157 GLWEPublicKey {
158 base2k: self.base2k,
159 k: self.k,
160 dist: self.dist,
161 data: self.data.to_mut(),
162 }
163 }
164}