-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cpp
1127 lines (929 loc) · 39.5 KB
/
benchmark.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <fstream>
#include <iomanip>
#include "seal/seal.h"
using namespace std;
using namespace seal;
// Helper function that prints a vector of floats
template <typename T>
inline void print_vector(std::vector<T> vec, std::size_t print_size = 4, int prec = 4)
{
/*
Save the formatting information for std::cout.
*/
std::ios old_fmt(nullptr);
old_fmt.copyfmt(std::cout);
std::size_t slot_count = vec.size();
std::cout << std::fixed << std::setprecision(prec);
std::cout << std::endl;
if (slot_count <= 2 * print_size)
{
std::cout << " [";
for (std::size_t i = 0; i < slot_count; i++)
{
std::cout << " " << vec[i] << ((i != slot_count - 1) ? "," : " ]\n");
}
}
else
{
vec.resize(std::max(vec.size(), 2 * print_size));
std::cout << " [";
for (std::size_t i = 0; i < print_size; i++)
{
std::cout << " " << vec[i] << ",";
}
if (vec.size() > 2 * print_size)
{
std::cout << " ...,";
}
for (std::size_t i = slot_count - print_size; i < slot_count; i++)
{
std::cout << " " << vec[i] << ((i != slot_count - 1) ? "," : " ]\n");
}
}
std::cout << std::endl;
/*
Restore the old std::cout formatting.
*/
std::cout.copyfmt(old_fmt);
}
// Helper function that prints a matrix (vector of vectors)
template <typename T>
inline void print_full_matrix(vector<vector<T>> matrix, int size, int precision = 3)
{
// save formatting for cout
ios old_fmt(nullptr);
old_fmt.copyfmt(cout);
cout << fixed << setprecision(precision);
for (unsigned int i = 0; i < size; i++)
{
cout << "[";
for (unsigned int j = 0; j < size - 1; j++)
{
cout << matrix[i][j] << ", ";
}
cout << matrix[i][size - 1];
cout << "]" << endl;
}
cout << endl;
// restore old cout formatting
cout.copyfmt(old_fmt);
}
template <typename T>
inline void print_partial_matrix(vector<vector<T>> matrix, int size, int precision = 3)
{
// save formatting for cout
ios old_fmt(nullptr);
old_fmt.copyfmt(cout);
cout << fixed << setprecision(precision);
int print_size = 4;
// print first 4 elements
for (unsigned int row = 0; row < print_size; row++)
{
cout << "\t[";
for (unsigned int col = 0; col < print_size; col++)
{
cout << matrix[row][col] << ", ";
}
cout << "..., ";
for (unsigned int col = size - print_size; col < size - 1; col++)
{
cout << matrix[row][col] << ", ";
}
cout << matrix[row][size - 1];
cout << "]" << endl;
}
cout << "\t..." << endl;
for (unsigned int row = size - print_size; row < size; row++)
{
cout << "\t[";
for (unsigned int col = 0; col < print_size; col++)
{
cout << matrix[row][col] << ", ";
}
cout << "..., ";
for (unsigned int col = size - print_size; col < size - 1; col++)
{
cout << matrix[row][col] << ", ";
}
cout << matrix[row][size - 1];
cout << "]" << endl;
}
cout << endl;
// restore old cout formatting
cout.copyfmt(old_fmt);
}
void ckksBenchmark(size_t poly_modulus_degree)
{
cout << "------CKKS TEST------\n"
<< endl;
// Set params
EncryptionParameters params(scheme_type::ckks);
params.set_poly_modulus_degree(poly_modulus_degree);
params.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
SEALContext context(params);
// Generate keys, encryptor, decryptor and evaluator
KeyGenerator keygen(context);
SecretKey sk = keygen.secret_key();
PublicKey pk;
keygen.create_public_key(pk);
Encryptor encryptor(context, pk);
Evaluator evaluator(context);
Decryptor decryptor(context, sk);
// Create CKKS encoder
CKKSEncoder ckks_encoder(context);
size_t slot_count = ckks_encoder.slot_count();
cout << "Slot count : " << slot_count << endl;
// Set output file
string filename = "bench_" + to_string(poly_modulus_degree) + ".dat";
ofstream outf(filename);
// Handle file error
if (!outf)
{
cerr << "Couldn't open file: " << filename << endl;
exit(1);
}
// Set output script
string script = "script_" + to_string(poly_modulus_degree) + ".p";
ofstream outscript(script);
// Handle script error
if (!outscript)
{
cerr << "Couldn't open file: " << script << endl;
exit(1);
}
// Write to script
outscript << "# Set the output terminal" << endl;
outscript << "set terminal canvas" << endl;
outscript << "set output \"canvas_" << to_string(poly_modulus_degree) << ".html\"" << endl;
outscript << "set title \"CKKS Benchmark " << to_string(poly_modulus_degree) << "\"" << endl;
outscript << "set xlabel 'Input Vector Size'" << endl;
outscript << "set ylabel 'Time (microseconds)'" << endl;
outscript << "\n# Set the styling " << endl;
outscript << "set style line 1\\\n"
<< "linecolor rgb '#0060ad'\\\n"
<< "linetype 1 linewidth 2\\\n"
<< "pointtype 7 pointsize 1.5\n"
<< endl;
outscript << "set style line 2\\\n"
<< "linecolor rgb '#dd181f'\\\n"
<< "linetype 1 linewidth 2\\\n"
<< "pointtype 5 pointsize 1.5\n"
<< endl;
outscript << "set style line 3\\\n"
<< "linecolor rgb '#00FF00'\\\n"
<< "linetype 1 linewidth 2\\\n"
<< "pointtype 6 pointsize 1.5\n"
<< endl;
outscript << "set style line 4\\\n"
<< "linecolor rgb '#EC00EC'\\\n"
<< "linetype 1 linewidth 2\\\n"
<< "pointtype 4 pointsize 1.5\n"
<< endl;
outscript << "\nplot 'bench_" << to_string(poly_modulus_degree) << ".dat' index 0 title \"C1 + P2\" with linespoints ls 1, \\\n"
<< "'' index 1 title \"C1 + C2\" with linespoints ls 2, \\\n"
<< "'' index 2 title \"C1 * P2\" with linespoints ls 3, \\\n"
<< "'' index 3 title \"C1 * C2\" with linespoints ls 4";
// Close script
outscript.close();
/*
3 sets of vectors:
1st set: sizes = 10
2nd set: sizes = 100
3rd set: sizes = 1000
*/
// ------------- FIRST SET -------------
// First vector
vector<double> pod_vec1_set1(10, 0);
for (unsigned int i = 0; i < 10; i++)
{
pod_vec1_set1[i] = static_cast<double>(i);
}
print_vector(pod_vec1_set1);
// Second vector
vector<double> pod_vec2_set1(10, 0);
for (unsigned int i = 0; i < 10; i++)
{
pod_vec2_set1[i] = static_cast<double>((i % 2) + 1);
}
print_vector(pod_vec2_set1);
// -------------- SECOND SET -------------
// First vector
vector<double> pod_vec1_set2(100, 0);
for (unsigned int i = 0; i < 100; i++)
{
pod_vec1_set2[i] = static_cast<double>(i);
}
print_vector(pod_vec1_set2);
// Second vector
vector<double> pod_vec2_set2(100, 0);
for (unsigned int i = 0; i < 100; i++)
{
pod_vec2_set2[i] = static_cast<double>((i % 2) + 1);
}
print_vector(pod_vec2_set2);
// -------------- THIRD SET -------------
// First vector
vector<double> pod_vec1_set3(1000, 0);
for (unsigned int i = 0; i < 1000; i++)
{
pod_vec1_set3[i] = static_cast<double>(i);
}
print_vector(pod_vec1_set3);
// Second vector
vector<double> pod_vec2_set3(1000, 0);
for (unsigned int i = 0; i < 1000; i++)
{
pod_vec2_set3[i] = static_cast<double>((i % 2) + 1);
}
print_vector(pod_vec2_set3);
// Encode all vectors
Plaintext plain_vec1_set1, plain_vec2_set1, plain_vec1_set2, plain_vec2_set2, plain_vec1_set3, plain_vec2_set3;
double scale = sqrt(static_cast<double>(params.coeff_modulus().back().value()));
// First set encode
ckks_encoder.encode(pod_vec1_set1, scale, plain_vec1_set1);
ckks_encoder.encode(pod_vec2_set1, scale, plain_vec2_set1);
// Second set encode
ckks_encoder.encode(pod_vec1_set2, scale, plain_vec1_set2);
ckks_encoder.encode(pod_vec2_set2, scale, plain_vec2_set2);
// Third set encode
ckks_encoder.encode(pod_vec1_set3, scale, plain_vec1_set3);
ckks_encoder.encode(pod_vec2_set3, scale, plain_vec2_set3);
// Encrypt all vectors
Ciphertext cipher_vec1_set1, cipher_vec2_set1, cipher_vec1_set2, cipher_vec2_set2, cipher_vec1_set3, cipher_vec2_set3;
// First set cipher
encryptor.encrypt(plain_vec1_set1, cipher_vec1_set1);
encryptor.encrypt(plain_vec2_set1, cipher_vec2_set1);
// Second set cipher
encryptor.encrypt(plain_vec1_set2, cipher_vec1_set2);
encryptor.encrypt(plain_vec2_set2, cipher_vec2_set2);
// Third set cipher
encryptor.encrypt(plain_vec1_set3, cipher_vec1_set3);
encryptor.encrypt(plain_vec2_set3, cipher_vec2_set3);
// Create Ciphertext Outputs
Ciphertext cipher_result1_set1, cipher_result1_set2, cipher_result1_set3;
Ciphertext cipher_result2_set1, cipher_result2_set2, cipher_result2_set3;
Ciphertext cipher_result3_set1, cipher_result3_set2, cipher_result3_set3;
Ciphertext cipher_result4_set1, cipher_result4_set2, cipher_result4_set3;
// ------------------ (cipher1 + plain2) ---------------
cout << "\n------------------ FIRST OPERATION ------------------\n"
<< endl;
outf << "# index 0" << endl;
outf << "# C1 + P2" << endl;
// Compute (cipher1 + plain2) for set 1
cout << "Compute (cipher1 + plain2) for set 1" << endl;
// TIME START
auto start_comp1_set1 = chrono::high_resolution_clock::now();
evaluator.add_plain(cipher_vec1_set1, plain_vec2_set1, cipher_result1_set1);
// TIME END
auto stop_comp1_set1 = chrono::high_resolution_clock::now();
auto duration_comp1_set1 = chrono::duration_cast<chrono::microseconds>(stop_comp1_set1 - start_comp1_set1);
// Decrypt and Decode
Plaintext plain_result1_set1;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result1_set1, plain_result1_set1);
vector<double> vec_result1_set1;
ckks_encoder.decode(plain_result1_set1, vec_result1_set1);
print_vector(vec_result1_set1);
cout << "\nTime to compute (cipher1 + plain2): " << duration_comp1_set1.count() << " microseconds" << endl;
outf << "10\t\t" << duration_comp1_set1.count() << endl;
// Compute (cipher1 + plain2) for set 2
cout << "Compute (cipher1 + plain2) for set 2" << endl;
// TIME START
auto start_comp1_set2 = chrono::high_resolution_clock::now();
evaluator.add_plain(cipher_vec1_set2, plain_vec2_set2, cipher_result1_set2);
// TIME END
auto stop_comp1_set2 = chrono::high_resolution_clock::now();
auto duration_comp1_set2 = chrono::duration_cast<chrono::microseconds>(stop_comp1_set2 - start_comp1_set2);
// Decrypt and Decode
Plaintext plain_result1_set2;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result1_set2, plain_result1_set2);
vector<double> vec_result1_set2;
ckks_encoder.decode(plain_result1_set2, vec_result1_set2);
print_vector(vec_result1_set2);
cout << "\nTime to compute (cipher1 + plain2): " << duration_comp1_set2.count() << " microseconds" << endl;
outf << "100\t\t" << duration_comp1_set2.count() << endl;
// Compute (cipher1 + plain2) for set 3
cout << "Compute (cipher1 + plain2) for set 3" << endl;
// TIME START
auto start_comp1_set3 = chrono::high_resolution_clock::now();
evaluator.add_plain(cipher_vec1_set3, plain_vec2_set3, cipher_result1_set3);
// TIME END
auto stop_comp1_set3 = chrono::high_resolution_clock::now();
auto duration_comp1_set3 = chrono::duration_cast<chrono::microseconds>(stop_comp1_set3 - start_comp1_set3);
// Decrypt and Decode
Plaintext plain_result1_set3;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result1_set3, plain_result1_set3);
vector<double> vec_result1_set3;
ckks_encoder.decode(plain_result1_set3, vec_result1_set3);
print_vector(vec_result1_set3);
cout << "\nTime to compute (cipher1 + plain2): " << duration_comp1_set3.count() << " microseconds" << endl;
outf << "1000\t\t" << duration_comp1_set3.count() << endl;
cout << endl;
outf << "\n"
<< endl;
// ------------------ (cipher1 + cipher2) ---------------
cout << "\n------------------ SECOND OPERATION ------------------\n"
<< endl;
// Compute (cipher1 + cipher2) for set 1
cout << "Compute (cipher1 + cipher2) for set 1" << endl;
outf << "# index 1" << endl;
outf << "# C1 + C2" << endl;
// TIME START
auto start_comp2_set1 = chrono::high_resolution_clock::now();
evaluator.add(cipher_vec1_set1, cipher_vec2_set1, cipher_result2_set1);
// TIME END
auto stop_comp2_set1 = chrono::high_resolution_clock::now();
auto duration_comp2_set1 = chrono::duration_cast<chrono::microseconds>(stop_comp2_set1 - start_comp2_set1);
// Decrypt and Decode
Plaintext plain_result2_set1;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result2_set1, plain_result2_set1);
vector<double> vec_result2_set1;
ckks_encoder.decode(plain_result2_set1, vec_result2_set1);
print_vector(vec_result2_set1);
cout << "\nTime to compute (cipher1 + cipher2): " << duration_comp2_set1.count() << " microseconds" << endl;
outf << "10\t\t" << duration_comp2_set1.count() << endl;
// Compute (cipher1 + cipher2) for set 2
cout << "Compute (cipher1 + cipher2) for set 2" << endl;
// TIME START
auto start_comp2_set2 = chrono::high_resolution_clock::now();
evaluator.add(cipher_vec1_set2, cipher_vec2_set2, cipher_result2_set2);
// TIME END
auto stop_comp2_set2 = chrono::high_resolution_clock::now();
auto duration_comp2_set2 = chrono::duration_cast<chrono::microseconds>(stop_comp2_set2 - start_comp2_set2);
// Decrypt and Decode
Plaintext plain_result2_set2;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result2_set2, plain_result2_set2);
vector<double> vec_result2_set2;
ckks_encoder.decode(plain_result2_set2, vec_result2_set2);
print_vector(vec_result2_set2);
cout << "\nTime to compute (cipher1 + cipher2): " << duration_comp2_set2.count() << " microseconds" << endl;
outf << "100\t\t" << duration_comp2_set2.count() << endl;
// Compute (cipher1 + cipher2) for set 3
cout << "Compute (cipher1 + cipher2) for set 3" << endl;
// TIME START
auto start_comp2_set3 = chrono::high_resolution_clock::now();
evaluator.add(cipher_vec1_set3, cipher_vec2_set3, cipher_result2_set3);
// TIME END
auto stop_comp2_set3 = chrono::high_resolution_clock::now();
auto duration_comp2_set3 = chrono::duration_cast<chrono::microseconds>(stop_comp2_set3 - start_comp2_set3);
// Decrypt and Decode
Plaintext plain_result2_set3;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result2_set3, plain_result2_set3);
vector<double> vec_result2_set3;
ckks_encoder.decode(plain_result2_set3, vec_result2_set3);
print_vector(vec_result2_set3);
cout << "\nTime to compute (cipher1 + cipher2): " << duration_comp2_set3.count() << " microseconds" << endl;
outf << "1000\t\t" << duration_comp2_set3.count() << endl;
cout << endl;
outf << "\n"
<< endl;
// ------------------ (cipher1 * plain2) ---------------
cout << "\n------------------ THIRD OPERATION ------------------\n"
<< endl;
// Compute (cipher1 + plain2) for set 1
cout << "Compute (cipher1 * plain2) for set 1" << endl;
outf << "# index 2" << endl;
outf << "# C1 * P2" << endl;
// TIME START
auto start_comp3_set1 = chrono::high_resolution_clock::now();
evaluator.multiply_plain(cipher_vec1_set1, plain_vec2_set1, cipher_result3_set1);
// TIME END
auto stop_comp3_set1 = chrono::high_resolution_clock::now();
auto duration_comp3_set1 = chrono::duration_cast<chrono::microseconds>(stop_comp3_set1 - start_comp3_set1);
// Decrypt and Decode
Plaintext plain_result3_set1;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result3_set1, plain_result3_set1);
vector<double> vec_result3_set1;
ckks_encoder.decode(plain_result3_set1, vec_result3_set1);
print_vector(vec_result3_set1);
cout << "\nTime to compute (cipher1 * plain2): " << duration_comp3_set1.count() << " microseconds" << endl;
outf << "10\t\t" << duration_comp3_set1.count() << endl;
// Compute (cipher1 * plain2) for set 2
cout << "Compute (cipher1 * plain2) for set 2" << endl;
// TIME START
auto start_comp3_set2 = chrono::high_resolution_clock::now();
evaluator.multiply_plain(cipher_vec1_set2, plain_vec2_set2, cipher_result3_set2);
// TIME END
auto stop_comp3_set2 = chrono::high_resolution_clock::now();
auto duration_comp3_set2 = chrono::duration_cast<chrono::microseconds>(stop_comp3_set2 - start_comp3_set2);
// Decrypt and Decode
Plaintext plain_result3_set2;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result3_set2, plain_result3_set2);
vector<double> vec_result3_set2;
ckks_encoder.decode(plain_result3_set2, vec_result3_set2);
print_vector(vec_result3_set2);
cout << "\nTime to compute (cipher1 * plain2): " << duration_comp3_set2.count() << " microseconds" << endl;
outf << "100\t\t" << duration_comp3_set2.count() << endl;
// Compute (cipher1 * plain2) for set 3
cout << "Compute (cipher1 * plain2) for set 3" << endl;
// TIME START
auto start_comp3_set3 = chrono::high_resolution_clock::now();
evaluator.multiply_plain(cipher_vec1_set3, plain_vec2_set3, cipher_result3_set3);
// TIME END
auto stop_comp3_set3 = chrono::high_resolution_clock::now();
auto duration_comp3_set3 = chrono::duration_cast<chrono::microseconds>(stop_comp3_set3 - start_comp3_set3);
// Decrypt and Decode
Plaintext plain_result3_set3;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result3_set3, plain_result3_set3);
vector<double> vec_result3_set3;
ckks_encoder.decode(plain_result3_set3, vec_result3_set3);
print_vector(vec_result3_set3);
cout << "\nTime to compute (cipher1 * plain2): " << duration_comp3_set3.count() << " microseconds" << endl;
outf << "1000\t\t" << duration_comp3_set3.count() << endl;
cout << endl;
outf << "\n"
<< endl;
// ------------------ (cipher1 * cipher2) ---------------
cout << "\n------------------ FOURTH OPERATION ------------------\n"
<< endl;
// Compute (cipher1 * cipher2) for set 1
cout << "Compute (cipher1 * cipher2) for set 1" << endl;
outf << "# index 3" << endl;
outf << "# C1 * C2" << endl;
// TIME START
auto start_comp4_set1 = chrono::high_resolution_clock::now();
evaluator.multiply(cipher_vec1_set1, cipher_vec2_set1, cipher_result4_set1);
// TIME END
auto stop_comp4_set1 = chrono::high_resolution_clock::now();
auto duration_comp4_set1 = chrono::duration_cast<chrono::microseconds>(stop_comp4_set1 - start_comp4_set1);
// Decrypt and Decode
Plaintext plain_result4_set1;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result4_set1, plain_result4_set1);
vector<double> vec_result4_set1;
ckks_encoder.decode(plain_result4_set1, vec_result4_set1);
print_vector(vec_result4_set1);
cout << "\nTime to compute (cipher1 * cipher2): " << duration_comp4_set1.count() << " microseconds" << endl;
outf << "10\t\t" << duration_comp4_set1.count() << endl;
// Compute (cipher1 * cipher2) for set 2
cout << "Compute (cipher1 * cipher2) for set 2" << endl;
// TIME START
auto start_comp4_set2 = chrono::high_resolution_clock::now();
evaluator.multiply(cipher_vec1_set2, cipher_vec2_set2, cipher_result4_set2);
// TIME END
auto stop_comp4_set2 = chrono::high_resolution_clock::now();
auto duration_comp4_set2 = chrono::duration_cast<chrono::microseconds>(stop_comp4_set2 - start_comp4_set2);
// Decrypt and Decode
Plaintext plain_result4_set2;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result4_set2, plain_result4_set2);
vector<double> vec_result4_set2;
ckks_encoder.decode(plain_result4_set2, vec_result4_set2);
print_vector(vec_result4_set2);
cout << "\nTime to compute (cipher1 * cipher2): " << duration_comp4_set2.count() << " microseconds" << endl;
outf << "100\t\t" << duration_comp4_set2.count() << endl;
// Compute (cipher1 * cipher2) for set 3
cout << "Compute (cipher1 * cipher2) for set 3" << endl;
// TIME START
auto start_comp4_set3 = chrono::high_resolution_clock::now();
evaluator.multiply(cipher_vec1_set3, cipher_vec2_set3, cipher_result4_set3);
// TIME END
auto stop_comp4_set3 = chrono::high_resolution_clock::now();
auto duration_comp4_set3 = chrono::duration_cast<chrono::microseconds>(stop_comp4_set3 - start_comp4_set3);
// Decrypt and Decode
Plaintext plain_result4_set3;
cout << "Decrypt and decode the result" << endl;
decryptor.decrypt(cipher_result4_set3, plain_result4_set3);
vector<double> vec_result4_set3;
ckks_encoder.decode(plain_result4_set3, vec_result4_set3);
print_vector(vec_result4_set3);
cout << "\nTime to compute (cipher1 * cipher2): " << duration_comp4_set3.count() << " microseconds" << endl;
outf << "1000\t\t" << duration_comp4_set3.count() << endl;
cout << endl;
outf << "\n"
<< endl;
// Close the file
outf.close();
}
void ckksBenchmarkMatrix(size_t poly_modulus_degree)
{
cout << "------CKKS Matrix TEST------\n"
<< endl;
// Set params
EncryptionParameters params(scheme_type::ckks);
params.set_poly_modulus_degree(poly_modulus_degree);
params.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
SEALContext context(params);
// Generate keys, encryptor, decryptor and evaluator
KeyGenerator keygen(context);
SecretKey sk = keygen.secret_key();
PublicKey pk;
keygen.create_public_key(pk);
Encryptor encryptor(context, pk);
Evaluator evaluator(context);
Decryptor decryptor(context, sk);
// Create CKKS encoder
CKKSEncoder ckks_encoder(context);
size_t slot_count = ckks_encoder.slot_count();
cout << "Slot count : " << slot_count << endl;
// Set output file
string filename = "bench_matrix_" + to_string(poly_modulus_degree) + ".dat";
ofstream outf(filename);
// Handle file error
if (!outf)
{
cerr << "Couldn't open file: " << filename << endl;
exit(1);
}
// Set output script
string script = "script_matrix_" + to_string(poly_modulus_degree) + ".p";
ofstream outscript(script);
// Handle script error
if (!outscript)
{
cerr << "Couldn't open file: " << script << endl;
exit(1);
}
// Write to script
outscript << "# Set the output terminal" << endl;
outscript << "set terminal canvas" << endl;
outscript << "set output \"canvas_matrix_" << to_string(poly_modulus_degree) << ".html\"" << endl;
outscript << "set title \"CKKS Matrix Benchmark " << to_string(poly_modulus_degree) << "\"" << endl;
outscript << "set xlabel 'Input Matrix Size (NxN)'" << endl;
outscript << "set ylabel 'Time (microseconds)'" << endl;
outscript << "\n# Set the styling " << endl;
outscript << "set style line 1\\\n"
<< "linecolor rgb '#0060ad'\\\n"
<< "linetype 1 linewidth 2\\\n"
<< "pointtype 7 pointsize 1.5\n"
<< endl;
outscript << "set style line 2\\\n"
<< "linecolor rgb '#dd181f'\\\n"
<< "linetype 1 linewidth 2\\\n"
<< "pointtype 5 pointsize 1.5\n"
<< endl;
outscript << "set style line 3\\\n"
<< "linecolor rgb '#00FF00'\\\n"
<< "linetype 1 linewidth 2\\\n"
<< "pointtype 6 pointsize 1.5\n"
<< endl;
outscript << "set style line 4\\\n"
<< "linecolor rgb '#EC00EC'\\\n"
<< "linetype 1 linewidth 2\\\n"
<< "pointtype 4 pointsize 1.5\n"
<< endl;
outscript << "\nplot 'bench_matrix_" << to_string(poly_modulus_degree) << ".dat' index 0 title \"C1 + P2\" with linespoints ls 1, \\\n"
<< "'' index 1 title \"C1 + C2\" with linespoints ls 2, \\\n"
<< "'' index 2 title \"C1 * P2\" with linespoints ls 3, \\\n"
<< "'' index 3 title \"C1 * C2\" with linespoints ls 4";
// Close script
outscript.close();
// ------------- FIRST SET -------------
// First Matrix
int set_size1 = 10;
vector<vector<double>> pod_matrix1_set1(set_size1, vector<double>(set_size1));
double k = 0.0;
for (unsigned int i = 0; i < set_size1; i++)
{
for (unsigned int j = 0; j < set_size1; j++)
{
pod_matrix1_set1[i][j] = k;
// cout << "k = " << k;
k++;
}
}
cout << "Matrix 1 Set 1:\n"
<< endl;
// print_full_matrix(pod_matrix1_set1, set_size1);
print_partial_matrix(pod_matrix1_set1, set_size1);
// Second Matrix
vector<vector<double>> pod_matrix2_set1(set_size1, vector<double>(set_size1));
k = 0.0;
for (unsigned int i = 0; i < set_size1; i++)
{
for (unsigned int j = 0; j < set_size1; j++)
{
pod_matrix2_set1[i][j] = static_cast<double>((int(k) % 2) + 1);
k++;
}
}
cout << "Matrix 2 Set 1:\n"
<< endl;
// print_full_matrix(pod_matrix2_set1, set_size1);
print_partial_matrix(pod_matrix2_set1, set_size1);
// ------------- Second SET -------------
// First Matrix
int set_size2 = 100;
vector<vector<double>> pod_matrix1_set2(set_size2, vector<double>(set_size2));
k = 0.0;
for (unsigned int i = 0; i < set_size2; i++)
{
for (unsigned int j = 0; j < set_size2; j++)
{
pod_matrix1_set2[i][j] = k;
// cout << "k = " << k;
k++;
}
}
cout << "Matrix 1 Set 2:\n"
<< endl;
print_partial_matrix(pod_matrix1_set2, set_size2);
// Second Matrix
vector<vector<double>> pod_matrix2_set2(set_size2, vector<double>(set_size2));
k = 0.0;
for (unsigned int i = 0; i < set_size2; i++)
{
for (unsigned int j = 0; j < set_size2; j++)
{
pod_matrix2_set2[i][j] = static_cast<double>((int(k) % 2) + 1);
k++;
}
}
cout << "Matrix 2 Set 2:\n"
<< endl;
print_partial_matrix(pod_matrix2_set2, set_size2);
// ------------- THIRD SET -------------
// First Matrix
int set_size3 = 1000;
vector<vector<double>> pod_matrix1_set3(set_size3, vector<double>(set_size3));
k = 0.0;
for (unsigned int i = 0; i < set_size3; i++)
{
for (unsigned int j = 0; j < set_size3; j++)
{
pod_matrix1_set3[i][j] = k;
// cout << "k = " << k;
k++;
}
}
cout << "Matrix 1 Set 3:\n"
<< endl;
print_partial_matrix(pod_matrix1_set3, set_size3);
// Second Matrix
vector<vector<double>> pod_matrix2_set3(set_size3, vector<double>(set_size3));
k = 0.0;
for (unsigned int i = 0; i < set_size3; i++)
{
for (unsigned int j = 0; j < set_size3; j++)
{
pod_matrix2_set3[i][j] = static_cast<double>((int(k) % 2) + 1);
k++;
}
}
cout << "Matrix 2 Set 3:\n"
<< endl;
print_partial_matrix(pod_matrix2_set3, set_size3);
// Encode the matrices
vector<Plaintext> plain_matrix1_set1(set_size1), plain_matrix2_set1(set_size1);
vector<Plaintext> plain_matrix1_set2(set_size2), plain_matrix2_set2(set_size2);
vector<Plaintext> plain_matrix1_set3(set_size3), plain_matrix2_set3(set_size3);
double scale = sqrt(static_cast<double>(params.coeff_modulus().back().value()));
// First set encode
for (unsigned int i = 0; i < pod_matrix1_set1.size(); i++)
{
ckks_encoder.encode(pod_matrix1_set1[i], scale, plain_matrix1_set1[i]);
}
for (unsigned int i = 0; i < pod_matrix2_set1.size(); i++)
{
ckks_encoder.encode(pod_matrix2_set1[i], scale, plain_matrix2_set1[i]);
}
// Second set encode
for (unsigned int i = 0; i < pod_matrix1_set2.size(); i++)
{
ckks_encoder.encode(pod_matrix1_set2[i], scale, plain_matrix1_set2[i]);
}
for (unsigned int i = 0; i < pod_matrix2_set2.size(); i++)
{
ckks_encoder.encode(pod_matrix2_set2[i], scale, plain_matrix2_set2[i]);
}
// Third set encode
for (unsigned int i = 0; i < pod_matrix1_set3.size(); i++)
{
ckks_encoder.encode(pod_matrix1_set3[i], scale, plain_matrix1_set3[i]);
}
for (unsigned int i = 0; i < pod_matrix2_set3.size(); i++)
{
ckks_encoder.encode(pod_matrix2_set3[i], scale, plain_matrix2_set3[i]);
}
// Encrypt the matrices
vector<Ciphertext> cipher_matrix1_set1(set_size1), cipher_matrix2_set1(set_size1);
vector<Ciphertext> cipher_matrix1_set2(set_size2), cipher_matrix2_set2(set_size2);
vector<Ciphertext> cipher_matrix1_set3(set_size3), cipher_matrix2_set3(set_size3);
// First set cipher
for (unsigned int i = 0; i < plain_matrix1_set1.size(); i++)
{
encryptor.encrypt(plain_matrix1_set1[i], cipher_matrix1_set1[i]);
}
for (unsigned int i = 0; i < plain_matrix2_set1.size(); i++)
{
encryptor.encrypt(plain_matrix2_set1[i], cipher_matrix2_set1[i]);
}
// Second set cipher
for (unsigned int i = 0; i < plain_matrix1_set2.size(); i++)
{
encryptor.encrypt(plain_matrix1_set2[i], cipher_matrix1_set2[i]);
}
for (unsigned int i = 0; i < plain_matrix2_set2.size(); i++)
{
encryptor.encrypt(plain_matrix2_set2[i], cipher_matrix2_set2[i]);
}
// Third set cipher
for (unsigned int i = 0; i < plain_matrix1_set3.size(); i++)
{
encryptor.encrypt(plain_matrix1_set3[i], cipher_matrix1_set3[i]);
}
for (unsigned int i = 0; i < plain_matrix2_set3.size(); i++)
{
encryptor.encrypt(plain_matrix2_set3[i], cipher_matrix2_set3[i]);
}
// Create ciphertext output
// Set 1 output
vector<Ciphertext> cipher_result1_set1(set_size1), cipher_result2_set1(set_size1), cipher_result3_set1(set_size1), cipher_result4_set1(set_size1);
// Set 2 output
vector<Ciphertext> cipher_result1_set2(set_size2), cipher_result2_set2(set_size2), cipher_result3_set2(set_size2), cipher_result4_set2(set_size2);
// Set 3 output
vector<Ciphertext> cipher_result1_set3(set_size3), cipher_result2_set3(set_size3), cipher_result3_set3(set_size3), cipher_result4_set3(set_size3);
// ------------------ (cipher1 + plain2) ---------------
cout << "\n------------------ FIRST OPERATION ------------------\n"
<< endl;
outf << "# index 0" << endl;
outf << "# C1 + P2" << endl;
// Compute (cipher1 + plain2) for set 1
cout << "Compute (cipher1 + plain2) for set 1" << endl;
// TIME START
auto start_comp1_set1 = chrono::high_resolution_clock::now();
for (unsigned int i = 0; i < cipher_matrix1_set1.size(); i++)
{
evaluator.add_plain(cipher_matrix1_set1[i], plain_matrix2_set1[i], cipher_result1_set1[i]);
}
// TIME END
auto stop_comp1_set1 = chrono::high_resolution_clock::now();
auto duration_comp1_set1 = chrono::duration_cast<chrono::microseconds>(stop_comp1_set1 - start_comp1_set1);
// Decrypt and Decode
vector<Plaintext> plain_result1_set1(set_size1);
cout << "Decrypt and decode the result" << endl;
for (unsigned int i = 0; i < cipher_result1_set1.size(); i++)
{
decryptor.decrypt(cipher_result1_set1[i], plain_result1_set1[i]);
}
vector<vector<double>> matrix_result1_set1(set_size1, vector<double>(set_size1));
for (unsigned int i = 0; i < plain_result1_set1.size(); i++)
{
ckks_encoder.decode(plain_result1_set1[i], matrix_result1_set1[i]);
}
print_partial_matrix(matrix_result1_set1, set_size1);
cout << "\nTime to compute cipher1 + plain2: " << duration_comp1_set1.count() << " microseconds" << endl;
outf << set_size1 << "\t\t" << duration_comp1_set1.count() << endl;
// Compute (cipher1 + plain2) for set 2
cout << "Compute (cipher1 + plain2) for set 2" << endl;
// TIME START
auto start_comp1_set2 = chrono::high_resolution_clock::now();
for (unsigned int i = 0; i < cipher_matrix1_set2.size(); i++)
{
evaluator.add_plain(cipher_matrix1_set2[i], plain_matrix2_set2[i], cipher_result1_set2[i]);
}
// TIME END
auto stop_comp1_set2 = chrono::high_resolution_clock::now();
auto duration_comp1_set2 = chrono::duration_cast<chrono::microseconds>(stop_comp1_set2 - start_comp1_set2);
// Decrypt and Decode
vector<Plaintext> plain_result1_set2(set_size2);
cout << "Decrypt and decode the result" << endl;
for (unsigned int i = 0; i < cipher_result1_set2.size(); i++)
{
decryptor.decrypt(cipher_result1_set2[i], plain_result1_set2[i]);
}
vector<vector<double>> matrix_result1_set2(set_size2, vector<double>(set_size2));
for (unsigned int i = 0; i < plain_result1_set2.size(); i++)
{
ckks_encoder.decode(plain_result1_set2[i], matrix_result1_set2[i]);
}
print_partial_matrix(matrix_result1_set2, set_size2);
cout << "\nTime to compute cipher1 + plain2: " << duration_comp1_set2.count() << " microseconds" << endl;
outf << set_size2 << "\t\t" << duration_comp1_set2.count() << endl;
// Compute (cipher1 + plain2) for set 3
cout << "Compute (cipher1 + plain2) for set 3" << endl;
// TIME START
auto start_comp1_set3 = chrono::high_resolution_clock::now();
for (unsigned int i = 0; i < cipher_matrix1_set3.size(); i++)
{
evaluator.add_plain(cipher_matrix1_set3[i], plain_matrix2_set3[i], cipher_result1_set3[i]);
}
// TIME END
auto stop_comp1_set3 = chrono::high_resolution_clock::now();
auto duration_comp1_set3 = chrono::duration_cast<chrono::microseconds>(stop_comp1_set3 - start_comp1_set3);
// Decrypt and Decode
vector<Plaintext> plain_result1_set3(set_size3);
cout << "Decrypt and decode the result" << endl;
for (unsigned int i = 0; i < cipher_result1_set3.size(); i++)
{
decryptor.decrypt(cipher_result1_set3[i], plain_result1_set3[i]);
}
vector<vector<double>> matrix_result1_set3(set_size3, vector<double>(set_size3));
for (unsigned int i = 0; i < plain_result1_set3.size(); i++)
{
ckks_encoder.decode(plain_result1_set3[i], matrix_result1_set3[i]);
}
print_partial_matrix(matrix_result1_set3, set_size3);
cout << "\nTime to compute cipher1 + plain2: " << duration_comp1_set3.count() << " microseconds" << endl;
outf << set_size3 << "\t\t" << duration_comp1_set3.count() << endl;
cout << endl;
outf << "\n"