forked from jamespayor/vector-homomorphic-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvhe.cpp
519 lines (402 loc) · 11.6 KB
/
vhe.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include <iostream>
#include <fstream>
#include <sstream>
#include <cassert>
//#include <NTL/mat_ZZ_p.h>
#include <NTL/mat_ZZ.h>
//#include <NTL/ZZ_p.h>
#include <NTL/ZZ.h>
//#include <NTL/vec_ZZ_p.h>
#include <NTL/vec_ZZ.h>
#include <cmath>
#include <stack>
using namespace std;
using namespace NTL;
const ZZ w(1ll << 40ll);
ZZ aBound(1000), tBound(aBound), eBound(1000);
int l = 100;
const mat_ZZ hCat(const mat_ZZ& A, const mat_ZZ& B);
const mat_ZZ vCat(const mat_ZZ& A, const mat_ZZ& B);
const vec_ZZ decrypt(const mat_ZZ& S, const vec_ZZ& c);
// returns c*
const vec_ZZ getBitVector(const vec_ZZ& c);
// returns S*
const mat_ZZ getBitMatrix(const mat_ZZ& S);
// returns S
const mat_ZZ getSecretKey(const mat_ZZ& T);
// returns M
const mat_ZZ keySwitchMatrix(const mat_ZZ& S, const mat_ZZ& T);
// finds c* then returns Mc*
const vec_ZZ keySwitch(const mat_ZZ& M, const vec_ZZ& c);
// as described, treating I as the secret key and wx as ciphertext
const vec_ZZ encrypt(const mat_ZZ& T, const vec_ZZ& x);
const mat_ZZ getRandomMatrix(long row, long col, const ZZ& bound);
// server side addition with same secret key
const vec_ZZ addn(const vec_ZZ& c1, const vec_ZZ& c2);
// server side linear transformation,
// returns S(Gx) given c=Sx and M (key switch matrix from GS to S)
const vec_ZZ linearTransform(const mat_ZZ& M, const vec_ZZ& c);
// returns M, the key switch matrix from GS to S,
// to be sent to server
const mat_ZZ linearTransformClient(const mat_ZZ& T, const mat_ZZ& G);
// computes an inner product, given two ciphertexts and the keyswitch matrix
const vec_ZZ innerProd(const vec_ZZ& c1, const vec_ZZ& c2, const mat_ZZ& M);
// returns M, the key switch matrix from vec(S^t S) to S,
// to be sent to the server
const mat_ZZ innerProdClient(const mat_ZZ& T);
// returns a column vector
const mat_ZZ vectorize(const mat_ZZ& M);
const mat_ZZ copyRows(const mat_ZZ& row, long numrows);
// finds c* then returns Mc*
const vec_ZZ keySwitch(const mat_ZZ& M, const vec_ZZ& c) {
vec_ZZ cstar = getBitVector(c);
return M * cstar;
}
const mat_ZZ getRandomMatrix(long row, long col, const ZZ& bound){
mat_ZZ A;
A.SetDims(row, col);
for (int i=0; i<row; ++i){
for (int j=0; j<col; ++j){
A[i][j] = RandomBnd(bound);
}
}
return A;
}
// returns S*
const mat_ZZ getBitMatrix(const mat_ZZ& S) {
mat_ZZ result;
int rows = S.NumRows(), cols = S.NumCols();
result.SetDims(rows, l * cols);
vec_ZZ powers;
powers.SetLength(l);
powers[0] = 1;
for(int i = 0; i < l - 1; ++i) {
powers[i+1] = powers[i]*2;
}
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
for(int k = 0; k < l; ++k) {
result[i][j*l + k] = S[i][j] * powers[k];
}
}
}
return result;
}
// returns c*
const vec_ZZ getBitVector(const vec_ZZ& c) {
vec_ZZ result;
int length = c.length();
result.SetLength(length * l);
for(int i = 0; i < length; ++i) {
ZZ sign = (c[i] < ZZ(0)) ? ZZ(-1) : ZZ(1);
ZZ value = c[i] * sign;
for(int j = 0; j < l; ++j) {
result[i * l + j] = sign*bit(value, j);
}
}
return result;
}
// returns S
const mat_ZZ getSecretKey(const mat_ZZ& T) {
mat_ZZ I;
ident(I, T.NumRows());
return hCat(I, T);
}
const mat_ZZ hCat(const mat_ZZ& A, const mat_ZZ& B) {
assert(A.NumRows() == B.NumRows());
int rows = A.NumRows(), colsA = A.NumCols(), colsB = B.NumCols();
mat_ZZ result;
result.SetDims(rows, colsA + colsB);
// Copy A
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < colsA; ++j) {
result[i][j] = A[i][j];
}
}
// Copy B
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < colsB; ++j) {
result[i][colsA + j] = B[i][j];
}
}
return result;
}
const mat_ZZ vCat(const mat_ZZ& A, const mat_ZZ& B) {
assert(A.NumCols() == B.NumCols());
int cols = A.NumCols(), rowsA = A.NumRows(), rowsB = B.NumRows();
mat_ZZ result;
result.SetDims(rowsA + rowsB, cols);
// Copy A
for(int i = 0; i < rowsA; ++i) {
for(int j = 0; j < cols; ++j) {
result[i][j] = A[i][j];
}
}
// Copy B
for(int i = 0; i < rowsB; ++i) {
for(int j = 0; j < cols; ++j) {
result[i + rowsA][j] = B[i][j];
}
}
return result;
}
inline const ZZ nearestInteger(const ZZ& x, const ZZ& w) {
return (x + (w+1)/2) / w;
}
const vec_ZZ decrypt(const mat_ZZ& S, const vec_ZZ& c) {
vec_ZZ Sc = S*c;
vec_ZZ output;
output.SetLength(Sc.length());
for (int i=0; i<Sc.length(); i++) {
output[i] = nearestInteger(Sc[i], w);
}
return output;
}
const mat_ZZ keySwitchMatrix(const mat_ZZ& S, const mat_ZZ& T) {
mat_ZZ Sstar = getBitMatrix(S);
mat_ZZ A = getRandomMatrix(T.NumCols(),Sstar.NumCols(),aBound);
mat_ZZ E = getRandomMatrix(Sstar.NumRows(),Sstar.NumCols(),eBound);
return vCat(Sstar + E - T*A, A);
}
const vec_ZZ encrypt(const mat_ZZ& T, const vec_ZZ& x) {
mat_ZZ I;
ident(I, x.length());
return keySwitch(keySwitchMatrix(I, T), w * x);
}
const vec_ZZ addVectors(const vec_ZZ& c1, const vec_ZZ& c2){
return c1 + c2;
}
const vec_ZZ linearTransform(const mat_ZZ& M, const vec_ZZ& c){
return M * getBitVector(c);
}
const mat_ZZ linearTransformClient(const mat_ZZ& G, const mat_ZZ& S, const mat_ZZ& T){
return keySwitchMatrix(G * S, T);
}
const vec_ZZ innerProd(const vec_ZZ& c1, const vec_ZZ& c2, const mat_ZZ& M){
mat_ZZ cc1;
mat_ZZ cc2;
mat_ZZ cc;
cc1.SetDims(c1.length(), 1);
for (int i=0; i<c1.length(); ++i){
cc1[i][0] = c1[i];
}
cc2.SetDims(1, c2.length());
for (int i=0; i<c2.length(); ++i){
cc2[0][i] = c2[i];
}
cc = vectorize(cc1 * cc2);
vec_ZZ output;
output.SetLength(cc.NumRows());
for (int i=0; i<cc.NumRows(); i++) {
output[i] = nearestInteger(cc[i][0], w);
}
return M * getBitVector(output);
}
const mat_ZZ innerProdClient(const mat_ZZ& T){
mat_ZZ S = getSecretKey(T);
mat_ZZ tvsts = transpose(vectorize(transpose(S) * S));
mat_ZZ mvsts = copyRows(tvsts, T.NumRows());
return keySwitchMatrix(mvsts, T);
}
const mat_ZZ copyRows(const mat_ZZ& row, long numrows){
mat_ZZ ans;
ans.SetDims(numrows, row.NumCols());
for (int i=0; i<ans.NumRows(); ++i){
for (int j=0; j<ans.NumCols(); ++j){
ans[i][j] = row[0][j];
}
}
return ans;
}
const mat_ZZ vectorize(const mat_ZZ& M){
mat_ZZ ans;
ans.SetDims(M.NumRows() * M.NumCols(), 1);
for (int i=0; i<M.NumRows(); ++i){
for (int j=0; j<M.NumCols(); ++j){
ans[i*M.NumCols() + j][0] = M[i][j];
}
}
return ans;
}
int main() {
ifstream cin("vhe.in");
cin.tie(NULL);
ios_base::sync_with_stdio(false);
stack<vec_ZZ*> vectors;
stack<mat_ZZ*> matrices;
string operation;
while (cin >> operation) {
//cerr << "Operation: " << operation << endl;
if (operation == "vector") {
vec_ZZ* v = new vec_ZZ();
cin >> (*v);
//cerr << "Vector (" << v.length() << ")" << endl;
vectors.push(v);
} else if (operation == "matrix") {
mat_ZZ* m = new mat_ZZ();
cin >> (*m);
//cerr << "Matrix (" << m.NumRows() << ", " << m.NumCols() << ")" << endl;
matrices.push(m);
} else if (operation == "duplicate-vector") {
vectors.push(vectors.top());
} else if (operation == "duplicate-matrix") {
matrices.push(matrices.top());
} else if (operation == "add") {
vec_ZZ& v1 = *vectors.top(); vectors.pop();
vec_ZZ& v2 = *vectors.top(); vectors.pop();
vec_ZZ* v = new vec_ZZ();
(*v) = addVectors(v1, v2);
vectors.push(v);
} else if (operation == "scalar-multiply") {
ZZ x;
cin >> x;
vec_ZZ& v = *vectors.top(); vectors.pop();
vec_ZZ* v2 = new vec_ZZ();
(*v2) = v * x;
vectors.push(v2);
} else if (operation == "linear-transform") {
vec_ZZ& v = *vectors.top(); vectors.pop();
mat_ZZ& m = *matrices.top(); matrices.pop();
vec_ZZ* r = new vec_ZZ();
(*r) = linearTransform(m, v);
vectors.push(r);
} else if (operation == "linear-transform-key-switch") {
mat_ZZ& T = *matrices.top(); matrices.pop();
mat_ZZ& S = *matrices.top(); matrices.pop();
mat_ZZ& G = *matrices.top(); matrices.pop();
mat_ZZ* m = new mat_ZZ();
(*m) = linearTransformClient(G, S, T);
matrices.push(m);
} else if (operation == "inner-product") {
vec_ZZ& v1 = *vectors.top(); vectors.pop();
vec_ZZ& v2 = *vectors.top(); vectors.pop();
mat_ZZ& m = *matrices.top(); matrices.pop();
vec_ZZ *v = new vec_ZZ();
(*v) = innerProd(v1, v2, m);
vectors.push(v);
} else if (operation == "inner-product-key-switch") {
mat_ZZ& T = *matrices.top(); matrices.pop();
mat_ZZ *m = new mat_ZZ();
(*m) = innerProdClient(T);
matrices.push(m);
} else if (operation == "key-switch") {
vec_ZZ& v = *vectors.top(); vectors.pop();
mat_ZZ& m = *matrices.top(); matrices.pop();
vec_ZZ *v2 = new vec_ZZ();
(*v2) = keySwitch(m, v);
vectors.push(v2);
} else if (operation == "random-matrix") {
int rows, cols;
cin >> rows >> cols;
mat_ZZ *m = new mat_ZZ();
(*m) = getRandomMatrix(rows, cols, tBound);
matrices.push(m);
} else if (operation == "identity") {
int rows;
cin >> rows;
mat_ZZ *I = new mat_ZZ();
ident(*I, rows);
matrices.push(I);
} else if (operation == "key-switch-matrix") {
mat_ZZ& T = *matrices.top(); matrices.pop();
mat_ZZ& S = *matrices.top(); matrices.pop();
mat_ZZ *m = new mat_ZZ();
(*m) = keySwitchMatrix(S, T);
matrices.push(m);
} else if (operation == "get-secret-key") {
mat_ZZ &T = *matrices.top(); matrices.pop();
mat_ZZ *s = new mat_ZZ();
(*s) = getSecretKey(T);
matrices.push(s);
} else if (operation == "encrypt") {
mat_ZZ& T = *matrices.top(); matrices.pop();
vec_ZZ& x = *vectors.top(); vectors.pop();
vec_ZZ *v = new vec_ZZ();
(*v) = encrypt(T, x);
vectors.push(v);
} else if (operation == "decrypt") {
mat_ZZ& S = *matrices.top(); matrices.pop();
vec_ZZ& c = *vectors.top(); vectors.pop();
vec_ZZ *x = new vec_ZZ();
(*x) = decrypt(S, c);
vectors.push(x);
} else {
cerr << "Unknown command: " << operation << endl;
}
}
stack<vec_ZZ> vectors2;
while (vectors.size()) {
vectors2.push(*vectors.top()); vectors.pop();
}
while (vectors2.size()) {
cout << vectors2.top() << endl; vectors2.pop();
}
stack<mat_ZZ> matrices2;
while (matrices.size()) {
matrices2.push(*matrices.top()); matrices.pop();
}
while (matrices2.size()) {
cout << matrices2.top() << endl; matrices2.pop();
}
// // Testing for the 3 fundamental operations:
// const int N = 10;
// vec_ZZ x1;
// vec_ZZ x2;
// x1.SetLength(N);
// x2.SetLength(N);
// for(int i = 0; i < N; ++i) {
// x1[i] = RandomBnd(10000);
// x2[i] = RandomBnd(10000);
// }
// cout << x1 << endl;
// cout << x2 << endl;
// mat_ZZ T = getRandomMatrix(N, N, tBound);
// mat_ZZ S = getSecretKey(T);
// vec_ZZ c1 = encrypt(T, x1);
// vec_ZZ c2 = encrypt(T, x2);
// // Testing for inner product no switch
// vec_ZZ cc = innerProdNoSwitch(x1, c2);
// vec_ZZ dxx = innerProdNoSwitchDecrypt(cc, S);
// ZZ xx;
// InnerProduct(xx, x1, x2);
// cout << xx << endl;
// cout << dxx[0] << endl;
// cout << xx - dxx[0] << endl;
// // Testing for inner product
// mat_ZZ M;
// vec_ZZ cc;
// vec_ZZ dxx;
// ZZ xx;
// M = innerProdClient(T);
// cc = innerProd(c1, c2, M);
// dxx = decrypt(getSecretKey(T), cc);
// InnerProduct(xx, x1, x2);
// cout << xx << endl;
// cout << dxx[0] << endl;
// cout << xx - dxx[0] << endl;
// // Testing for linear transform
// mat_ZZ G;
// mat_ZZ M;
// vec_ZZ cc;
// vec_ZZ dxx;
// vec_ZZ xx;
// G = getRandomMatrix(N, N, aBound);
// M = linearTransformClient(T, G);
// cc = linearTransform(M, c1);
// dxx = decrypt(getSecretKey(T), cc);
// xx = G * x1;
// cout << G << endl;
// cout << xx << endl;
// cout << dxx << endl;
// cout << xx - dxx << endl;
// // Testing for addition:
// vec_ZZ cplus;
// vec_ZZ dxplus;
// vec_ZZ xplus;
// cplus = addn(c1, c2);
// dxplus = decrypt(getSecretKey(T), cplus);
// xplus = x1 + x2;
// cout << xplus << endl;
// cout << dxplus << endl;
// cout << xplus - dxplus << endl;
}