forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FHE_Keys.cpp
416 lines (330 loc) · 10.2 KB
/
FHE_Keys.cpp
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "FHE_Keys.h"
#include "Ciphertext.h"
#include "P2Data.h"
#include "PPData.h"
#include "FFT_Data.h"
FHE_SK::FHE_SK(const FHE_PK& pk) : FHE_SK(pk.get_params(), pk.p())
{
}
void add(FHE_SK& a,const FHE_SK& b,const FHE_SK& c)
{
if (a.params!=b.params) { throw params_mismatch(); }
if (a.params!=c.params) { throw params_mismatch(); }
add(a.sk,b.sk,c.sk);
}
void KeyGen(FHE_PK& PK,FHE_SK& SK,PRNG& G)
{
if (PK.params!=SK.params) { throw params_mismatch(); }
if (PK.pr!=SK.pr) { throw pr_mismatch(); }
Rq_Element sk = PK.sample_secret_key(G);
SK.assign(sk);
PK.KeyGen(sk, G);
}
Rq_Element FHE_PK::sample_secret_key(PRNG& G)
{
Rq_Element sk = FHE_SK(*this).s();
// Generate the secret key
sk.from_vec((*params).sampleHwt(G));
return sk;
}
void FHE_PK::KeyGen(Rq_Element& sk, PRNG& G, int noise_boost)
{
FHE_PK& PK = *this;
// Generate the main public key
PK.a0.randomize(G);
// b0=a0*s+p*e0
Rq_Element e0((*PK.params).FFTD(),evaluation,evaluation);
e0.from_vec((*PK.params).sampleGaussian(G, noise_boost));
mul(PK.b0,PK.a0,sk);
mul(e0,e0,PK.pr);
add(PK.b0,PK.b0,e0);
#ifdef CHECK_NOISE
// strict check not working for GF(2^n)
PK.check_noise(PK.b0 - PK.a0 * sk, false);
#endif
if (params->n_mults() > 0)
{
// Generating the switching key data
PK.Sw_a.randomize(G);
// bs=as*s+p*es
Rq_Element es((*PK.params).FFTD(),evaluation,evaluation);
es.from_vec((*PK.params).sampleGaussian(G, noise_boost));
mul(PK.Sw_b,PK.Sw_a,sk);
mul(es,es,PK.pr);
add(PK.Sw_b,PK.Sw_b,es);
// Lowering level as we only decrypt at level 0
sk.lower_level();
// bs=bs-p1*s^2
Rq_Element s2;
mul(s2,sk,sk); // Mult at level 0
s2.mul_by_p1(); // This raises back to level 1
sub(PK.Sw_b,PK.Sw_b,s2);
}
}
void FHE_PK::check_noise(const FHE_SK& SK)
{
Rq_Element sk = SK.s();
if (params->n_mults() > 0)
sk.mul_by_p1();
check_noise(b0 - a0 * sk);
}
void FHE_PK::check_noise(const Rq_Element& x, bool check_modulo)
{
assert(pr != 0);
vector<bigint> noise = x.to_vec_bigint();
bigint m = 0;
if (check_modulo)
cout << "checking multiplicity of noise" << endl;
for (size_t i = 0; i < noise.size(); i++)
{
// cout << "noise mod pr: " << noise[i] << " pr: " << pr << " " << noise[i] % pr << "\n";
if (check_modulo and noise[i] % pr != 0)
{
cout << i << " " << noise[i] % pr << endl;
throw runtime_error("invalid public key");
}
noise[i] /= pr;
m = m > noise[i] ? m : noise[i];
}
cerr << "max noise: " << m << endl;
}
template<>
void FHE_PK::encrypt(Ciphertext& c,
const Plaintext<gfp,FFT_Data,bigint>& mess,const Random_Coins& rc) const
{
if (&c.get_params()!=params) { throw params_mismatch(); }
if (&rc.get_params()!=params) { throw params_mismatch(); }
if (pr==2) { throw pr_mismatch(); }
Rq_Element mm((*params).FFTD(),polynomial,polynomial);
mm.from(mess.get_iterator());
quasi_encrypt(c,mm,rc);
}
template<>
void FHE_PK::encrypt(Ciphertext& c,
const Plaintext<gfp,PPData,bigint>& mess,const Random_Coins& rc) const
{
if (&c.get_params()!=params) { throw params_mismatch(); }
if (&rc.get_params()!=params) { throw params_mismatch(); }
if (pr==2) { throw pr_mismatch(); }
mess.to_poly();
encrypt(c, mess.get_poly(), rc);
}
template<>
void FHE_PK::encrypt(Ciphertext& c,
const Plaintext<gf2n_short,P2Data,int>& mess,const Random_Coins& rc) const
{
if (&c.get_params()!=params) { throw params_mismatch(); }
if (&rc.get_params()!=params) { throw params_mismatch(); }
if (pr!=2) { throw pr_mismatch(); }
mess.to_poly();
encrypt(c, mess.get_poly(), rc);
}
template <class S>
void FHE_PK::encrypt(Ciphertext& c, const vector<S>& mess,
const Random_Coins& rc) const
{
Rq_Element mm((*params).FFTD(),polynomial,polynomial);
mm.from(Iterator<S>(mess));
quasi_encrypt(c, mm, rc);
}
void FHE_PK::quasi_encrypt(Ciphertext& c,
const Rq_Element& mess,const Random_Coins& rc) const
{
if (&c.get_params()!=params) { throw params_mismatch(); }
if (&rc.get_params()!=params) { throw params_mismatch(); }
Rq_Element ed,edd,c0,c1,aa;
// c1=a0*u+p*v
mul(aa,a0,rc.u());
mul(ed,rc.v(),pr);
add(c1,aa,ed);
// c0 = b0 * u + p * w + mess
mul(c0,b0,rc.u());
mul(edd,rc.w(),pr);
add(edd,edd,mess);
if (params->n_mults() == 0)
edd.change_rep(evaluation);
else
edd.change_rep(evaluation, evaluation);
add(c0,c0,edd);
c.set(c0,c1,*this);
}
template<class FD>
Ciphertext FHE_PK::encrypt(const Plaintext<typename FD::T, FD, typename FD::S>& mess,
const Random_Coins& rc) const
{
Ciphertext res(*params);
encrypt(res, mess, rc);
return res;
}
template<class FD>
Ciphertext FHE_PK::encrypt(
const Plaintext<typename FD::T, FD, typename FD::S>& mess) const
{
Random_Coins rc(*params);
PRNG G;
G.ReSeed();
rc.generate(G);
return encrypt(mess, rc);
}
template<>
void FHE_SK::decrypt(Plaintext<gfp,FFT_Data,bigint>& mess,const Ciphertext& c) const
{
if (&c.get_params()!=params) { throw params_mismatch(); }
if (pr==2) { throw pr_mismatch(); }
Rq_Element ans;
mul(ans,c.c1(),sk);
sub(ans,c.c0(),ans);
ans.change_rep(polynomial);
mess.set_poly_mod(ans.get_iterator(), ans.get_modulus());
}
template<>
void FHE_SK::decrypt(Plaintext<gfp,PPData,bigint>& mess,const Ciphertext& c) const
{
if (&c.get_params()!=params) { throw params_mismatch(); }
if (pr==2) { throw pr_mismatch(); }
Rq_Element ans;
mul(ans,c.c1(),sk);
sub(ans,c.c0(),ans);
mess.set_poly_mod(ans.to_vec_bigint(),ans.get_modulus());
}
template<>
void FHE_SK::decrypt(Plaintext<gf2n_short,P2Data,int>& mess,const Ciphertext& c) const
{
if (&c.get_params()!=params) { throw params_mismatch(); }
if (pr!=2) { throw pr_mismatch(); }
Rq_Element ans;
mul(ans,c.c1(),sk);
sub(ans,c.c0(),ans);
ans.change_rep(polynomial);
mess.set_poly_mod(ans.get_iterator(), ans.get_modulus());
}
template<class FD>
Plaintext<typename FD::T, FD, typename FD::S> FHE_SK::decrypt(const Ciphertext& c, const FD& FieldD)
{
Plaintext<typename FD::T, FD, typename FD::S> res(FieldD);
decrypt_any(res, c);
return res;
}
template <class FD>
void FHE_SK::decrypt_any(Plaintext_<FD>& res, const Ciphertext& c)
{
if (sk.level())
sk.lower_level();
if (c.level())
{
Ciphertext cc = c;
cc.Scale(res.get_field().get_prime());
decrypt(res, cc);
}
else
decrypt(res, c);
}
/* Distributed Decryption Stuff */
void FHE_SK::dist_decrypt_1(vector<bigint>& vv,const Ciphertext& ctx,int player_number,int num_players) const
{
// Need Ciphertext to be at level 0, so we force this here
Ciphertext cc=ctx; cc.Scale(pr);
// First do the basic decryption
Rq_Element dec_sh;
mul(dec_sh,cc.c1(),sk);
if (player_number==0)
{ sub(dec_sh,cc.c0(),dec_sh); }
else
{ dec_sh.negate(); }
// Now convert to a vector of bigint's and add the required randomness
assert(pr != 0);
bigint Bd=((*params).B()<<(*params).secp())/(num_players*pr);
Bd=Bd/2; // make slightly smaller due to rounding issues
dec_sh.to_vec_bigint(vv);
if ((int)vv.size() != params->phi_m())
throw length_error("wrong length of ring element");
bigint mod=(*params).p0();
PRNG G; G.ReSeed();
bigint mask;
bigint two_Bd = 2 * Bd;
for (int i=0; i<(*params).phi_m(); i++)
{
G.randomBnd(mask, two_Bd);
mask -= Bd;
mask *= pr;
vv[i] += mask;
vv[i] %= mod;
if (vv[i]<0) { vv[i]+=mod; }
}
}
void FHE_SK::dist_decrypt_2(vector<bigint>& vv,const vector<bigint>& vv1) const
{
bigint mod=(*params).p0();
for (int i=0; i<(*params).phi_m(); i++)
{
vv[i] += vv1[i];
vv[i] %= mod;
}
}
void FHE_PK::pack(octetStream& o) const
{
o.append((octet*) "PKPKPKPK", 8);
a0.pack(o);
b0.pack(o);
Sw_a.pack(o);
Sw_b.pack(o);
pr.pack(o);
}
void FHE_PK::unpack(octetStream& o)
{
char tag[8];
o.consume((octet*) tag, 8);
if (memcmp(tag, "PKPKPKPK", 8))
throw runtime_error("invalid serialization of public key");
a0.unpack(o);
b0.unpack(o);
Sw_a.unpack(o);
Sw_b.unpack(o);
pr.unpack(o);
}
bool FHE_PK::operator!=(const FHE_PK& x) const
{
if ((*params) != *(x.params) or pr != x.pr or a0 != x.a0 or b0 != x.b0
or Sw_a != x.Sw_a or Sw_b != x.Sw_b)
{
throw runtime_error("pk");
return true;
}
else
return false;
}
void FHE_SK::check(const FHE_Params& params, const FHE_PK& pk,
const bigint& pr) const
{
if (this->params != ¶ms)
throw params_mismatch();
if (this->pr != pr)
throw pr_mismatch();
pk.check(params, pr);
sk.check(params);
}
void FHE_PK::check(const FHE_Params& params, const bigint& pr) const
{
if (this->pr != pr)
throw pr_mismatch();
a0.check(params);
b0.check(params);
Sw_a.check(params);
Sw_b.check(params);
}
template Ciphertext FHE_PK::encrypt(const Plaintext_<FFT_Data>& mess,
const Random_Coins& rc) const;
template Ciphertext FHE_PK::encrypt(const Plaintext_<FFT_Data>& mess) const;
template Ciphertext FHE_PK::encrypt(const Plaintext_<P2Data>& mess) const;
template void FHE_PK::encrypt(Ciphertext& c, const vector<int>& mess,
const Random_Coins& rc) const;
template void FHE_PK::encrypt(Ciphertext& c, const vector<fixint<GFP_MOD_SZ>>& mess,
const Random_Coins& rc) const;
template Plaintext_<FFT_Data> FHE_SK::decrypt(const Ciphertext& c,
const FFT_Data& FieldD);
template Plaintext_<P2Data> FHE_SK::decrypt(const Ciphertext& c,
const P2Data& FieldD);
template void FHE_SK::decrypt_any(Plaintext_<FFT_Data>& res,
const Ciphertext& c);
template void FHE_SK::decrypt_any(Plaintext_<P2Data>& res,
const Ciphertext& c);