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