poulpy/poulpy-core/src/layouts/glwe_pt.rs
poulpy-core/src/layouts/glwe_pt.rsRaw
1use std::fmt;
2
3use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos};
4
5use crate::layouts::{
6 Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision,
7};
8
9#[derive(PartialEq, Eq, Copy, Clone, Debug)]
10pub struct GLWEPlaintextLayout {
11 pub n: Degree,
12 pub base2k: Base2K,
13 pub k: TorusPrecision,
14}
15
16impl LWEInfos for GLWEPlaintextLayout {
17 fn base2k(&self) -> Base2K {
18 self.base2k
19 }
20
21 fn k(&self) -> TorusPrecision {
22 self.k
23 }
24
25 fn n(&self) -> Degree {
26 self.n
27 }
28}
29
30impl GLWEInfos for GLWEPlaintextLayout {
31 fn rank(&self) -> Rank {
32 Rank(0)
33 }
34}
35
36pub struct GLWEPlaintext<D: Data> {
37 pub data: VecZnx<D>,
38 pub base2k: Base2K,
39 pub k: TorusPrecision,
40}
41
42impl<D: DataMut> SetGLWEInfos for GLWEPlaintext<D> {
43 fn set_base2k(&mut self, base2k: Base2K) {
44 self.base2k = base2k
45 }
46
47 fn set_k(&mut self, k: TorusPrecision) {
48 self.k = k
49 }
50}
51
52impl<D: Data> LWEInfos for GLWEPlaintext<D> {
53 fn base2k(&self) -> Base2K {
54 self.base2k
55 }
56
57 fn k(&self) -> TorusPrecision {
58 self.k
59 }
60
61 fn size(&self) -> usize {
62 self.data.size()
63 }
64
65 fn n(&self) -> Degree {
66 Degree(self.data.n() as u32)
67 }
68}
69
70impl<D: Data> GLWEInfos for GLWEPlaintext<D> {
71 fn rank(&self) -> Rank {
72 Rank(self.data.cols() as u32 - 1)
73 }
74}
75
76impl<D: DataRef> fmt::Display for GLWEPlaintext<D> {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 write!(
79 f,
80 "GLWEPlaintext: base2k={} k={}: {}",
81 self.base2k().0,
82 self.k().0,
83 self.data
84 )
85 }
86}
87
88pub trait GLWEPlaintextAlloc
89where
90 Self: GetDegree,
91{
92 fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext<Vec<u8>> {
93 GLWEPlaintext {
94 data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize),
95 base2k,
96 k,
97 }
98 }
99
100 fn alloc_glwe_plaintext_from_infos<A>(&self, infos: &A) -> GLWEPlaintext<Vec<u8>>
101 where
102 A: GLWEInfos,
103 {
104 self.alloc_glwe_plaintext(infos.base2k(), infos.k())
105 }
106
107 fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize {
108 VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize)
109 }
110
111 fn bytes_of_glwe_plaintext_from_infos<A>(&self, infos: &A) -> usize
112 where
113 A: GLWEInfos,
114 {
115 self.bytes_of_glwe_plaintext(infos.base2k(), infos.k())
116 }
117}
118
119impl<B: Backend> GLWEPlaintextAlloc for Module<B> where Self: GetDegree {}
120
121impl GLWEPlaintext<Vec<u8>> {
122 pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
123 where
124 A: GLWEInfos,
125 M: GLWEPlaintextAlloc,
126 {
127 module.alloc_glwe_plaintext_from_infos(infos)
128 }
129
130 pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self
131 where
132 M: GLWEPlaintextAlloc,
133 {
134 module.alloc_glwe_plaintext(base2k, k)
135 }
136
137 pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
138 where
139 A: GLWEInfos,
140 M: GLWEPlaintextAlloc,
141 {
142 module.bytes_of_glwe_plaintext_from_infos(infos)
143 }
144
145 pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> usize
146 where
147 M: GLWEPlaintextAlloc,
148 {
149 module.bytes_of_glwe_plaintext(base2k, k)
150 }
151}
152
153impl<D: DataRef> GLWEToRef for GLWEPlaintext<D> {
154 fn to_ref(&self) -> GLWE<&[u8]> {
155 GLWE {
156 k: self.k,
157 base2k: self.base2k,
158 data: self.data.to_ref(),
159 }
160 }
161}
162
163impl<D: DataMut> GLWEToMut for GLWEPlaintext<D> {
164 fn to_mut(&mut self) -> GLWE<&mut [u8]> {
165 GLWE {
166 k: self.k,
167 base2k: self.base2k,
168 data: self.data.to_mut(),
169 }
170 }
171}
172
173pub trait GLWEPlaintextToRef {
174 fn to_ref(&self) -> GLWEPlaintext<&[u8]>;
175}
176
177impl<D: DataRef> GLWEPlaintextToRef for GLWEPlaintext<D> {
178 fn to_ref(&self) -> GLWEPlaintext<&[u8]> {
179 GLWEPlaintext {
180 data: self.data.to_ref(),
181 base2k: self.base2k,
182 k: self.k,
183 }
184 }
185}
186
187pub trait GLWEPlaintextToMut {
188 fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]>;
189}
190
191impl<D: DataMut> GLWEPlaintextToMut for GLWEPlaintext<D> {
192 fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]> {
193 GLWEPlaintext {
194 base2k: self.base2k,
195 k: self.k,
196 data: self.data.to_mut(),
197 }
198 }
199}