forked from MarwanNour/SEAL-FYP-Logistic-Regression
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix_multiplication.cpp
537 lines (452 loc) · 16.6 KB
/
matrix_multiplication.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include <iostream>
#include <iomanip>
#include <fstream>
#include <unistd.h>
#include "seal/seal.h"
#include "helper.h"
using namespace std;
using namespace seal;
Ciphertext CC_Matrix_Multiplication(Ciphertext ctA, Ciphertext ctB, int dimension, vector<Plaintext> U_sigma_diagonals, vector<Plaintext> U_tau_diagonals, vector<vector<Plaintext>> V_diagonals, vector<vector<Plaintext>> W_diagonals, GaloisKeys gal_keys, EncryptionParameters params)
{
auto context = SEALContext::Create(params);
Evaluator evaluator(context);
vector<Ciphertext> ctA_result(dimension);
vector<Ciphertext> ctB_result(dimension);
cout << "----------Step 1----------- " << endl;
// Step 1-1
ctA_result[0] = Linear_Transform_Plain(ctA, U_sigma_diagonals, gal_keys, params);
// Step 1-2
ctB_result[0] = Linear_Transform_Plain(ctB, U_tau_diagonals, gal_keys, params);
/*
// Test scale
cout << "\nSCALE TEST -----------:" << endl;
for (int i = 0; i < dimension; i++)
{
cout << "CTA scale at i = " << i << ":\t" << log2(ctA_result[i].scale()) << endl;
cout << "CTB scale at i = " << i << ":\t" << log2(ctB_result[i].scale()) << endl;
}
*/
// Step 2
cout << "----------Step 2----------- " << endl;
for (int k = 1; k < dimension; k++)
{
cout << "Linear Transf at k = " << k;
ctA_result[k] = Linear_Transform_Plain(ctA_result[0], V_diagonals[k - 1], gal_keys, params);
ctB_result[k] = Linear_Transform_Plain(ctB_result[0], W_diagonals[k - 1], gal_keys, params);
cout << "..... Done" << endl;
}
/*
// Test scale
for (int i = 0; i < dimension; i++)
{
cout << "CTA scale at i = " << i << ":\t" << log2(ctA_result[i].scale()) << endl;
cout << "CTB scale at i = " << i << ":\t" << log2(ctB_result[i].scale()) << endl;
}
// Test Chain
cout << "\nCHAIN TEST -----------:" << endl;
for (int i = 0; i < dimension; i++)
{
cout << "chain index A at i = " << i << ":\t" << context->get_context_data(ctA_result[i].parms_id())->chain_index() << endl;
cout << "chain index B at i = " << i << ":\t" << context->get_context_data(ctB_result[i].parms_id())->chain_index() << endl;
}
cout << "context data total coeff modulus bit count = " << context->get_context_data(U_sigma_diagonals[0].parms_id())->total_coeff_modulus_bit_count() << endl;
*/
// Step 3
cout << "----------Step 3----------- " << endl;
// Test Rescale
cout << "RESCALE--------" << endl;
for (int i = 1; i < dimension; i++)
{
evaluator.rescale_to_next_inplace(ctA_result[i]);
evaluator.rescale_to_next_inplace(ctB_result[i]);
}
/*
// Test scale
for (int i = 0; i < dimension; i++)
{
cout << "CTA scale at i = " << i << ":\t" << log2(ctA_result[i].scale()) << endl;
cout << "CTB scale at i = " << i << ":\t" << log2(ctB_result[i].scale()) << endl;
}
cout << "Exact scale" << endl;
ios old_fmt(nullptr);
old_fmt.copyfmt(cout);
cout << fixed << setprecision(10);
for (int i = 0; i < dimension; i++)
{
cout << "\t Exact scale in ctA at i = " << i << ":\t" << ctA_result[i].scale() << endl;
cout << "\t Exact scale in ctB at i = " << i << ":\t" << ctB_result[i].scale() << endl;
}
cout << endl;
cout.copyfmt(old_fmt);
// Test Chain
cout << "\nCHAIN TEST -----------:" << endl;
for (int i = 0; i < dimension; i++)
{
cout << "chain index A at i = " << i << ":\t" << context->get_context_data(ctA_result[i].parms_id())->chain_index() << endl;
cout << "chain index B at i = " << i << ":\t" << context->get_context_data(ctB_result[i].parms_id())->chain_index() << endl;
}
*/
Ciphertext ctAB;
evaluator.multiply(ctA_result[0], ctB_result[0], ctAB);
// cout << "TEST" << endl;
// cout << "CTAB scale :\t" << log2(ctAB.scale()) << endl;
// cout << "CTAB chain index :\t" << context->get_context_data(ctAB.parms_id())->chain_index() << endl;
// Mod switch CTAB
// cout << "MOD SWITCH CTAB:" << endl;
evaluator.mod_switch_to_next_inplace(ctAB);
// cout << "CTAB chain index :\t" << context->get_context_data(ctAB.parms_id())->chain_index() << endl;
// Manual scale set
cout << "\nMANUAL SCALE:" << endl;
for (int i = 1; i < dimension; i++)
{
ctA_result[i].scale() = pow(2, (int)log2(ctA_result[i].scale()));
ctB_result[i].scale() = pow(2, (int)log2(ctB_result[i].scale()));
}
for (int k = 1; k < dimension; k++)
{
cout << "Iteration k = " << k << endl;
Ciphertext temp_mul;
evaluator.multiply(ctA_result[k], ctB_result[k], temp_mul);
evaluator.add_inplace(ctAB, temp_mul);
}
return ctAB;
}
void Matrix_Multiplication(size_t poly_modulus_degree, int dimension)
{
// Handle Rotation Error First
if (dimension > poly_modulus_degree / 4)
{
cerr << "Dimension is too large. Choose a dimension less than " << poly_modulus_degree / 4 << endl;
exit(1);
}
EncryptionParameters params(scheme_type::CKKS);
params.set_poly_modulus_degree(poly_modulus_degree);
cout << "MAX BIT COUNT: " << CoeffModulus::MaxBitCount(poly_modulus_degree) << endl;
params.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, {60, 40, 40, 40, 40, 60}));
auto context = SEALContext::Create(params);
// Generate keys, encryptor, decryptor and evaluator
KeyGenerator keygen(context);
PublicKey pk = keygen.public_key();
SecretKey sk = keygen.secret_key();
GaloisKeys gal_keys = keygen.galois_keys();
Encryptor encryptor(context, pk);
Evaluator evaluator(context);
Decryptor decryptor(context, sk);
// Create CKKS encoder
CKKSEncoder ckks_encoder(context);
// Create Scale
double scale = pow(2.0, 40);
vector<vector<double>> pod_matrix1_set1(dimension, vector<double>(dimension));
vector<vector<double>> pod_matrix2_set1(dimension, vector<double>(dimension));
// Fill input matrices
// double r = ((double)rand() / (RAND_MAX));
double filler = 1;
// Matrix 1
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension; j++)
{
pod_matrix1_set1[i][j] = filler;
filler++;
// r = ((double)rand() / (RAND_MAX));
}
}
cout << "Matrix 1:" << endl;
print_full_matrix(pod_matrix1_set1, 0);
filler = 1;
// Matrix 2
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension; j++)
{
pod_matrix2_set1[i][j] = filler;
// r = ((double)rand() / (RAND_MAX));
filler++;
}
}
cout << "Matrix 2:" << endl;
print_full_matrix(pod_matrix2_set1, 0);
int dimensionSq = pow(dimension, 2);
// Get U_sigma for first matrix
vector<vector<double>> U_sigma = get_U_sigma(pod_matrix1_set1);
cout << "\nU_sigma:" << endl;
print_full_matrix(U_sigma, 0);
// Get U_tau for second matrix
vector<vector<double>> U_tau = get_U_tau(pod_matrix1_set1);
cout << "\nU_tau:" << endl;
print_full_matrix(U_tau, 0);
// Get V_k (3D matrix)
vector<vector<vector<double>>> V_k(dimension - 1, vector<vector<double>>(dimensionSq, vector<double>(dimensionSq)));
for (int i = 1; i < dimension; i++)
{
V_k[i - 1] = get_V_k(pod_matrix1_set1, i);
cout << "\nV_" << to_string(i) << ":" << endl;
print_full_matrix(V_k[i - 1], 0);
}
// Get W_k (3D matrix)
vector<vector<vector<double>>> W_k(dimension - 1, vector<vector<double>>(dimensionSq, vector<double>(dimensionSq)));
for (int i = 1; i < dimension; i++)
{
W_k[i - 1] = get_W_k(pod_matrix1_set1, i);
cout << "\nW_" << to_string(i) << ":" << endl;
print_full_matrix(W_k[i - 1], 0);
}
// Get Diagonals for U_sigma
vector<vector<double>> U_sigma_diagonals = get_all_diagonals(U_sigma);
cout << "U_sigma Diagonal Matrix:" << endl;
print_full_matrix(U_sigma_diagonals, 0);
// Test ADD EPSILON
double epsilon = 0.00000001;
for (int i = 0; i < dimensionSq; i++)
{
for (int j = 0; j < dimensionSq; j++)
{
U_sigma_diagonals[i][j] += epsilon;
}
}
// Get Diagonals for U_tau
vector<vector<double>> U_tau_diagonals = get_all_diagonals(U_tau);
// Test ADD EPSILON
for (int i = 0; i < dimensionSq; i++)
{
for (int j = 0; j < dimensionSq; j++)
{
U_tau_diagonals[i][j] += epsilon;
}
}
// Get Diagonals for V_k
vector<vector<vector<double>>> V_k_diagonals(dimension - 1, vector<vector<double>>(dimensionSq, vector<double>(dimensionSq)));
for (int i = 1; i < dimension; i++)
{
V_k_diagonals[i - 1] = get_all_diagonals(V_k[i - 1]);
}
// Test ADD EPSILON
for (int i = 0; i < dimension - 1; i++)
{
for (int j = 0; j < dimensionSq; j++)
{
for (int k = 0; k < dimensionSq; k++)
{
V_k_diagonals[i][j][k] += epsilon;
}
}
}
// Get Diagonals for W_k
vector<vector<vector<double>>> W_k_diagonals(dimension - 1, vector<vector<double>>(dimensionSq, vector<double>(dimensionSq)));
for (int i = 1; i < dimension; i++)
{
W_k_diagonals[i - 1] = get_all_diagonals(W_k[i - 1]);
}
// Test ADD EPSILON
for (int i = 0; i < dimension - 1; i++)
{
for (int j = 0; j < dimensionSq; j++)
{
for (int k = 0; k < dimensionSq; k++)
{
W_k_diagonals[i][j][k] += epsilon;
}
}
}
// --------------- ENCODING ----------------
// Encode U_sigma diagonals
vector<Plaintext> U_sigma_diagonals_plain(dimensionSq);
cout << "\nEncoding U_sigma_diagonals...";
for (int i = 0; i < dimensionSq; i++)
{
ckks_encoder.encode(U_sigma_diagonals[i], scale, U_sigma_diagonals_plain[i]);
}
cout << "Done" << endl;
// Encode U_tau diagonals
vector<Plaintext> U_tau_diagonals_plain(dimensionSq);
cout << "\nEncoding U_tau_diagonals...";
for (int i = 0; i < dimensionSq; i++)
{
ckks_encoder.encode(U_tau_diagonals[i], scale, U_tau_diagonals_plain[i]);
}
cout << "Done" << endl;
// Encode V_k diagonals
vector<vector<Plaintext>> V_k_diagonals_plain(dimension - 1, vector<Plaintext>(dimensionSq));
cout << "\nEncoding V_K_diagonals...";
for (int i = 1; i < dimension; i++)
{
for (int j = 0; j < dimensionSq; j++)
{
ckks_encoder.encode(V_k_diagonals[i - 1][j], scale, V_k_diagonals_plain[i - 1][j]);
}
}
cout << "Done" << endl;
// Encode W_k
vector<vector<Plaintext>> W_k_diagonals_plain(dimension - 1, vector<Plaintext>(dimensionSq));
cout << "\nEncoding W_k_diagonals...";
for (int i = 1; i < dimension; i++)
{
for (int j = 0; j < dimensionSq; j++)
{
ckks_encoder.encode(W_k_diagonals[i - 1][j], scale, W_k_diagonals_plain[i - 1][j]);
}
}
cout << "Done" << endl;
// Encode Matrices
// Encode Matrix 1
vector<Plaintext> plain_matrix1_set1(dimension);
cout << "\nEncoding Matrix 1...";
for (int i = 0; i < dimension; i++)
{
ckks_encoder.encode(pod_matrix1_set1[i], scale, plain_matrix1_set1[i]);
}
cout << "Done" << endl;
// Encode Matrix 2
vector<Plaintext> plain_matrix2_set1(dimension);
cout << "\nEncoding Matrix 2...";
for (int i = 0; i < dimension; i++)
{
ckks_encoder.encode(pod_matrix2_set1[i], scale, plain_matrix2_set1[i]);
}
cout << "Done" << endl;
// --------------- ENCRYPTING ----------------
// Encrypt Matrix 1
vector<Ciphertext> cipher_matrix1_set1(dimension);
cout << "\nEncrypting Matrix 1...";
for (int i = 0; i < dimension; i++)
{
encryptor.encrypt(plain_matrix1_set1[i], cipher_matrix1_set1[i]);
}
cout << "Done" << endl;
// Encrypt Matrix 2
vector<Ciphertext> cipher_matrix2_set1(dimension);
cout << "\nEncrypting Matrix 2...";
for (int i = 0; i < dimension; i++)
{
encryptor.encrypt(plain_matrix2_set1[i], cipher_matrix2_set1[i]);
}
cout << "Done" << endl;
// --------------- MATRIX ENCODING ----------------
// Matrix Encode Matrix 1
cout << "\nMatrix Encoding Matrix 1...";
Ciphertext cipher_encoded_matrix1_set1 = C_Matrix_Encode(cipher_matrix1_set1, gal_keys, evaluator);
cout << "Done" << endl;
// Matrix Encode Matrix 2
cout << "\nMatrix Encoding Matrix 2...";
Ciphertext cipher_encoded_matrix2_set1 = C_Matrix_Encode(cipher_matrix2_set1, gal_keys, evaluator);
cout << "Done" << endl;
/*
// Test Matrix Encoding
Plaintext test_matrix_encoding;
decryptor.decrypt(cipher_encoded_matrix1_set1, test_matrix_encoding);
vector<double> test_matrix_encoding_result(dimensionSq);
ckks_encoder.decode(test_matrix_encoding, test_matrix_encoding_result);
cout << "Decoded Matrix : " << endl;
cout << "\t[";
for (int i = 0; i < dimensionSq - 1; i++)
{
cout << test_matrix_encoding_result[i] << ", ";
}
cout << test_matrix_encoding_result[dimensionSq - 1] << "]" << endl;
*/
// --------------- MATRIX MULTIPLICATION ----------------
cout << "\nMatrix Multiplication...";
cout << "test " << endl;
Ciphertext ct_result = CC_Matrix_Multiplication(cipher_encoded_matrix1_set1, cipher_encoded_matrix2_set1, dimension, U_sigma_diagonals_plain, U_tau_diagonals_plain, V_k_diagonals_plain, W_k_diagonals_plain, gal_keys, params);
cout << "Done" << endl;
// --------------- DECRYPT ----------------
Plaintext pt_result;
cout << "\nResult Decrypt...";
decryptor.decrypt(ct_result, pt_result);
cout << "Done" << endl;
// --------------- DECODE ----------------
vector<double> result_matrix;
cout << "\nResult Decode...";
ckks_encoder.decode(pt_result, result_matrix);
cout << "Done" << endl;
// print_partial_vector(result_matrix, result_matrix.size());
cout << "Resulting matrix: ";
for (int i = 0; i < dimensionSq; i++)
{
if (i % 4 == 0)
{
cout << "\n\t";
}
cout << result_matrix[i] << ", ";
}
cout << endl;
/*
cout << "------------ TESTING ------------" << endl;
vector<Ciphertext> ctA_result(dimension);
vector<Ciphertext> ctB_result(dimension);
cout << "----------Step 1----------- " << endl;
// Step 1-1
ctA_result[0] = Linear_Transform_Plain(cipher_encoded_matrix1_set1, U_sigma_diagonals_plain, gal_keys, params);
// Step 1-2
ctB_result[0] = Linear_Transform_Plain(cipher_encoded_matrix2_set1, U_tau_diagonals_plain, gal_keys, params);
// TEST CTA _ RESULT [0]
Plaintext cta_0;
decryptor.decrypt(ctA_result[0], cta_0);
vector<double> cta_0_res;
ckks_encoder.decode(cta_0, cta_0_res);
cout << "Resulting matrix cta[0]: ";
for (int i = 0; i < dimensionSq; i++)
{
if (i % 4 == 0)
{
cout << "\n\t";
}
cout << cta_0_res[i] << ", ";
}
cout << endl;
// TEST CTB _ RESULT [0]
Plaintext ctb_0;
decryptor.decrypt(ctB_result[0], ctb_0);
vector<double> ctb_0_res;
ckks_encoder.decode(ctb_0, ctb_0_res);
cout << "Resulting matrix ctb[0]: ";
for (int i = 0; i < dimensionSq; i++)
{
if (i % 4 == 0)
{
cout << "\n\t";
}
cout << ctb_0_res[i] << ", ";
}
cout << endl;
// Step 2
cout << "----------Step 2----------- " << endl;
for (int k = 1; k < dimension; k++)
{
cout << "Linear Transf at k = " << k;
ctA_result[k] = Linear_Transform_Plain(ctA_result[0], V_k_diagonals_plain[k - 1], gal_keys, params);
ctB_result[k] = Linear_Transform_Plain(ctB_result[0], W_k_diagonals_plain[k - 1], gal_keys, params);
cout << "..... Done" << endl;
}
// Step 3
cout << "----------Step 3----------- " << endl;
// Test Rescale
cout << "RESCALE--------" << endl;
for (int i = 1; i < dimension; i++)
{
evaluator.rescale_to_next_inplace(ctA_result[i]);
evaluator.rescale_to_next_inplace(ctB_result[i]);
}
Ciphertext ctAB;
evaluator.multiply(ctA_result[0], ctB_result[0], ctAB);
evaluator.mod_switch_to_next_inplace(ctAB);
// Manual scale set
for (int i = 1; i < dimension; i++)
{
ctA_result[i].scale() = pow(2, 80);
ctB_result[i].scale() = pow(2, 80);
}
for (int k = 1; k < dimension; k++)
{
cout << "Iteration k = " << k << endl;
Ciphertext temp_mul;
evaluator.multiply(ctA_result[k], ctB_result[k], temp_mul);
evaluator.add_inplace(ctAB, temp_mul);
}
*/
}
int main()
{
Matrix_Multiplication(8192 * 2, 4);
return 0;
}