-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrsign.c
416 lines (341 loc) · 11.2 KB
/
rsign.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
#include "rsign.h"
#include "seedtree.h"
static inline
uint64_t rdtsc(){
unsigned int lo,hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
}
#define TIC printf("\n"); uint64_t cl = rdtsc();
#define TOC(A) printf("%s cycles = %lu \n",#A ,rdtsc() - cl); cl = rdtsc();
uint64_t restarts = 0;
uint64_t restarts2 = 0;
void keygen(unsigned char *pk, unsigned char *sk){
GRPELTS1 s;
init_grpelt(s);
RAND_bytes(sk,SEED_BYTES);
sample_S1(s,sk);
public_key *X = (public_key *) pk;
derive_pk(X,s);
clear_grpelt(s);
}
#ifdef BG
void commit(const XELT *R, const unsigned char *randomness, const unsigned char *salt, unsigned char *commitment){
unsigned char buf[512 + 3*SEED_BYTES];
memcpy(buf+512,randomness,SEED_BYTES);
memcpy(buf+512+SEED_BYTES,salt,2*SEED_BYTES);
for (int i = 0; i < K; ++i)
{
for (int j = 0; j < 128; ++j)
{
buf[i*128 + j] = (*R).high.vec[i].coeffs[j] || ((*R).high.vec[i].coeffs[j] << 4);
}
}
HASH(buf, 512 + SEED_BYTES, commitment);
}
#else
void commit(const XELT *R, const unsigned char *randomness, const unsigned char *salt, unsigned char *commitment){
unsigned char buf[sizeof(XELT) + 3*SEED_BYTES];
memcpy(buf,(const unsigned char *)R,sizeof(XELT));
memcpy(buf+sizeof(XELT),randomness,SEED_BYTES);
memcpy(buf+sizeof(XELT)+SEED_BYTES,salt,2*SEED_BYTES);
HASH(buf, sizeof(XELT) + SEED_BYTES, commitment);
}
#endif
void build_tree_and_path(const unsigned char *commitments_in, int logN, int64_t I, unsigned char * root, unsigned char *path){
uint64_t rings_round_up = ((uint64_t)1)<<logN;
unsigned char commitments[rings_round_up*HASH_BYTES];
memcpy(commitments,commitments_in,rings_round_up*HASH_BYTES);
int64_t *intpath = (int64_t *) path;
unsigned char temp[HASH_BYTES];
if(I >= 0){
memset(path,0,logN*HASH_BYTES);
}
for (int depth = logN-1; depth >= 0; --depth)
{
if (I >= 0){
for (int64_t i = 0; i < ((uint64_t) 1) << (depth+1); ++i)
{
int64_t mask = ((I^1) - i) | (i- (I^1));
mask >>=63;
mask ^= 0xffffffffffffffff;
intpath[4*depth] ^= (mask & *((int64_t *) (commitments + i*HASH_BYTES )));
intpath[4*depth+1] ^= (mask & *((int64_t *) (commitments + i*HASH_BYTES + 8 )));
intpath[4*depth+2] ^= (mask & *((int64_t *) (commitments + i*HASH_BYTES + 16)));
intpath[4*depth+3] ^= (mask & *((int64_t *) (commitments + i*HASH_BYTES + 24)));
}
// the above just does the following in constant time:
// memcpy(path + depth*HASH_BYTES, commitments + (I^1)*HASH_BYTES, HASH_BYTES);
I /= 2;
}
for (int i = 0; i < ((uint64_t) 1) << depth; ++i)
{
if(memcmp(commitments + HASH_BYTES*i*2,commitments + (2*i+1)*HASH_BYTES, HASH_BYTES ) > 0){
memcpy(temp, commitments + HASH_BYTES*i*2, HASH_BYTES);
memcpy(commitments + HASH_BYTES*i*2, commitments + HASH_BYTES*(i*2+1), HASH_BYTES);
memcpy(commitments + HASH_BYTES*(i*2+1), temp, HASH_BYTES);
}
HASH(commitments + 2*i*HASH_BYTES,2*HASH_BYTES, commitments + i*HASH_BYTES);
}
}
memcpy(root,commitments,HASH_BYTES);
}
void reconstruct_root(const unsigned char *data, const unsigned char *path, int logN, unsigned char *root){
unsigned char current[2*HASH_BYTES];
unsigned char temp[HASH_BYTES];
memcpy(current, data, HASH_BYTES);
for (int depth = logN-1; depth >= 0; --depth)
{
memcpy(current+HASH_BYTES, path+depth*HASH_BYTES, HASH_BYTES);
if(memcmp(current,current + HASH_BYTES, HASH_BYTES ) > 0){
memcpy(temp, current, HASH_BYTES);
memcpy(current, current + HASH_BYTES, HASH_BYTES);
memcpy(current + HASH_BYTES, temp, HASH_BYTES);
}
HASH(current,2*HASH_BYTES, current);
}
memcpy(root,current,HASH_BYTES);
}
#define SHAKE128_RATE 168
void derive_challenge(const unsigned char *challenge_seed, unsigned char *challenge){
memset(challenge,1,EXECUTIONS);
int zeros = 0;
int pos = SHAKE128_RATE;
unsigned char inbuf[SEED_BYTES + 4];
uint16_t out_buf[SHAKE128_RATE/2];
memcpy(inbuf,challenge_seed,SEED_BYTES);
uint32_t *ctr = (uint32_t *) (inbuf + SEED_BYTES);
(*ctr) = 0;
while(zeros < ZEROS){
if (pos >= SHAKE128_RATE/2){
EXPAND(inbuf,SEED_BYTES+4,(unsigned char *) out_buf, SHAKE128_RATE);
pos = 0;
(*ctr)++;
}
out_buf[pos] &= EXECUTIONS_MASK;
if (out_buf[pos] < EXECUTIONS && challenge[out_buf[pos]] == 1){
challenge[out_buf[pos]] = 0;
zeros += 1;
}
pos += 1;
}
}
#ifdef BG
int bg_check(XELT *X){
if (polyveck_chknorm(&(*X).low, (1<<(D-1)) - ETA) ){
return 0;
}
polyveck temp;
temp = (*X).all;
//printf("%d \n", temp.vec[0].coeffs[0] );
for (int i = 0; i < K; ++i)
{
for (int j = 0; j < N; ++j)
{
temp.vec[i].coeffs[j] += (Q/2);
}
}
polyveck_freeze(&temp);
if (polyveck_chknorm(&temp, (Q-1)/2 - ETA ) ) {
restarts2 ++;
return 0;
}
//printf("pass \n");
return 1;
}
#endif
int log_round_up(int64_t a){
int logN = 0;
while ((((int64_t) 1) << logN) < a ){
logN ++;
}
return logN;
}
int rsign(const unsigned char *sk, const int64_t I, const unsigned char *pks, const int64_t rings, const unsigned char *m, uint64_t mlen, unsigned char *sig, uint64_t *sig_len){
if (I >= rings || rings > (((uint64_t) 1) << 32))
return -1;
int logN = log_round_up(rings);
uint64_t rings_round_up = (((uint64_t)1) << logN);
GRPELTS2 *r = aligned_alloc(32, sizeof(GRPELTS2)*EXECUTIONS);
for (int i = 0; i < EXECUTIONS; ++i)
{
init_grpelt(r[i]);
}
unsigned char *seed_tree = malloc((2*EXECUTIONS-1)*SEED_BYTES);
unsigned char *seeds = seed_tree + (EXECUTIONS-1)*SEED_BYTES;
#define BUF_LEN (SEED_BYTES*(rings+2))
unsigned char seedbuf[SEED_BUF_BYTES];
unsigned char buf[BUF_LEN];
unsigned char commitments[HASH_BYTES*rings_round_up];
unsigned char *commitment_randomness = malloc(EXECUTIONS*SEED_BYTES);
XELT R;
unsigned char *roots = malloc(HASH_BYTES*(EXECUTIONS+2));
unsigned char *paths = malloc(HASH_BYTES*EXECUTIONS*logN);
// generate response
GRPELTS2 z;
GRPELTS1 s;
init_grpelt(z);
init_grpelt(s);
sample_S1(s,sk);
// choose salt
RAND_bytes(RSIG_SALT(sig),HASH_BYTES);
// copy salt
memcpy(roots + (EXECUTIONS+1)*HASH_BYTES, RSIG_SALT(sig), HASH_BYTES);
memcpy(seedbuf + SEED_BYTES, RSIG_SALT(sig), HASH_BYTES);
uint32_t *ctr = (uint32_t *) (seedbuf + HASH_BYTES + SEED_BYTES);
// pick random seeds
restart: generate_seed_tree(seed_tree,EXECUTIONS,RSIG_SALT(sig));
// hash message
HASH(m,mlen,roots + EXECUTIONS*HASH_BYTES);
for (int i = 0; i < EXECUTIONS; ++i)
{
// generate commitment randomness and r
memcpy(seedbuf, seeds + i*SEED_BYTES, SEED_BYTES);
(*ctr) = EXECUTIONS + i;
EXPAND(seedbuf, SEED_BUF_BYTES, buf, BUF_LEN);
// sample r
sample_S2_with_seed(buf + SEED_BYTES*rings, r[i]);
PREP_GRPELT pg;
do_half_action(&pg,r[i]);
// TODO: do this without accessing secret indices !!
memcpy(commitment_randomness + i*SEED_BYTES , buf + I*SEED_BYTES , SEED_BYTES);
// compute R_i and commitments
for (int j = 0; j < rings; ++j)
{
finish_action(&R,(public_key*) (pks + j*sizeof(public_key)), &pg);
commit(&R,buf + j*SEED_BYTES, RSIG_SALT(sig), commitments + j*HASH_BYTES);
}
// generate dummy commitments
EXPAND(buf + SEED_BYTES * (rings +1), SEED_BYTES, commitments + rings*HASH_BYTES, (rings_round_up-rings)*HASH_BYTES);
build_tree_and_path(commitments, logN, I, roots + i*HASH_BYTES, paths + i*HASH_BYTES*logN );
}
// generate challenge
EXPAND(roots, HASH_BYTES*(EXECUTIONS+2), RSIG_CHALLENGE(sig), SEED_BYTES);
unsigned char *challenge = malloc(EXECUTIONS);
derive_challenge(RSIG_CHALLENGE(sig),challenge);
int zeros = 0;
int ones = 0;
for (int i = 0; i < EXECUTIONS; ++i)
{
if (challenge[i] == 0)
{
// compute and pack z in signature
add(z, s, r[i]);
if( !is_in_S3(z) ){
restarts += 1;
goto restart;
}
#ifdef BG
XELT W;
do_action(&W,&X0,z);
if( !bg_check(&W) ){
restarts += 1;
goto restart;
}
#endif
pack_S3(RSIG_Z(sig) + zeros*S3_BYTES, z);
// copy commitment randomess to signature
memcpy(RSIG_COMMITMENT_RANDOMNESS(sig) + zeros*SEED_BYTES, commitment_randomness + i*SEED_BYTES, SEED_BYTES);
// copy Merkle tree path to signature
memcpy(RSIG_PATHS(sig) + zeros*logN*HASH_BYTES, paths + i*HASH_BYTES*logN, HASH_BYTES*logN);
zeros++;
}
}
release_seeds(seed_tree, EXECUTIONS, challenge, RSIG_SEEDS(sig,logN) , sig_len );
(*sig_len) *= SEED_BYTES;
(*sig_len) += RSIG_SEEDS(0,logN);
for (int i = 0; i < EXECUTIONS; ++i)
{
clear_grpelt(r[i]);
}
free(r);
free(seed_tree);
free(commitment_randomness);
free(roots);
free(paths);
free(challenge);
clear_grpelt(z);
clear_grpelt(s);
}
int rverify(const unsigned char *pks, const int64_t rings, const unsigned char *m, uint64_t mlen, const unsigned char *sig){
if (rings > (((uint64_t) 1) << 32))
return -1;
int valid = 0;
int logN = log_round_up(rings);
uint64_t rings_round_up = (((uint64_t)1) << logN);
// expand challenge
unsigned char challenge[EXECUTIONS];
derive_challenge(RSIG_CHALLENGE(sig),challenge);
// derive seeds
unsigned char seed_tree[(2*EXECUTIONS-1)*SEED_BYTES];
unsigned char *seeds = seed_tree + (EXECUTIONS-1)*SEED_BYTES;
uint64_t nodes_used;
fill_down(seed_tree,EXECUTIONS, challenge, RSIG_SEEDS(sig,logN), &nodes_used, RSIG_SALT(sig));
// reconstruct roots
unsigned char roots[(EXECUTIONS+2)*HASH_BYTES];
// hash message
HASH(m,mlen,roots + EXECUTIONS*HASH_BYTES)
// copy salt
memcpy(roots + (EXECUTIONS+1)*HASH_BYTES, RSIG_SALT(sig), HASH_BYTES);
int zeros = 0;
int ones = 0;
GRPELTS2 r,z;
XELT R;
init_grpelt(r);
init_grpelt(z);
unsigned char buf[BUF_LEN];
unsigned char seedbuf[SEED_BUF_BYTES];
memcpy(seedbuf + SEED_BYTES, RSIG_SALT(sig) , HASH_BYTES);
uint32_t *ctr = (uint32_t *) (seedbuf + SEED_BYTES + HASH_BYTES);
unsigned char commitments[HASH_BYTES*rings_round_up];
for (int i = 0; i < EXECUTIONS; ++i)
{
if (challenge[i] == 0){
// unpack z
unpack_S3(RSIG_Z(sig) + zeros*S3_BYTES, z);
if(!is_in_S3(z)){
printf("z not in S3! \n");
valid = -1;
break;
}
// compute z*X_0
do_action(&R,&X0,z);
// commit to it
commit(&R,RSIG_COMMITMENT_RANDOMNESS(sig) + SEED_BYTES*zeros, RSIG_SALT(sig), commitments);
// reconstruct root
reconstruct_root(commitments, RSIG_PATHS(sig) + zeros*logN*HASH_BYTES, logN, roots + i*HASH_BYTES);
zeros++;
}
else{
// generate commitment randomness and r
memcpy(seedbuf, seeds + i*SEED_BYTES, SEED_BYTES);
(*ctr) = EXECUTIONS + i;
EXPAND(seedbuf, SEED_BUF_BYTES, buf, BUF_LEN);
// sample r
sample_S2_with_seed(buf + SEED_BYTES*rings, r);
PREP_GRPELT pg;
do_half_action(&pg,r);
// compute R_i and commitments
for (int j = 0; j < rings; ++j)
{
finish_action(&R, (public_key*) (pks + j*sizeof(public_key)), &pg);
commit(&R,buf + j*SEED_BYTES, RSIG_SALT(sig), commitments + j*HASH_BYTES);
}
// generate dummy commitments
EXPAND(buf + SEED_BYTES * (rings +1), SEED_BYTES, commitments + rings*HASH_BYTES, (rings_round_up-rings)*HASH_BYTES);
// compute root
build_tree_and_path(commitments, logN, -1 , roots + i*HASH_BYTES, NULL );
ones++;
}
}
clear_grpelt(r);
clear_grpelt(z);
// check hash of roots
unsigned char challenge_seed[SEED_BYTES];
EXPAND(roots, HASH_BYTES*(EXECUTIONS+2), challenge_seed, SEED_BYTES);
if(memcmp(RSIG_CHALLENGE(sig) , challenge_seed, SEED_BYTES) != 0){
printf("challenge seed does not match! \n");
return -1;
}
return valid;
}