52 bool mutation =
false;
53 while (hashes.size() > 1) {
60 for (
size_t pos = 0; pos + 1 < hashes.size(); pos += 2) {
61 if (hashes[pos] == hashes[pos + 1]) mutation =
true;
64 if (hashes.size() & 1) {
65 hashes.push_back(hashes.back());
67 SHA256D64(hashes[0].begin(), hashes[0].begin(), hashes.size() / 2);
68 hashes.resize(hashes.size() / 2);
70 if (mutated) *mutated = mutation;
71 if (hashes.size() == 0)
return uint256();
78 std::vector<uint256> leaves;
79 leaves.reserve((block.
vtx.size() + 1) & ~1ULL);
80 for (
size_t s = 0;
s < block.
vtx.size();
s++) {
81 leaves.push_back(block.
vtx[
s]->GetHash().ToUint256());
88 std::vector<uint256> leaves;
89 leaves.reserve((block.
vtx.size() + 1) & ~1ULL);
90 leaves.emplace_back();
91 for (
size_t s = 1;
s < block.
vtx.size();
s++) {
92 leaves.push_back(block.
vtx[
s]->GetWitnessHash().ToUint256());
98static void MerkleComputation(
const std::vector<uint256>& leaves, uint32_t leaf_pos, std::vector<uint256>& path)
101 Assume(leaves.size() <= UINT32_MAX);
102 if (leaves.size() == 0) {
116 while (
count < leaves.size()) {
118 bool matchh =
count == leaf_pos;
124 for (level = 0; !(
count & ((uint32_t{1}) << level)); level++) {
126 path.push_back(inner[level]);
127 }
else if (matchlevel == level) {
131 h =
Hash(inner[level], h);
145 while (!(
count & ((uint32_t{1}) << level))) {
149 bool matchh = matchlevel == level;
150 while (
count != ((uint32_t{1}) << level)) {
160 count += ((uint32_t{1}) << level);
163 while (!(
count & ((uint32_t{1}) << level))) {
165 path.push_back(inner[level]);
166 }
else if (matchlevel == level) {
170 h =
Hash(inner[level], h);
176static std::vector<uint256>
ComputeMerklePath(
const std::vector<uint256>& leaves, uint32_t position) {
177 std::vector<uint256>
ret;
184 std::vector<uint256> leaves;
185 leaves.resize(block.
vtx.size());
186 for (
size_t s = 0;
s < block.
vtx.size();
s++) {
187 leaves[
s] = block.
vtx[
s]->GetHash().ToUint256();
#define Assume(val)
Assume is the identity function.
std::vector< CTransactionRef > vtx
uint256 ComputeMerkleRoot(std::vector< uint256 > hashes, bool *mutated)
Compute a Merkle root from the provided leaf hashes.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
static std::vector< uint256 > ComputeMerklePath(const std::vector< uint256 > &leaves, uint32_t position)
static void MerkleComputation(const std::vector< uint256 > &leaves, uint32_t leaf_pos, std::vector< uint256 > &path)
std::vector< uint256 > TransactionMerklePath(const CBlock &block, uint32_t position)
Compute merkle path to the specified transaction.
uint256 BlockWitnessMerkleRoot(const CBlock &block)
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
void SHA256D64(unsigned char *out, const unsigned char *in, size_t blocks)
Compute multiple double-SHA256's of 64-byte blobs.