47 bool mutation =
false;
48 while (hashes.size() > 1) {
50 for (
size_t pos = 0; pos + 1 < hashes.size(); pos += 2) {
51 if (hashes[pos] == hashes[pos + 1]) mutation =
true;
54 if (hashes.size() & 1) {
55 hashes.push_back(hashes.back());
57 SHA256D64(hashes[0].begin(), hashes[0].begin(), hashes.size() / 2);
58 hashes.resize(hashes.size() / 2);
60 if (mutated) *mutated = mutation;
61 if (hashes.size() == 0)
return uint256();
68 std::vector<uint256> leaves;
69 leaves.resize(block.
vtx.size());
70 for (
size_t s = 0;
s < block.
vtx.size();
s++) {
71 leaves[
s] = block.
vtx[
s]->GetHash();
78 std::vector<uint256> leaves;
79 leaves.resize(block.
vtx.size());
81 for (
size_t s = 1;
s < block.
vtx.size();
s++) {
82 leaves[
s] = block.
vtx[
s]->GetWitnessHash();
88static void MerkleComputation(
const std::vector<uint256>& leaves,
uint256* proot,
bool* pmutated, uint32_t leaf_pos, std::vector<uint256>* path)
90 if (path) path->clear();
91 Assume(leaves.size() <= UINT32_MAX);
92 if (leaves.size() == 0) {
93 if (pmutated) *pmutated =
false;
109 while (
count < leaves.size()) {
111 bool matchh =
count == leaf_pos;
117 for (level = 0; !(
count & ((uint32_t{1}) << level)); level++) {
120 path->push_back(inner[level]);
121 }
else if (matchlevel == level) {
126 mutated |= (inner[level] == h);
127 h =
Hash(inner[level], h);
141 while (!(
count & ((uint32_t{1}) << level))) {
145 bool matchh = matchlevel == level;
146 while (
count != ((uint32_t{1}) << level)) {
150 if (path && matchh) {
156 count += ((uint32_t{1}) << level);
159 while (!(
count & ((uint32_t{1}) << level))) {
162 path->push_back(inner[level]);
163 }
else if (matchlevel == level) {
168 h =
Hash(inner[level], h);
173 if (pmutated) *pmutated = mutated;
174 if (proot) *proot = h;
177static std::vector<uint256>
ComputeMerklePath(
const std::vector<uint256>& leaves, uint32_t position) {
178 std::vector<uint256>
ret;
185 std::vector<uint256> leaves;
186 leaves.resize(block.
vtx.size());
187 for (
size_t s = 0;
s < block.
vtx.size();
s++) {
188 leaves[
s] = block.
vtx[
s]->GetHash();
#define Assume(val)
Assume is the identity function.
std::vector< CTransactionRef > vtx
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
uint256 ComputeMerkleRoot(std::vector< uint256 > hashes, bool *mutated)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
static void MerkleComputation(const std::vector< uint256 > &leaves, uint256 *proot, bool *pmutated, uint32_t leaf_pos, std::vector< uint256 > *path)
static std::vector< uint256 > ComputeMerklePath(const std::vector< uint256 > &leaves, uint32_t position)
std::vector< uint256 > TransactionMerklePath(const CBlock &block, uint32_t position)
Compute merkle path to the specified transaction.
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
void SHA256D64(unsigned char *out, const unsigned char *in, size_t blocks)
Compute multiple double-SHA256's of 64-byte blobs.