| 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 |
#include <assert.h> |
| 32 |
#include <stddef.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include "hash-ops.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
size_t tree_hash_cnt(size_t count) { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
assert( count >= 3 ); |
| 55 |
assert( count <= 0x10000000 ); |
| 56 |
|
| 57 |
size_t pow = 2; |
| 58 |
while(pow < count) pow <<= 1; |
| 59 |
return pow >> 1; |
| 60 |
} |
| 61 |
|
| 62 |
void tree_hash(const char (*hashes)[HASH_SIZE], size_t count, char *root_hash) { |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
assert(count > 0); |
| 77 |
if (count == 1) { |
| 78 |
memcpy(root_hash, hashes, HASH_SIZE); |
| 79 |
} else if (count == 2) { |
| 80 |
cn_fast_hash(hashes, 2 * HASH_SIZE, root_hash); |
| 81 |
} else { |
| 82 |
size_t i, j; |
| 83 |
|
| 84 |
size_t cnt = tree_hash_cnt( count ); |
| 85 |
|
| 86 |
char *ints = calloc(cnt, HASH_SIZE); |
| 87 |
assert(ints); |
| 88 |
|
| 89 |
memcpy(ints, hashes, (2 * cnt - count) * HASH_SIZE); |
| 90 |
|
| 91 |
for (i = 2 * cnt - count, j = 2 * cnt - count; j < cnt; i += 2, ++j) { |
| 92 |
cn_fast_hash(hashes[i], 64, ints + j * HASH_SIZE); |
| 93 |
} |
| 94 |
assert(i == count); |
| 95 |
|
| 96 |
while (cnt > 2) { |
| 97 |
cnt >>= 1; |
| 98 |
for (i = 0, j = 0; j < cnt; i += 2, ++j) { |
| 99 |
cn_fast_hash(ints + i * HASH_SIZE, 64, ints + j * HASH_SIZE); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
cn_fast_hash(ints, 64, root_hash); |
| 104 |
free(ints); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
bool tree_path(size_t count, size_t idx, uint32_t *path) |
| 109 |
{ |
| 110 |
if (count == 0) |
| 111 |
return false; |
| 112 |
|
| 113 |
if (count == 1) { |
| 114 |
*path = 0; |
| 115 |
} else if (count == 2) { |
| 116 |
*path = idx == 0 ? 0 : 1; |
| 117 |
} else { |
| 118 |
size_t i, j; |
| 119 |
|
| 120 |
*path = 0; |
| 121 |
size_t cnt = tree_hash_cnt( count ); |
| 122 |
|
| 123 |
for (i = 2 * cnt - count, j = 2 * cnt - count; j < cnt; i += 2, ++j) { |
| 124 |
if (idx == i || idx == i+1) |
| 125 |
{ |
| 126 |
*path = (*path << 1) | (idx == i ? 0 : 1); |
| 127 |
idx = j; |
| 128 |
} |
| 129 |
} |
| 130 |
assert(i == count); |
| 131 |
|
| 132 |
while (cnt > 2) { |
| 133 |
cnt >>= 1; |
| 134 |
for (i = 0, j = 0; j < cnt; i += 2, ++j) { |
| 135 |
if (idx == i || idx == i + 1) |
| 136 |
{ |
| 137 |
*path = (*path << 1) | (idx == i ? 0 : 1); |
| 138 |
idx = j; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if (idx == 0 || idx == 1) |
| 144 |
{ |
| 145 |
*path = (*path << 1) | (idx == 0 ? 0 : 1); |
| 146 |
idx = 0; |
| 147 |
} |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
bool tree_branch(const char (*hashes)[HASH_SIZE], size_t count, const char *hash, char (*branch)[HASH_SIZE], size_t *depth, uint32_t *path) |
| 153 |
{ |
| 154 |
size_t idx; |
| 155 |
|
| 156 |
if (count == 0) |
| 157 |
return false; |
| 158 |
|
| 159 |
for (idx = 0; idx < count; ++idx) |
| 160 |
if (!memcmp(hash, hashes[idx], HASH_SIZE)) |
| 161 |
break; |
| 162 |
if (idx == count) |
| 163 |
return false; |
| 164 |
|
| 165 |
assert(count > 0); |
| 166 |
if (count == 1) { |
| 167 |
*depth = 0; |
| 168 |
*path = 0; |
| 169 |
} else if (count == 2) { |
| 170 |
*depth = 1; |
| 171 |
*path = idx == 0 ? 0 : 1; |
| 172 |
memcpy(branch[0], hashes[idx ^ 1], HASH_SIZE); |
| 173 |
} else { |
| 174 |
size_t i, j; |
| 175 |
|
| 176 |
*depth = 0; |
| 177 |
*path = 0; |
| 178 |
size_t cnt = tree_hash_cnt( count ); |
| 179 |
|
| 180 |
char *ints = calloc(cnt, HASH_SIZE); |
| 181 |
assert(ints); |
| 182 |
|
| 183 |
memcpy(ints, hashes, (2 * cnt - count) * HASH_SIZE); |
| 184 |
|
| 185 |
for (i = 2 * cnt - count, j = 2 * cnt - count; j < cnt; i += 2, ++j) { |
| 186 |
if (idx == i || idx == i+1) |
| 187 |
{ |
| 188 |
memcpy(branch[*depth], hashes[idx == i ? i + 1 : i], HASH_SIZE); |
| 189 |
++*depth; |
| 190 |
*path = (*path << 1) | (idx == i ? 0 : 1); |
| 191 |
idx = j; |
| 192 |
} |
| 193 |
cn_fast_hash(hashes[i], 64, ints + j * HASH_SIZE); |
| 194 |
} |
| 195 |
assert(i == count); |
| 196 |
|
| 197 |
while (cnt > 2) { |
| 198 |
cnt >>= 1; |
| 199 |
for (i = 0, j = 0; j < cnt; i += 2, ++j) { |
| 200 |
if (idx == i || idx == i + 1) |
| 201 |
{ |
| 202 |
memcpy(branch[*depth], ints + (idx == i ? i + 1 : i) * HASH_SIZE, HASH_SIZE); |
| 203 |
++*depth; |
| 204 |
*path = (*path << 1) | (idx == i ? 0 : 1); |
| 205 |
idx = j; |
| 206 |
} |
| 207 |
cn_fast_hash(ints + i * HASH_SIZE, 64, ints + j * HASH_SIZE); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if (idx == 0 || idx == 1) |
| 212 |
{ |
| 213 |
memcpy(branch[*depth], ints + (idx == 0 ? 1 : 0) * HASH_SIZE, HASH_SIZE); |
| 214 |
++*depth; |
| 215 |
*path = (*path << 1) | (idx == 0 ? 0 : 1); |
| 216 |
idx = 0; |
| 217 |
} |
| 218 |
|
| 219 |
free(ints); |
| 220 |
} |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
bool tree_branch_hash(const char hash[HASH_SIZE], const char (*branch)[HASH_SIZE], size_t depth, uint32_t path, char root[HASH_SIZE]) |
| 225 |
{ |
| 226 |
size_t d; |
| 227 |
char partial[HASH_SIZE]; |
| 228 |
|
| 229 |
memcpy(partial, hash, HASH_SIZE); |
| 230 |
|
| 231 |
for (d = 0; d < depth; ++d) |
| 232 |
{ |
| 233 |
char buffer[2 * HASH_SIZE]; |
| 234 |
if ((path >> (depth - d - 1)) & 1) |
| 235 |
{ |
| 236 |
memcpy(buffer, branch[d], HASH_SIZE); |
| 237 |
memcpy(buffer + HASH_SIZE, partial, HASH_SIZE); |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
memcpy(buffer, partial, HASH_SIZE); |
| 242 |
memcpy(buffer + HASH_SIZE, branch[d], HASH_SIZE); |
| 243 |
} |
| 244 |
cn_fast_hash(buffer, 2 * HASH_SIZE, partial); |
| 245 |
} |
| 246 |
|
| 247 |
memcpy(root, partial, HASH_SIZE); |
| 248 |
return true; |
| 249 |
} |
| 250 |
|
| 251 |
bool is_branch_in_tree(const char hash[HASH_SIZE], const char root[HASH_SIZE], const char (*branch)[HASH_SIZE], size_t depth, uint32_t path) |
| 252 |
{ |
| 253 |
char res[HASH_SIZE]; |
| 254 |
if (!tree_branch_hash(hash, branch, depth, path, res)) |
| 255 |
return false; |
| 256 |
return memcmp(res, root, HASH_SIZE) == 0; |
| 257 |
} |