1
+ // Created by nassimaab on 1/10/19.
2
+ //
3
+
4
+ #include " BENCHBuildingBlocks.h"
5
+ #include < chrono>
6
+ #include < gmp.h>
7
+ #include < string.h>
8
+
9
+
10
+ using namespace std ;
11
+ using namespace std ::chrono;
12
+
13
+
14
+
15
+ BENCHBuildingBlocks::BENCHBuildingBlocks () {
16
+ gmp_randinit_default (randstate);
17
+ gmp_randseed_ui (randstate,time (NULL ));
18
+ dtpkc.keygen (precision, randstate, nb_bits, error);
19
+
20
+ dtpkc.getKey (pkey, skey);
21
+ dtpkc.updatePkw (pkey);
22
+
23
+ A.set_str (std::to_string (a), 10 );
24
+ B.set_str (std::to_string (b), 10 );
25
+
26
+ C_A = dtpkc.enc (A, pkey);
27
+ C_B = dtpkc.enc (B, pkey);
28
+ PC_A = dtpkc.PSdec0 (C_A);
29
+
30
+ }
31
+
32
+ void BENCHBuildingBlocks::set_keysize (unsigned int keysize){
33
+ nb_bits = keysize;
34
+ gmp_randinit_default (randstate);
35
+ gmp_randseed_ui (randstate, time (NULL ));
36
+ dtpkc.keygen (precision, randstate, nb_bits, error);
37
+
38
+ dtpkc.getKey (pkey, skey);
39
+ dtpkc.updatePkw (pkey);
40
+
41
+ a = 10 , b = 3 ;
42
+ A.set_str (std::to_string (a), 10 );
43
+ B.set_str (std::to_string (b), 10 );
44
+
45
+ C_A = dtpkc.enc (A, pkey);
46
+ C_B = dtpkc.enc (B, pkey);
47
+ PC_A = dtpkc.PSdec0 (C_A);
48
+ }
49
+
50
+ void BENCHBuildingBlocks::set_iterations (int iter) {
51
+ iterations = iter;
52
+ }
53
+
54
+ long BENCHBuildingBlocks::bench_keygen () {
55
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
56
+ for (int i = 0 ; i < iterations; i++) {
57
+ gmp_randinit_default (randstate);
58
+ gmp_randseed_ui (randstate, time (NULL ));
59
+ dtpkc.keygen (precision, randstate, nb_bits, error);
60
+
61
+ dtpkc.getKey (pkey, skey);
62
+ dtpkc.updatePkw (pkey);
63
+ }
64
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
65
+ auto duration = duration_cast<microseconds>(end - start).count ();
66
+ return lround (duration / iterations);
67
+ }
68
+
69
+
70
+ long BENCHBuildingBlocks::bench_encryption () {
71
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
72
+ for (int i = 0 ; i < iterations; i++) {
73
+ C_A = dtpkc.enc (A, pkey);
74
+ C_B = dtpkc.enc (B, pkey);
75
+ }
76
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
77
+ auto duration = duration_cast<microseconds>(end - start).count ();
78
+ return lround (duration / (2 * iterations));
79
+ }
80
+
81
+ long BENCHBuildingBlocks::bench_decryption () {
82
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
83
+ mpz_class clear;
84
+ for (int i = 0 ; i < iterations; i++) {
85
+ clear = dtpkc.Sdec (C_A);
86
+ }
87
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
88
+ // cout << "Decryption result : " << clear.get_str() << endl;
89
+ auto duration = duration_cast<microseconds>(end - start).count ();
90
+ return lround (duration / iterations);
91
+ }
92
+
93
+ long BENCHBuildingBlocks::bench_partial_decryption0 () {
94
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
95
+ for (int i = 0 ; i < iterations; i++) {
96
+ Cipher PC = dtpkc.PSdec0 (C_A);
97
+ }
98
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
99
+ auto duration = duration_cast<microseconds>(end - start).count ();
100
+ return lround (duration / iterations);
101
+ }
102
+
103
+ long BENCHBuildingBlocks::bench_partial_decryption1 () {
104
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
105
+ for (int i = 0 ; i < iterations; i++) {
106
+ mpz_class clear = dtpkc.PSdec1 (C_A, PC_A);
107
+ }
108
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
109
+ auto duration = duration_cast<microseconds>(end - start).count ();
110
+ return lround (duration / iterations);
111
+ }
112
+
113
+ long BENCHBuildingBlocks::bench_partial_decryption_complete () {
114
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
115
+ for (int i = 0 ; i < iterations; i++) {
116
+ PC_A = dtpkc.PSdec0 (C_A);
117
+ mpz_class clear = dtpkc.PSdec1 (C_A, PC_A);
118
+ }
119
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
120
+ auto duration = duration_cast<microseconds>(end - start).count ();
121
+ return lround (duration / iterations);
122
+ }
123
+
124
+ void BENCHBuildingBlocks::set_operands (long op_a, long op_b) {
125
+ A.set_str (std::to_string (op_a), 10 );
126
+ B.set_str (std::to_string (op_b), 10 );
127
+ }
128
+
129
+
130
+ long BENCHBuildingBlocks::bench_addition () {
131
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
132
+ for (int i = 0 ; i < iterations; i++) {
133
+ EvalAdd add (C_A, C_B);
134
+ C_C = add.EvalAdd_U1 ();
135
+ }
136
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
137
+ mpz_class clear = dtpkc.Sdec (C_C);
138
+ // cout << "Addition result : " + clear.get_str() << endl;
139
+ auto duration = duration_cast<microseconds>(end - start).count ();
140
+ // cout << "ADDITION TAKES : " << duration << "us." << endl;
141
+ return lround (duration / iterations);
142
+ }
143
+
144
+ long BENCHBuildingBlocks::bench_batch_encryption (std::vector<long > v) {
145
+ string str_v (nb_slots * slot_size, ' 0' );
146
+ mpz_class vec;
147
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
148
+
149
+ for (int i = 0 ; i < v.size (); i++) {
150
+ string val = to_string (v[i]);
151
+ for (int j = 0 ; j < val.length (); ++j) {
152
+ str_v[(nb_slots - i - 1 ) * slot_size + slot_size - j - 1 ] = val[val.length () - j - 1 ];
153
+ }
154
+ }
155
+ vec.set_str (str_v, 10 );
156
+ // cout << str_v << endl;
157
+ for (int i = 0 ; i < iterations; i++) {
158
+ C_VA = dtpkc.enc (vec, pkey);
159
+ C_VB = dtpkc.enc (vec, pkey);
160
+ }
161
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
162
+ auto duration = duration_cast<microseconds>(end - start).count ();
163
+ return lround (duration / (2 * iterations));
164
+ }
165
+
166
+
167
+ long BENCHBuildingBlocks::bench_batch_encryption_partial_key (std::vector<int > v, mpz_class key) {
168
+ string str_v (nb_slots * slot_size, ' 0' );
169
+ mpz_class vec;
170
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
171
+
172
+ for (int i = 0 ; i < v.size (); i++) {
173
+ string val = to_string (v[i]);
174
+ for (int j = 0 ; j < val.length (); ++j) {
175
+ str_v[(nb_slots - i - 1 ) * slot_size + slot_size - j - 1 ] = val[val.length () - j - 1 ];
176
+ }
177
+ }
178
+ vec.set_str (str_v, 10 );
179
+ // cout << str_v << endl;
180
+ for (int i = 0 ; i < iterations; i++) {
181
+ C_VA = dtpkc.enc (vec, key);
182
+ C_VB = dtpkc.enc (vec, key);
183
+ }
184
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
185
+ auto duration = duration_cast<microseconds>(end - start).count ();
186
+ return lround (duration / (2 * iterations));
187
+ }
188
+
189
+
190
+ long BENCHBuildingBlocks::bench_batch_decryption () {
191
+ string str_v;
192
+ string dstr_v;
193
+ vector<int > vec;
194
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
195
+ for (int k = 0 ; k < iterations; k++) {
196
+ VA = dtpkc.Sdec (C_VC);
197
+ str_v = VA.get_str ();
198
+ // cout << str_v << endl;
199
+ string padd (nb_slots * slot_size - str_v.length (), ' 0' );
200
+ str_v = padd + str_v;
201
+ // cout << str_v << endl;
202
+ for (int i = nb_slots - 1 ; i >= 0 ; i--) {
203
+ // cout << str_v.substr(i * slot_size, slot_size) << endl;
204
+ vec.push_back (stoi (str_v.substr (i * slot_size, slot_size)));
205
+ }
206
+ }
207
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
208
+ /* cout << "BATCH DECRYPT RESULT : " << endl;
209
+ for (int i = 0; i < nb_slots; i++) {
210
+ dstr_v += to_string(vec[i]) + " ";
211
+ }*/
212
+ cout << dstr_v << endl;
213
+ auto duration = duration_cast<microseconds>(end - start).count ();
214
+ return lround (duration / iterations);
215
+ }
216
+
217
+
218
+ long BENCHBuildingBlocks::bench_batch_decryption_partial_key (mpz_class key) {
219
+ string str_v;
220
+ string dstr_v;
221
+ vector<int > vec;
222
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
223
+ for (int k = 0 ; k < iterations; k++) {
224
+ VA = dtpkc.dec (C_VC, key);
225
+ str_v = VA.get_str ();
226
+ // cout << str_v << endl;
227
+ string padd (nb_slots * slot_size - str_v.length (), ' 0' );
228
+ str_v = padd + str_v;
229
+ // cout << str_v << endl;
230
+ for (int i = nb_slots - 1 ; i >= 0 ; i--) {
231
+ // cout << str_v.substr(i * slot_size, slot_size) << endl;
232
+ vec.push_back (stoi (str_v.substr (i * slot_size, slot_size)));
233
+ }
234
+ }
235
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
236
+ /* cout << "BATCH DECRYPT PARTIAL KEY RESULT : " << endl;
237
+ for (int i = 0; i < nb_slots; i++) {
238
+ dstr_v += to_string(vec[i]) + " ";
239
+ }
240
+ cout << dstr_v << endl;*/
241
+ auto duration = duration_cast<microseconds>(end - start).count ();
242
+ return lround (duration / iterations);
243
+ }
244
+
245
+ long BENCHBuildingBlocks::bench_batch_addition () {
246
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
247
+ for (int i = 0 ; i < iterations; i++) {
248
+ EvalAdd add (C_VA, C_VB);
249
+ C_VC = add.EvalAdd_U1 ();
250
+ }
251
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
252
+ mpz_class clear = dtpkc.Sdec (C_VC);
253
+ // cout << "BATCH Addition result : " + clear.get_str() << endl;
254
+ auto duration = duration_cast<microseconds>(end - start).count ();
255
+ // cout << "BATCH ADDITION TAKES : " << duration << "us." << endl;
256
+ return lround (duration / iterations);
257
+ }
258
+
259
+
260
+ long BENCHBuildingBlocks::bench_subtraction () {
261
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
262
+ for (int i = 0 ; i < iterations; i++) {
263
+ EvalSub sub (C_A, C_B, dtpkc);
264
+ C_C = sub.EvalSub_U1 ();
265
+ }
266
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
267
+ mpz_class clear_A = dtpkc.Sdec (C_A);
268
+ mpz_class clear_B = dtpkc.Sdec (C_B);
269
+ mpz_class clear = dtpkc.Sdec (C_C);
270
+ // cout << "Subtraction result : " + clear.get_str() << endl;
271
+ // cout << "A = " + clear_A.get_str() << endl;
272
+ // cout << "B = " + clear_B.get_str() << endl;
273
+
274
+ auto duration = duration_cast<microseconds>(end - start).count ();
275
+ return lround (duration / iterations);
276
+ }
277
+
278
+ long BENCHBuildingBlocks::bench_multiplication () {
279
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
280
+
281
+ for (int i = 0 ; i < iterations; i++) {
282
+ EvalMult e1 (C_A, C_B, dtpkc);
283
+ C_C = e1 .EvalMult_U1_step3 (e1 .EvalMult_U2_step2 (e1 .EvalMult_U1_step1 ()));
284
+ }
285
+
286
+ mpz_class clear = dtpkc.Sdec (C_C);
287
+ // cout << "Multiplication result : " + clear.get_str() << endl;
288
+
289
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
290
+ auto duration = duration_cast<microseconds>(end - start).count ();
291
+ return lround (duration / iterations);
292
+ }
293
+
294
+
295
+ long BENCHBuildingBlocks::bench_keyswitch () {
296
+ high_resolution_clock::time_point start = high_resolution_clock::now ();
297
+ for (int i = 0 ; i < iterations; i++) {
298
+ dtpkc.getKey (newPkey, newSkey);
299
+ dtpkc.updatePkw (newPkey);
300
+
301
+ SkeySwitch skeySwitch (C_A, newPkey, dtpkc);
302
+ C_C = skeySwitch.SkeySwitch_U1_step3 (skeySwitch.SkeySwitch_U2_step2 (skeySwitch.SkeySwitch_U1_step1 ()));
303
+ C = dtpkc.Sdec (C_C);
304
+ }
305
+ high_resolution_clock::time_point end = high_resolution_clock::now ();
306
+ auto duration = duration_cast<microseconds>(end - start).count ();
307
+ return lround (duration / iterations);
308
+ }
309
+
310
+
311
+
312
+
313
+ void BENCHBuildingBlocks::generate_newkeys () {
314
+ gmp_randinit_default (randstate);
315
+ gmp_randseed_ui (randstate,time (NULL ));
316
+ dtpkc.keygen (precision, randstate, nb_bits, error);
317
+
318
+ dtpkc.getKey (newPkey, newSkey);
319
+ dtpkc.updatePkw (newPkey);
320
+ }
0 commit comments