Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Taproot: key path and script path

After this lesson: I can work out a Taproot script tree's root from its leaves.

70 min lesson2 commandsBitcoin Script lab: timelocks, multisig, Miniscript and Taproot on a regtest node

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

WordWhat it means
Key pathspending with one signature from the output key
Script pathspending by showing one script and proving it is in the tree
Leafone Tapscript in the tree
Merkle rootone hash that stands for every leaf
Tweaka number added to the inner key to commit to the root
Control blockthe 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

Command
docker exec bitcoind sh steps/taproot.sh

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: the same coin, two spends

SpendWitnessAllowedvsize
Key pathOne signaturetrue99
Script path, Bob's leafSignature, leaf, control blocktrue125

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

Command
docker exec jupyter python3 steps/tapkeys.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: from leaves to address

StepValue the lab printed
Leaf: Bob signsf0da2e156a4ad480..
Leaf: Carol after 144 blocksa04f26c685e157df..
Rootac56eadca6277808..
Output key5ed39505e70149c1..
Addressbcrt1ptmfe2p08..., the same as the node's

The cells students fill in are left blank in this preview.

Try it: tagged hashes

Code: python
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.

About this lab and its source: Bitcoin Script lab: timelocks, multisig, Miniscript and Taproot on a regtest node

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.