poulpy/core/src/gglwe_ciphertext.rs
core/src/gglwe_ciphertext.rsRaw
1use base2k::{
2 Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnx, ScalarZnxDft,
3 ScalarZnxDftToRef, ScalarZnxToRef, Scratch, VecZnxAlloc, VecZnxDft, VecZnxDftAlloc, VecZnxDftToMut, VecZnxDftToRef,
4 VecZnxOps, ZnxInfos, ZnxZero,
5};
6use sampling::source::Source;
7
8use crate::{
9 elem::{GetRow, Infos, SetRow},
10 glwe_ciphertext::GLWECiphertext,
11 glwe_ciphertext_fourier::GLWECiphertextFourier,
12 glwe_plaintext::GLWEPlaintext,
13 keys::SecretKeyFourier,
14 utils::derive_size,
15};
16
17pub struct GGLWECiphertext<C, B: Backend> {
18 pub(crate) data: MatZnxDft<C, B>,
19 pub(crate) basek: usize,
20 pub(crate) k: usize,
21}
22
23impl<B: Backend> GGLWECiphertext<Vec<u8>, B> {
24 pub fn new(module: &Module<B>, basek: usize, k: usize, rows: usize, rank_in: usize, rank_out: usize) -> Self {
25 Self {
26 data: module.new_mat_znx_dft(rows, rank_in, rank_out + 1, derive_size(basek, k)),
27 basek: basek,
28 k,
29 }
30 }
31}
32
33impl<T, B: Backend> Infos for GGLWECiphertext<T, B> {
34 type Inner = MatZnxDft<T, B>;
35
36 fn inner(&self) -> &Self::Inner {
37 &self.data
38 }
39
40 fn basek(&self) -> usize {
41 self.basek
42 }
43
44 fn k(&self) -> usize {
45 self.k
46 }
47}
48
49impl<T, B: Backend> GGLWECiphertext<T, B> {
50 pub fn rank(&self) -> usize {
51 self.data.cols_out() - 1
52 }
53
54 pub fn rank_in(&self) -> usize {
55 self.data.cols_in()
56 }
57
58 pub fn rank_out(&self) -> usize {
59 self.data.cols_out() - 1
60 }
61}
62
63impl<C, B: Backend> MatZnxDftToMut<B> for GGLWECiphertext<C, B>
64where
65 MatZnxDft<C, B>: MatZnxDftToMut<B>,
66{
67 fn to_mut(&mut self) -> MatZnxDft<&mut [u8], B> {
68 self.data.to_mut()
69 }
70}
71
72impl<C, B: Backend> MatZnxDftToRef<B> for GGLWECiphertext<C, B>
73where
74 MatZnxDft<C, B>: MatZnxDftToRef<B>,
75{
76 fn to_ref(&self) -> MatZnxDft<&[u8], B> {
77 self.data.to_ref()
78 }
79}
80
81impl GGLWECiphertext<Vec<u8>, FFT64> {
82 pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
83 GLWECiphertext::encrypt_sk_scratch_space(module, size)
84 + module.bytes_of_vec_znx(rank + 1, size)
85 + module.bytes_of_vec_znx(1, size)
86 + module.bytes_of_vec_znx_dft(rank + 1, size)
87 }
88
89 pub fn encrypt_pk_scratch_space(_module: &Module<FFT64>, _rank: usize, _pk_size: usize) -> usize {
90 unimplemented!()
91 }
92}
93
94impl<DataSelf> GGLWECiphertext<DataSelf, FFT64>
95where
96 MatZnxDft<DataSelf, FFT64>: MatZnxDftToMut<FFT64> + ZnxInfos,
97{
98 pub fn encrypt_sk<DataPt, DataSk>(
99 &mut self,
100 module: &Module<FFT64>,
101 pt: &ScalarZnx<DataPt>,
102 sk_dft: &SecretKeyFourier<DataSk, FFT64>,
103 source_xa: &mut Source,
104 source_xe: &mut Source,
105 sigma: f64,
106 scratch: &mut Scratch,
107 ) where
108 ScalarZnx<DataPt>: ScalarZnxToRef,
109 ScalarZnxDft<DataSk, FFT64>: ScalarZnxDftToRef<FFT64>,
110 {
111 #[cfg(debug_assertions)]
112 {
113 assert_eq!(self.rank_in(), pt.cols());
114 assert_eq!(self.rank_out(), sk_dft.rank());
115 assert_eq!(self.n(), module.n());
116 assert_eq!(sk_dft.n(), module.n());
117 assert_eq!(pt.n(), module.n());
118 }
119
120 let rows: usize = self.rows();
121 let size: usize = self.size();
122 let basek: usize = self.basek();
123 let k: usize = self.k();
124
125 let cols_in: usize = self.rank_in();
126 let cols_out: usize = self.rank_out() + 1;
127
128 let (tmp_znx_pt, scrach_1) = scratch.tmp_vec_znx(module, 1, size);
129 let (tmp_znx_ct, scrach_2) = scrach_1.tmp_vec_znx(module, cols_out, size);
130 let (tmp_znx_dft_ct, scratch_3) = scrach_2.tmp_vec_znx_dft(module, cols_out, size);
131
132 let mut vec_znx_pt: GLWEPlaintext<&mut [u8]> = GLWEPlaintext {
133 data: tmp_znx_pt,
134 basek,
135 k,
136 };
137
138 let mut vec_znx_ct: GLWECiphertext<&mut [u8]> = GLWECiphertext {
139 data: tmp_znx_ct,
140 basek,
141 k,
142 };
143
144 let mut vec_znx_ct_dft: GLWECiphertextFourier<&mut [u8], FFT64> = GLWECiphertextFourier {
145 data: tmp_znx_dft_ct,
146 basek,
147 k,
148 };
149
150 // For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns
151 //
152 // Example for ksk rank 2 to rank 3:
153 //
154 // (-(a0*s0 + a1*s1 + a2*s2) + s0', a0, a1, a2)
155 // (-(b0*s0 + b1*s1 + b2*s2) + s0', b0, b1, b2)
156 //
157 // Example ksk rank 2 to rank 1
158 //
159 // (-(a*s) + s0, a)
160 // (-(b*s) + s1, b)
161 (0..cols_in).for_each(|col_i| {
162 (0..rows).for_each(|row_i| {
163 // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
164 vec_znx_pt.data.zero(); // zeroes for next iteration
165 module.vec_znx_add_scalar_inplace(&mut vec_znx_pt, 0, row_i, pt, col_i); // Selects the i-th
166 module.vec_znx_normalize_inplace(basek, &mut vec_znx_pt, 0, scratch_3);
167
168 // rlwe encrypt of vec_znx_pt into vec_znx_ct
169 vec_znx_ct.encrypt_sk(
170 module,
171 &vec_znx_pt,
172 sk_dft,
173 source_xa,
174 source_xe,
175 sigma,
176 scratch_3,
177 );
178
179 // Switch vec_znx_ct into DFT domain
180 vec_znx_ct.dft(module, &mut vec_znx_ct_dft);
181
182 // Stores vec_znx_dft_ct into thw i-th row of the MatZnxDft
183 module.vmp_prepare_row(self, row_i, col_i, &vec_znx_ct_dft);
184 });
185 });
186 }
187}
188
189impl<C> GetRow<FFT64> for GGLWECiphertext<C, FFT64>
190where
191 MatZnxDft<C, FFT64>: MatZnxDftToRef<FFT64>,
192{
193 fn get_row<R>(&self, module: &Module<FFT64>, row_i: usize, col_j: usize, res: &mut GLWECiphertextFourier<R, FFT64>)
194 where
195 VecZnxDft<R, FFT64>: VecZnxDftToMut<FFT64>,
196 {
197 module.vmp_extract_row(res, self, row_i, col_j);
198 }
199}
200
201impl<C> SetRow<FFT64> for GGLWECiphertext<C, FFT64>
202where
203 MatZnxDft<C, FFT64>: MatZnxDftToMut<FFT64>,
204{
205 fn set_row<R>(&mut self, module: &Module<FFT64>, row_i: usize, col_j: usize, a: &GLWECiphertextFourier<R, FFT64>)
206 where
207 VecZnxDft<R, FFT64>: VecZnxDftToRef<FFT64>,
208 {
209 module.vmp_prepare_row(self, row_i, col_j, a);
210 }
211}