Merge bitcoin/bitcoin#36457: Test: Cover malformed merkle trees and nBits
What changed, and why it matters
This commit only adds new test cases to Bitcoin Core. It does not change any production code, so it cannot introduce a runtime vulnerability or directly fix one. The tests exercise how the software rejects malformed partial Merkle trees and how it handles unusual proof-of-work target values. The pull request description says the test ideas came from comparing against another implementation (bitcoin-s), but the commit itself is purely additional test coverage.
No security action required. Review the new tests for correctness and completeness as part of normal QA.
Security signals we found
Adds regression-style tests for malformed partial Merkle tree deserialization
Adds tests for GetBitsProof behavior on zero, negative, and overflowing compact nBits targets
No changes to non-test source files
Evidence from the diff
The diff adds two BOOST_AUTO_TEST_CASE blocks. In src/test/pmt_tests.cpp it adds pmt_malformed, which checks that CPartialMerkleTree::ExtractMatches returns a null uint256 for various invalid inputs: tx count above the 16666 limit, empty tree, more hashes than transactions, fewer bits than hashes, hash exhaustion, bit exhaustion, extra bits, and extra hashes. In src/test/pow_tests.cpp it adds GetBitsProof_test, verifying that GetBitsProof returns zero for zero, negative, and overflowing nBits encodings, and returns the expected nonzero value for the genesis target. No consensus, networking, or wallet code is modified.
Changed components
src/test/pmt_tests.cppsrc/test/pow_tests.cppInspect captured patch +57 / −0
### src/test/pmt_tests.cpp
@@ -2,10 +2,12 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <consensus/consensus.h>
#include <consensus/merkle.h>
#include <merkleblock.h>
#include <serialize.h>
#include <streams.h>
+#include <test/util/common.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <uint256.h>
@@ -125,4 +127,42 @@ BOOST_AUTO_TEST_CASE(pmt_malleability)
BOOST_CHECK(tree.ExtractMatches(vTxid, vIndex).IsNull());
}
+BOOST_AUTO_TEST_CASE(pmt_malformed)
+{
+ // Deserialize a partial merkle tree with the given fields and extract its matches.
+ const auto extract{[](uint32_t tx_count, uint32_t hash_count, std::vector<unsigned char> bits) {
+ std::vector<uint256> hashes;
+ for (uint32_t i{1}; i <= hash_count; ++i) hashes.emplace_back(i);
+ DataStream ss;
+ ss << tx_count << hashes << bits;
+ CPartialMerkleTree tree;
+ ss >> tree;
+ std::vector<Txid> matches;
+ std::vector<unsigned int> indexes;
+ return tree.ExtractMatches(matches, indexes);
+ }};
+
+ // The transaction count is limited to MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT.
+ constexpr uint32_t max_tx_count{MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT};
+ static_assert(max_tx_count == 16666);
+ BOOST_CHECK_EQUAL(extract(max_tx_count, 1, {0x00}), uint256{1});
+ BOOST_CHECK(extract(max_tx_count + 1, 1, {0x00}).IsNull());
+ // An empty tree, more hashes than transactions, and fewer bits than hashes are
+ // rejected before traversal. Traversal would reject each of them anyway, so these
+ // check the result, not the early returns.
+ BOOST_CHECK(extract(0, 0, {0x00}).IsNull());
+ BOOST_CHECK(extract(1, 2, {0x00}).IsNull());
+ BOOST_CHECK(extract(9, 9, {0xff}).IsNull());
+ // Traversal runs out of hashes: the root is a parent of a match, so both of its
+ // leaves need a hash, but only one is provided.
+ BOOST_CHECK(extract(2, 1, {0x01}).IsNull());
+ // Traversal runs out of bits: eight bits reach the third leaf but not the fourth.
+ BOOST_CHECK(extract(16, 8, {0xff}).IsNull());
+ // Not all bits are consumed: a second byte of bits is more than padding.
+ BOOST_CHECK(extract(1, 1, {0x00, 0x00}).IsNull());
+ // Not all hashes are consumed: the root is not a parent of a match, so only its
+ // own hash is used.
+ BOOST_CHECK(extract(2, 2, {0x00}).IsNull());
+}
+
BOOST_AUTO_TEST_SUITE_END()
### src/test/pow_tests.cpp
@@ -202,6 +202,23 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
}
+BOOST_AUTO_TEST_CASE(GetBitsProof_test)
+{
+ // Targets that are zero, negative or overflow encode no work.
+ BOOST_CHECK_EQUAL(GetBitsProof(0x00000000), arith_uint256{0});
+ BOOST_CHECK_EQUAL(GetBitsProof(0x01003456), arith_uint256{0});
+ BOOST_CHECK_EQUAL(GetBitsProof(0x04923456), arith_uint256{0});
+ BOOST_CHECK_EQUAL(GetBitsProof(0xff123456), arith_uint256{0});
+ // The exponent byte is unsigned, so this is a large exponent that overflows.
+ BOOST_CHECK_EQUAL(GetBitsProof(0x80010203), arith_uint256{0});
+ // The two overflows above shift every mantissa bit out of 256 bits, so they would
+ // also give zero work without the overflow check. This one keeps the low bits of
+ // its mantissa when truncated.
+ BOOST_CHECK_EQUAL(GetBitsProof(0x21012345), arith_uint256{0});
+ // The genesis block target.
+ BOOST_CHECK_EQUAL(GetBitsProof(0x1d00ffff), arith_uint256{0x100010001});
+}
+
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
{
const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);Why this scored 14/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.