Skip to content
BIP ATLAS

BIP 0342

What changes when Bitcoin runs a Taproot script?

A Taproot script can count signatures one at a time. Empty slots, failed checks and the cost of verification each have a precise meaning.

A script can require several people to approve a payment. In tapscript, it checks their signatures one at a time and counts the ones that pass. A signer who does not take part leaves an empty slot. An invalid signature stops the script.1234

BIP 342 defines these execution rules for Taproot. It replaces the old multisignature opcode, changes how signature checks are charged, and reserves space for future upgrades. The script still has to be the one the output committed to; BIP 341 checks that part before tapscript runs.567891011

Source revision
bitcoin/bips @ 3a10b5b
Source checked
1 October 2026
Review state
Awaiting review
Source details

This chapter is an independent explanation, not the proposal itself. Preamble fields are shown as the pinned files record them, without author e-mail addresses. Status is the proposal’s own field. It is not an endorsement or a sign of consensus.

BIP 342: Validation of Taproot Scripts

BIP
342
Layer
Consensus (soft fork)
Title
Validation of Taproot Scripts
Authors
Pieter Wuille
Jonas Nick
Anthony Towns
Status
Deployed
Type
Specification
Assigned
2020-01-19
License
BSD-3-Clause
Requires
340, 341

Pinned source · Reader view on bips.dev
SHA-256 29641340adec741b695be99ae3dc6e2fd025ea6566f9d6c2544ed5d47db4d822
Git blob d073b701ce785ab497d5d409640a16727af3a25b

Fig. A08.1 Counting the signatures that arrive

Counting signatures in a symbolic policyA symbolic illustration of a 2-of-3 policy, not a recorded script run: no keys or signatures from a test vector are drawn. Signature, empty slot, signature: the count goes 1, 1, 2. CHECKSIG starts the count; CHECKSIGADD adds one for a valid signature or zero for an empty one. NUMEQUAL compares the result with the required count. A non-empty invalid signature fails immediately. Separately, Core case 804's failure witness reaches the disabled OP_CHECKMULTISIG after 5 opcodes and fails.SYMBOLIC POLICY · 2 OF 3 KEYS · NOT A RECORDED RUNA slot can be empty. A bad signature cannot pass.SIGNATUREvalidKEY 1CHECKSIG1contributes oneEMPTY SLOTno signatureKEY 2CHECKSIGADD1contributes zeroSIGNATUREvalidKEY 3CHECKSIGADD2contributes oneNUMEQUAL · REQUIRED 22 = 2 ✓ POLICY SATISFIED✕ OP_CHECKMULTISIG IS DISABLEDCORE 804: FAILS AFTER 5 OPCODES
A symbolic threshold policy: each valid signature adds one, an empty slot adds nothing, and the final count must equal the script’s requirement. OP_CHECKMULTISIG itself is disabled in tapscript.14912

From the witness to the stack

The witness supplies the script, its control block, and the data the script will use. After Taproot has checked the commitment, that data becomes the starting stack. Normal execution must finish with exactly one true element; OP_SUCCESS opcodes, discussed below, bypass execution entirely.7813

Fig. A08.2 What reaches the interpreter

Two specifications, one witnessThe success witness of Bitcoin Core test case 1135 has 4 items. item 0 · empty; item 1 · 64-byte signature; item 2 · script · 70 B; item 3 · control block · 545 B. The control block is a byte of leaf version 0xc0 and parity, the 32-byte internal key and 16 sibling hashes. BIP 341 uses the last two items to check that the output key commits to this script. BIP 342 then runs the script, <32-byte push> OP_CHECKSIGVERIFY OP_0 <32-byte push> OP_CHECKSIGADD OP_NOT, starting from the items before it as the stack.WITNESS · CORE CASE 1135 · 4 ITEMSITEM 0 IS THE STACK BOTTOMitem 0 · emptyitem 1 · 64-byte signatureitem 2 · script · 70 BP16 hashes · 545 BCONTROL BLOCK: VERSION · KEY · SIBLINGSBIP 342RUNS THE SCRIPTON THE ITEMSBEFORE ITBIP 341CHECKS THECOMMITMENTTHE SCRIPT, OPCODE BY OPCODE<32-byte push> OP_CHECKSIGVERIFY OP_0 <32-byte push>OP_CHECKSIGADD OP_NOT
Exact values
Item 0: empty
(empty)
Item 1: 64-byte signature
1cecfe0d8b353338f9dedfa5c45db0772cf198993d57e2319259269d205ba8a088be60cf39fcc2c59ea87e4ab7ca064d867de18fa7d1f50ddd5d4e5690cdf5d0
Item 2: script
209847ef0ad775f9346edae702efe32dc8eaac2bc3a8482f0ecdce80d5b0925316ad00201aa6528fd172599ed6f26cb8805585a15351223e227553b7d801c6d1e35f0709ba91
Item 3: control block
c01aa6528fd172599ed6f26cb8805585a15351223e227553b7d801c6d1e35f0709f3a0a337de1fa532836649a83d4aa4eea0ae1aef6ceef39a59ec84f4c36c9844e697090c38cdd136089aa96d7e96ab202e14d1db036209e3d6ef01f27d1f085f8d126abebfa82a3460dac97671090919723b96b15f7ff7f4b41167b0ab5263fc0c2c68756d9e61526932c27b7a05d5fadad11855be99c68ad16b680e6b9f5afbc061209f5aacea30877cd90ba497cfe490352b34d02c9a5c06fdc33cdd3f70c18e4695e4dd1862d6259e3869829b052f5b1babbc86f24b3c84f473b41eb157c7fd1e3e225359879a5c87acf1400a3927c2a1289da1402c90fc61419146226526c1d91c3685d0145cb0e5af0783a322a5c585fcbb79eaaec856aeb3832a635630257d811b43362257f965815995abdbadc13e11f946ddce5a2d140d3200b87d0f9f81a72f027b51937d2cff45253f975c9adf948df0894137a19d139119fb52f4581f841da186c25e6c93872326b8eeb4dd7ddfb8bb4bca17ecf56325d9a6405f12fb14cccdb2bc4d194b0f21db169073d7f99c97007c07998973375fb25201dfd2fb1e9ba4ce3ec3bb610f59be1e4e640852d0bd99878bd9edf6cfcbf81e0fb8ae195b104bed02c59d313df5d3c5e0d916882fb8700ec9821a593e156e50e81f91e2bd9c9fdae9a9b851abb477069d76ccdd945c25580db704ba3e6b4976c77231b18197417090cf318d49cfd1af20bb89102bfa4f1be729591828ddc81a358f
The script and control block establish what may run. The remaining witness items become its inputs.7814
Where tapscript applies and the order of validation

BIP 342 applies only in one situation: a version 1, 32-byte Taproot output, spent by its script path, with leaf version 0xc0. Spends with other leaf versions simply succeed under BIP 341, leaving them for future rules. The checks happen in a fixed order. First, anything BIP 141 or BIP 341 rejects is rejected. Then the script is decoded, the starting stack is checked against resource limits, the script runs, and it must finish with exactly one element on the stack, and that element must be true.78

The execution rules themselves start from those of P2WSH, SegWit version 0’s script spends, and change a short list of things. The rest of this chapter walks through that list.15

An empty slot is different from a bad signature

OP_CHECKSIG and OP_CHECKSIGVERIFY use 32-byte public keys to check BIP 340 Schnorr signatures. The signed message is BIP 341’s signature message (SigMsg) plus a tapscript extension: the leaf hash, a key version byte and the position of the last executed OP_CODESEPARATOR. A signature therefore authorizes this script, through its leaf hash.1617

A signature opcode fails if it cannot take the required items from the stack or if the public key is empty. With a 32-byte key, a non-empty signature must pass BIP 340 verification. A bad signature ends the script immediately; it does not merely contribute false.218

An empty signature deliberately skips that check. OP_CHECKSIG pushes false and continues; OP_CHECKSIGVERIFY fails. A non-empty key of another length is reserved for future signature schemes: today, a non-empty signature with such a key counts as success without cryptographic verification.319

Fig. A08.3 What a signature opcode does

What a signature opcode doesA signature opcode pops a public key and a signature (CHECKSIGADD also pops a number n). bad args: Empty key, too few items, or n over 4 bytes. must fail; recorded witnesses here: 1135 ✕, 1109 ✕. empty sig: Not checked. CHECKSIG pushes empty, CHECKSIGADD pushes n, CHECKSIGVERIFY MUST fail. costs 0; recorded witnesses here: 1135 ✓. 32-b key, sig: BIP 340 check. Valid: push 1, push n + 1, or carry on. Invalid: the script fails. costs 50; recorded witnesses here: 1135 ✓, 1135 ✕, 1109 ✓, 804 ✓, 662 ✓, 824 ✕. other key, sig: Unknown key type: not checked, counts as a success. costs 50; recorded witnesses here: 824 ✓. The last three trays apply to a non-empty signature.POP A KEY AND A SIGNATURESIGNATURE OPCODESBAD ARGSEmpty key, toofew items, orn over 4bytes.MUST FAILRECORDED:case 1135 ✕case 1109 ✕EMPTY SIGNot checked.CHECKSIGpushes empty,CHECKSIGADDpushes n,CHECKSIGVERIFYMUST fail.COSTS 0RECORDED:case 1135 ✓32-B KEY, SIGBIP 340 check.Valid: push 1,push n + 1, orcarry on.Invalid: thescript fails.COSTS 50RECORDED:case 1135 ✓case 1135 ✕case 1109 ✓case 804 ✓case 662 ✓case 824 ✕OTHER KEY, SIGUnknown keytype: notchecked,counts as asuccess.COSTS 50RECORDED:case 824 ✓
The rules for CHECKSIG, CHECKSIGVERIFY and CHECKSIGADD, one tray per shape of key and signature. Under each tray, the recorded witnesses that land in it.2310181920

Count, then compare

OP_CHECKSIGADD takes a public key, a running count n and a signature. A valid signature adds one to n; an empty signature leaves n unchanged. A threshold script can check its first key with OP_CHECKSIG, use OP_CHECKSIGADD for each remaining key, then compare the total with the required count. Each key has its own witness slot.13420

Start with the empty-signature example below. Compare its accepted and rejected witnesses, then step to the first difference. An empty signature and an empty public key have different effects.31421

Fig. A08.4 Recorded tapscript runs

Static view: the success witness of the first case, played to the end. With JavaScript you can step through each of the 6 cases opcode by opcode, compare the success and failure witnesses, and watch the budget.

A recorded tapscript runBitcoin Core test case 1135, success witness. Script: <32-byte push> OP_CHECKSIGVERIFY OP_0 <32-byte push> OP_CHECKSIGADD OP_NOT. Initial stack, top first: 64-byte signature, empty. Budget 736. Step 1, <32-byte push>: push 32 bytes; stack after, top first: 32-byte key, 64-byte signature, empty. Step 2, OP_CHECKSIGVERIFY: signature valid: continue; stack after, top first: empty; budget 686. Step 3, OP_0: push the empty vector; stack after, top first: empty, empty. Step 4, <32-byte push>: push 32 bytes; stack after, top first: 32-byte key, empty, empty. Step 5, OP_CHECKSIGADD: empty signature: push n = 0; stack after, top first: empty; budget 686. Step 6, OP_NOT: 0 becomes 1; stack after, top first: number 1. Valid: exactly one true element remains. Bitcoin Core labels this witness valid; the recording agrees.SCRIPT · CORE CASE 1135 · SUCCESS WITNESS<32-byte push>OP_CHECKSIGVERIFYOP_0<32-byte push>OP_CHECKSIGADDOP_NOTSTACK, TOP HIGHESTnumber 1 · topBUDGET686OF 7361 SIG COUNTED✓VALID
A recorded tapscript runBitcoin Core test case 1135, success witness. Script: <32-byte push> OP_CHECKSIGVERIFY OP_0 <32-byte push> OP_CHECKSIGADD OP_NOT. Initial stack, top first: 64-byte signature, empty. Budget 736. Step 1, <32-byte push>: push 32 bytes; stack after, top first: 32-byte key, 64-byte signature, empty. Step 2, OP_CHECKSIGVERIFY: signature valid: continue; stack after, top first: empty; budget 686. Step 3, OP_0: push the empty vector; stack after, top first: empty, empty. Step 4, <32-byte push>: push 32 bytes; stack after, top first: 32-byte key, empty, empty. Step 5, OP_CHECKSIGADD: empty signature: push n = 0; stack after, top first: empty; budget 686. Step 6, OP_NOT: 0 becomes 1; stack after, top first: number 1. Valid: exactly one true element remains. Bitcoin Core labels this witness valid; the recording agrees.SCRIPT · CORE CASE 1135 · SUCCESS WITNESS<32-byte push>OP_CHECKSIGVERIFYOP_0<32-byte push>OP_CHECKSIGADDOP_NOTSTACK, TOP HIGHESTnumber 1 · topBUDGET686OF 7361 SIG COUNTED✓VALID

Step 6 of 6, OP_NOT: 0 becomes 1. Valid: exactly one true element remains. Bitcoin Core labels this witness valid; the recording agrees.

Exact values for this witness
Script
209847ef0ad775f9346edae702efe32dc8eaac2bc3a8482f0ecdce80d5b0925316ad00201aa6528fd172599ed6f26cb8805585a15351223e227553b7d801c6d1e35f0709ba91
empty
(empty)
64-byte signature
1cecfe0d8b353338f9dedfa5c45db0772cf198993d57e2319259269d205ba8a088be60cf39fcc2c59ea87e4ab7ca064d867de18fa7d1f50ddd5d4e5690cdf5d0
32-byte key
9847ef0ad775f9346edae702efe32dc8eaac2bc3a8482f0ecdce80d5b0925316
32-byte key
1aa6528fd172599ed6f26cb8805585a15351223e227553b7d801c6d1e35f0709
number 1
01
Witness size
686 bytes = 1 (item count) + initial stack 66 + script 71 + control block 548, each with its length prefix

Bitcoin Core qa-assets script_assets_test.json, case 1135 (“tapscript/emptysigs/checksigadd”), pinned by commit and linked from BIP 341. Each control block was checked against its output key first, as in Fig. A07.6.

Six Core test cases, twelve recorded witnesses. Every recorded verdict matches Core’s own label, and every control block was checked against its output key first.1012142122232425
Why CHECKMULTISIG was replaced

Tapscript disables OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY. Run one, and the script fails on the spot, exactly as OP_RETURN would; in a branch that is not taken it is ignored. The authors chose an outright failure so that anyone still writing the old opcode notices immediately.9

Its replacement is OP_CHECKSIGADD. It pops a key, a number n and a signature; a valid signature pushes n + 1 and an empty one pushes n unchanged. The authors note that the old multisignature opcodes cannot be batch verified, and that the new one does the work of four existing opcodes in one byte. Their suggested rewrite of a k-of-n policy is a chain: check the first key with OP_CHECKSIG, add each further key with OP_CHECKSIGADD, and compare the total with k. Each signer’s slot in the witness holds either a signature or an empty vector.1342026

That is not the only design. They also describe putting each k-of-k combination in its own leaf, aggregating each combination into one key, or using threshold signatures, each trading privacy, cost and interaction differently.27

The recorded examples

The explorer replays six spends from Bitcoin Core’s validation tests, each with a witness Core accepts and one it rejects. Step through them opcode by opcode. In one, a valid signature turns 23 into 24 and the script compares the result; in its failing twin, n is five bytes long, and the BIP says that MUST fail. Another hands OP_CHECKSIGADD an empty signature and pays nothing for it, while its failure script hands it an empty key instead.142122

The CHECKMULTISIG case shows the change most plainly. Its success witness runs a one-key OP_CHECKSIG. Its failure witness tries the same 1-of-1 policy with OP_CHECKMULTISIG, which fails the moment it executes, before any signature is looked at.912

The witness pays for its checks

Older scripts count signature operations against a limit for the whole block. Tapscript signature operations are left out of that count. Instead each input gets its own budget: 50 plus the size of its witness in bytes. Every signature opcode run with a non-empty signature subtracts 50, and if the budget drops below zero the script fails. The authors’ reason is practical: a second block-wide limit, alongside weight, made it harder to choose transactions for a block.1028

A normal signature brings enough bytes to pay for its own check. With a 32-byte key, it is 64 or 65 bytes long, plus a length prefix; its check costs 50. An empty signature costs nothing. Unknown key types still incur the charge for a non-empty signature, even though they skip verification.10192429

Fig. A08.5 A signature pays its own way

A signature pays its own wayA 64-byte signature (as in Core case 1135) adds 65 serialized witness bytes, so 65 to the budget; its check costs 50: net +15. An empty signature (as in Core case 1135) adds 1 serialized witness byte, so 1 to the budget; it is not checked and costs nothing: net +1.64-BYTE SIGNATURE · CASE 1135L+65 WITNESS BYTES= +65 BUDGETCHECK −50NET +15EMPTY SIGNATURE · CASE 1135L+1 WITNESS BYTES= +1 BUDGETNOT CHECKED, −0NET +1L = THE ITEM'S LENGTH BYTE
A checked signature brings its own bytes and a length byte into the witness, and so into the budget; its check costs less than that. An empty signature costs nothing.31029
Other resource limits

Other limits move too. The 10,000-byte script size limit and the 201 non-push opcode limit do not apply to tapscript. The limit of 1,000 stack elements remains and now also covers the starting stack taken from the witness, and elements still may not exceed 520 bytes.30

Rules left for a future upgrade

OP_SUCCESSx opcodes leave room for future soft forks. After the BIP 141 and BIP 341 checks, encountering one while decoding makes the spend valid immediately. The interpreter does not run the script, and even undecodable bytes after that opcode do not change the result. The recorded example uses OP_SUCCESS126. The BIP warns that using an undefined OP_SUCCESS opcode is insecure and leads to fund loss.111325

Fig. A08.6 A trapdoor in the decoder

  1. Core case 50: an OP_SUCCESS opcode, step 1 of 2The script 7e decodes to OP_SUCCESS126. OP_SUCCESS126 found while decoding: validation succeeds without executing anything.DECODER0x7eOP_SUCCESS126TRAPDOOR✓VALIDRUN, STACK CHECKS:NEVER REACHED

    Success witness: the script is the byte 0x7e, OP_SUCCESS126. The decoder stops there and the spend is valid; nothing runs.

  2. Core case 50: an OP_SUCCESS opcode, step 2 of 2The script 61 decodes to OP_NOP; it runs and execution ends with 0 elements; exactly one is required.DECODER0x61OP_NOPRUNS · STACK AFTER: EMPTY✕INVALID

    Failure witness: the script is the byte 0x61, OP_NOP. It runs, leaves no element, and fails: execution ends with 0 elements; exactly one is required.

Core case 50’s two witnesses. An OP_SUCCESS opcode ends validation, valid, before anything runs; OP_NOP runs and leaves an empty stack.81325

One rule went the other way, from advice to consensus. In P2WSH, MINIMALIF is only a standardness rule; in tapscript, OP_IF and OP_NOTIF must receive exactly an empty vector or the single byte 0x01. In the recorded MINIMALIF case, the failing witness offers a three-byte value instead, and the script stops at OP_IF.2331

Fig. A08.7 OP_IF takes 0x01 or the empty vector

OP_IF takes 0x01 or the empty vectorUnder MINIMALIF, OP_IF and OP_NOTIF accept only an empty vector or the single byte 0x01. In Bitcoin Core case 662, the success witness gives OP_IF the byte 01, which fits. The failure witness gives it 003031, a 3-byte value: MINIMALIF: the argument must be empty or exactly 0x01, not a 3-byte value.THE ONLY SHAPES OP_IF TAKESSUCCESS WITNESS0x01empty0x01✓FITSFAILURE WITNESS0x003031empty0x01✕STOPS
Core case 662: the success witness gives OP_IF the byte 0x01; the failure witness gives it a longer value, and the script stops there.2331
Caveat

The figure is a player for recorded runs, not a script interpreter. The recorder behind it supports a small reviewed set of opcodes and refuses anything else, so it cannot quietly accept a script it does not understand. A trace reaching true here says nothing about scripts outside these six cases.14

Sources and the limits of these recordings

BIP 342 was assigned in 2020 and deployed with BIP 341. Its authors changed the language alongside Taproot because Schnorr signatures, batch validation and the new signature hash required different opcode behavior.632

BIP 342 says that BIP 341’s test vectors contain tapscript examples. The wallet vector file pinned for this site contains tapscript leaves but no script path spends, so there is nothing there to execute. BIP 341 also links Bitcoin Core’s validation test vectors, on the main branch of Core’s test-asset repository. This site chose one commit of that file, and this chapter uses six cases from it, copied and checked by hash.3334

Two of the cases are upgrade hooks: an OP_SUCCESS opcode and an unknown key type. Core’s tests show what consensus accepts, not what is safe; the BIP warns against using OP_SUCCESS opcodes before a soft fork defines them, and unknown key types exist only so that one can.11192425

Each recorded run was checked twice at build time: the control block against the output key, as in the previous chapter, and the final verdict against Core’s label. If either check had disagreed, this page would not have been built.14

Taproot checks that the revealed script belongs to the output. Tapscript then checks whether the witness data satisfies that script. A commitment proof and successful execution do different jobs.78

Evidence

Each numbered marker in the text points here, and the ↩ links lead back to the text. Quotes are verbatim from the BIP files at commit 3a10b5b, wiki markup included. Links open the exact lines. Labels say what kind of statement each entry is: a rule, the author’s rationale, history, a test vector, or our own inference.

  1. ↩ a b c d Rule Opcode 186 is OP_CHECKSIGADD, a new signature opcode added by BIP 342.

    Quoted source text (2)

    The opcode 186 (<code>0xba</code>) is named as <code>OP_CHECKSIGADD</code>.

    BIP 342, line 76

    and a new opcode <code>OP_CHECKSIGADD</code> is added.

    BIP 342, line 75
  2. ↩ a b c Rule With a 32-byte key, a non-empty signature is checked as a BIP 340 signature, and a failed check ends the script with failure.

    Quoted source text (1)

    If the public key size is 32 bytes, it is considered to be a public key as described in BIP340: ** If the signature is not the empty vector, the signature is validated against the public key (see the next subsection). Validation failure in this case immediately terminates script execution with failure.

    BIP 342, lines 92–93
  3. ↩ a b c d e f g Rule An empty signature is not checked: CHECKSIGVERIFY MUST fail, CHECKSIG pushes an empty vector, and CHECKSIGADD pushes n unchanged.

    Quoted source text (1)

    If the signature is the empty vector: *** For <code>OP_CHECKSIGVERIFY</code>, the script MUST fail and terminate immediately. *** For <code>OP_CHECKSIG</code>, an empty vector is pushed onto the stack, and execution continues with the next opcode. *** For <code>OP_CHECKSIGADD</code>, a <code>CScriptNum</code> with value <code>n</code> is pushed onto the stack, and execution continues with the next opcode.

    BIP 342, lines 96–99
  4. ↩ a b c d Author’s rationale In a rationale footnote, the authors show that a CHECKMULTISIG policy can be rewritten as <pubkey_1> CHECKSIG <pubkey_2> CHECKSIGADD … m NUMEQUAL, with one witness element per key that is either a signature or empty.

    Quoted source text (1)

    can be rewritten as script <code><pubkey_1> CHECKSIG <pubkey_2> CHECKSIGADD ... <pubkey_n> CHECKSIGADD m NUMEQUAL</code> with witness <code><w_n> ... <w_1></code>. Every witness element <code>w_i</code> is either a signature corresponding to <code>pubkey_i</code> or an empty vector.

    BIP 342, line 77
  5. ↩ Rule BIP 342 specifies the semantics of the initial scripting system under BIP 341.

    Quoted source text (1)

    This document specifies the semantics of the initial scripting system under [[bip-0341.mediawiki|BIP341]].

    BIP 342, line 20
  6. ↩ a b Author’s rationale The authors write that BIP 341 changes only the script structure, while some of its goals conflict with how certain opcodes behave, so Schnorr signatures, batch validation and the new signature hash had to reach script spends as well.

    Quoted source text (3)

    proposes improvements to just the script structure, but some of its goals are incompatible with the semantics of certain opcodes within the scripting language itself.

    BIP 342, line 28

    their impact is not guaranteed unless they are addressed simultaneously with [[bip-0341.mediawiki|BIP341]] itself.

    BIP 342, line 29

    Specifically, the goal is making '''Schnorr signatures''', '''batch validation''', and '''signature hash''' improvements available to spends that use the script system as well.

    BIP 342, line 31
  7. ↩ a b c d e Rule The rules apply only to a witness version 1, 32-byte, non-P2SH Taproot script path spend whose leaf version is 0xc0; under BIP 341, spends with other leaf versions succeed, leaving them for future rules.

    Quoted source text (2)

    This implies that for the future leaf versions (non-''0xC0'') the execution must succeed.

    BIP 341, line 81

    It is a '''taproot spend''' as defined in [[bip-0341.mediawiki#design|BIP341]] (i.e., the witness version is 1, the witness program is 32 bytes, and it is not P2SH wrapped). * It is a '''script path spend''' as defined in [[bip-0341.mediawiki#design|BIP341]] (i.e., after removing the optional annex from the witness stack, two or more stack elements remain). * The leaf version is ''0xc0''

    BIP 342, lines 50–52
  8. ↩ a b c d e f Rule Validation must be equivalent to these steps in order: fail if BIP 141 or BIP 341 fails; decode the tapscript; check the initial stack's resource limits; execute; and require exactly one true element at the end.

    Quoted source text (3)

    Validation of such inputs must be equivalent to performing the following steps in the specified order. # If the input is invalid due to BIP141 or BIP341, fail.

    BIP 342, lines 54–55

    If the '''initial stack''' as defined in BIP341 (i.e., the witness stack after removing both the optional annex and the two last stack elements after that) violates any resource limits

    BIP 342, line 63

    If the execution results in anything but exactly one element on the stack which evaluates to true with <code>CastToBool()</code>, fail.

    BIP 342, line 66
  9. ↩ a b c d Rule OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY are disabled: like OP_RETURN, they fail the script immediately when executed and are ignored in unexecuted branches. The authors chose this so that anyone who keeps using them notices immediately.

    Quoted source text (2)

    The disabled opcodes behave in the same way as <code>OP_RETURN</code>, by failing and terminating the script immediately when executed, and being ignored when found in unexecuted branch of the script.

    BIP 342, line 72

    This is a precaution to make sure people who accidentally keep using <code>OP_CHECKMULTISIG</code> in Tapscript notice a problem immediately.

    BIP 342, line 72
  10. ↩ a b c d e f Rule Tapscript signature operations do not count toward the block-wide sigops limit; instead each input has a budget of 50 plus its serialized witness size, and every signature opcode with a non-empty signature subtracts 50, failing the script if the budget goes below zero. Unknown key types count too.

    Quoted source text (1)

    The sigops in tapscripts do not count towards the block-wide limit of 80000 (weighted). Instead, there is a per-script sigops ''budget''. The budget equals 50 + the total serialized size in bytes of the transaction input's witness (including the <code>CompactSize</code> prefix). Executing a signature opcode (<code>OP_CHECKSIG</code>, <code>OP_CHECKSIGVERIFY</code>, or <code>OP_CHECKSIGADD</code>) with a non-empty signature decrements the budget by 50. If that brings the budget below zero, the script fails immediately. Signature opcodes with unknown public key type and non-empty signature are also counted.

    BIP 342, line 130
  11. ↩ a b c Author’s rationale The authors describe OP_SUCCESSx as an upgrade mechanism: a soft fork can give one a meaning, only ever turning valid scripts invalid, and they allow new opcodes more cleanly than OP_NOP. In a rationale footnote they warn that using one before it is defined is insecure and leads to fund loss.

    Quoted source text (4)

    Additionally, the new tapscript <code>OP_SUCCESS</code> opcodes allow introducing new opcodes more cleanly than through <code>OP_NOP</code>.

    BIP 342, line 44

    is a mechanism to upgrade the Script system.

    BIP 342, line 57

    Any rule changes to an <code>OP_SUCCESSx</code>-containing script may only turn a valid script into an invalid one, and this is always achievable with softforks.

    BIP 342, line 58

    Using an <code>OP_SUCCESSx</code> before its meaning is defined by a softfork is insecure and leads to fund loss.

    BIP 342, line 57
  12. ↩ a b c Test vector In Core case 804 the success witness's script is <key> CHECKSIG; the failure witness tries a 1-of-1 CHECKMULTISIG, which fails as soon as it runs.

    Quoted source text (1)

    by failing and terminating the script immediately when executed

    BIP 342, line 72
  13. ↩ a b c Rule After the BIP 141 and BIP 341 checks, any OP_SUCCESSx opcode met while decoding the script makes validation succeed, before the remaining tapscript rules and even if later bytes would not decode; a push that runs past the end fails.

    Quoted source text (2)

    is encountered, validation succeeds (none of the rules below apply). This is true even if later bytes in the tapscript would fail to decode otherwise.

    BIP 342, line 57

    If any push opcode fails to decode because it would extend past the end of the tapscript, fail.

    BIP 342, line 62
  14. ↩ a b c d e f Test vector For each of the six recorded Core cases, the recorder's verdict on both the success and the failure witness matches Core's label, and every control block passes the BIP 341 commitment check; the recorder refuses any opcode outside its reviewed set rather than guessing.

    Quoted source text (1)

    Validation of such inputs must be equivalent to performing the following steps in the specified order.

    BIP 342, line 54
  15. ↩ Rule Tapscript execution follows the P2WSH rules of BIP 141 with a list of modifications.

    Quoted source text (2)

    The execution rules for tapscript are based on those for P2WSH according to BIP141

    BIP 342, line 71

    but with the following modifications:

    BIP 342, line 71
  16. ↩ Rule OP_CHECKSIG and OP_CHECKSIGVERIFY verify BIP 340 Schnorr signatures, over a signature message based on BIP 341's.

    Quoted source text (1)

    signature opcodes <code>OP_CHECKSIG</code> and <code>OP_CHECKSIGVERIFY</code> are modified to verify Schnorr signatures as specified in [[bip-0340.mediawiki|BIP340]] and to use a signature message algorithm based on the common message calculation in [[bip-0341.mediawiki|BIP341]].

    BIP 342, line 35
  17. ↩ Rule A tapscript signature signs hash_TapSighash(0x00 || SigMsg(hash_type, 1) || ext), where ext adds the tapleaf hash, a key version 0x00 and the position of the last executed OP_CODESEPARATOR; it commits to the executed script through the tapleaf hash rather than a scriptCode.

    Quoted source text (3)

    If the ''sig'' is 64 bytes long, return ''Verify(p, hash<sub>TapSighash</sub>(0x00 || SigMsg(0x00, 1) || ext), sig)''

    BIP 342, line 116

    ''tapleaf_hash'' (32): the tapleaf hash as defined in [[bip-0341.mediawiki#design|BIP341]] * ''key_version'' (1): a constant value ''0x00'' representing the current version of public keys in the tapscript signature opcode execution. * ''codesep_pos'' (4): the opcode position of the last executed <code>OP_CODESEPARATOR</code>

    BIP 342, lines 108–110

    The signature message commits to the executed script through the ''tapleaf_hash'' which includes the leaf version and script instead of ''scriptCode''.

    BIP 342, line 122
  18. ↩ a b Rule CHECKSIG and CHECKSIGVERIFY pop a public key and a signature; CHECKSIGADD also pops a number n between them. Too few elements, an n longer than 4 bytes, or an empty public key MUST make the script fail immediately.

    Quoted source text (1)

    For <code>OP_CHECKSIGVERIFY</code> and <code>OP_CHECKSIG</code>, the public key (top element) and a signature (second to top element) are popped from the stack. ** If fewer than 2 elements are on the stack, the script MUST fail and terminate immediately. * For <code>OP_CHECKSIGADD</code>, the public key (top element), a <code>CScriptNum</code> <code>n</code> (second to top element), and a signature (third to top element) are popped from the stack. ** If fewer than 3 elements are on the stack, the script MUST fail and terminate immediately. ** If <code>n</code> is larger than 4 bytes, the script MUST fail and terminate immediately. * If the public key size is zero, the script MUST fail and terminate immediately.

    BIP 342, lines 86–91
  19. ↩ a b c d Rule A public key that is neither empty nor 32 bytes is an unknown key type: no signature check is applied and a non-empty signature counts as successful, leaving room for soft forks to add new signature rules.

    Quoted source text (3)

    If the public key size is not zero and not 32 bytes, the public key is of an ''unknown public key type''

    BIP 342, line 94

    and no actual signature verification is applied. During script execution of signature opcodes they behave exactly as known public key types except that signature validation is considered to be successful.

    BIP 342, line 94

    allow adding new signature validation rules through softforks.

    BIP 342, line 94
  20. ↩ a b c Rule A non-empty signature counts toward the sigops budget; then CHECKSIGVERIFY continues, CHECKSIG pushes 0x01, and CHECKSIGADD pushes n + 1.

    Quoted source text (1)

    If the signature is not the empty vector, the opcode is counted towards the sigops budget (see further). *** For <code>OP_CHECKSIGVERIFY</code>, execution continues without any further changes to the stack. *** For <code>OP_CHECKSIG</code>, a 1-byte value <code>0x01</code> is pushed onto the stack. *** For <code>OP_CHECKSIGADD</code>, a <code>CScriptNum</code> with value of <code>n + 1</code> is pushed onto the stack.

    BIP 342, lines 100–103
  21. ↩ a b c Test vector In Core case 1135 the script checks one signature with CHECKSIGVERIFY and gives CHECKSIGADD an empty signature, which leaves n = 0 and costs no budget; the failure witness's script hands CHECKSIGADD an empty public key, which stops it.

    Quoted source text (2)

    For <code>OP_CHECKSIGADD</code>, a <code>CScriptNum</code> with value <code>n</code> is pushed onto the stack

    BIP 342, line 99

    If the public key size is zero, the script MUST fail and terminate immediately.

    BIP 342, line 91
  22. ↩ a b Test vector In Core case 1109, CHECKSIGADD with n = 23 and a valid signature leaves 24, which equals the pushed 24; the failure witness's 5-byte n stops the script.

    Quoted source text (2)

    If <code>n</code> is larger than 4 bytes, the script MUST fail and terminate immediately.

    BIP 342, line 90

    For <code>OP_CHECKSIGADD</code>, a <code>CScriptNum</code> with value of <code>n + 1</code> is pushed onto the stack.

    BIP 342, line 103
  23. ↩ a b c Test vector In Core case 662 the success witness gives OP_IF the byte 0x01; the failure witness gives it a 3-byte value, which MINIMALIF rejects.

    Quoted source text (1)

    must be either exactly 0 (the empty vector) or exactly 1 (the one-byte vector with value 1)

    BIP 342, line 73
  24. ↩ a b c Test vector In Core case 824 a 33-byte key makes CHECKSIG skip verification and push 1, while still spending 50 of the budget; the failure witness uses the 32-byte form of the same key, so the signature is checked for real and fails.

    Quoted source text (2)

    If the public key size is not zero and not 32 bytes, the public key is of an ''unknown public key type''

    BIP 342, line 94

    Signature opcodes with unknown public key type and non-empty signature are also counted.

    BIP 342, line 130
  25. ↩ a b c d Test vector In Core case 50 the success script is the single opcode 126, an OP_SUCCESSx, so the spend is valid without running anything; the failure script is OP_NOP, which leaves an empty stack and fails.

    Quoted source text (2)

    ## If any opcode numbered ''80, 98, 126-129

    BIP 342, lines 56–57

    If the execution results in anything but exactly one element on the stack which evaluates to true

    BIP 342, line 66
  26. ↩ Author’s rationale In a rationale footnote, the authors say OP_CHECKSIGADD compensates for CHECKMULTISIG-like opcodes, which are incompatible with batch verification, and that it is equivalent to OP_ROT OP_SWAP OP_CHECKSIG OP_ADD in one byte.

    Quoted source text (1)

    This opcode is added to compensate for the loss of <code>OP_CHECKMULTISIG</code>-like opcodes, which are incompatible with batch verification. <code>OP_CHECKSIGADD</code> is functionally equivalent to <code>OP_ROT OP_SWAP OP_CHECKSIG OP_ADD</code>, but only takes 1 byte.

    BIP 342, line 76
  27. ↩ Author’s rationale The authors list other ways to express k-of-n: a k-of-k script per combination in separate leaves, an aggregated key per combination, or native threshold signatures, each with trade-offs.

    Quoted source text (3)

    A ''k''-of-''n'' policy can be implemented by splitting the script into several leaves of the Merkle tree, each implementing a ''k''-of-''k'' policy

    BIP 342, line 78

    This approach is far more efficient, but does require a 3-round interactive signing protocol to jointly produce the (single) signature.

    BIP 342, line 79

    Multisig policies can also be realized with [http://cacr.uwaterloo.ca/techreports/2001/corr2001-13.ps threshold signatures] using verifiable secret sharing.

    BIP 342, line 80
  28. ↩ Author’s rationale The authors explain that tying signature operations to witness weight removes a second constraint that made block construction harder.

    Quoted source text (1)

    The old sigop limit makes transaction selection in block construction unnecessarily difficult because it is a second constraint in addition to weight. Instead, the number of tapscript signature opcodes is limited by witness weight.

    BIP 342, line 130
  29. ↩ a b Editorial inference A signature checked against a 32-byte key must be 64 or 65 bytes, so with its length prefix it adds at least 65 bytes, more than the 50 its check costs; an unknown-key-type signature can be shorter and is still charged 50.

    Quoted source text (3)

    If the ''sig'' is 64 bytes long, return ''Verify(p, hash<sub>TapSighash</sub>(0x00 || SigMsg(0x00, 1) || ext), sig)''

    BIP 342, lines 116–118

    * Otherwise, fail.

    BIP 342, line 118

    Signature opcodes with unknown public key type and non-empty signature are also counted.

    BIP 342, line 130
  30. ↩ Rule The 10,000-byte script size limit and the 201 non-push opcode limit do not apply; the 1,000-element stack limit remains and is extended to the initial stack, and the 520-byte element limit remains for the initial stack and pushes.

    Quoted source text (3)

    The maximum script size of 10000 bytes does not apply.

    BIP 342, line 128

    The maximum non-push opcodes limit of 201 per script does not apply.

    BIP 342, line 129

    The existing limit of 1000 elements in the stack and altstack together after every executed opcode remains. It is extended to also apply to the size of initial stack. * '''Stack element size limit''' The existing limit of maximum 520 bytes per stack element remains, both in the initial stack and in push opcodes.

    BIP 342, lines 131–132
  31. ↩ a b Rule MINIMALIF, only a standardness rule in P2WSH, is consensus in tapscript: OP_IF and OP_NOTIF take exactly the empty vector or the single byte 0x01.

    Quoted source text (1)

    The MINIMALIF rules, which are only a standardness rule in P2WSH, are consensus enforced in tapscript. This means that the input argument to the <code>OP_IF</code> and <code>OP_NOTIF</code> opcodes must be either exactly 0 (the empty vector) or exactly 1 (the one-byte vector with value 1)

    BIP 342, line 73
  32. ↩ History BIP 342 is a consensus soft-fork proposal assigned in 2020; its preamble records the status Deployed and that it requires BIPs 340 and 341, and it is deployed identically to BIP 341.

    Quoted source text (5)

    Layer: Consensus (soft fork)

    BIP 342, line 3

    Status: Deployed

    BIP 342, line 8

    Assigned: 2020-01-19

    BIP 342, line 10

    Requires: 340, 341

    BIP 342, line 13

    This proposal is deployed identically to Taproot ([[bip-0341.mediawiki|BIP341]]).

    BIP 342, line 140
  33. ↩ Rule BIP 342 says the BIP 341 test vectors also contain tapscript examples; BIP 341 links Bitcoin Core's validation test vectors, used in its unit test framework, on that repository's main branch.

    Quoted source text (2)

    The Taproot ([[bip-0341.mediawiki|BIP341]]) test vectors also contain examples for Tapscript execution.

    BIP 342, line 144

    Validation test vectors used in the [https://github.com/bitcoin/bitcoin/blob/3820090bd619ac85ab35eff376c03136fe4a9f04/src/test/script_tests.cpp#L1718 Bitcoin Core unit test framework] can be found [https://github.com/bitcoin-core/qa-assets/blob/main/unit_test_data/script_assets_test.json?raw=true here].

    BIP 341, line 304
  34. ↩ Editorial inference The wallet vector file in this snapshot holds tapscript leaves but publishes no script path spends, so the traces here come from the Core validation cases BIP 341 links to, pinned at a fixed commit.

    Quoted source text (2)

    For each of its BIP341 inputs, the internal private key and the Merkle root it was derived from is given, as well as the expected witness to spend it.

    BIP 341, line 301

    Test vectors for wallet operation (scriptPubKey computation, key path spending, control block construction) can be found

    BIP 341, line 298