Skip to main content

poulpy_core/layouts/
glwe_public_key.rs

1use poulpy_hal::layouts::{Backend, Data, HostDataMut, HostDataRef, ReaderFrom, VecZnx, WriterTo};
2
3use crate::{
4    GetDistribution, GetDistributionMut,
5    dist::Distribution,
6    layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToBackendMut, GLWEToBackendRef, 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: HostDataMut> GetDistributionMut for GLWEPublicKey<D> {
16    fn dist_mut(&mut self) -> &mut Distribution {
17        &mut self.dist
18    }
19}
20
21impl<D: HostDataRef> 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 n(&self) -> Degree {
41        self.key.n()
42    }
43
44    fn size(&self) -> usize {
45        self.key.size()
46    }
47}
48
49impl<D: Data> GLWEInfos for GLWEPublicKey<D> {
50    fn rank(&self) -> Rank {
51        self.key.rank()
52    }
53}
54
55impl LWEInfos for GLWEPublicKeyLayout {
56    fn base2k(&self) -> Base2K {
57        self.base2k
58    }
59
60    fn n(&self) -> Degree {
61        self.n
62    }
63
64    fn size(&self) -> usize {
65        self.k.0.div_ceil(self.base2k.0) as usize
66    }
67}
68
69impl GLWEInfos for GLWEPublicKeyLayout {
70    fn rank(&self) -> Rank {
71        self.rank
72    }
73}
74
75#[expect(
76    dead_code,
77    reason = "host-owned constructors are kept for serialization and host-only staging"
78)]
79impl GLWEPublicKey<Vec<u8>> {
80    pub(crate) fn alloc_from_infos<A>(infos: &A) -> Self
81    where
82        A: GLWEInfos,
83    {
84        Self::alloc(infos.n(), infos.base2k(), infos.max_k(), infos.rank())
85    }
86
87    pub(crate) fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
88        GLWEPublicKey {
89            key: GLWE::alloc(n, base2k, k, rank),
90            dist: Distribution::NONE,
91        }
92    }
93
94    pub fn bytes_of_from_infos<A>(infos: &A) -> usize
95    where
96        A: GLWEInfos,
97    {
98        Self::bytes_of(infos.n(), infos.base2k(), infos.max_k(), infos.rank())
99    }
100
101    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
102        VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
103    }
104}
105
106impl<D: HostDataMut> ReaderFrom for GLWEPublicKey<D> {
107    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
108        self.dist = Distribution::read_from(reader)?;
109        self.key.read_from(reader)
110    }
111}
112
113impl<D: HostDataRef> WriterTo for GLWEPublicKey<D> {
114    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
115        match self.dist.write_to(writer) {
116            Ok(()) => {}
117            Err(e) => return Err(e),
118        }
119        self.key.write_to(writer)
120    }
121}
122
123impl<BE: Backend, D: Data> GLWEToBackendRef<BE> for GLWEPublicKey<D>
124where
125    GLWE<D>: GLWEToBackendRef<BE>,
126{
127    fn to_backend_ref(&self) -> GLWE<BE::BufRef<'_>> {
128        self.key.to_backend_ref()
129    }
130}
131
132impl<BE: Backend, D: Data> GLWEToBackendMut<BE> for GLWEPublicKey<D>
133where
134    GLWE<D>: GLWEToBackendMut<BE>,
135{
136    fn to_backend_mut(&mut self) -> GLWE<BE::BufMut<'_>> {
137        self.key.to_backend_mut()
138    }
139}