Debian Codegraph @

src/crypto/tree-hash.c

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
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 * Round to power of two, for count>=3 and for count being not too large (as reasonable for tree hash calculations)
40 */
41 size_t tree_hash_cnt(size_t count) {
42 // This algo has some bad history but all we are doing is 1 << floor(log2(count))
43 // There are _many_ ways to do log2, for some reason the one selected was the most obscure one,
44 // and fixing it made it even more obscure.
45 //
46 // Iterative method implemented below aims for clarity over speed, if performance is needed
47 // then my advice is to use the BSR instruction on x86
48 //
49 // All the paranoid asserts have been removed since it is trivial to mathematically prove that
50 // the return will always be a power of 2.
51 // Problem space has been defined as 3 <= count <= 2^28. Of course quarter of a billion transactions
52 // is not a sane upper limit for a block, so there will be tighter limits in other parts of the code
53
54 assert( count >= 3 ); // cases for 0,1,2 are handled elsewhere
55 assert( count <= 0x10000000 ); // sanity limit to 2^28, MSB=1 will cause an inf loop
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 // The blockchain block at height 202612 https://moneroblocks.info/block/202612
64 // contained 514 transactions, that triggered bad calculation of variable "cnt" in the original version of this function
65 // as from CryptoNote code.
66 //
67 // This bug applies to all CN altcoins.
68 //
69 // Mathematical bug here was first published on 14:45:34 (GMT+2) 2014-09-04 by Rafal Freeman <rfree>
70 // https://github.com/rfree2monero/bitmonero/commit/b417abfb7a297d09f1bbb6de29030f8de9952ac8
71 // and soon also applied to CryptoNote (15:10 GMT+2), and BoolBerry used not fully correct work around:
72 // the work around of sizeof(size_t)*8 or <<3 as used before in 2 coins and in BBL later was blocking
73 // exploitation on normal platforms, how ever we strongly recommend the following fix because it removes
74 // mistake in mathematical formula.
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); // zero out as extra protection for using uninitialized mem
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); // zero out as extra protection for using uninitialized mem
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 }