Pay to Taproot
So far, we've seen many types of scriptPubKey in the previous modules: P2PKH, P2WPKH, P2SH, and P2WSH.
Reminder of the previous scripts
OP_DUP OP_HASH160 <20B pubkey hash> OP_EQUALVERIFY OP_CHECKSIGP2WPKHOP_0 <20B pubkey hash>P2SHOP_HASH160 <20B script hash> OP_EQUALP2WSHOP_0 <32B script hash>In this lesson, we will learn about a new type of scriptPubKey called P2TR.
P2TR has the following format:
OP_1 <32B tweaked key>OP_1means segwit version 1<32B tweaked key>is what we are going to spend the next lessons explaining
Segwit v0 vs v1
Segwit v0 is used in P2WPKH and P2WSH:
OP_0 <20B pubkey hash>P2WSHOP_0 <32B script hash>Both start with OP_0, and that is what marks them as segwit v0.
Segwit v1 is used in P2TR, and it is the one we are going to learn about in this module:
OP_1 <32B tweaked key>So far, P2TR looks like a simple script.
One way to spend it is with the private key corresponding to the <32B tweaked key>. This is called a key-path spend.
But the tweaked key is built in a way that makes it interesting. Let's see how.
Tweaking a public key
The term "tweak" means we start with an original public key and make a change to it to get a tweaked public key.
And that's exactly what we are going to do here.
Let's say we have an original public key .
Tweaking it means altering it with a value, which we call the tweak , to get a new tweaked public key :
There's a lot going on here, so let's break this equation into two parts: , the original public key, and .
Part 1:
is our original public key. Let's call its private key .
That's just scalar multiplication, the operation that turns a private key into a public key . We can think of it as adding the generator point to itself times.
is also a point on the secp256k1 curve.
Part 2:
Now notice the second part of the equation: .
Doesn't this also look like a normal point on the secp256k1 curve, with acting like a private key?
That's exactly what it is mathematically. is a point made by adding to itself times.
We are going to call this point , so it is easier to reference:
- is the tweak point
- is the tweak value
Putting it back together
So , the tweaked public key, is just the addition of two points: , the original public key, and , the tweak point.
Adding the two curve points and gives us a point whose private key is the sum of and .
The tweaked private key is therefore:
Here, is the order of the secp256k1 generator point.
That means if we have funds locked to the tweaked public key , one way to spend them is by signing with the tweaked private key .
This works because is just another point on the curve, created through point addition.
The P2TR output
Remember the scriptPubKey we saw at the beginning:
OP_1 <32B tweaked key>That <32B tweaked key> is the we just computed.
OP_1 <32B tweaked key> = OP_1 <Q> = OP_1 <P + T> = OP_1 <d·G + t·G>A tweaked key looks like any other public key
From the outside, looks like any other public key.
Someone looking at it cannot tell whether it was created by tweaking another key or generated directly from a private key. The original key and the tweak remain hidden.
This is one of Taproot's privacy properties.
Right now, the tweak looks like just an arbitrary value. In the next lessons, we will see how we make it commit to data.
That is where Taproot gets interesting.
Signing with a tweaked keypair
Prove in code that the tweaked private key signs for the tweaked public key. Skip it if you only want the theory.
We said that funds locked to can be spent using the tweaked private key .
Let's prove it in code.
Fill in the two tweaked values, then press Run.
- T is already t*G. Add the two points with add(P, T).
- The tweaked private key is the sum of the scalars, reduced mod n: (d + t) % n.
Solution code
In this example, the verifier only sees . It has no idea that the key was built by adding a tweak.
In the next lessons, we will make that tweak commit to data, which is where Taproot gets powerful.
