forked from libbitcoin/libbitcoin-database
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmerkle.ipp
More file actions
280 lines (235 loc) · 8.32 KB
/
merkle.ipp
File metadata and controls
280 lines (235 loc) · 8.32 KB
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
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_DATABASE_QUERY_MERKLE_IPP
#define LIBBITCOIN_DATABASE_QUERY_MERKLE_IPP
#include <algorithm>
#include <iterator>
#include <ranges>
#include <utility>
#include <bitcoin/database/define.hpp>
namespace libbitcoin {
namespace database {
// merkle
// ----------------------------------------------------------------------------
// static/protected
TEMPLATE
CLASS::positions CLASS::merkle_branch(size_t leaf, size_t leaves,
bool compress) NOEXCEPT
{
using namespace system;
BC_ASSERT(leaves <= power2(sub1(bits<size_t>)));
BC_ASSERT(is_even(leaves) || is_one(leaves));
positions branch{};
if (is_zero(leaves) || leaf >= leaves)
return branch;
// Upper bound, actual count may be less given compression.
branch.reserve(ceilinged_log2(leaves));
for (auto width = one, current = leaves; current > one;)
{
const auto sibling = bit_xor(leaf, one);
if (!compress || sibling < current)
branch.emplace_back(sibling, width);
++current;
shift_left_into(width);
shift_right_into(leaf);
shift_right_into(current);
}
return branch;
}
// protected
TEMPLATE
CLASS::hash_option CLASS::create_interval(header_link link,
size_t height) const NOEXCEPT
{
// Interval ends at nth block where n is a multiple of span.
// One is a functional but undesirable case (no optimization).
const auto span = interval_span();
BC_ASSERT(!is_zero(span));
// If valid link is provided then empty return implies non-interval height.
if (link.is_terminal() || !system::is_multiple(add1(height), span))
return {};
// Generate the leaf nodes for the span.
hashes leafs(span);
for (auto& leaf: std::views::reverse(leafs))
{
leaf = get_header_key(link);
link = to_parent(link);
}
// Generate the merkle root of the interval ending on link header.
return system::merkle_root(std::move(leafs));
}
// protected
TEMPLATE
CLASS::hash_option CLASS::get_confirmed_interval(size_t height) const NOEXCEPT
{
const auto span = interval_span();
BC_ASSERT(!is_zero(span));
if (!system::is_multiple(add1(height), span))
return {};
table::txs::get_interval txs{};
if (!store_.txs.at(to_confirmed(height), txs))
return {};
return txs.interval;
}
// static/protected
TEMPLATE
void CLASS::merge_merkle(hashes& path, hashes&& leaves, size_t first,
size_t lift) NOEXCEPT
{
auto size = leaves.size();
if (!is_one(size) && is_odd(size))
{
// leaves is either even or has +1 element of reserved space.
leaves.push_back(leaves.back());
++size;
}
for (const auto& row: merkle_branch(first, size + lift))
{
hashes subroot{};
if (const auto leaf = row.sibling * row.width; leaf < size)
{
const auto count = std::min(row.width, size - leaf);
const auto next = std::next(leaves.begin(), leaf);
const auto it = std::make_move_iterator(next);
subroot = { it, std::next(it, count) };
}
else
{
subroot = { std::move(leaves.back()) };
}
path.push_back(partial_subroot(std::move(subroot), row.width));
}
}
// protected
TEMPLATE
code CLASS::get_merkle_proof(hashes& proof, hashes roots, size_t target,
size_t waypoint) const NOEXCEPT
{
const auto span = interval_span();
BC_ASSERT(!is_zero(span));
const auto first = (target / span) * span;
const auto close = sub1(first + span);
const auto last = std::min(waypoint, close);
const auto size = add1(last - first);
auto parts = get_confirmed_hashes(first, size);
if (is_zero(parts.size()))
return error::merkle_proof;
const auto count = parts.size();
const auto pad = to_int<size_t>(!is_one(count) && is_odd(count));
const auto lift = is_zero(first) ? zero : (span - (count + pad));
using namespace system;
proof.clear();
proof.reserve(ceilinged_log2(parts.size()) + ceilinged_log2(roots.size()));
merge_merkle(proof, std::move(parts), target % span, lift);
merge_merkle(proof, std::move(roots), target / span, zero);
return error::success;
}
// static/private
TEMPLATE
hash_digest CLASS::partial_subroot(hashes&& tree, size_t span) NOEXCEPT
{
// Tree cannot be empty or exceed span (a power of 2).
if (tree.empty() || tree.size() > span)
return {};
// Zero depth implies single tree element, which is the root.
using namespace system;
const auto depth = ceilinged_log2(span);
if (is_zero(depth))
return tree.front();
// merkle_root() treats a single hash as top/complete, but for a partial
// depth subtree, an odd leaf (including a single) requires duplication.
if (is_odd(tree.size()))
tree.push_back(tree.back());
// Log2 of the evened breadth gives the elevation by merkle_root.
// Partial cannot exceed depth, since tree.size() <= span (a power of 2).
auto partial = ceilinged_log2(tree.size());
if (is_subtract_overflow(depth, partial))
return {};
// Elevate hashes to partial level, and then from partial to depth.
auto hash = merkle_root(std::move(tree));
for (; partial < depth; ++partial)
hash = sha256::double_hash(hash, hash);
return hash;
}
// protected
TEMPLATE
code CLASS::get_merkle_subroots(hashes& roots, size_t waypoint) const NOEXCEPT
{
const auto span = interval_span();
BC_ASSERT(!is_zero(span));
using namespace system;
const auto leafs = add1(waypoint);
const auto limit = ceilinged_divide(leafs, span);
const auto count = limit + to_int<size_t>(!is_one(limit) && is_odd(limit));
// Roots is even-size-except-one-reserved for merkle root push.
roots.reserve(count);
// Either all subroots elevated to same level, or there is a single root.
for (size_t first{}; first < leafs; first += span)
{
const auto last = std::min(sub1(first + span), waypoint);
const auto size = add1(last - first);
if (size == span)
{
auto interval = get_confirmed_interval(last);
if (!interval.has_value()) return error::merkle_interval;
roots.push_back(std::move(interval.value()));
}
else if (is_zero(first))
{
// Single hash, is the complete merkle root.
auto complete = get_confirmed_hashes(zero, size);
roots.push_back(merkle_root(std::move(complete)));
}
else
{
// Roots is even-size-except-one-reserved for merkle root push.
auto partial = get_confirmed_hashes(first, size);
if (partial.empty()) return error::merkle_hashes;
roots.push_back(partial_subroot(std::move(partial), span));
}
}
return error::success;
}
TEMPLATE
code CLASS::get_merkle_root_and_proof(hash_digest& root, hashes& proof,
size_t target, size_t waypoint) const NOEXCEPT
{
if (target > waypoint)
return error::invalid_argument;
if (waypoint > get_top_confirmed())
return error::not_found;
hashes roots{};
if (const auto ec = get_merkle_subroots(roots, waypoint))
return ec;
if (const auto ec = get_merkle_proof(proof, roots, target, waypoint))
return ec;
root = system::merkle_root(std::move(roots));
return {};
}
TEMPLATE
hash_digest CLASS::get_merkle_root(size_t height) const NOEXCEPT
{
hashes roots{};
if (const auto ec = get_merkle_subroots(roots, height))
return {};
return system::merkle_root(std::move(roots));
}
} // namespace database
} // namespace libbitcoin
#endif