-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmerkle.c
229 lines (198 loc) · 5.75 KB
/
cmerkle.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <openssl/sha.h>
#include <sys/time.h>
#define LEAF_SIZE 1024
typedef unsigned char leaf[LEAF_SIZE];
typedef unsigned char digest[20];
typedef struct {
digest left;
digest right;
} node;
typedef struct {
int height;
node *root;
leaf *data;
} tree;
typedef struct {
leaf item;
digest *siblings;
} path;
static void print_hex(const unsigned char *d) {
for (int i = 0; i < 20; i++) printf("%02x", d[i]);
printf("\n");
}
static inline node *layer(const tree *t, int k) {
return t->root + (1<<k) - 1;
}
static void hash_leaf(const leaf l, digest d) {
//printf("leaf:");
//print_hex((unsigned char*) l);
SHA1((unsigned char*) l, sizeof(leaf), d);
}
static void hash_node(const node *n, digest d) {
//print_hex(n->left);
//print_hex(n->right);
SHA1((unsigned char *) n, sizeof(node), d);
}
static tree *build_tree(int height) {
int n = 1 << height;
tree *t = (tree *) malloc(sizeof(tree));
t->height = height;
t->root = malloc((n-1)*sizeof(node));
t->data = malloc(n*sizeof(leaf));
// Load the leaf data from a file
char filename[256];
sprintf(filename, "data/leaves_%03d.dat", height);
printf("opening: %s, reading %d bytes\n", filename, n*sizeof(leaf));
FILE *file = fopen(filename, "rb");
assert (fread(t->data, sizeof(leaf), n, file) == n);
// Process each layer of the tree, starting with the leaves
node *layer0 = layer(t, height-1);
node *layer1 = 0;
for (int i = 0; i < n/2; i++) {
hash_leaf(t->data[2*i+0], layer0[i]. left);
hash_leaf(t->data[2*i+1], layer0[i].right);
}
for (int k = height-2; k >= 0; k--) {
layer1 = layer0;
layer0 = layer(t, k);
for (int i = 0; i < (1<<k); i++) {
hash_node(&layer1[2*i+0], layer0[i]. left);
hash_node(&layer1[2*i+1], layer0[i].right);
}
}
return t;
}
static void free_tree(tree *t) {
free(t->root);
free(t->data);
free(t);
}
path *alloc_path(int height) {
path *p = (path *) malloc(sizeof(path));
p->siblings = (digest *) malloc(sizeof(digest) * height);
return p;
}
void build_path(tree *t, int idx, path *p) {
assert(idx >= 0 && idx < (1<<t->height));
memcpy(p->item, t->data[idx], sizeof(leaf));
for (int k = 0; k < t->height; k++) {
node *layer0 = layer(t, t->height-1-k);
int bit = idx % 2;
if (!bit) {
memcpy(p->siblings[k], layer0[idx/2].right, sizeof(digest));
} else {
memcpy(p->siblings[k], layer0[idx/2].left, sizeof(digest));
}
idx /= 2;
}
}
void read_path(int height, FILE *file, path *p) {
assert (fread(p->item, sizeof(leaf), 1, file) == 1);
assert (fread(p->siblings, sizeof(digest), height, file) == height);
}
void write_path(int height, FILE *file, path *p) {
assert (fwrite(p->item, sizeof(leaf), 1, file) == 1);
assert (fwrite(p->siblings, sizeof(digest), height, file) == height);
}
void *free_path(path *p) {
free(p->siblings);
free(p);
}
void validate_path(int height, digest root, int idx, path *p) {
static node n1, n2;
node *ping = &n1;
node *pong = &n2;
if (!(idx & 1))
hash_leaf(p->item, ping->left);
else
hash_leaf(p->item, ping->right);
for (int k = 0; k < height; k++) {
int bit = idx & 1;
if (!bit) {
memcpy(ping->right, p->siblings[k], sizeof(digest));
} else {
memcpy(ping->left , p->siblings[k], sizeof(digest));
}
if (!((idx >> 1) & 1)) {
hash_node(ping, pong->left);
} else {
hash_node(ping, pong->right);
}
idx >>= 1;
node *tmp = ping;
ping = pong;
pong = tmp;
}
assert(!strncmp(ping->left,root,sizeof(digest)));
}
struct timeval tv;
void timer_reset() {
gettimeofday(&tv, NULL);
}
int timer_sample() {
struct timeval now;
gettimeofday(&now, NULL);
return (now.tv_sec*1000 + now.tv_usec/1000) -
(tv.tv_sec*1000 + tv.tv_usec/1000);
}
void prover_test(int k, int rep, int iter) {
tree *t = build_tree(k);
int n = 1<<k;
char filename[256];
for (int r = 0; r < rep; r++) {
sprintf(filename, "data/proof_cmerkle_%03d.dat", k);
FILE *f = fopen(filename, "wb");
srand(0x7071);
timer_reset();
path *p = alloc_path(t->height);
for (int i = 0; i < iter; i++) {
int idx = rand() % n;
build_path(t, idx, p);
write_path(t->height, f, p);
}
fclose(f);
free_path(p);
int ms = timer_sample();
printf("(prover) lookup in size 2^%d, x%d: %0.3f seconds\n", k, iter, ms / 1000.0);
}
free_tree(t);
}
void verifier_test(int k, int rep, int iter) {
digest root;
tree *t = build_tree(k);
hash_node(t->root, root);
free_tree(t);
int n = 1<<k;
char filename[256];
for (int r = 0; r < rep; r++) {
sprintf(filename, "data/proof_cmerkle_%03d.dat", k);
FILE *f = fopen(filename, "rb");
srand(0x7071);
path *p = alloc_path(k);
timer_reset();
for (int i = 0; i < iter; i++) {
int idx = rand() % n;
read_path(k, f, p);
validate_path(k, root, idx, p);
}
fclose(f);
int ms = timer_sample();
free_path(p);
printf("(verifier) lookup in size 2^%d, x%d: %0.3f seconds\n", k, iter, ms / 1000.0);
}
}
int main(int argc, char *argv[]) {
tree *t = build_tree(4);
digest root;
hash_node(t->root, root);
for (int k = 4; k <= 18; k++) {
prover_test(k, 1, 100000);
}
for (int k = 4; k <= 18; k++) {
verifier_test(k, 7, 100000);
}
}