<h4>Before we start</h4>
<p>I realize your question is only about single-sig scenarios, and you want an answer focused on how validation works. I will address these things, but I think I need to elaborate a bit on how P2TR outputs are created, and how scripts fit in first, as I fear it'd be a lot more confusing otherwise.</p>
<p>In what follows I'm going to make a simplification: instead of describing the actual BIP341 taproot rules, I'm going to describe a variant in which P2TR outputs can only be spendable with exactly one script, rather than with a tree of scripts of variable size. This doesn't affect the answer to your question, but avoids the need to explain the tree structure which would distract from the matter.</p>
<h4>Constructing a taproot output</h4>
<p>In this simplified version of taproot, every output can be spent by signing with a key (called <code>P</code>), or by satisfying a script (called <code>S</code>). The key <code>P</code> can be a key that (provably) nobody knows the private key to, in case you want a taproot output than can only be spent by a script. The script <code>S</code> can be <code>OP_FALSE</code> or so, if you want a taproot output that can only be spent by a key (as would be the case for a &quot;single-sig&quot; taproot). But in every case, both <code>P</code> and <code>S</code> are <em>something</em>.</p>
<p>In the leading example, I'm going to assume <code>P</code> is a public key which someone knows the private key <code>d</code> to. I'm also going to assume the script <code>S</code> equals <code>&lt;P1&gt; OP_CHECKSIGVERIFY &lt;P2&gt; OP_CHECKSIG</code>, which is a script that enforces a 2-of-2 policy with keys <code>P1</code> and <code>P2</code>, and consumes two signatures from the stack (one by <code>P1</code>, and one by <code>P2</code>). In other words, our taproot output will have a &quot;spending policy&quot; corresponding to <code>P or (P1 and P2)</code>. At spending time, the spender can either use the &quot;key path&quot; (<code>P</code>), or the &quot;script path&quot; (<code>P1 and P2</code>). The ways of doing that will be very different.</p>
<p>The first step however is creating the taproot output. To do so, the receivers first compute the so-called &quot;tweaked pubkey&quot; <code>Q</code>. This is done using a tweak function: <code>Q = tweak(P,S) = P + hash(P || S)*G</code>. It has two important properties:</p>
<ul>
<li>If someone knows the private key <code>d</code> of <code>P</code>, and they know <code>S</code>, they can compute the private key for <code>Q</code> (namely <code>d + hash(P || S)</code>).</li>
<li>Given <code>Q</code>, or given <code>P</code> and<code> S</code>, nobody can find <em>another</em> P' and S' for which <code>Q = tweak(P',S')</code>.</li>
</ul>
<p><code>Q</code> is still a public key in a way, but it also somehow &quot;commits&quot; to the script <code>S</code> in the sense that only one <code>P</code> and <code>S</code> can be revealed for it. The scriptPubKey in the transaction output will be exactly <code>OP_1 &lt;Q&gt;</code>, so it stores <code>Q</code> without any hashing. The <code>OP_1</code> signifies it is a version 1 witness output with 32-byte push (aka P2TR). In practice, this is done by the receiver(s) encoding <code>Q</code> into a <a href="https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki" rel="noreferrer">BIP350</a> (bech32m) address, and giving it to the sender. The sender's wallet then knows how to construct a transaction output with the intended scriptPubKey. This means the sender knows <code>Q</code>, but they don't know (or care) about <code>P</code> and <code>S</code>.</p>
<h4>Spending and validating spends</h4>
<p>The taproot spending rules now say that any transaction output matching the template <code>OP_1 &lt;32 bytes&gt;</code> is to be treated as a taproot output, and the 32 byte push is to be interpreted as the (tweaked) public key <code>Q</code>. It can be spent in two ways:</p>
<ul>
<li><strong>key path</strong>: the witness stack of the transaction input consists of exactly one element, which is interpreted as a BIP340 signature for public key <code>Q</code>.</li>
<li><strong>script path</strong>: the witness stack of the transaction input consists of two or more elements. The last one is interpreted as the &quot;untweaked&quot; public key <code>P</code>, the penultimate one as the script <code>S</code>, and all the elements before that as the script arguments to script <code>S</code>. To be valid, it must be the case that <code>Q = tweak(P,S)</code> (thereby showing that <code>Q</code> committed to script <code>S</code>, indicating it is permitted to spend), and running the script <code>S</code> with the provided script arguments must return true.</li>
</ul>
<p>So in the case of a key path spend, the (tweaked) public key <code>Q</code> is just treated as a public key, and spending with it is literally just requiring a valid signature with it. The validation code doesn't know or care that <code>Q</code> is actually tweaked in this case, but the wallet of course does: it doesn't sign with its normal private key <code>d</code> (because that's the private key for <code>P</code>), but with the tweaked private key <code>d + hash(P || s)</code> (the private key for <code>Q</code>). So in this case, the witness stack is exactly <code>[sig(Q)]</code>. Note that by <code>sig(Q)</code> I mean &quot;sign with the <em>private</em> key to <code>Q</code>&quot;.</p>
<p>In the case of a script path spend, the signing wallets basically construct a witness that proves &quot;You saw public key <code>Q</code>, but guess what! In fact that wasn't the public key, it's an <code>S</code>-tweaked version of <code>P</code>, and that means I'm also allowed to spend it by satisfying script <code>S</code>!&quot;. In our example, that means the witness stack would be <code>[sig(P2),sig(P1),S,P]</code>; the last two elements prove that <code>Q</code> permitted spending by <code>S</code>, and the first two are script arguments to <code>S</code> providing the required signatures. At that point, the validation just proceeds by running the script <code>S</code> = <code>&lt;P1&gt; OP_CHECKSIGVERIFY &lt;P2&gt; OP_CHECKSIG</code> with inputs <code>[sig(P2),sig(P1)]</code>.</p>
<h4>Why is this useful?</h4>
<p>You may now think... ok, that could work, but what's the point of all this?</p>
<p>The idea is that we've discovered various schemes to make a single public key actually do a whole bunch of things, including the possibility of computing an &quot;aggregate&quot; public key out of a set of other keys, such that all individual private keys are needed to sign for the aggregate (e.g. <a href="https://eprint.iacr.org/2020/1261" rel="noreferrer">MuSig2</a>).</p>
<p>With that, it is possible for the &quot;inner&quot; untweaked public key <code>P</code> to be an aggregate as well. In more complex spending policies than just single sig (e.g. Lightning) it would then be possible to make <code>P</code> be the aggregate of both sides' public keys, and the &quot;cooperative&quot; case of spending would be <em>just</em> a single signature on-chain, which is very cheap. Moreover, as long as this key path can be followed, the entire construction would be indistinguishable from a normal single sig: in both cases the scriptPubKey is <code>OP_1 &lt;Q&gt;</code> for some <code>Q</code>, and the witness stack is just <code>[sig(Q)]</code>.</p>
<h4>Some final notes</h4>
<p>If you want a single-sig taproot output for public key <code>P</code>, it is in fact perfectly possible to just use <code>Q=P</code> directly, and to sign with its untweaked private key <code>d</code> directly. As the validation code does not know or care about the tweaking aspect in the key path spend, this is perfectly legal. BIP341 recommends against doing that, and suggests still tweaking with the equivalent of an invalid script <code>S</code> instead, to deal with situation where someone lies about their public key secretly being an already-tweaked version of some other key. Tweaking whatever key you're going to use with an invalid script guarantees that no valid script path spend can be constructed with it at all.</p>
<p>In the real BIP341 <code>S</code> is not a single script, but the Merkle root of a tree of scripts, where every leaf can be a different one. The tree can be empty (key path only), consist of a single script (like here), or be a giant tree with lots of different leaves. These leaf scripts also have their own internal version number, which would permit introducing new script features that remain indistinguishable from the current taproot scriptPubKeys, as long as those scripts don't actually need to be used. The last witness stack element in the real BIP341 isn't just the untweaked public key <code>P</code>, but it also includes the internal version number of the chosen script, as well as the Merkle path to prove how the revealed script related to the Merkle root (and the resulting version+key+path structure is called the control block). There are various other changes to the script language (documented in BIP342), to the signature hash (see BIP341), and an additional extension mechanism called the annex, which is so far unused. None of those are particularly important here, but I wanted to mention them to make the link with the actual specification.</p>
<p>Ok, I guess I also haven't actually answered your question of explaining what scripts things translate to. That's because there <em>is</em> no real script change. The taproot spending rules aren't really changing how scripts work at all (apart from BIP342). Instead, they're introducing a layer of spending logic <em>between</em> the transaction validation and script validation. In the case of key path spends, there simply is no script to speak of, and instead there is just a signature. In the case of script path spends, the rules change how it is determined which script is used, and how it is determined to be the correct script, but <em>what</em> that script is nothing more than the claimed script directly.</p>

CC BY-SA 4.0
2021-12-04T21:42:07.753Z
2021-12-25T14:11:26.103Z
2021-12-25T14:11:26.103Z
Pieter Wuille
<p>So I <a href="https://learnmeabitcoin.com/technical/p2pkh" rel="noreferrer">have found</a> and fundamentally understand how P2PKH is assembled from inputs and outputs to create a bitcoin <a href="https://en.bitcoin.it/wiki/Script" rel="noreferrer">forth-like script</a> for execution.</p>
<p>I looked at the SegWit BIP <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#examples" rel="noreferrer">examples section</a> and I found them easy enough to understand as well.</p>
<p>But looking at the <a href="https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#constructing-and-spending-taproot-outputs" rel="noreferrer">Taproot BIP</a>, I've likely reached my limit.  It's very well written, but I just can't get a handle on it.</p>
<p>Can anyone look at my recent testnet <a href="https://blockchair.com/bitcoin/testnet/transaction/821c5e4dd98c217efc7e2653b24cfba84369d696cf22ec873ae1f93dff075de1" rel="noreferrer">P2TR spend</a> (<a href="https://api.blockchair.com/bitcoin/testnet/dashboards/transaction/821c5e4dd98c217efc7e2653b24cfba84369d696cf22ec873ae1f93dff075de1" rel="noreferrer">raw-ish JSON</a>) and show me how the <code>witnessData</code>, <code>scriptPubKey</code>, and <code>sigScript</code> data is used to construct a bitcoin forth-like script for execution and validation?  I'm only interested in single-sig Taproot.  After I grasp that perhaps I can expand to multisig-taproot.  I know the python samples are provided in the BIP, but honestly, still just don't get it.</p>
<p>BTW.. I can provide the testnet privkey if needed, though I might need to rebuild the TXN.</p>
<p>I've <a href="https://www.reddit.com/r/Bitcoin/comments/r8wfio/what_is_the_script_assembly_and_execution_in_p2tr/" rel="noreferrer">XPosted on reddit</a> if you lack enough SE-karma to comment.</p>

CC BY-SA 4.0
2021-12-04T20:00:19.820Z
In the case of a P2TR key path spend, there are no scripts involved on chain at all (except the scriptPubKey with is just OP_1 <pubkey>, and an empty scriptSig). That's quite possibly what makes it hard to grasp, but also hard to answer your question as stated.
Pieter Wuille
CC BY-SA 4.0
2021-12-04T20:44:03.313Z
@PieterWuille, fair enough.  Perhaps a better formed question: "What data does a node gather to validate a TXN attempting to spend a single-sig-P2TR output, and how does it use that data to do the validation?"
Dan
CC BY-SA 4.0
2021-12-08T05:56:38.267Z
FWIW, the 'tap' utility in btcdeb lets you create/manipulate -- and 'btcdeb' itself lets you examine details of -- taproot stuff, which may be helpful to learn how things fit together: https://github.com/bitcoin-core/btcdeb/blob/master/doc/tapscript-example-with-tap.md (disclaimer: I am the author)
Kalle
CC BY-SA 4.0
2021-12-08T06:01:09.700Z
@Kalle, Wow... looks great, I'll check it out!  I actually just finished hijacking some of the python in the bitcoin-core test_framework python just to replicate the pay-to-key key tweaks: https://www.reddit.com/r/Bitcoin/comments/r8wfio/what_is_the_script_assembly_and_execution_in_p2tr/hnoojlm/
Dan
<script><taproot><p2tr>
What is the script assembly and execution in P2TR spend (spend from Taproot)
Dan
# What is the script assembly and execution in P2TR spend (spend from Taproot)

(Asked by: username_0 on 2021-12-04)

So I [have found](https://learnmeabitcoin.com/technical/p2pkh) and fundamentally understand how P2PKH is assembled from inputs and outputs to create a bitcoin [forth-like script](https://en.bitcoin.it/wiki/Script) for execution.

I looked at the SegWit BIP [examples section](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#examples) and I found them easy enough to understand as well.

But looking at the [Taproot BIP](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#constructing-and-spending-taproot-outputs), I've likely reached my limit. It's very well written, but I just can't get a handle on it.

Can anyone look at my recent testnet [P2TR spend](https://blockchair.com/bitcoin/testnet/transaction/821c5e4dd98c217efc7e2653b24cfba84369d696cf22ec873ae1f93dff075de1) ([raw-ish JSON](https://api.blockchair.com/bitcoin/testnet/dashboards/transaction/821c5e4dd98c217efc7e2653b24cfba84369d696cf22ec873ae1f93dff075de1)) and show me how the `witnessData`, `scriptPubKey`, and `sigScript` data is used to construct a bitcoin forth-like script for execution and validation? I'm only interested in single-sig Taproot. After I grasp that perhaps I can expand to multisig-taproot. I know the python samples are provided in the BIP, but honestly, still just don't get it.

BTW.. I can provide the testnet privkey if needed, though I might need to rebuild the TXN.

I've [XPosted on reddit](https://www.reddit.com/r/Bitcoin/comments/r8wfio/what_is_the_script_assembly_and_execution_in_p2tr/) if you lack enough SE-karma to comment.

**Tags:** script, taproot, p2tr

> **Comments:**
> **username_1**: In the case of a P2TR key path spend, there are no scripts involved on chain at all (except the scriptPubKey with is just OP_1 <pubkey>, and an empty scriptSig). That's quite possibly what makes it hard to grasp, but also hard to answer your question as stated.  
> **username_0**: @username_1, fair enough.  Perhaps a better formed question: "What data does a node gather to validate a TXN attempting to spend a single-sig-P2TR output, and how does it use that data to do the validation?"  
> **username_2**: FWIW, the 'tap' utility in btcdeb lets you create/manipulate -- and 'btcdeb' itself lets you examine details of -- taproot stuff, which may be helpful to learn how things fit together: https://github.com/bitcoin-core/btcdeb/blob/master/doc/tapscript-example-with-tap.md (disclaimer: I am the author)  
> **username_0**: @username_2, Wow... looks great, I'll check it out!  I actually just finished hijacking some of the python in the bitcoin-core test_framework python just to replicate the pay-to-key key tweaks: https://www.reddit.com/r/Bitcoin/comments/r8wfio/what_is_the_script_assembly_and_execution_in_p2tr/hnoojlm/  

---

## 1 answers

### Answer by username_1 on 2021-12-04. Score: 30 | Accepted answer

#### Before we start

I realize your question is only about single-sig scenarios, and you want an answer focused on how validation works. I will address these things, but I think I need to elaborate a bit on how P2TR outputs are created, and how scripts fit in first, as I fear it'd be a lot more confusing otherwise.

In what follows I'm going to make a simplification: instead of describing the actual BIP341 taproot rules, I'm going to describe a variant in which P2TR outputs can only be spendable with exactly one script, rather than with a tree of scripts of variable size. This doesn't affect the answer to your question, but avoids the need to explain the tree structure which would distract from the matter.

#### Constructing a taproot output

In this simplified version of taproot, every output can be spent by signing with a key (called `P`), or by satisfying a script (called `S`). The key `P` can be a key that (provably) nobody knows the private key to, in case you want a taproot output than can only be spent by a script. The script `S` can be `OP_FALSE` or so, if you want a taproot output that can only be spent by a key (as would be the case for a "single-sig" taproot). But in every case, both `P` and `S` are *something*.

In the leading example, I'm going to assume `P` is a public key which someone knows the private key `d` to. I'm also going to assume the script `S` equals `<P1> OP_CHECKSIGVERIFY <P2> OP_CHECKSIG`, which is a script that enforces a 2-of-2 policy with keys `P1` and `P2`, and consumes two signatures from the stack (one by `P1`, and one by `P2`). In other words, our taproot output will have a "spending policy" corresponding to `P or (P1 and P2)`. At spending time, the spender can either use the "key path" (`P`), or the "script path" (`P1 and P2`). The ways of doing that will be very different.

The first step however is creating the taproot output. To do so, the receivers first compute the so-called "tweaked pubkey" `Q`. This is done using a tweak function: `Q = tweak(P,S) = P + hash(P || S)*G`. It has two important properties:

* If someone knows the private key `d` of `P`, and they know `S`, they can compute the private key for `Q` (namely `d + hash(P || S)`).
* Given `Q`, or given `P` and `S`, nobody can find *another* P' and S' for which `Q = tweak(P',S')`.

`Q` is still a public key in a way, but it also somehow "commits" to the script `S` in the sense that only one `P` and `S` can be revealed for it. The scriptPubKey in the transaction output will be exactly `OP_1 <Q>`, so it stores `Q` without any hashing. The `OP_1` signifies it is a version 1 witness output with 32-byte push (aka P2TR). In practice, this is done by the receiver(s) encoding `Q` into a [BIP350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki) (bech32m) address, and giving it to the sender. The sender's wallet then knows how to construct a transaction output with the intended scriptPubKey. This means the sender knows `Q`, but they don't know (or care) about `P` and `S`.

#### Spending and validating spends

The taproot spending rules now say that any transaction output matching the template `OP_1 <32 bytes>` is to be treated as a taproot output, and the 32 byte push is to be interpreted as the (tweaked) public key `Q`. It can be spent in two ways:

* **key path**: the witness stack of the transaction input consists of exactly one element, which is interpreted as a BIP340 signature for public key `Q`.
* **script path**: the witness stack of the transaction input consists of two or more elements. The last one is interpreted as the "untweaked" public key `P`, the penultimate one as the script `S`, and all the elements before that as the script arguments to script `S`. To be valid, it must be the case that `Q = tweak(P,S)` (thereby showing that `Q` committed to script `S`, indicating it is permitted to spend), and running the script `S` with the provided script arguments must return true.

So in the case of a key path spend, the (tweaked) public key `Q` is just treated as a public key, and spending with it is literally just requiring a valid signature with it. The validation code doesn't know or care that `Q` is actually tweaked in this case, but the wallet of course does: it doesn't sign with its normal private key `d` (because that's the private key for `P`), but with the tweaked private key `d + hash(P || s)` (the private key for `Q`). So in this case, the witness stack is exactly `[sig(Q)]`. Note that by `sig(Q)` I mean "sign with the *private* key to `Q`".

In the case of a script path spend, the signing wallets basically construct a witness that proves "You saw public key `Q`, but guess what! In fact that wasn't the public key, it's an `S`-tweaked version of `P`, and that means I'm also allowed to spend it by satisfying script `S`!". In our example, that means the witness stack would be `[sig(P2),sig(P1),S,P]`; the last two elements prove that `Q` permitted spending by `S`, and the first two are script arguments to `S` providing the required signatures. At that point, the validation just proceeds by running the script `S` = `<P1> OP_CHECKSIGVERIFY <P2> OP_CHECKSIG` with inputs `[sig(P2),sig(P1)]`.

#### Why is this useful?

You may now think... ok, that could work, but what's the point of all this?

The idea is that we've discovered various schemes to make a single public key actually do a whole bunch of things, including the possibility of computing an "aggregate" public key out of a set of other keys, such that all individual private keys are needed to sign for the aggregate (e.g. [MuSig2](https://eprint.iacr.org/2020/1261)).

With that, it is possible for the "inner" untweaked public key `P` to be an aggregate as well. In more complex spending policies than just single sig (e.g. Lightning) it would then be possible to make `P` be the aggregate of both sides' public keys, and the "cooperative" case of spending would be *just* a single signature on-chain, which is very cheap. Moreover, as long as this key path can be followed, the entire construction would be indistinguishable from a normal single sig: in both cases the scriptPubKey is `OP_1 <Q>` for some `Q`, and the witness stack is just `[sig(Q)]`.

#### Some final notes

If you want a single-sig taproot output for public key `P`, it is in fact perfectly possible to just use `Q=P` directly, and to sign with its untweaked private key `d` directly. As the validation code does not know or care about the tweaking aspect in the key path spend, this is perfectly legal. BIP341 recommends against doing that, and suggests still tweaking with the equivalent of an invalid script `S` instead, to deal with situation where someone lies about their public key secretly being an already-tweaked version of some other key. Tweaking whatever key you're going to use with an invalid script guarantees that no valid script path spend can be constructed with it at all.

In the real BIP341 `S` is not a single script, but the Merkle root of a tree of scripts, where every leaf can be a different one. The tree can be empty (key path only), consist of a single script (like here), or be a giant tree with lots of different leaves. These leaf scripts also have their own internal version number, which would permit introducing new script features that remain indistinguishable from the current taproot scriptPubKeys, as long as those scripts don't actually need to be used. The last witness stack element in the real BIP341 isn't just the untweaked public key `P`, but it also includes the internal version number of the chosen script, as well as the Merkle path to prove how the revealed script related to the Merkle root (and the resulting version+key+path structure is called the control block). There are various other changes to the script language (documented in BIP342), to the signature hash (see BIP341), and an additional extension mechanism called the annex, which is so far unused. None of those are particularly important here, but I wanted to mention them to make the link with the actual specification.

Ok, I guess I also haven't actually answered your question of explaining what scripts things translate to. That's because there *is* no real script change. The taproot spending rules aren't really changing how scripts work at all (apart from BIP342). Instead, they're introducing a layer of spending logic *between* the transaction validation and script validation. In the case of key path spends, there simply is no script to speak of, and instead there is just a signature. In the case of script path spends, the rules change how it is determined which script is used, and how it is determined to be the correct script, but *what* that script is nothing more than the claimed script directly.


---

