Repository navigation
Expand file tree
/
Copy pathuser.cpp
More file actions
329 lines (253 loc) · 8.72 KB
/
Copy pathuser.cpp
File metadata and controls
329 lines (253 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include<cstdint>
#include<cmath>
#include<random>
#include<algorithm>
#include<memory>
#include<thread>
#include<nfl.hpp>
#include "user.hpp"
#include "params.hpp"
/* --------------------------------------------------------------------------------------------------- */
/* Const Values -------------------------------------------------------------------------------------- */
/* Const Values End ---------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
/* Static Functions ---------------------------------------------------------------------------------- */
/* Static Functions End ------------------------------------------------------------------------------ */
/* --------------------------------------------------------------------------------------------------- */
/*
* Private fields
*/
struct Ibe::User::encapsulation {
//
const uint32_t dimension; // n
const uint32_t param; // k
const uint64_t modulus; // q
uint32_t m; // m
// ID
NFL_POLY_COEF_TYPE id;
Poly_t h_1{0};
Poly_t a_id[MAX_Q_BITS];
// encryption status
mutable Poly_t encryption_a_id[MAX_Q_BITS];
// trusted party & public/private key
Poly_t * mpkA;
Poly_t mpkU{0};
Poly_t sk[MAX_Q_BITS];
TrustedParty * trustedParty;
// Related to Trapdoor
Poly_t gs[MAX_Q_BITS];
// Gaussian errors
std::unique_ptr<Gauss_t> gaussianNoiseE0;
std::unique_ptr<Gauss_t> gaussianNoiseE1;
std::unique_ptr<Gauss_t> gaussianNoiseE2;
std::unique_ptr<fastGauss_t> fastGaussNoiseE0;
std::unique_ptr<fastGauss_t> fastGaussNoiseE1;
std::unique_ptr<fastGauss_t> fastGaussNoiseE2;
//
std::independent_bits_engine<std::mt19937_64, NFL_POLY_Q_BITS, NFL_POLY_COEF_TYPE> randomGen;
// Functions
void hash(Poly_t * output, const NFL_POLY_COEF_TYPE x);
void compute_a_id(Poly_t * output, const Poly_t & h);
NFL_POLY_COEF_TYPE inverse(const NFL_POLY_COEF_TYPE a);
void inverse_poly(Poly_t * output, Poly_t & input);
};
void Ibe::User::encapsulation::hash(Poly_t * output, const NFL_POLY_COEF_TYPE x) {
randomGen.seed(x);
for(auto & output_i : output->poly_obj()) {
output_i = randomGen();
}
}
void Ibe::User::encapsulation::compute_a_id(Poly_t * output, const Poly_t & h) {
// output[0] = mpkA[0]; output[1] = mpkA[1];
memcpy(output, mpkA, sizeof(Poly_t)*2);
// Pool of threads
std::thread pool[THREADS_NUMBER];
// Thread shares
uint32_t thread_shares[THREADS_NUMBER];
const uint32_t thread_share = param/THREADS_NUMBER;
std::fill_n(thread_shares, THREADS_NUMBER, thread_share);
for(uint32_t i = 0; i < param - thread_share*THREADS_NUMBER; ++i) {
++thread_shares[i];
}
// Lambda for a_id
auto lambda = [&h](Poly_t * outputs, const Poly_t * gs, const Poly_t * a, const uint32_t length) {
for(uint32_t i = 0; i < length; ++i) {
*outputs = h*(*gs) + (*a);
++outputs;
++gs;
++a;
}
};
// compute a_id
const Poly_t * gsIndex = gs;
const Poly_t * AIndex = (mpkA + 2);
for(uint32_t i = 0; i < THREADS_NUMBER; ++i) {
const uint32_t share = thread_shares[i];
pool[i] = std::thread(lambda, output, gsIndex, AIndex, share);
output += share;
AIndex += share;
gsIndex += share;
}
for(uint32_t i = 0; i < THREADS_NUMBER; ++i) {
pool[i].join();
}
}
NFL_POLY_COEF_TYPE Ibe::User::encapsulation::inverse(const NFL_POLY_COEF_TYPE a) {
NFL_POLY_COEF_TYPE u0 = 1, v0 = 0;
NFL_POLY_COEF_TYPE u1 = 0, v1 = 1;
NFL_POLY_COEF_TYPE res0 = a, res1 = modulus, quo = 0;
NFL_POLY_COEF_TYPE tmp;
while (res1 != 0) {
quo = res0 / res1;
tmp = res0, res0 = res1, res1 = tmp % res1;
tmp = u0, u0 = u1, u1 = tmp - quo*u1;
tmp = v0, v0 = v1, v1 = tmp - quo*u1;
}
return (u0 > 0) ? u0 : u0 + modulus;
}
void Ibe::User::encapsulation::inverse_poly(Poly_t * output, Poly_t & input) {
uint32_t i = 0;
NFL_POLY_COEF_TYPE invs[dimension];
for(auto & input_i : input.poly_obj()) {
invs[i++] = inverse(input_i);
}
output->set(invs, invs + dimension, false);
}
/* --------------------------------------------------------------------------------------------------- */
/* Public Interface ---------------------------------------------------------------------------------- */
/* @Override */
Ibe::User::User(const uint32_t n, const uint32_t k, const uint64_t q, const double_t tau, const double_t gamma, const uint32_t lambda, \
const NFL_POLY_COEF_TYPE id, TrustedParty * trustedParty) noexcept : \
impl(new encapsulation {.dimension = n, .param = k, .modulus = q}) {
// initialize global parameters
impl->id = id;
impl->m = k + 2;
// initialize trusted party & public keys
impl->mpkA = trustedParty->getPublicKey(&impl->mpkU);
impl->trustedParty = trustedParty;
// prebuild the Gaussian noises
impl->fastGaussNoiseE0.reset(new fastGauss_t(tau, lambda, n));
impl->gaussianNoiseE0.reset(new Gauss_t(impl->fastGaussNoiseE0.get()));
impl->fastGaussNoiseE1.reset(new fastGauss_t(gamma, lambda, n));
impl->gaussianNoiseE1.reset(new Gauss_t(impl->fastGaussNoiseE1.get()));
impl->fastGaussNoiseE2.reset(new fastGauss_t(tau, lambda, n));
impl->gaussianNoiseE2.reset(new Gauss_t(impl->fastGaussNoiseE2.get()));
// build gs and NTT
NFL_POLY_COEF_TYPE gi = 1;
for(uint32_t i = 0; i < k; ++i) {
impl->gs[i].set(gi);
impl->gs[i].ntt_pow_phi();
gi <<= 1;
}
// compute h
Poly_t h{0};
impl->hash(&h, id);
// inverse h
impl->inverse_poly(&(impl->h_1), h);
// compute a_id
impl->compute_a_id(impl->a_id, h);
}
/* @Override */
Ibe::User::~User(void) noexcept {
}
/* @override */
void Ibe::User::extractPrivateKey(void) const noexcept {
impl->trustedParty->extract(impl->sk, impl->a_id, impl->h_1);
}
/* @override */
void Ibe::User::setEncrypt(const NFL_POLY_COEF_TYPE targetId) const noexcept {
// compute h
Poly_t h{0};
impl->hash(&h, targetId);
// compute a_id
impl->compute_a_id(impl->encryption_a_id, h);
}
/* @override */
void Ibe::User::encrypt(Poly_t * output, const Poly_t & msg) const noexcept {
// prepare the plaintext
Poly_t plaintext{msg};
for (auto & plaintext_i : plaintext.poly_obj()) {
plaintext_i *= (impl->modulus/2);
}
plaintext.ntt_pow_phi();
// uniform noise
Poly_t s = nfl::uniform();
s.ntt_pow_phi();
// gaussian noise e0
const Poly_t * A_idIndex = impl->encryption_a_id;
Gauss_t noiseE0 = *(impl->gaussianNoiseE0.get());
for(uint32_t i = 0; i < impl->m - impl->param; ++i) {
output->set(noiseE0);
output->ntt_pow_phi();
*output = *output + s*(*A_idIndex);
++output;
++A_idIndex;
}
// gaussian noise e1
Gauss_t noiseE1 = *(impl->gaussianNoiseE1.get());
for(uint32_t i = 0; i < impl->param; ++i) {
output->set(noiseE1);
output->ntt_pow_phi();
*output = *output + s*(*A_idIndex);
++output;
++A_idIndex;
}
// Pool of threads
/*
std::thread pool[THREADS_NUMBER];
// Thread shares
uint32_t thread_shares[THREADS_NUMBER];
const uint32_t thread_share = impl->param/THREADS_NUMBER;
std::fill_n(thread_shares, THREADS_NUMBER, thread_share);
for(uint32_t i = 0; i < impl->param - thread_share*THREADS_NUMBER; ++i) {
++thread_shares[i];
}
// Lambda for gaussian noise e1
auto lambda = [this,&s](Poly_t * output, const Poly_t * a_id, const uint32_t length) {
Gauss_t noiseE1 = *(impl->gaussianNoiseE1.get());
for(uint32_t i = 0; i < length; ++i) {
output->set(noiseE1);
output->ntt_pow_phi();
*output = *output + s*(*a_id);
++output;
++a_id;
}
};
// gaussian noise e1
for(uint32_t i = 0; i < THREADS_NUMBER; ++i) {
pool[i] = std::thread(lambda, output, A_idIndex, thread_shares[i]);
output += thread_shares[i];
A_idIndex += thread_shares[i];
}
*/
// final
Gauss_t noiseE2 = *(impl->gaussianNoiseE2.get());
output->set(noiseE2);
output->ntt_pow_phi();
*output = *output + impl->mpkU*s + plaintext;
/*
for(uint32_t i = 0; i < THREADS_NUMBER; ++i) {
pool[i].join();
}
*/
}
/* @override */
void Ibe::User::decrypt(Poly_t * output, const Poly_t * cipher) const noexcept {
Poly_t bx{0};
const Poly_t * sk_id = impl->sk;
for(uint32_t i = 0; i < impl->m; ++i) {
bx = bx + (*sk_id)*(*cipher);
++sk_id;
++cipher;
}
*output = *cipher - bx;
output->invntt_pow_invphi();
const uint32_t modulus_4 = impl->modulus >> 2;
const uint32_t modulus_34 = 3*modulus_4;
for(auto & output_i : output->poly_obj()) {
output_i = ((output_i >= modulus_4) && output_i <= modulus_34) ? 1 : 0;
}
}
/* Public Interface End ------------------------------------------------------------------------------ */
/* --------------------------------------------------------------------------------------------------- */