poulpy_core/layouts/
glwe_public_key.rs1use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, WriterTo};
2
3use crate::{
4 GetDistribution, GetDistributionMut,
5 dist::Distribution,
6 layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, Rank, TorusPrecision},
7};
8
9#[derive(PartialEq, Eq)]
10pub struct GLWEPublicKey<D: Data> {
11 pub(crate) key: GLWE<D>,
12 pub(crate) dist: Distribution,
13}
14
15impl<D: DataMut> GetDistributionMut for GLWEPublicKey<D> {
16 fn dist_mut(&mut self) -> &mut Distribution {
17 &mut self.dist
18 }
19}
20
21impl<D: DataRef> GetDistribution for GLWEPublicKey<D> {
22 fn dist(&self) -> &Distribution {
23 &self.dist
24 }
25}
26
27#[derive(PartialEq, Eq, Copy, Clone, Debug)]
28pub struct GLWEPublicKeyLayout {
29 pub n: Degree,
30 pub base2k: Base2K,
31 pub k: TorusPrecision,
32 pub rank: Rank,
33}
34
35impl<D: Data> LWEInfos for GLWEPublicKey<D> {
36 fn base2k(&self) -> Base2K {
37 self.key.base2k()
38 }
39
40 fn k(&self) -> TorusPrecision {
41 self.key.k()
42 }
43
44 fn n(&self) -> Degree {
45 self.key.n()
46 }
47
48 fn size(&self) -> usize {
49 self.key.size()
50 }
51}
52
53impl<D: Data> GLWEInfos for GLWEPublicKey<D> {
54 fn rank(&self) -> Rank {
55 self.key.rank()
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_from_infos<A>(infos: &A) -> Self
85 where
86 A: GLWEInfos,
87 {
88 Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
89 }
90
91 pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
92 GLWEPublicKey {
93 key: GLWE::alloc(n, base2k, k, rank),
94 dist: Distribution::NONE,
95 }
96 }
97
98 pub fn bytes_of_from_infos<A>(infos: &A) -> usize
99 where
100 A: GLWEInfos,
101 {
102 Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank())
103 }
104
105 pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
106 VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
107 }
108}
109
110impl<D: DataMut> ReaderFrom for GLWEPublicKey<D> {
111 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
112 match Distribution::read_from(reader) {
113 Ok(dist) => self.dist = dist,
114 Err(e) => return Err(e),
115 }
116 self.key.read_from(reader)
117 }
118}
119
120impl<D: DataRef> WriterTo for GLWEPublicKey<D> {
121 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
122 match self.dist.write_to(writer) {
123 Ok(()) => {}
124 Err(e) => return Err(e),
125 }
126 self.key.write_to(writer)
127 }
128}
129
130impl<D: DataRef> GLWEToRef for GLWEPublicKey<D> {
131 fn to_ref(&self) -> GLWE<&[u8]> {
132 self.key.to_ref()
133 }
134}
135
136impl<D: DataMut> GLWEToMut for GLWEPublicKey<D> {
137 fn to_mut(&mut self) -> GLWE<&mut [u8]> {
138 self.key.to_mut()
139 }
140}