-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
159 lines (116 loc) · 4.3 KB
/
main.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
#include "common.h"
#include "parameters.h"
int main(int argc, char **argv) {
//Initializing encryption cryptosystem parameters
pairing_t pairing;
element_t g, Z;
element_t a, b, pk_a, pk_b, rk;
element_t tempz, temp1, tempt, tpa_sum;
//Initializing timer variables
double t0, t1;
int i, k;
double temp;
// Read cryptosystem parameters
const char *param_file[2] = { "", "param/a.param" };
pbc_demo_pairing_init(pairing, 2, param_file);
element_init_Zr(tempz, pairing);
//Initializing cryptosystem
printf("===== E-Zone-based SAS test=====\n");
element_init_G1(g, pairing);
element_init_GT(Z, pairing);
element_random(g);
element_pairing(Z, g, g);
//Generating IU key pair
printf("* Key generation\n");
element_init_Zr(a, pairing);
element_init_G1(pk_a, pairing);
t0 = pbc_get_time();
element_random(a);
element_pow_zn(pk_a,g,a);
t1 = pbc_get_time();
printf("Established IU key pair, time elapsed %fms\n",(t1-t0)*1000);
//Generating SU key pair
element_init_Zr(b, pairing);
element_init_G1(pk_b, pairing);
t0 = pbc_get_time();
element_random(b);
element_pow_zn(pk_b,g,b);
t1 = pbc_get_time();
printf("Established SU key pair, time elapsed %fms\n",(t1-t0)*1000);
//Calculating IU to SU re-encryption conversion key
element_init_G1(rk, pairing);
t0 = pbc_get_time();
element_invert(tempz, a);
element_pow_zn(rk, pk_b, tempz);
t1 = pbc_get_time();
printf("Established conversion key, time elapsed %fms\n",(t1-t0)*1000);
// IU E-Zone generation and aggregation
printf("\n# of entries in T_k:%d\n", N);
ct * T_ct = (ct *)malloc(N * sizeof(ct));
init_array_ct(T_ct, pairing);
ct * R_ct = (ct *)malloc(N * sizeof(ct));
init_array_ct(R_ct, pairing);
element_init_Zr(tpa_sum, pairing);
element_set0(tpa_sum);
for(k = 0; k < IUs; k++){
ct * R_k_ct = (ct *)malloc(N * sizeof(ct));
init_array_ct(R_k_ct, pairing);
//t0 = pbc_get_time();
printf("E-Zone data generation with of IU %d, time elapsed ",k);
ct* T_k_ct = generate_e_zone_ct(pk_a, Z, pairing, R_k_ct, tpa_sum);
//t1 = pbc_get_time();
//printf("E-Zone data generation of IU %d, time elapsed %fms\n",k, (t1-t0)*1000);
t0 = pbc_get_time();
if (k == 0){
for(i = 0; i < N; i++){
element_set((T_ct+i)->c1, (T_k_ct+i)->c1);
element_set((T_ct+i)->c2, (T_k_ct+i)->c2);
element_set((R_ct+i)->c1, (R_k_ct+i)->c1);
element_set((R_ct+i)->c2, (R_k_ct+i)->c2);
}
}
else{
for(i = 0; i < N; i++){
element_mul((T_ct+i)->c1, (T_ct+i)->c1, (T_k_ct+i)->c1);
element_mul((T_ct+i)->c2, (T_ct+i)->c2, (T_k_ct+i)->c2);
element_mul((R_ct+i)->c1, (R_ct+i)->c1, (R_k_ct+i)->c1);
element_mul((R_ct+i)->c2, (R_ct+i)->c2, (R_k_ct+i)->c2);
}
}
t1 = pbc_get_time();
printf("Aggregation of IU %d, time elapsed %fms\n\n",k, (t1-t0)*1000);
}
printf("\n* Starting to serve SU......\n");
// SU processing
int su_id = 0;
element_t c1o;
element_t c2o;
element_init_GT(c1o, pairing);
element_init_GT(c2o, pairing);
element_t r1o;
element_t r2o;
element_init_GT(r1o, pairing);
element_init_GT(r2o, pairing);
t0 = pbc_get_time();
ReEnc(c1o, c2o, (T_ct+su_id)->c1, (T_ct+su_id)->c2, rk);
ReEnc(r1o, r2o, (R_ct+su_id)->c1, (R_ct+su_id)->c2, rk);
t1 = pbc_get_time();
printf("\nData rencryption to SU's key, time elapsed %fms\n",(t1-t0)*1000);
element_t Md;
element_init_GT(Md, pairing);
element_t Rd;
element_init_GT(Rd, pairing);
t0 = pbc_get_time();
ReDec(Md, c1o, c2o, b, pairing);
ReDec(Rd, r1o, r2o, b, pairing);
t1 = pbc_get_time();
printf("\nData decryption by SU's key, time elapsed %fms\n",(t1-t0)*1000);
element_printf("Decrypted plaintext: %B\n", Md);
element_printf("Decrypted R: %B\n", Rd);
element_div(Md, Md, Rd);
element_printf("Depacked plaintext: %B\n", Md);
element_t tpa_SUM;
element_init_GT(tpa_SUM, pairing);
element_pow_zn(tpa_SUM, Z, tpa_sum);
printf("TPA Confirmation result: %d\n", element_cmp(tpa_SUM, Md));
}