MASTERING-BITCOIN Β· Interactive Practice
| Formula | Name | Description |
|---|---|---|
| Hashes revealed (balanced tree) | Sibling hashes along path to root for leaves | |
| Bytes revealed | Each hash is 32 bytes (SHA-256) | |
| Expected cost (Huffman tree) | Probability of using leaf at depth | |
| Merkle node hash | Each parent commits to its two children |
A Merkle tree compresses many leaves (spending scripts) into a single 32-byte root. To prove that one specific leaf belongs in the tree, you only need that leaf plus the sibling hash at each level along its path to the root.
Adjust the number of leaves and pick which one is being "spent" to see the proof path.
β interactive visualization β coming to this page format soon
Notice: As you double the number of leaves, the tree grows by only one extra level β so the proof grows by exactly one extra hash. That's the logarithmic scaling that makes MAST so powerful.
A legacy Bitcoin script with OP_IF branches must include every branch on chain when spent β
cost grows linearly with the number of conditions.
MAST reveals only one script + logβ(n) sibling hashes β cost grows logarithmically.
Adjust the average bytes per branch and see where the two curves diverge.
β interactive visualization β coming to this page format soon
Reflect: The legacy curve crashes through the 10,000-byte cap quickly β that ceiling is a hard limit. The MAST curve barely moves. Doubling adds exactly one 32-byte hash.
A balanced tree treats every spending path equally. But your daily 2-of-3 multisig might be used 90% of the time, while a 12-month timelock fallback fires almost never. Huffman coding places frequent leaves shallow (cheap) and rare leaves deep.
Adjust the probabilities and see how the expected number of revealed hashes changes.
β interactive visualization β coming to this page format soon
Notice: When the daily spend dominates (probability close to 1), Huffman shaping pushes the expected reveal close to 1 hash per spend β even cheaper than a balanced tree. The tree shape itself stays hidden on chain, so this optimization has no privacy cost.
## Quiz Questions
Question 1 Β· Hashes Revealed for n = 8
A MAST commitment contains 8 spending conditions in a balanced tree. When you spend, how many sibling hashes must you reveal along with the chosen script?
β Correct! β one sibling hash per level.
β Not quite. The proof size scales with the depth of the tree, not its leaf count. Use .
Solution:
The number of sibling hashes equals the depth of the tree:
A balanced tree with 8 leaves has depth 3, so the path from any leaf to the root crosses 3 levels β and at each level you must reveal exactly one sibling hash to let the verifier re-hash up to the root. Total: 3 hashes = 96 bytes.
Question 2 Β· Privacy Property
True or False: By examining a MAST spending transaction on the blockchain, an outside observer can determine how many unused spending conditions were committed in the original output.
β Correct! Unused branches are committed but invisible β observers see only the revealed leaf and its proof hashes.
β Not quite. A core privacy benefit of MAST is that the existence of unused branches stays hidden.
Solution:
The answer is False.
A MAST spend reveals one script and a small stack of sibling hashes. From those hashes alone, an observer cannot tell whether the tree had 2 leaves or 2 trillion leaves β every internal hash looks identical to a random 32-byte value.
A simple wallet and an enterprise vault with thousands of contingency clauses produce indistinguishable on-chain spends. The unused branches don't just stay private β even their existence stays private.
(One subtlety: the depth of the path is visible from the proof length, but Huffman shaping plus padding can be used to obscure even that.)
Question 3 Β· Bytes Revealed for n = 1024
A vault commits to 1,024 distinct spending conditions in a balanced MAST tree. Each SHA-256 hash is 32 bytes.
How many bytes of sibling hashes (not counting the revealed script itself) appear on chain when the vault is spent?
β Correct! bytes.
β That's the linear answer (). MAST scales logarithmically β you only reveal one hash per tree level.
β Not quite. Use . For , the depth is 10.
Solution:
Compute the depth:
Bytes revealed for the proof:
That's 320 bytes of sibling hashes to commit to 1,024 different spending conditions. For comparison, an equivalent OP_IF script with 1,024 branches at even 50 bytes per branch would need 51,200 bytes β already 5Γ the legacy 10,000-byte cap, making such a script impossible to construct under legacy rules.
Question 4 Β· Why Use Huffman Shaping?
Suppose your wallet has three spending paths with usage probabilities: daily multisig (90%), cold-key recovery (9%), and timelock fallback (1%).
What is the primary reason to use a Huffman-shaped MAST tree (placing the daily path at depth 1) instead of a balanced tree?
β Correct! Frequent leaves shallow, rare leaves deep β average cost drops dramatically.
β Not quite. Think about which path is taken most often, and what that implies for the average number of hashes revealed per spend.
Solution:
Huffman shaping minimizes the expected proof size:
With the daily path at depth 1 (used 90% of the time):
vs. a balanced 4-leaf tree where every path is at depth 2:
That's a 45% reduction in average fees, with no loss of security or privacy. The other options are wrong:
Solved: 0 / 4