-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpack.c
324 lines (281 loc) · 6.98 KB
/
pack.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
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "malloc.h"
#include "labrador.h"
#include "chihuahua.h"
#include "dachshund.h"
#include "greyhound.h"
#include "pack.h"
void free_composite(composite *p) {
size_t i;
free_witness(&p->owt);
for(i=0;i<p->l;i++) {
free_proof(p->pi[i]);
free(p->pi[i]);
p->pi[i] = NULL;
}
p->l = 0;
}
static int composite_prove(composite *p, statement *tst, witness *twt, double *twtsize) {
int ret;
size_t i = 0;
double pisize;
while(p->l < 16) {
p->pi[p->l] = _malloc(sizeof(proof));
ret = prove(&tst[i^1],&twt[i^1],p->pi[p->l],&tst[i],&twt[i],0);
if(ret) return ret;
pisize = print_proof_pp(p->pi[p->l]);
print_statement_pp(&tst[i^1]);
twtsize[i^1] = print_witness_pp(&twt[i^1]);
if(pisize + twtsize[i^1] >= twtsize[i]) {
free_proof(p->pi[p->l]);
free_statement(&tst[i^1]);
free_witness(&twt[i^1]);
break;
}
free_statement(&tst[i]);
free_witness(&twt[i]);
p->size += pisize;
p->l += 1;
i ^= 1;
}
if(p->l < 16) {
ret = prove(&tst[i^1],&twt[i^1],p->pi[p->l],&tst[i],&twt[i],1);
if(ret) return ret;
pisize = print_proof_pp(p->pi[p->l]);
print_statement_pp(&tst[i^1]);
twtsize[i^1] = print_witness_pp(&twt[i^1]);
if(pisize + twtsize[i^1] >= twtsize[i]) {
free_proof(p->pi[p->l]);
free(p->pi[p->l]);
}
else {
p->size += pisize;
p->l += 1;
i ^= 1;
}
free_statement(&tst[i^1]);
free_witness(&twt[i^1]);
}
free_statement(&tst[i]);
p->owt = twt[i];
p->size += twtsize[i];
return 0;
}
int composite_prove_principle(composite *p, const prncplstmnt *st, const witness *wt) {
int ret;
statement tst[2] = {};
witness twt[2] = {};
double twtsize[2];
clock_t t;
p->l = 0;
memset(&p->owt,0,sizeof(witness));
p->pi[p->l] = _malloc(sizeof(proof));
t = clock();
ret = principle_prove(tst,twt,p->pi[p->l],st,wt,0);
if(ret)
goto err;
p->size = print_proof_pp(p->pi[p->l]);
print_statement_pp(tst);
twtsize[0] = print_witness_pp(twt);
p->l += 1;
ret = composite_prove(p,tst,twt,twtsize);
if(ret) {
ret += 10;
goto err;
}
t = clock() - t;
printf("Commitment key length: %zu\n",comkey_len);
printf("Chihuahua Pack members: %zu\n",p->l);
printf("Chihuahua Pack size: %.2f KB\n",p->size);
printf("Chihuahua Pack proving time: %.4fs\n",(double)t/CLOCKS_PER_SEC);
return 0;
err:
free_statement(&tst[0]);
free_statement(&tst[1]);
free_witness(&twt[0]);
free_witness(&twt[1]);
free(p->pi[p->l]);
p->pi[p->l] = NULL;
free_composite(p);
return ret;
}
int composite_prove_simple(composite *p, commitment *com, const smplstmnt *st, const witness *wt) {
int ret;
statement tst[2] = {};
witness twt[2] = {};
double twtsize[2];
clock_t t;
p->l = 0;
memset(&p->owt,0,sizeof(witness));
p->pi[p->l] = _malloc(sizeof(proof));
t = clock();
ret = simple_prove(tst,twt,p->pi[p->l],com,st,wt,0);
if(ret)
goto err;
p->size = print_proof_pp(p->pi[p->l]);
print_statement_pp(tst);
twtsize[0] = print_witness_pp(twt);
p->l += 1;
ret = composite_prove(p,tst,twt,twtsize);
if(ret) {
ret += 10;
goto err;
}
t = clock() - t;
printf("Commitment key length: %zu\n",comkey_len);
printf("Dachshund Pack members: %zu\n",p->l);
printf("Dachshund Pack size: %.2f KB\n",p->size);
printf("Dachshund Pack proving time: %.4fs\n",(double)t/CLOCKS_PER_SEC);
return 0;
err:
free_statement(&tst[0]);
free_statement(&tst[1]);
free_witness(&twt[0]);
free_witness(&twt[1]);
free(p->pi[p->l]);
p->pi[p->l] = NULL;
free_composite(p);
return ret;
}
int composite_prove_polcom(composite *p, polcomprf *ppi, polcomctx *ctx, uint32_t x, uint32_t y) {
int ret;
prncplstmnt tst0[1] = {};
statement tst[2] = {};
witness twt[2] = {};
double twtsize[2];
clock_t t;
p->l = 0;
memset(&p->owt,0,sizeof(witness));
p->pi[p->l] = _malloc(sizeof(proof));
t = clock();
polcom_eval(&twt[1],ppi,ctx,x,y);
ret = polcom_reduce(tst0,ppi);
if(ret)
goto err;
p->size = print_polcomprf_pp(ppi);
print_prncplstmnt_pp(tst0);
twtsize[1] = print_witness_pp(&twt[1]);
ret = principle_prove(tst,twt,p->pi[p->l],tst0,&twt[1],0);
if(ret) {
ret += 10;
goto err;
}
p->size += print_proof_pp(p->pi[p->l]);
print_statement_pp(tst);
twtsize[0] = print_witness_pp(twt);
free_prncplstmnt(tst0);
free_witness(&twt[1]);
p->l += 1;
ret = composite_prove(p,tst,twt,twtsize);
if(ret) {
ret += 20;
goto err;
}
t = clock() - t;
printf("Commitment key length: %zu\n",comkey_len);
printf("Greyhound Pack members: %zu\n",p->l);
printf("Greyhound Pack size: %.2f KB\n",p->size);
printf("Greyhound Pack proving time: %.4fs\n",(double)t/CLOCKS_PER_SEC);
return 0;
err:
free_prncplstmnt(tst0);
free_statement(&tst[0]);
free_statement(&tst[1]);
free_witness(&twt[0]);
free_witness(&twt[1]);
free(p->pi[p->l]);
p->pi[p->l] = NULL;
free_composite(p);
return ret;
}
static int composite_verify(const composite *p, statement *tst) {
size_t i,j;
int ret;
i = 0;
for(j=1;j<p->l;j++) {
ret = reduce(&tst[i^1],p->pi[j],&tst[i]);
free_statement(&tst[i]);
i ^= 1;
if(ret) // projection too long or commitments not secure (1/2/3)
return ret + 4*j;
}
ret = verify(&tst[i],&p->owt);
if(ret)
return ret + 100;
return 0;
}
int composite_verify_principle(const composite *p, const prncplstmnt *st) {
int ret;
statement tst[2] = {};
clock_t t;
t = clock();
ret = principle_reduce(tst,p->pi[0],st);
if(ret) // projection too long or commitments not secure (1/2/3)
goto err;
ret = composite_verify(p,tst);
if(ret) {
ret += 10;
goto err;
}
t = clock() - t;
printf("Chihuahua Pack verification time: %.4fs\n",(double)t/CLOCKS_PER_SEC);
return 0;
err:
free_statement(&tst[0]);
free_statement(&tst[1]);
return ret;
}
int composite_verify_simple(const composite *p, const commitment *com, const smplstmnt *st) {
int ret;
statement tst[2] = {};
clock_t t;
t = clock();
ret = simple_reduce(tst,p->pi[0],com,st);
if(ret)
goto err;
ret = composite_verify(p,tst);
if(ret) {
ret += 10;
goto err;
}
t = clock() - t;
printf("Dachshund Pack verification time: %.4fs\n",(double)t/CLOCKS_PER_SEC);
return 0;
err:
free_statement(&tst[0]);
free_statement(&tst[1]);
return ret;
}
int composite_verify_polcom(const composite *p, const polcomprf *ppi) {
int ret;
prncplstmnt tst0[1] = {};
statement tst[2] = {};
clock_t t;
t = clock();
ret = polcom_reduce(tst0,ppi);
if(ret)
goto err;
ret = principle_reduce(tst,p->pi[0],tst0);
free_prncplstmnt(tst0);
if(ret) {
ret += 10;
goto err;
}
ret = composite_verify(p,tst);
if(ret) {
ret += 20;
goto err;
}
t = clock() - t;
printf("Greyhound Pack verification time: %.4fs\n",(double)t/CLOCKS_PER_SEC);
return 0;
err:
free_prncplstmnt(tst0);
free_statement(&tst[0]);
free_statement(&tst[1]);
return ret;
}