Skip to content

Commit

Permalink
Added verify_merkle_proof by index
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed Oct 23, 2023
1 parent 12a3eb8 commit 92713bd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
78 changes: 78 additions & 0 deletions src/merkle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,82 @@ bool verify_merkle_proof(hash h, const std::vector<std::pair<bool, hash>>& proof
return (h == root);
}

bool verify_merkle_proof(hash h, const std::vector<hash>& proof, uint32_t index, uint32_t count, const hash& root)
{
if (index >= count) {
return false;
}

hash tmp[2];

if (count == 1) {
}
else if (count == 2) {
if (proof.empty()) {
return false;
}

if (index & 1) {
tmp[0] = proof[0];
tmp[1] = h;
}
else {
tmp[0] = h;
tmp[1] = proof[0];
}

keccak(tmp[0].h, HASH_SIZE * 2, h.h);
}
else {
size_t cnt = 1;
do { cnt <<= 1; } while (cnt <= count);
cnt >>= 1;

size_t proof_index = 0;

const size_t k = cnt * 2 - count;

if (index >= k) {
index -= k;

if (proof_index >= proof.size()) {
return false;
}

if (index & 1) {
tmp[0] = proof[proof_index];
tmp[1] = h;
}
else {
tmp[0] = h;
tmp[1] = proof[proof_index];
}

keccak(tmp[0].h, HASH_SIZE * 2, h.h);

index = (index >> 1) + k;
++proof_index;
}

for (size_t i = 1; cnt >= 2; ++i, ++proof_index, index >>= 1, cnt >>= 1) {
if (proof_index >= proof.size()) {
return false;
}

if (index & 1) {
tmp[0] = proof[proof_index];
tmp[1] = h;
}
else {
tmp[0] = h;
tmp[1] = proof[proof_index];
}

keccak(tmp[0].h, HASH_SIZE * 2, h.h);
}
}

return (h == root);
}

} // namespace p2pool
1 change: 1 addition & 0 deletions src/merkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ void merkle_hash(const std::vector<hash>& hashes, hash& root);
void merkle_hash_full_tree(const std::vector<hash>& hashes, std::vector<std::vector<hash>>& tree);
bool get_merkle_proof(const std::vector<std::vector<hash>>& tree, const hash& h, std::vector<std::pair<bool, hash>>& proof);
bool verify_merkle_proof(hash h, const std::vector<std::pair<bool, hash>>& proof, const hash& root);
bool verify_merkle_proof(hash h, const std::vector<hash>& proof, uint32_t index, uint32_t count, const hash& root);

} // namespace p2pool
13 changes: 12 additions & 1 deletion tests/src/merkle_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,21 @@ TEST(merkle, root_hash)
}
}

for (const hash& h : hashes) {
for (size_t i = 0, n = hashes.size(); i < n; ++i) {
const hash& h = hashes[i];
std::vector<std::pair<bool, hash>> proof;

ASSERT_TRUE(get_merkle_proof(tree, h, proof));
ASSERT_TRUE(verify_merkle_proof(h, proof, root));

std::vector<hash> proof2;
proof2.reserve(proof.size());

for (const auto& i : proof) {
proof2.emplace_back(i.second);
}

ASSERT_TRUE(verify_merkle_proof(h, proof2, i, n, root));
}
};

Expand Down

0 comments on commit 92713bd

Please sign in to comment.