A spare key nobody needs to see
In the last lesson, spending a P2WSH output showed every branch of its script, even the ones nobody used. Anybody could read the backup plan.
Taproot fixes that. When everybody agrees, a spend looks like a plain single signature. A backup script is shown only when it is used, and the other scripts stay hidden.
By the end of this lesson
Note
I can work out a Taproot script tree's root from its leaves.
Key words
| Word | What it means |
|---|---|
| Key path | spending with one signature from the output key |
| Script path | spending by showing one script and proving it is in the tree |
| Leaf | one Tapscript in the tree |
| Merkle root | one hash that stands for every leaf |
| Tweak | a number added to the inner key to commit to the root |
| Control block | the inner key and the hashes that prove a leaf is in the tree |
The cells students fill in are left blank in this preview.
One output, two ways to spend
A Taproot output holds one 32-byte key, the output key. It is an inner key, here Alice's, with a tweak added that commits to a tree of scripts.
On the key path, the owner signs with the tweaked key and the witness is one 64-byte Schnorr signature. On the script path, the spender shows one leaf, its inputs and a control block.
Spend the Taproot output both ways
docker exec bitcoind sh steps/taproot.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: the same coin, two spends
| Spend | Witness | Allowed | vsize |
|---|---|---|---|
| Key path | One signature | true | 99 |
| Script path, Bob's leaf | Signature, leaf, control block | true | 125 |
The cells students fill in are left blank in this preview.
Leaves hash up to one root
Each leaf is a Tapscript. Here one says "Bob signs" and one says "Carol signs after 144 blocks". A leaf is hashed with the tag TapLeaf.
Two hashes are joined with the tag TapBranch, smaller one first, until one root is left. The tweak is a TapTweak hash of the inner key and that root.
Work out the output key in Python
docker exec jupyter python3 steps/tapkeys.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: from leaves to address
| Step | Value the lab printed |
|---|---|
| Leaf: Bob signs | f0da2e156a4ad480.. |
| Leaf: Carol after 144 blocks | a04f26c685e157df.. |
| Root | ac56eadca6277808.. |
| Output key | 5ed39505e70149c1.. |
| Address | bcrt1ptmfe2p08..., the same as the node's |
The cells students fill in are left blank in this preview.
Try it: tagged hashes
import hashlib
def tagged(tag, data):
t = hashlib.sha256(tag.encode()).digest()
return hashlib.sha256(t + t + data).digest()
# The same bytes under two tags give two unrelated hashes.
print(tagged("TapLeaf", b"hello").hex()[:16])
print(tagged("TapBranch", b"hello").hex()[:16])
# A branch sorts its two children first, so their order does not matter.
a = tagged("TapLeaf", b"left")
b = tagged("TapLeaf", b"right")
print(tagged("TapBranch", min(a, b) + max(a, b)).hex()[:16])
# Swap a and b in the line above and run again. Does the branch change?
Why Taproot helps privacy
A key path spend looks like any other single signature, so nobody learns there was a tree. A script path spend shows one leaf and a path of hashes, and the other leaves stay hidden.
The control block is 33 bytes plus 32 for each level of the tree. Bob's leaf is one level down, so its control block is 65 bytes.
A common mistake
Warning
Many people think Taproot hides everything about a spend. The amount, the output and a used leaf are still public. Taproot hides the scripts that were not used, and the tree itself on a key path spend.