A Merkle root algorithm testing project based on Python + pytest, with tests designed around functional correctness, boundary scenarios, differential testing, real block test vectors, and security regression. After expanding parametrize, there are currently 72 pytest test items in total.
The project also includes an algorithm-level regression for the duplicate-transaction scenario related to CVE-2012-2459: it constructs inputs whose transaction lists differ but whose Merkle roots are identical, and verifies that the mutated detection flag behaves as expected.
The code under test is located in merkle.py and mainly includes:
dsha256(data): applies SHA-256 twice in succession to the input bytes and returns a 32-byte result.compute_merkle_root(tx_hashes): iteratively computes the Merkle root and detects adjacent duplicate hashes at paired positions.
(root, mutated).mutated=False.mutated detection occurs before odd-node padding.The current implementation computes the result by copying the input list and generating a new hash list layer by layer, with an extra space complexity of O(n); it is not an in-place constant-space implementation.
| Test Category | Count | Design Method | Main Coverage |
|---|---|---|---|
| Hand-crafted known answers | 3 | Known-answer testing, error guessing | Two leaves, three leaves with odd padding, adjacent duplicate leaves and mutated detection |
| Differential testing | 64 | Independent reference implementation, fixed-seed random data | Transaction sizes from 1 to 64, cross-checked group by group against a recursive reference implementation |
| Real block test vector | 1 | Real-data regression | Transaction list and Merkle root of Bitcoin block at height 170 |
| Boundary testing | 2 | Boundary value analysis | Empty input, single-leaf input |
| CVE security regression | 2 | Control inputs, error guessing | Two constructions with 5/6 and 6/8 transactions, verifying identical roots and opposite mutated |
| Total | 72 |
The counts here are the number of test items after pytest parameterization expansion, not the number of test functions.
test_merkle.py implements a recursive reference algorithm reference_root() that does not use mutated detection, serving as an independent reference:
random.Random(2024 + n) to set a fixed random seed.n ∈ [1, 64], generate n random 32-byte transaction hashes.The fixed seed makes failing inputs reproducible, which helps with locating problems and adding regression cases.
fetch_block_data.py obtains real block data through the public Blockstream API:
The currently saved data/block_170.json corresponds to the block at height 170 and contains 2 transaction IDs and the expected Merkle root. Daily pytest runs only read this local file and require no network access; only when re-collecting data is it necessary to connect to the network and install requests.
The tests also handle the difference between Bitcoin RPC display byte order and internal computation byte order: after reading the transaction IDs, they are reversed to internal byte order, and after computation the Merkle root is reversed back to display byte order for comparison.
The project includes two sets of algorithm-level control tests:
Both sets of tests verify:
mutated=False, and the constructed input's mutated=True.Verification boundary: This project verifies the duplicate-transaction construction and the mutated detection logic in the Python implementation. It does not start bitcoind, nor does it verify that real nodes reject blocks or network consensus behavior; it should not be described as a complete reproduction of the consensus vulnerability in a real network.
merkle_lab/
├─ merkle.py # 双 SHA-256、Merkle 根及 mutated 检测
├─ test_merkle.py # pytest 测试与递归参考实现
├─ fetch_block_data.py # 可选:重新采集真实区块数据
├─ data/
│ └─ block_170.json # 本地真实区块测试向量
└─ README.md
Python 3.9 or later is recommended (random data generation uses random.Random.randbytes).
Create and activate a virtual environment in the project root directory:
python -m venv .venv
Windows:
.venv\Scripts\activate
python -m pip install pytest
macOS / Linux:
source .venv/bin/activate
python -m pip install pytest
requests is only needed when re-collecting block data:
python -m pip install requests
Please execute this in the project root directory to ensure the tests can read data/block_170.json:
python -m pytest -q
On Windows, you can also skip activating the virtual environment and run directly:
.venv\Scripts\python.exe -m pytest -q
If you only want to view the test collection results:
python -m pytest --collect-only -q
The actual number of passing tests and execution time are subject to the results of a complete run on your local machine; this project's documentation does not pre-fill unverified pass rates or execution times.
After installing requests, run the following in the project root directory:
python fetch_block_data.py
This command accesses the Blockstream public API and overrides data/block_170.json. Running this script is not required for normal test execution.
test_merkle.py, so you can quickly locate the corresponding scenario by combining it with the test name.
Ghidra ist ein Software-Reverse-Engineering (SRE)-Framework.

Regelbasiertes statisches und dynamisches Analyse-Tool, das Fähigkeiten in PE-, ELF-, .NET- und Shellcode-Dateien identifiziert und sie MITRE ATT&CK-Techniken zur Malware-Triage zuordnet.

Eine Neuimplementierung von YARA in Rust.