-
Notifications
You must be signed in to change notification settings - Fork 5
/
vc.c
330 lines (285 loc) · 11.6 KB
/
vc.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
/*
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "vc.h"
#include "random_oracle.h"
#include "compat.h"
#include "aes.h"
#include "instances.h"
#include <assert.h>
#include <string.h>
typedef struct tree_t {
uint8_t* nodes; /* The data for each node */
size_t numNodes; /* The total number of nodes in the tree */
size_t numLeaves; /* The total number of leaves in the tree */
} tree_t;
#define NODE(tree, node, lambda_bytes) (&(tree).nodes[(node) * (lambda_bytes)])
static ATTR_CONST int is_left_child(size_t node) {
assert(node != 0);
return (node % 2 == 1);
}
static tree_t create_tree(const faest_paramset_t* params, unsigned int depth) {
tree_t tree;
uint32_t lambdaBytes = params->faest_param.lambda / 8;
tree.numNodes = getBinaryTreeNodeCount(depth);
tree.numLeaves = ((size_t)1) << depth;
tree.nodes = calloc(tree.numNodes, lambdaBytes);
return tree;
}
static ATTR_CONST size_t get_parent(size_t node) {
assert(node != 0);
if (is_left_child(node)) {
return (node - 1) / 2;
}
return (node - 2) / 2;
}
static void expand_seeds(tree_t* tree, const uint8_t* iv, const faest_paramset_t* params) {
const unsigned int lambda_bytes = params->faest_param.lambda / 8;
/* Walk the tree, expanding seeds where possible. Compute children of
* non-leaf nodes. */
size_t lastNonLeaf = get_parent(tree->numNodes - 1);
// for scan build
assert(2 * lastNonLeaf + 2 < tree->numNodes);
for (size_t i = 0; i <= lastNonLeaf; i++) {
// the nodes are located other in memory consecutively
prg(NODE(*tree, i, lambda_bytes), iv, NODE(*tree, 2 * i + 1, lambda_bytes),
params->faest_param.lambda, lambda_bytes * 2);
}
}
static tree_t generate_seeds(const uint8_t* rootSeed, const uint8_t* iv,
const faest_paramset_t* params, unsigned int depth) {
uint32_t lambdaBytes = params->faest_param.lambda / 8;
tree_t tree = create_tree(params, depth);
memcpy(NODE(tree, 0, lambdaBytes), rootSeed, lambdaBytes);
expand_seeds(&tree, iv, params);
return tree;
}
/* Gets how many nodes will be there in the tree in total including root node */
uint64_t getBinaryTreeNodeCount(unsigned int depth) {
return (1 << (depth + 1)) - 1;
}
/* Calculates the flat array index of the binary tree position */
uint64_t getNodeIndex(uint64_t depth, uint64_t levelIndex) {
if (depth == 0) {
return 0;
}
return (((2 << (depth - 1)) - 2) + (levelIndex + 1));
}
/* Gets the bit string of a node according to its position in the binary tree */
/* idx -> 2 -> {0,1},, Little Endian */
int BitDec(unsigned int leafIndex, unsigned int depth, uint8_t* out) {
if (leafIndex >= (1u << depth)) {
return -1;
}
for (unsigned int j = 0; j < depth; j++, leafIndex /= 2) {
out[j] = leafIndex % 2;
}
return 1;
}
unsigned int NumRec(unsigned int depth, const uint8_t* bi) {
unsigned int out = 0;
for (unsigned int i = 0; i < depth; i++) {
out += ((unsigned int)bi[i]) << i;
}
return out;
}
void vector_commitment(const uint8_t* rootKey, const uint8_t* iv, const faest_paramset_t* params,
uint32_t lambda, vec_com_t* vecCom, uint32_t depth) {
const unsigned int lambdaBytes = lambda / 8;
const unsigned int numVoleInstances = 1 << depth;
// Generating the tree
tree_t tree = generate_seeds(rootKey, iv, params, depth);
// Initialzing stuff
vecCom->h = malloc(lambdaBytes * 2);
vecCom->com = malloc(numVoleInstances * lambdaBytes * 2);
vecCom->sd = malloc(numVoleInstances * lambdaBytes);
// Step: 1..3
vecCom->k = NODE(tree, 0, lambdaBytes);
// Step: 4..5
const size_t base_index = tree.numNodes - tree.numLeaves;
unsigned int i = 0;
// compute commitments for 4 instances in parallel
for (; i < numVoleInstances / 4 * 4; i += 4) {
H0_context_x4_t h0_ctx;
H0_x4_init(&h0_ctx, lambda);
H0_x4_update(&h0_ctx, NODE(tree, base_index + i, lambdaBytes),
NODE(tree, base_index + i + 1, lambdaBytes),
NODE(tree, base_index + i + 2, lambdaBytes),
NODE(tree, base_index + i + 3, lambdaBytes), lambdaBytes);
H0_x4_update(&h0_ctx, iv, iv, iv, iv, IV_SIZE);
H0_x4_final(&h0_ctx, vecCom->sd + i * lambdaBytes, vecCom->sd + (i + 1) * lambdaBytes,
vecCom->sd + (i + 2) * lambdaBytes, vecCom->sd + (i + 3) * lambdaBytes, lambdaBytes,
vecCom->com + i * lambdaBytes * 2, vecCom->com + (i + 1) * lambdaBytes * 2,
vecCom->com + (i + 2) * lambdaBytes * 2, vecCom->com + (i + 3) * lambdaBytes * 2,
(lambdaBytes * 2));
}
// compute commitments for remaining instances
for (; i < numVoleInstances; i++) {
H0_context_t h0_ctx;
H0_init(&h0_ctx, lambda);
H0_update(&h0_ctx, NODE(tree, base_index + i, lambdaBytes), lambdaBytes);
H0_update(&h0_ctx, iv, 16);
H0_final(&h0_ctx, vecCom->sd + (i * lambdaBytes), lambdaBytes,
vecCom->com + (i * (lambdaBytes * 2)), (lambdaBytes * 2));
}
tree.nodes = NULL;
// Step: 6
H1_context_t h1_ctx;
H1_init(&h1_ctx, lambda);
for (unsigned int j = 0; j < numVoleInstances; j++) {
H1_update(&h1_ctx, vecCom->com + (j * (lambdaBytes * 2)), (lambdaBytes * 2));
}
H1_final(&h1_ctx, vecCom->h, lambdaBytes * 2);
}
void vector_open(const uint8_t* k, const uint8_t* com, const uint8_t* b, uint8_t* cop,
uint8_t* com_j, uint32_t depth, uint32_t lambdaBytes) {
// Step: 1
uint64_t leafIndex = NumRec(depth, b);
// Step: 3..6
uint32_t a = 0;
for (uint32_t i = 0; i < depth; i++) {
memcpy(cop + (lambdaBytes * i),
k + (lambdaBytes * getNodeIndex(i + 1, (2 * a) + !b[depth - 1 - i])), lambdaBytes);
a = (2 * a) + b[depth - 1 - i];
}
// Step: 7
memcpy(com_j, com + (leafIndex * lambdaBytes * 2), lambdaBytes * 2);
}
void vector_reconstruction(const uint8_t* iv, const uint8_t* cop, const uint8_t* com_j,
const uint8_t* b, uint32_t lambda, uint32_t depth,
vec_com_rec_t* vecComRec) {
// Initializing
const unsigned int lambdaBytes = lambda / 8;
const unsigned int numVoleInstances = 1 << depth;
const uint64_t leafIndex = NumRec(depth, b);
// Step: 3..9
uint32_t a = 0;
for (uint32_t i = 1; i <= depth; i++) {
memcpy(vecComRec->k + (lambdaBytes * getNodeIndex(i, 2 * a + !b[depth - i])),
cop + (lambdaBytes * (i - 1)), lambdaBytes);
memset(vecComRec->k + (lambdaBytes * getNodeIndex(i, 2 * a + b[depth - i])), 0, lambdaBytes);
const uint32_t current_depth = (1 << (i - 1));
for (uint32_t j = 0; j < current_depth; j++) {
if (j == a) {
continue;
}
uint8_t out[2 * MAX_LAMBDA_BYTES];
prg(vecComRec->k + (lambdaBytes * getNodeIndex(i - 1, j)), iv, out, lambda, lambdaBytes * 2);
memcpy(vecComRec->k + (lambdaBytes * getNodeIndex(i, 2 * j)), out, lambdaBytes);
memcpy(vecComRec->k + (lambdaBytes * getNodeIndex(i, (2 * j) + 1)), out + lambdaBytes,
lambdaBytes);
}
a = a * 2 + b[depth - i];
}
// Step: 10..11
unsigned int j = 0;
// reconstruct commitments for 4 instances in parallel
for (; j < leafIndex / 4 * 4; j += 4) {
H0_context_x4_t h0_ctx;
H0_x4_init(&h0_ctx, lambda);
H0_x4_update(&h0_ctx, vecComRec->k + (getNodeIndex(depth, j) * lambdaBytes),
vecComRec->k + (getNodeIndex(depth, j + 1) * lambdaBytes),
vecComRec->k + (getNodeIndex(depth, j + 2) * lambdaBytes),
vecComRec->k + (getNodeIndex(depth, j + 3) * lambdaBytes), lambdaBytes);
H0_x4_update(&h0_ctx, iv, iv, iv, iv, IV_SIZE);
H0_x4_final(&h0_ctx, vecComRec->s + j * lambdaBytes, vecComRec->s + (j + 1) * lambdaBytes,
vecComRec->s + (j + 2) * lambdaBytes, vecComRec->s + (j + 3) * lambdaBytes,
lambdaBytes, vecComRec->com + j * lambdaBytes * 2,
vecComRec->com + (j + 1) * lambdaBytes * 2,
vecComRec->com + (j + 2) * lambdaBytes * 2,
vecComRec->com + (j + 3) * lambdaBytes * 2, lambdaBytes * 2);
}
// reconstruct commitments up until the leafIndex
for (; j < leafIndex; ++j) {
H0_context_t h0_ctx;
H0_init(&h0_ctx, lambda);
H0_update(&h0_ctx, vecComRec->k + getNodeIndex(depth, j) * lambdaBytes, lambdaBytes);
H0_update(&h0_ctx, iv, IV_SIZE);
H0_final(&h0_ctx, vecComRec->s + j * lambdaBytes, lambdaBytes,
vecComRec->com + j * lambdaBytes * 2, lambdaBytes * 2);
}
// skip leafIndex
++j;
// reconstruct until index is divisible by 4 again
for (; j < numVoleInstances && j % 4; ++j) {
H0_context_t h0_ctx;
H0_init(&h0_ctx, lambda);
H0_update(&h0_ctx, vecComRec->k + getNodeIndex(depth, j) * lambdaBytes, lambdaBytes);
H0_update(&h0_ctx, iv, IV_SIZE);
H0_final(&h0_ctx, vecComRec->s + j * lambdaBytes, lambdaBytes,
vecComRec->com + j * lambdaBytes * 2, lambdaBytes * 2);
}
// reconstruct 4 instances in parallel
for (; j < numVoleInstances / 4 * 4; j += 4) {
H0_context_x4_t h0_ctx;
H0_x4_init(&h0_ctx, lambda);
H0_x4_update(&h0_ctx, vecComRec->k + (getNodeIndex(depth, j) * lambdaBytes),
vecComRec->k + (getNodeIndex(depth, j + 1) * lambdaBytes),
vecComRec->k + (getNodeIndex(depth, j + 2) * lambdaBytes),
vecComRec->k + (getNodeIndex(depth, j + 3) * lambdaBytes), lambdaBytes);
H0_x4_update(&h0_ctx, iv, iv, iv, iv, IV_SIZE);
H0_x4_final(&h0_ctx, vecComRec->s + j * lambdaBytes, vecComRec->s + (j + 1) * lambdaBytes,
vecComRec->s + (j + 2) * lambdaBytes, vecComRec->s + (j + 3) * lambdaBytes,
lambdaBytes, vecComRec->com + j * lambdaBytes * 2,
vecComRec->com + (j + 1) * lambdaBytes * 2,
vecComRec->com + (j + 2) * lambdaBytes * 2,
vecComRec->com + (j + 3) * lambdaBytes * 2, lambdaBytes * 2);
}
// reconstruct remaining instances
for (; j < numVoleInstances; ++j) {
H0_context_t h0_ctx;
H0_init(&h0_ctx, lambda);
H0_update(&h0_ctx, vecComRec->k + getNodeIndex(depth, j) * lambdaBytes, lambdaBytes);
H0_update(&h0_ctx, iv, IV_SIZE);
H0_final(&h0_ctx, vecComRec->s + j * lambdaBytes, lambdaBytes,
vecComRec->com + j * lambdaBytes * 2, lambdaBytes * 2);
}
// Step: 12..13
memcpy(vecComRec->com + (lambdaBytes * 2 * leafIndex), com_j, lambdaBytes * 2);
H1_context_t h1_ctx;
H1_init(&h1_ctx, lambda);
H1_update(&h1_ctx, vecComRec->com, lambdaBytes * 2 * numVoleInstances);
H1_final(&h1_ctx, vecComRec->h, lambdaBytes * 2);
}
#if defined(FAEST_TESTS)
int vector_verify(const uint8_t* iv, const uint8_t* pdec, const uint8_t* com_j, const uint8_t* b,
uint32_t lambda, uint32_t depth, vec_com_rec_t* rec, const uint8_t* vecComH) {
const unsigned int lambdaBytes = lambda / 8;
const unsigned int numVoleInstances = 1 << depth;
vec_com_rec_t vecComRec;
vecComRec.h = malloc(lambdaBytes * 2);
vecComRec.k = calloc(getBinaryTreeNodeCount(depth), lambdaBytes);
vecComRec.com = malloc(numVoleInstances * lambdaBytes * 2);
vecComRec.s = malloc(numVoleInstances * lambdaBytes);
// Step: 2
vector_reconstruction(iv, pdec, com_j, b, lambda, depth, &vecComRec);
// Step: 3
int ret = memcmp(vecComH, vecComRec.h, lambdaBytes * 2);
if (!rec || ret) {
vec_com_rec_clear(&vecComRec);
}
if (ret == 0) {
if (rec) {
*rec = vecComRec;
}
return 1;
} else {
return 0;
}
}
#endif
void vec_com_clear(vec_com_t* com) {
free(com->sd);
free(com->com);
free(com->k);
free(com->h);
}
void vec_com_rec_clear(vec_com_rec_t* rec) {
free(rec->s);
free(rec->com);
free(rec->k);
free(rec->h);
}