-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACtrlGroupIden.c
417 lines (363 loc) · 11.8 KB
/
ACtrlGroupIden.c
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
//
// main.c
// ACtrlGroupIden
//
// Created by cnyao on 1/12/16.
// Copyright © 2016 cnyao. All rights reserved.
//
// Let me clarify it: we haven't implement step 1 or 2,
// because they are currently not planned for evaluation
// (they are bootstrap steps and not bottleneck). Instead,
// you only need to implement and evaluate step 3
// (prove and verify for access control).
// Note that to implement step 3,
// you may need to assume some parameters (e.g., you assume the certificate,
// random numbers and public/private keys are given as input).
// You can write them as input of a function.
#include <pbc.h>
#include <pbc_test.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//system variable & public keys shared
element_t g, X, Y, x, y;
pairing_t pairing;
pairing_t pairing2;
int verbose;
int ifsize;
static inline void pbc_single_pairing_init(pairing_t pairing, int argc, char *argv);
void UEActivity(unsigned char **da, unsigned char **db, unsigned char **dc, unsigned char **dcu);
void BSActivity(unsigned char *da, unsigned char *db, unsigned char *dc, unsigned char *dcu);
void usage();
int main(int argc, char **argv) {
verbose = 0;
ifsize = 0;
int canrun =0;
clock_t start_t, end_t;
float total_t;
int user_num = 100;
int k;
int choose;
char *para1, *para2;
while ((choose = getopt (argc, argv, "vfn:hgs")) != -1) {
switch (choose) {
case 's':
ifsize = 1;
break;
case 'h':
usage();
exit(0);
break;
case 'v':
verbose = 1;
break;
case 'n':
user_num = atoi(optarg);
break;
case 'g':
//printf("Initializing pairing parameters...\n");
if(canrun) {
fprintf(stderr, "Pairing parameters have been set, \'-g\' should not set paring parameters again.\n");
break;
}
canrun = 1;
k=0;
for( ; optind<argc && !(*argv[optind] == '-'); optind++) k++;
if(k==2) {
int rbits = atoi(argv[optind-k]);
int qbits = atoi(argv[optind-k+1]);
pbc_param_t param;
// printf("rbits=%d qbits=%d\n",rbits,qbits);
pbc_param_init_a_gen(param, rbits, qbits);
pairing_init_pbc_param(pairing, param);
pairing_init_pbc_param(pairing2, param);
pbc_param_clear(param);
} else {
fprintf(stderr, "Input invalid!\n");
usage();
exit(-1);
}
break;
case 'f':
//printf("Initializing pairing parameters...\n");
if(canrun) {
fprintf(stderr, "Pairing parameters have been set, \'-f\' should not set paring parameters again.\n");
break;
}
canrun = 1;
k=0;
for( ; optind<argc && !(*argv[optind] == '-'); optind++) k++;
if(k==2) {
pbc_single_pairing_init(pairing, argc, argv[optind-k]);
pbc_single_pairing_init(pairing2, argc, argv[optind-k+1]);
}
else {
fprintf(stderr, "Input invalid!\n");
usage();
exit(-1);
}
break;
case '?':
fprintf(stderr, "Invalid parameters!\n");
usage();
exit(-1);
break;
default:
abort();
}
}
if(!canrun) {
printf("Please at least set \'-f\' or \'-g\'\n");
usage();
exit(-1);
}
//printf("Initializing system variable and public key....\n");
element_init_G2(g, pairing);
element_init_G2(X, pairing);
element_init_G2(Y, pairing);
element_init_Zr(x, pairing2);
element_init_Zr(y, pairing2);
element_random(x);
element_random(y);
printf("g=%lu X=%lu Y=%lu x=%lu y=%lu\n",sizeof(g),sizeof(X),sizeof(Y),sizeof(x),sizeof(y));
//system variable & public key generation
element_random(g);
if(verbose) element_printf("system parameter g = %B\n", g);
element_pow_zn(X, g, x);
element_pow_zn(Y, g, y);
// mpz_t mpz_g, mpz_X, mpz_Y;
// mpz_inits(mpz_g, mpz_X, mpz_Y, mpz_x, mpz_y, NULL);
// element_to_mpz(mpz_g, g);
// element_to_mpz(mpz_x, x);
// element_to_mpz(mpz_y, y);
// mpz_powm(mpz_X, mpz_g, mpz_x, pairing->r);
// element_set_mpz(X, mpz_X);
// //element_pow_zn(X, g, x);
// mpz_powm(mpz_Y, mpz_g, mpz_y, pairing->r);
// element_set_mpz(Y, mpz_Y);
// if(verbose) {
// gmp_printf("pair order %zd\n", pairing->r);
// gmp_printf("mpz g %zd\n", mpz_g);
// element_printf("x = %B\n", x);
// gmp_printf("mpz x %zd\n", mpz_x);
// gmp_printf("mpz y %zd\n", mpz_y);
// gmp_printf("mpz X %zd\n", mpz_X);
// element_printf("public key X = %B\n", X);
// element_printf("public key Y = %B\n", Y);
// }
// //element_pow_zn(Y, g, y);
// mpz_clear(mpz_g);mpz_clear(mpz_X);mpz_clear(mpz_Y);
unsigned char *a, *b, *c, *cu;
/*******Working********/
start_t = clock();
clock_t tmp_start;
clock_t bscurtotal = 0;
float bstotal;
clock_t tmp=0;
clock_t max;
clock_t min;
for(int i=0; i<user_num; i++) {
//printf("New user comes...\n");
UEActivity(&a, &b, &c, &cu);
tmp_start = clock();
BSActivity(a, b, c, cu);
tmp = clock() - tmp_start;
if(i==0) {min = tmp; max = tmp;}
else {
if(tmp > max) max = tmp;
if(tmp < min) min = tmp;
}
printf("Processing time for this user is %f ms \n",(float)tmp*1000 / CLOCKS_PER_SEC);
bscurtotal += tmp;
}
clock_t avg = bscurtotal / user_num;
printf("max single user time is %f ms \n",(float)max*1000 / CLOCKS_PER_SEC);
printf("min single user time is %f ms \n",(float)min*1000 / CLOCKS_PER_SEC);
printf("average single user time is %f ms \n",(float)avg*1000 / CLOCKS_PER_SEC);
//printf("************************\n");
end_t = clock();
total_t = (float)(end_t - start_t) / CLOCKS_PER_SEC;
bstotal = (float)bscurtotal / CLOCKS_PER_SEC;
//printf("User number: %d. \nTotal Generation & verification time taken by CPU: %f seconds.\n", user_num, total_t);
//printf("Total verification time at base station taken by CPU: %f seconds.\n", bstotal);
//printf("Exiting of the program...\n");
element_clear(g);
element_clear(X);
element_clear(Y);
element_clear(x);
element_clear(y);
return 0;
}
void UEActivity(unsigned char **da, unsigned char **db, unsigned char **dc, unsigned char **dcu) {
//printf("Generating keys.....\n");
element_t a, b, c, cu, r, A, B, C;
element_t ax, a1cuxy;
element_t xy, cuxy;
element_init_G1(a, pairing);
element_init_G1(b, pairing);
element_init_G1(c, pairing);
element_init_Zr(r, pairing);
element_init_G1(A, pairing);
element_init_G1(B, pairing);
element_init_G1(C, pairing);
element_init_G1(ax, pairing);
element_init_G1(a1cuxy, pairing);
//temporarily regard p and q are independent
//instead of p = 2q + 1
element_init_Zr(xy, pairing2);
element_init_Zr(cuxy, pairing2);
element_init_Zr(cu, pairing2);
//temporarily regard cu as a random number in Zr
//instead of Cu = r^k&^ru
element_random(cu);
element_random(a);
if(verbose) element_printf("sig component a = %B\n", a);
element_pow_zn(b, a, y);
// mpz_t mpz_a, mpz_b, mpz_c;
// mpz_inits(mpz_a, mpz_b, mpz_c, NULL);
// element_to_mpz(mpz_a, a);
// mpz_powm(mpz_b, mpz_a, mpz_y, pairing->r);
// element_set_mpz(b, mpz_b);
//element_pow_zn(b, a, y);
if(verbose) element_printf("sig component b = %B\n", b);
element_pow_zn(ax, a, x);
// mpz_t mpz_ax, mpz_a1cuxy, mpz_cuxy;
// mpz_inits(mpz_ax, mpz_a1cuxy, mpz_cuxy, NULL);
// mpz_powm(mpz_ax, mpz_a, mpz_x, pairing->r);
// element_set_mpz(ax, mpz_ax);
element_mul(xy, x, y);
element_mul(cuxy, xy, cu);
element_pow_zn(a1cuxy, a, cuxy);
// element_to_mpz(mpz_cuxy, cuxy);
// mpz_powm(mpz_a1cuxy, mpz_a, mpz_cuxy, pairing->r);
// element_set_mpz(a1cuxy, mpz_a1cuxy);
element_mul(c, ax, a1cuxy);
if(verbose) element_printf("sig component c = %B\n", c);
//blind the signature
// mpz_t mpz_A, mpz_B, mpz_C, mpz_r;
// mpz_inits(mpz_A, mpz_B, mpz_C, mpz_r, NULL);
element_random(r);
element_pow_zn(A, a, r);
element_pow_zn(B, b, r);
element_pow_zn(C, c, r);
// element_to_mpz(mpz_r, r);
// mpz_powm(mpz_A, mpz_a, mpz_r, pairing->r);
// mpz_powm(mpz_B, mpz_b, mpz_r, pairing->r);
// mpz_powm(mpz_C, mpz_c, mpz_r, pairing->r);
// element_set_mpz(A, mpz_A);
// element_set_mpz(B, mpz_B);
// element_set_mpz(C, mpz_C);
//clear meta elements
element_clear(ax);
element_clear(a1cuxy);
element_clear(xy);
element_clear(cuxy);
element_clear(r);
element_clear(a);
element_clear(b);
element_clear(c);
// mpz_clear(mpz_a);
// mpz_clear(mpz_b);
// mpz_clear(mpz_c);
// mpz_clear(mpz_ax);
// mpz_clear(mpz_a1cuxy);
// mpz_clear(mpz_cuxy);
// mpz_clear(mpz_A);
// mpz_clear(mpz_B);
// mpz_clear(mpz_C);
// mpz_clear(mpz_r);
//signature compress
int n = pairing_length_in_bytes_compressed_G1(pairing);
int m = pairing_length_in_bytes_Zr(pairing2);
*da = pbc_malloc(n);
*db = pbc_malloc(n);
*dc = pbc_malloc(n);
*dcu = pbc_malloc(m);
element_to_bytes_compressed(*da, A);
element_to_bytes_compressed(*db, B);
element_to_bytes_compressed(*dc, C);
element_to_bytes(*dcu, cu);
return;
}
void BSActivity(unsigned char *da, unsigned char *db, unsigned char *dc, unsigned char *dcu) {
//signature decompress
//printf("Verifying....\n");
element_t cu, A, B, C;
element_init_G1(A, pairing);
element_init_G1(B, pairing);
element_init_G1(C, pairing);
element_init_Zr(cu, pairing2);
element_from_bytes_compressed(A, da);
element_from_bytes_compressed(B, db);
element_from_bytes_compressed(C, dc);
element_from_bytes(cu, dcu);
pbc_free(da);
pbc_free(db);
pbc_free(dc);
pbc_free(dcu);
//verification I
element_t exbcu;
element_t tmp1, tmp2, right, left;
element_init_GT(exbcu, pairing);
element_init_GT(tmp1, pairing);
element_init_GT(tmp2, pairing);
element_init_GT(right, pairing);
element_init_GT(left, pairing);
element_pairing(tmp1, X, A);
element_pairing(tmp2, X, B);
element_pow_zn(exbcu, tmp2, cu);
element_mul(left, tmp1, exbcu);
element_pairing(right, g, C);
if (!element_cmp(left, right)) {
//printf("part 1 verifies\n");
} else {
printf("*BUG* part 1 does not verify *BUG*\n");
}
//verification II
element_pairing(left, A, Y);
element_pairing(right, g, B);
if (!element_cmp(left, right)) {
//printf("part 2 verifies\n");
} else {
printf("*BUG* part 2 does not verify *BUG*\n");
}
if(ifsize) {
int totalsize = sizeof(tmp1) + sizeof(tmp2) + sizeof(right) + sizeof(left) +
sizeof(A) + sizeof(B) + sizeof(C) + sizeof(cu);
printf("Memory used at base station is %d bytes. \n", totalsize);
}
element_clear(exbcu);
element_clear(tmp1);
element_clear(tmp2);
element_clear(right);
element_clear(left);
element_clear(A);
element_clear(B);
element_clear(C);
element_clear(cu);
return;
}
void usage() {
printf("****************USAGE***************\n \
-v: Turn on verbose mode. Print signature details.\n \
-h: Help info. \n \
-f [FILE1][FILE2]: Set pairing parameter info from files on disk. FILE1 and FILE2 stores pairing parameters.\n \
-n: Number of users of the signature generation & verification process. 100 by default. \n \
-g [rbits] [qbits]: Set pairing parameter info by rbits and qbits. The group order r is rbits long, and the order of \
the base field q is qbits long. Elements take qbits to represent.\n \
-s: once used, the memory used by BS will be printed (in bytes). ");
return;
}
static inline void pbc_single_pairing_init(pairing_t pairing, int argc, char *argv) {
char s[16384];
FILE *fp = stdin;
if (argc > 1) {
fp = fopen(argv, "r");
if (!fp) pbc_die("error opening %s", argv);
}
size_t count = fread(s, 1, 16384, fp);
if (!count) pbc_die("input error");
fclose(fp);
if (pairing_init_set_buf(pairing, s, count)) pbc_die("pairing init failed");
}