forked from bcoin-org/bcrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerkle.js
232 lines (192 loc) · 4.92 KB
/
merkle.js
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
/*!
* merkle.js - merkle trees for bcrypto
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* Copyright (c) 2014-2019, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcrypto
*
* Parts of this software are based on bitcoin/bitcoin:
* Copyright (c) 2009-2019, The Bitcoin Core Developers (MIT License).
* Copyright (c) 2009-2019, The Bitcoin Developers (MIT License).
* https://github.com/bitcoin/bitcoin
*/
'use strict';
const assert = require('./internal/assert');
// Notes about unbalanced merkle trees:
//
// Bitcoin hashes odd nodes with themselves,
// allowing an attacker to add a duplicate
// TXID, creating an even number of leaves
// and computing the same root (CVE-2012-2459).
// In contrast, RFC 6962 simply propagates
// odd nodes up.
//
// RFC 6962:
//
// R
// / \
// / \
// / \
// / \
// / \
// k j <-- same as below
// / \ |
// / \ |
// / \ |
// h i j
// / \ / \ / \
// a b c d e f
//
// Bitcoin Behavior:
//
// R
// / \
// / \
// / \
// / \
// / \
// k l <-- HASH(j || j)
// / \ |
// / \ |
// / \ |
// h i j
// / \ / \ / \
// a b c d e f
//
// This creates a situation where these leaves:
//
// R
// / \
// / \
// / \
// d e <-- HASH(c || c)
// / \ / \
// a b c c
//
// Compute the same root as:
//
// R
// / \
// / \
// d e <-- HASH(c || c)
// / \ |
// a b c
//
// Why does this matter? Duplicate TXIDs are
// invalid right? They're spending the same
// inputs! The problem arises in certain
// implementation optimizations which may
// mark a block hash invalid. In other words,
// an invalid block shares the same block
// hash as a valid one!
//
// See:
// https://tools.ietf.org/html/rfc6962#section-2.1
// https://nvd.nist.gov/vuln/detail/CVE-2012-2459
// https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-2459
// https://bitcointalk.org/?topic=81749
/**
* Build a merkle tree from leaves.
* @param {Object} alg
* @param {Buffer[]} leaves
* @returns {Array} [nodes, malleated]
*/
function createTree(alg, leaves) {
assert(alg && typeof alg.root === 'function');
assert(Array.isArray(leaves));
const nodes = new Array(leaves.length);
for (let i = 0; i < leaves.length; i++)
nodes[i] = leaves[i];
let size = nodes.length;
let malleated = false;
let i = 0;
if (size === 0) {
nodes.push(alg.zero);
return [nodes, malleated];
}
while (size > 1) {
for (let j = 0; j < size; j += 2) {
const k = Math.min(j + 1, size - 1);
const left = nodes[i + j];
const right = nodes[i + k];
if (k === j + 1 && k + 1 === size
&& left.equals(right)) {
malleated = true;
}
const hash = alg.root(left, right);
nodes.push(hash);
}
i += size;
size = (size + 1) >>> 1;
}
return [nodes, malleated];
}
/**
* Calculate merkle root from leaves.
* @param {Object} alg
* @param {Buffer[]} leaves
* @returns {Array} [root, malleated]
*/
function createRoot(alg, leaves) {
assert(alg && typeof alg.root === 'function');
assert(Array.isArray(leaves));
const [nodes, malleated] = createTree(alg, leaves);
const root = nodes[nodes.length - 1];
return [root, malleated];
}
/**
* Collect a merkle branch from vector index.
* @param {Object} alg
* @param {Number} index
* @param {Buffer[]} leaves
* @returns {Buffer[]} branch
*/
function createBranch(alg, index, leaves) {
assert(alg && typeof alg.root === 'function');
assert((index >>> 0) === index);
assert(Array.isArray(leaves));
assert(index < leaves.length);
let size = leaves.length;
const [nodes] = createTree(alg, leaves);
const branch = [];
let i = 0;
while (size > 1) {
const j = Math.min(index ^ 1, size - 1);
branch.push(nodes[i + j]);
index >>>= 1;
i += size;
size = (size + 1) >>> 1;
}
return branch;
}
/**
* Derive merkle root from branch.
* @param {Object} alg
* @param {Buffer} hash
* @param {Buffer[]} branch
* @param {Number} index
* @returns {Buffer} root
*/
function deriveRoot(alg, hash, branch, index) {
assert(alg && typeof alg.root === 'function');
assert(Buffer.isBuffer(hash));
assert(Array.isArray(branch));
assert((index >>> 0) === index);
let root = hash;
for (const hash of branch) {
if ((index & 1) && hash.equals(root))
return alg.zero;
if (index & 1)
root = alg.root(hash, root);
else
root = alg.root(root, hash);
index >>>= 1;
}
return root;
}
/*
* Expose
*/
exports.createTree = createTree;
exports.createRoot = createRoot;
exports.createBranch = createBranch;
exports.deriveRoot = deriveRoot;