-
Notifications
You must be signed in to change notification settings - Fork 48
/
tree.c
584 lines (479 loc) · 16.8 KB
/
tree.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*! @file tree.c
* @brief This file has the tree implementation used to generate random seeds
* and commit to multiple values with a Merkle tree.
*
* This file is part of the reference implementation of the Picnic signature scheme.
* See the accompanying documentation for complete details.
*
* The code is provided under the MIT license, see LICENSE for
* more details.
* SPDX-License-Identifier: MIT
*/
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include "picnic_types.h"
#include "hash.h"
#include "picnic.h"
#include "picnic_impl.h"
#include "tree.h"
static int contains(size_t* list, size_t len, size_t value)
{
for (size_t i = 0; i < len; i++) {
if (list[i] == value) {
return 1;
}
}
return 0;
}
int exists(tree_t* tree, size_t i)
{
if (i >= tree->numNodes) {
return 0;
}
if (tree->exists[i]) {
return 1;
}
return 0;
}
tree_t* createTree(size_t numLeaves, size_t dataSize)
{
tree_t* tree = malloc(sizeof(tree_t));
tree->depth = ceil_log2(numLeaves) + 1;
tree->numNodes = ((1 << (tree->depth)) - 1) - ((1 << (tree->depth - 1)) - numLeaves); /* Num nodes in complete - number of missing leaves */
tree->numLeaves = numLeaves;
tree->dataSize = dataSize;
tree->nodes = malloc(tree->numNodes * sizeof(uint8_t*));
uint8_t* slab = calloc(tree->numNodes, dataSize);
for (size_t i = 0; i < tree->numNodes; i++) {
tree->nodes[i] = slab;
slab += dataSize;
}
tree->haveNode = calloc(tree->numNodes, 1);
/* Depending on the number of leaves, the tree may not be complete */
tree->exists = calloc(tree->numNodes, 1);
memset(tree->exists + tree->numNodes - tree->numLeaves, 1, tree->numLeaves); /* Set leaves */
for (int i = tree->numNodes - tree->numLeaves; i > 0; i--) {
if (exists(tree, 2 * i + 1) || exists(tree, 2 * i + 2) ) {
tree->exists[i] = 1;
}
}
tree->exists[0] = 1;
return tree;
}
void freeTree(tree_t* tree)
{
if (tree != NULL) {
free(tree->nodes[0]);
free(tree->nodes);
free(tree->haveNode);
free(tree->exists);
free(tree);
}
}
int isLeftChild(size_t node)
{
assert(node != 0);
return(node % 2 == 1);
}
int hasRightChild(tree_t* tree, size_t node)
{
return(2 * node + 2 < tree->numNodes && exists(tree, node));
}
int hasLeftChild(tree_t* tree, size_t node)
{
return(2 * node + 1 < tree->numNodes);
}
size_t getParent(size_t node)
{
assert(node != 0);
if (isLeftChild(node)) {
return (node - 1) / 2;
}
return (node - 2) / 2;
}
uint8_t** getLeaves(tree_t* tree)
{
return &tree->nodes[tree->numNodes - tree->numLeaves];
}
uint8_t* getLeaf(tree_t* tree, size_t leafIndex)
{
assert(leafIndex < tree->numLeaves);
size_t firstLeaf = tree->numNodes - tree->numLeaves;
return tree->nodes[firstLeaf + leafIndex];
}
void hashSeed(uint8_t* digest, const uint8_t* inputSeed, uint8_t* salt, uint8_t hashPrefix, size_t repIndex, size_t nodeIndex, paramset_t* params)
{
HashInstance ctx;
HashInit(&ctx, params, hashPrefix);
HashUpdate(&ctx, inputSeed, params->seedSizeBytes);
HashUpdate(&ctx, salt, params->saltSizeBytes);
HashUpdateIntLE(&ctx, (uint16_t)repIndex);
HashUpdateIntLE(&ctx, (uint16_t)nodeIndex);
HashFinal(&ctx);
HashSqueeze(&ctx, digest, 2 * params->seedSizeBytes);
}
void expandSeeds(tree_t* tree, uint8_t* salt, size_t repIndex, paramset_t* params)
{
uint8_t tmp[2*MAX_SEED_SIZE_BYTES];
/* Walk the tree, expanding seeds where possible. Compute children of
* non-leaf nodes. */
size_t lastNonLeaf = getParent(tree->numNodes - 1);
for (size_t i = 0; i <= lastNonLeaf; i++) {
if (!tree->haveNode[i]) {
continue;
}
hashSeed(tmp, tree->nodes[i], salt, HASH_PREFIX_1, repIndex, i, params);
if (!tree->haveNode[2 * i + 1]) {
/* left child = H_left(seed_i || salt || t || i) */
memcpy(tree->nodes[2 * i + 1], tmp, params->seedSizeBytes);
tree->haveNode[2 * i + 1] = 1;
}
/* The last non-leaf node will only have a left child when there are an odd number of leaves */
if (exists(tree, 2 * i + 2) && !tree->haveNode[2 * i + 2]) {
/* right child = H_right(seed_i || salt || t || i) */
memcpy(tree->nodes[2 * i + 2], tmp + params->seedSizeBytes, params->seedSizeBytes);
tree->haveNode[2 * i + 2] = 1;
}
}
}
tree_t* generateSeeds(size_t nSeeds, uint8_t* rootSeed, uint8_t* salt, size_t repIndex, paramset_t* params)
{
tree_t* tree = createTree(nSeeds, params->seedSizeBytes);
memcpy(tree->nodes[0], rootSeed, params->seedSizeBytes);
tree->haveNode[0] = 1;
expandSeeds(tree, salt, repIndex, params);
return tree;
}
int isLeafNode(tree_t* tree, size_t node)
{
return (2 * node + 1 >= tree->numNodes);
}
int hasSibling(tree_t* tree, size_t node)
{
if (!exists(tree, node)) {
return 0;
}
if (isLeftChild(node) && !exists(tree, node + 1)) {
return 0;
}
return 1;
}
size_t getSibling(tree_t* tree, size_t node)
{
assert(node < tree->numNodes);
assert(node != 0);
assert(hasSibling(tree, node));
if (isLeftChild(node)) {
if (node + 1 < tree->numNodes) {
return node + 1;
}
else {
assert(!"getSibling: request for node with not sibling");
return 0;
}
}
else {
return node - 1;
}
}
void printSeeds(uint8_t* seedsBuf, size_t seedLen, size_t numSeeds)
{
for (size_t i = 0; i < numSeeds; i++) {
printf("seed %lu", i);
printHex("", seedsBuf, seedLen);
seedsBuf += seedLen;
}
}
void printLeaves(tree_t* tree)
{
size_t firstLeaf = tree->numNodes - tree->numLeaves;
printSeeds(tree->nodes[firstLeaf], tree->dataSize, tree->numLeaves);
}
/* Returns the number of bytes written to output */
static size_t* getRevealedNodes(tree_t* tree, uint16_t* hideList, size_t hideListSize, size_t* outputSize)
{
/* Compute paths up from hideList to root, store as sets of nodes */
size_t pathLen = tree->depth - 1;
/* pathSets[i][0...hideListSize] stores the nodes in the path at depth i
* for each of the leaf nodes in hideListSize */
size_t** pathSets = malloc(pathLen * sizeof(size_t*));
size_t* slab = malloc(hideListSize * pathLen * sizeof(size_t));
for (size_t i = 0; i < pathLen; i++) {
pathSets[i] = slab;
slab += hideListSize;
}
/* Compute the paths back to the root */
for (size_t i = 0; i < hideListSize; i++) {
size_t pos = 0;
size_t node = hideList[i] + (tree->numNodes - tree->numLeaves); /* input lists leaf indexes, translate to nodes */
pathSets[pos][i] = node;
pos++;
while ( (node = getParent(node)) != 0 ) {
pathSets[pos][i] = node;
pos++;
}
}
/* Determine seeds to reveal */
size_t* revealed = malloc(tree->numLeaves * sizeof(size_t));
size_t revealedPos = 0;
for (size_t d = 0; d < pathLen; d++) {
for (size_t i = 0; i < hideListSize; i++) {
if (!hasSibling(tree, pathSets[d][i])) {
continue;
}
size_t sibling = getSibling(tree, pathSets[d][i]);
if (!contains(pathSets[d], hideListSize, sibling )) {
// Determine the seed to reveal
while(!hasRightChild(tree, sibling) && !isLeafNode(tree, sibling)) {
sibling = 2 * sibling + 1; // sibling = leftChild(sibling)
}
// Only reveal if we haven't already
if (!contains(revealed, revealedPos, sibling)) {
revealed[revealedPos] = sibling;
revealedPos++;
}
}
}
}
free(pathSets[0]);
free(pathSets);
*outputSize = revealedPos;
return revealed;
}
size_t revealSeedsSize(size_t numNodes, uint16_t* hideList, size_t hideListSize, paramset_t* params)
{
tree_t* tree = createTree(numNodes, params->seedSizeBytes);
size_t numNodesRevealed = 0;
size_t* revealed = getRevealedNodes(tree, hideList, hideListSize, &numNodesRevealed);
freeTree(tree);
free(revealed);
return numNodesRevealed * params->seedSizeBytes;
}
size_t revealSeeds(tree_t* tree, uint16_t* hideList, size_t hideListSize, uint8_t* output, size_t outputSize, paramset_t* params)
{
uint8_t* outputBase = output;
size_t revealedSize = 0;
if (outputSize > INT_MAX) {
return -1;
}
int outLen = (int)outputSize;
size_t* revealed = getRevealedNodes(tree, hideList, hideListSize, &revealedSize);
for (size_t i = 0; i < revealedSize; i++) {
outLen -= params->seedSizeBytes;
if (outLen < 0) {
assert(!"Insufficient sized buffer provided to revealSeeds");
free(revealed);
return 0;
}
memcpy(output, tree->nodes[revealed[i]], params->seedSizeBytes);
output += params->seedSizeBytes;
}
free(revealed);
return output - outputBase;
}
int reconstructSeeds(tree_t* tree, uint16_t* hideList, size_t hideListSize,
uint8_t* input, size_t inputLen, uint8_t* salt, size_t repIndex, paramset_t* params)
{
int ret = 0;
if (inputLen > INT_MAX) {
return -1;
}
int inLen = (int)inputLen;
size_t revealedSize = 0;
size_t* revealed = getRevealedNodes(tree, hideList, hideListSize, &revealedSize);
for (size_t i = 0; i < revealedSize; i++) {
inLen -= params->seedSizeBytes;
if (inLen < 0) {
ret = -1;
goto Exit;
}
memcpy(tree->nodes[revealed[i]], input, params->seedSizeBytes);
tree->haveNode[revealed[i]] = 1;
input += params->seedSizeBytes;
}
expandSeeds(tree, salt, repIndex, params);
Exit:
free(revealed);
return ret;
}
static void computeParentHash(tree_t* tree, size_t child, uint8_t* salt, paramset_t* params)
{
if (!exists(tree, child)) {
return;
}
size_t parent = getParent(child);
if (tree->haveNode[parent]) {
return;
}
/* Compute the hash for parent, if we have everything */
if (!tree->haveNode[2 * parent + 1]) {
return;
}
if (exists(tree, 2 * parent + 2) && !tree->haveNode[2 * parent + 2]) {
return;
}
/* Compute parent data = H(left child data || [right child data] || salt || parent idx) */
HashInstance ctx;
HashInit(&ctx, params, HASH_PREFIX_3);
HashUpdate(&ctx, tree->nodes[2 * parent + 1], params->digestSizeBytes);
if (hasRightChild(tree, parent)) {
/* One node may not have a right child when there's an odd number of leaves */
HashUpdate(&ctx, tree->nodes[2 * parent + 2], params->digestSizeBytes);
}
HashUpdate(&ctx, salt, params->saltSizeBytes);
HashUpdateIntLE(&ctx, (uint16_t)parent);
HashFinal(&ctx);
HashSqueeze(&ctx, tree->nodes[parent], params->digestSizeBytes);
tree->haveNode[parent] = 1;
}
/* Create a Merkle tree by hashing up all nodes.
* leafData must have length tree->numNodes, but some may be NULL. */
void buildMerkleTree(tree_t* tree, uint8_t** leafData, uint8_t* salt, paramset_t* params)
{
size_t firstLeaf = tree->numNodes - tree->numLeaves;
/* Copy data to the leaves. The actual data being committed to has already been
* hashed, according to the spec. */
for (size_t i = 0; i < tree->numLeaves; i++) {
if (leafData[i] != NULL) {
memcpy(tree->nodes[firstLeaf + i], leafData[i], tree->dataSize);
tree->haveNode[firstLeaf + i] = 1;
}
}
/* Starting at the leaves, work up the tree, computing the hashes for intermediate nodes */
for (int i = (int)tree->numNodes; i > 0; i--) {
computeParentHash(tree, i, salt, params);
}
}
/* Note that we never output the root node */
static size_t* getRevealedMerkleNodes(tree_t* tree, uint16_t* missingLeaves,
size_t missingLeavesSize, size_t* outputSize)
{
size_t firstLeaf = tree->numNodes - tree->numLeaves;
uint8_t* missingNodes = calloc(tree->numNodes, 1);
/* Mark leaves that are missing */
for (size_t i = 0; i < missingLeavesSize; i++) {
missingNodes[firstLeaf + missingLeaves[i]] = 1;
}
/* For the nonleaf nodes, if both leaves are missing, mark it as missing too */
int lastNonLeaf = getParent(tree->numNodes - 1);
for (int i = lastNonLeaf; i > 0; i--) {
if (!exists(tree, i)) {
continue;
}
if (exists(tree, 2 * i + 2)) {
if (missingNodes[2 * i + 1] && missingNodes[2 * i + 2]) {
missingNodes[i] = 1;
}
}
else {
if (missingNodes[2 * i + 1]) {
missingNodes[i] = 1;
}
}
}
/* For each missing leaf node, add the highest missing node on the path
* back to the root to the set to be revealed */
size_t* revealed = malloc(tree->numLeaves * sizeof(size_t));
size_t pos = 0;
for (size_t i = 0; i < missingLeavesSize; i++) {
size_t node = missingLeaves[i] + firstLeaf; /* input is leaf indexes, translate to nodes */
do {
if (!missingNodes[getParent(node)]) {
if (!contains(revealed, pos, node)) {
revealed[pos] = node;
pos++;
}
break;
}
} while ((node = getParent(node)) != 0);
}
free(missingNodes);
*outputSize = pos;
return revealed;
}
size_t openMerkleTreeSize(size_t numNodes, uint16_t* missingLeaves, size_t missingLeavesSize, paramset_t* params)
{
tree_t* tree = createTree(numNodes, params->digestSizeBytes);
size_t revealedSize = 0;
size_t* revealed = getRevealedMerkleNodes(tree, missingLeaves, missingLeavesSize, &revealedSize);
freeTree(tree);
free(revealed);
return revealedSize * params->digestSizeBytes;
}
/* Serialze the missing nodes that the verifier will require to check commitments for non-missing leaves */
uint8_t* openMerkleTree(tree_t* tree, uint16_t* missingLeaves, size_t missingLeavesSize, size_t* outputSizeBytes)
{
size_t revealedSize = 0;
size_t* revealed = getRevealedMerkleNodes(tree, missingLeaves, missingLeavesSize, &revealedSize);
/* Serialize output */
*outputSizeBytes = revealedSize * tree->dataSize;
uint8_t* output = malloc(*outputSizeBytes);
uint8_t* outputBase = output;
for (size_t i = 0; i < revealedSize; i++) {
memcpy(output, tree->nodes[revealed[i]], tree->dataSize);
output += tree->dataSize;
}
free(revealed);
return outputBase;
}
/* addMerkleNodes: deserialize and add the data for nodes provided by the committer */
int addMerkleNodes(tree_t* tree, uint16_t* missingLeaves, size_t missingLeavesSize, uint8_t* input, size_t inputSize)
{
int ret = 0;
assert(missingLeavesSize < tree->numLeaves);
if (inputSize > INT_MAX) {
return -1;
}
int intLen = (int)inputSize;
size_t revealedSize = 0;
size_t* revealed = getRevealedMerkleNodes(tree, missingLeaves, missingLeavesSize, &revealedSize);
assert(!contains(revealed, revealedSize, 0));
/* Deserialize input */
for (size_t i = 0; i < revealedSize; i++) {
intLen -= tree->dataSize;
if (intLen < 0) {
ret = -1;
goto Exit;
}
memcpy(tree->nodes[revealed[i]], input, tree->dataSize);
input += tree->dataSize;
tree->haveNode[revealed[i]] = 1;
}
if (intLen != 0) {
ret = -1;
goto Exit;
}
Exit:
free(revealed);
return ret;
}
/* verifyMerkleTree: verify for each leaf that is set */
int verifyMerkleTree(tree_t* tree, /* uint16_t* missingLeaves, size_t missingLeavesSize, */
uint8_t** leafData, uint8_t* salt, paramset_t* params)
{
size_t firstLeaf = tree->numNodes - tree->numLeaves;
/* Copy the leaf data, where we have it. The actual data being committed to has already been
* hashed, according to the spec. */
for (size_t i = 0; i < tree->numLeaves; i++) {
if (leafData[i] != NULL) {
if (tree->haveNode[firstLeaf + i] == 1) {
return -1; /* A leaf was assigned from the prover for a node we've recomputed */
}
if (leafData[i] != NULL) {
memcpy(tree->nodes[firstLeaf + i], leafData[i], tree->dataSize);
tree->haveNode[firstLeaf + i] = 1;
}
}
}
/* At this point the tree has some of the leaves, and some intermediate nodes
* Work up the tree, computing all nodes we don't have that are missing. */
for (int i = (int)tree->numNodes; i > 0; i--) {
computeParentHash(tree, i, salt, params);
}
/* Fail if the root was not computed. */
if (!tree->haveNode[0]) {
return -1;
}
return 0;
}