4-leaf Taproot Merkle Tree in Python: testnet implementation + control block analysis
1 message, 8 Jul 2025. Started by aaron.recompile.
aaron.recompile
Hi all,
I'm Aaron Zhang, an independent developer focused on Bitcoin script engineering and Taproot experimentation.
I’d like to share a hands-on project involving a fully constructed 4-leaf Taproot Merkle tree in Python, with testnet-validated spending transactions and in-depth analysis of control block internals.
This work features:
- Construction of a *Taproot address with 4 distinct script leaves*:
1. `OP_SHA256` + `OP_EQUAL` hashlock
2. 2-of-2 `OP_CHECKMULTISIG`
3. `OP_CSV` + `OP_CHECKSIG`
4. `OP_CHECKSIG`
- A total of *five spending paths*, including four script path spends and one key path spend, all confirmed through on-chain transactions on Bitcoin testnet.
- Merkle tree construction and Taproot output key tweaking (internal key +
script tree root), followed by a **byte-by-byte breakdown of the control
blocks**, including:
- Control byte (leaf version + parity)
- Internal public key
- Ordered Merkle sibling hashes
- *Code-level analysis and experimental validation of how these control blocks* are constructed and verified against the Taproot output, based on actual testnet spends.
- *Witness stack inspection and visualization of script execution*, showing the actual runtime behavior for each script path.
Resources:
*- Part 1: 4-leaf tree construction and testnet validation * https://medium.com/@aaron.recompile/building-a-4-leaf-taproot-tree-in-python-the-first-complete-implementation-on-bitcoin-testnet-c8b66c331f29
*- Part 2: Control block structure and stack visualization * https://medium.com/@aaron.recompile/taproot-control-block-deep-analysis-stack-execution-visualization-5ff10f98032c
- GitHub repo with code, testnet transactions, and diagrams
This project aims to provide a fully verifiable and pedagogical example of non-trivial Taproot path spends, helping developers better understand script trees, control blocks, and Tapscript execution through practical experimentation.
I’d be happy to receive any feedback or discussion around: - Control block design and merkle path optimization - Spend path visibility and privacy tradeoffs - Tooling for visualizing script-path execution and verification
Thanks and best regards, Aaron Zhang [@aaron.recompile](https://medium.com/@aaron.recompile) GitHub: https://github.com/btcstudy