poulpy_core/layouts/compressed/
glwe_ct.rs1use poulpy_hal::{
2 api::{VecZnxCopy, VecZnxFillUniform},
3 layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, WriterTo, ZnxInfos},
4 source::Source,
5};
6
7use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress};
8use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
9use std::fmt;
10
11#[derive(PartialEq, Eq, Clone)]
12pub struct GLWECiphertextCompressed<D: Data> {
13 pub(crate) data: VecZnx<D>,
14 pub(crate) base2k: Base2K,
15 pub(crate) k: TorusPrecision,
16 pub(crate) rank: Rank,
17 pub(crate) seed: [u8; 32],
18}
19
20impl<D: Data> LWEInfos for GLWECiphertextCompressed<D> {
21 fn base2k(&self) -> Base2K {
22 self.base2k
23 }
24
25 fn k(&self) -> TorusPrecision {
26 self.k
27 }
28
29 fn size(&self) -> usize {
30 self.data.size()
31 }
32
33 fn n(&self) -> Degree {
34 Degree(self.data.n() as u32)
35 }
36}
37impl<D: Data> GLWEInfos for GLWECiphertextCompressed<D> {
38 fn rank(&self) -> Rank {
39 self.rank
40 }
41}
42
43impl<D: DataRef> fmt::Debug for GLWECiphertextCompressed<D> {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 write!(f, "{self}")
46 }
47}
48
49impl<D: DataRef> fmt::Display for GLWECiphertextCompressed<D> {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 write!(
52 f,
53 "GLWECiphertextCompressed: base2k={} k={} rank={} seed={:?}: {}",
54 self.base2k(),
55 self.k(),
56 self.rank(),
57 self.seed,
58 self.data
59 )
60 }
61}
62
63impl<D: DataMut> FillUniform for GLWECiphertextCompressed<D> {
64 fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
65 self.data.fill_uniform(log_bound, source);
66 }
67}
68
69impl GLWECiphertextCompressed<Vec<u8>> {
70 pub fn alloc<A>(infos: &A) -> Self
71 where
72 A: GLWEInfos,
73 {
74 Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank())
75 }
76
77 pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
78 Self {
79 data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize),
80 base2k,
81 k,
82 rank,
83 seed: [0u8; 32],
84 }
85 }
86
87 pub fn alloc_bytes<A>(infos: &A) -> usize
88 where
89 A: GLWEInfos,
90 {
91 Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k())
92 }
93
94 pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
95 VecZnx::alloc_bytes(n.into(), 1, k.0.div_ceil(base2k.0) as usize)
96 }
97}
98
99impl<D: DataMut> ReaderFrom for GLWECiphertextCompressed<D> {
100 fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
101 self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
102 self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
103 self.rank = Rank(reader.read_u32::<LittleEndian>()?);
104 reader.read_exact(&mut self.seed)?;
105 self.data.read_from(reader)
106 }
107}
108
109impl<D: DataRef> WriterTo for GLWECiphertextCompressed<D> {
110 fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
111 writer.write_u32::<LittleEndian>(self.k.into())?;
112 writer.write_u32::<LittleEndian>(self.base2k.into())?;
113 writer.write_u32::<LittleEndian>(self.rank.into())?;
114 writer.write_all(&self.seed)?;
115 self.data.write_to(writer)
116 }
117}
118
119impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GLWECiphertextCompressed<DR>> for GLWECiphertext<D>
120where
121 Module<B>: VecZnxFillUniform + VecZnxCopy,
122{
123 fn decompress(&mut self, module: &Module<B>, other: &GLWECiphertextCompressed<DR>) {
124 #[cfg(debug_assertions)]
125 {
126 assert_eq!(
127 self.n(),
128 other.n(),
129 "invalid receiver: self.n()={} != other.n()={}",
130 self.n(),
131 other.n()
132 );
133 assert_eq!(
134 self.size(),
135 other.size(),
136 "invalid receiver: self.size()={} != other.size()={}",
137 self.size(),
138 other.size()
139 );
140 assert_eq!(
141 self.rank(),
142 other.rank(),
143 "invalid receiver: self.rank()={} != other.rank()={}",
144 self.rank(),
145 other.rank()
146 );
147 }
148
149 let mut source: Source = Source::new(other.seed);
150 self.decompress_internal(module, other, &mut source);
151 }
152}
153
154impl<D: DataMut> GLWECiphertext<D> {
155 pub(crate) fn decompress_internal<DataOther, B: Backend>(
156 &mut self,
157 module: &Module<B>,
158 other: &GLWECiphertextCompressed<DataOther>,
159 source: &mut Source,
160 ) where
161 DataOther: DataRef,
162 Module<B>: VecZnxCopy + VecZnxFillUniform,
163 {
164 #[cfg(debug_assertions)]
165 {
166 assert_eq!(self.rank(), other.rank());
167 debug_assert_eq!(self.size(), other.size());
168 }
169
170 module.vec_znx_copy(&mut self.data, 0, &other.data, 0);
171 (1..(other.rank() + 1).into()).for_each(|i| {
172 module.vec_znx_fill_uniform(other.base2k.into(), &mut self.data, i, source);
173 });
174
175 self.base2k = other.base2k;
176 self.k = other.k;
177 }
178}