-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_encryption.c
More file actions
207 lines (188 loc) · 6.58 KB
/
benchmark_encryption.c
File metadata and controls
207 lines (188 loc) · 6.58 KB
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
#include "deks.h"
#include "peks.h"
#include "sa_peks.h"
#include <sys/time.h>
#define TEST_COST 25.55 // Time of testing a ciphertext
char dataset[1024][64];
static double time_cost(struct timeval A, struct timeval B) {
double res;
res = (B.tv_sec - A.tv_sec) * 1000000 + B.tv_usec - A.tv_usec;
return res / 1000;
}
int main(int argc, char * argv[]) {
char * ec_param = "type f\nq 16283262548997601220198008118239886027035269286659395419233331082106632227801\nr 16283262548997601220198008118239886026907663399064043451383740756301306087801\nb 7322757890651446833342173470888950103129198494728585151431956701557392549679\nbeta 9776063510951480907546829895586341975790848099009256891110816835958685274282\nalpha0 1776307819061905444848005605541335123416176649043693774569737527341520482926\nalpha1 4922472003107175314522406564324183657125055707028154503359780540299732014568";
//Type-f BN256
pairing_t pairing;
element_t g, pk, sk, Ca;
mpz_t N, pi, e, d, phi_N;
int g2_len, zr_len;
FILE * fp;
char * g2_buf, * zr_buf;
char w[64], Cb[HASH_LEN];
struct timeval start, end;
double cost1, cost2, cost3, kga_cost, result;
pairing_init_set_str(pairing, ec_param);
g2_len = pairing_length_in_bytes_compressed_G2(pairing);
zr_len = pairing_length_in_bytes_Zr(pairing);
g2_buf = (char *)malloc(g2_len);
zr_buf = (char *)malloc(zr_len);
element_init_G2(Ca, pairing);
element_init_G2(g, pairing);
element_init_G2(pk, pairing);
element_init_Zr(sk, pairing);
mpz_init(N);
mpz_init(pi);
mpz_init(e);
mpz_init(d);
mpz_init(phi_N);
if ((fp = fopen("keyword.txt", "r"))== NULL) {
printf("Please provide at least 1000 keywords in a file named \'keyword.txt\'.\n");
return STS_ERR;
}
for(int i = 0; i < 1000; i++) {
if(fscanf(fp, "%s", w) != EOF) {
strcpy(dataset[i], w);
}else {
printf("Error!\n");
break;
}
}
fclose(fp);
if ((fp = fopen("params.txt", "rb")) == NULL) {
printf("Please execute benchmark_params_ciphers first!\n");
return STS_ERR;
}
fread(g2_buf, g2_len, 1, fp);
element_from_bytes_compressed(g, g2_buf);
fread(g2_buf, g2_len, 1, fp);
element_from_bytes_compressed(pk, g2_buf);
fread(zr_buf, zr_len, 1, fp);
element_from_bytes(sk, zr_buf);
mpz_inp_raw(N, fp);
mpz_inp_raw(e, fp);
mpz_inp_raw(d, fp);
mpz_inp_raw(phi_N, fp);
fclose(fp);
cost1 = 0.0;
printf("Time cost of DEKS encryption with T=2^0:(ms)\n");
gettimeofday(&start, NULL);
for(int i = 0; i < 1000; i++) {
deks_encrypt(Ca, Cb, 0, pairing, g, pk, N, dataset[i]);
if(i % 50 == 49) {
gettimeofday(&end, NULL);
cost1 += time_cost(start, end);
printf("%9.3f\n", cost1);
gettimeofday(&start, NULL);
}
}
printf("avg-%.3f\n", cost1/1000);
cost1 = 0.0;
printf("Time cost of DEKS encryption with T=2^12:(ms)\n");
gettimeofday(&start, NULL);
for(int i = 0; i < 1000; i++) {
deks_encrypt(Ca, Cb, 12, pairing, g, pk, N, dataset[i]);
if(i % 50 == 49) {
gettimeofday(&end, NULL);
cost1 += time_cost(start, end);
printf("%9.3f\n", cost1);
gettimeofday(&start, NULL);
}
}
printf("avg-%.3f\n", cost1/1000);
cost1 = 0.0;
printf("Time cost of DEKS encryption with T=2^24:(ms)\n");
gettimeofday(&start, NULL);
for(int i = 0; i < 50; i++) {
deks_encrypt(Ca, Cb, 24, pairing, g, pk, N, dataset[i]);
}
gettimeofday(&end, NULL);
cost1 = time_cost(start, end);
printf("%.3f\n", cost1);
printf("avg-%.3f\n", cost1/50);
cost2 = 0.0;
printf("Time cost of PEKS encryption:(ms)\n");
gettimeofday(&start, NULL);
for(int i = 0; i < 1000; i++) {
peks_encrypt(Ca, Cb, pairing, g, pk, dataset[i]);
if(i % 50 == 49) {
gettimeofday(&end, NULL);
cost2 += time_cost(start, end);
printf("%9.3f\n", cost2);
gettimeofday(&start, NULL);
}
}
printf("avg-%.3f\n", cost2/1000);
cost3 = 0.0;
printf("Time cost of SA-PEKS encryption:(ms)\n");
gettimeofday(&start, NULL);
for(int i = 0; i < 1000; i++) {
sa_peks_encrypt(Ca, Cb, pairing, g, pk, N, e, d, dataset[i]);
if(i % 50 == 49) {
gettimeofday(&end, NULL);
cost3 += time_cost(start, end);
printf("%9.3f\n", cost3);
gettimeofday(&start, NULL);
}
}
printf("avg-%.3f\n", cost3/1000);
printf("Time cost of DEKS encryption with various T and average KGA: without-/with-catalyst,Enron,Wikipedia(ms,ms,h,h)\n");
for (int i = 0; i < 29; ++i) {
catalyst_gen(i, phi_N, pi);
gettimeofday(&start, NULL);
for (int j = 0; j < 100; ++j) {
deks_encrypt(Ca, Cb, i, pairing, g, pk, N, dataset[j]);
}
gettimeofday(&end, NULL);
cost1 = time_cost(start, end);
gettimeofday(&start, NULL);
for (int j = 0; j < 100; ++j) {
deks_encrypt_catalyst(Ca, Cb, i, pairing, g, pk, pi, N, dataset[j]);
}
gettimeofday(&end, NULL);
cost3 = time_cost(start, end);
printf("%2d\t%10.3f\t%5.3f", i, cost1/100, cost3/100); // without/with catalyst
if (i > 9) {
kga_cost = cost1/100 + TEST_COST;
result = 0.0;
for (int j = 1; j <= 400087; ++j) { // Enron dataset
result += kga_cost * j;
}
printf("\t%8.3f", result/400087/3600000);
result = 0.0;
if (i < 25) {
for (int j = 1; j <= 6756439; ++j) { // Wikipedia dataset
result += kga_cost * j;
}
printf("\t%8.3f", result/6756439/3600000);
}else {
printf("\t-----.---");
}
} else {
printf("\t-----.---\t-----.---");
}
printf("\n");
}
printf("Time cost of PEKS average KGA: Enron,Wikipedia(min,min)\n");
kga_cost = cost2/1000 + TEST_COST;
result = 0.0;
for (int i = 1; i <= 400087; ++i) {
result += kga_cost * i;
}
printf("%7.3f", result/400087/60000);
result = 0.0;
for (int i = 1; i <= 6756439; ++i) {
result += kga_cost * i;
}
printf("\t%7.3f\n", result/6756439/60000);
element_clear(g);
element_clear(pk);
element_clear(sk);
element_clear(Ca);
mpz_clear(N);
mpz_clear(pi);
mpz_clear(e);
mpz_clear(d);
mpz_clear(phi_N);
pairing_clear(pairing);
return STS_OK;
}