Bitcoin Book of BIPs

BIP 342

Introduction

Abstract

This document specifies the semantics of the initial scripting system under BIP341.

This document is licensed under the 3-clause BSD license.

Motivation

BIP341 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. While it is possible to deal with these in separate optional improvements, their impact is not guaranteed unless they are addressed simultaneously with BIP341 itself.

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

Design

In order to achieve these goals, signature opcodes OP_CHECKSIG and OP_CHECKSIGVERIFY are modified to verify Schnorr signatures as specified in BIP340 and to use a signature message algorithm based on the common message calculation in BIP341. The tapscript signature message also simplifies OP_CODESEPARATOR handling and makes it more efficient.

The inefficient OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY opcodes are disabled. Instead, a new opcode OP_CHECKSIGADD is introduced to allow creating the same multisignature policies in a batch-verifiable way. Tapscript uses a new, simpler signature opcode limit fixing complicated interactions with transaction weight. Furthermore, a potential malleability vector is eliminated by requiring MINIMALIF.

Tapscript can be upgraded through soft forks by defining unknown key types, for example to add new hash_types or signature algorithms. Additionally, the new tapscript OP_SUCCESS opcodes allow introducing new opcodes more cleanly than through OP_NOP.

Specification

The rules below only apply when validating a transaction input for which all of the conditions below are true:

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

  1. If the input is invalid due to BIP141 or BIP341, fail.
  2. The script as defined in BIP341 (i.e., the penultimate witness stack element after removing the optional annex) is called the tapscript and is decoded into opcodes, one by one:
    1. If any opcode numbered 80, 98, 126-129, 131-134, 137-138, 141-142, 149-153, 187-254 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. These opcodes are renamed to OP_SUCCESS80, …, OP_SUCCESS254, and collectively known as OP_SUCCESSxOP_SUCCESSx OP_SUCCESSx is a mechanism to upgrade the Script system. Using an OP_SUCCESSx before its meaning is defined by a softfork is insecure and leads to fund loss. The inclusion of OP_SUCCESSx in a script will pass it unconditionally. It precedes any script execution rules to avoid the difficulties in specifying various edge cases, for example: OP_SUCCESSx in a script with an input stack larger than 1000 elements, OP_SUCCESSx after too many signature opcodes, or even scripts with conditionals lacking OP_ENDIF. The mere existence of an OP_SUCCESSx anywhere in the script will guarantee a pass for all such cases. OP_SUCCESSx are similar to the OP_RETURN in very early bitcoin versions (v0.1 up to and including v0.3.5). The original OP_RETURN terminates script execution immediately, and return pass or fail based on the top stack element at the moment of termination. This was one of a major design flaws in the original bitcoin protocol as it permitted unconditional third party theft by placing an OP_RETURN in scriptSig. This is not a concern in the present proposal since it is not possible for a third party to inject an OP_SUCCESSx to the validation process, as the OP_SUCCESSx is part of the script (and thus committed to by the taproot output), implying the consent of the coin owner. OP_SUCCESSx can be used for a variety of upgrade possibilities:
    1. If any push opcode fails to decode because it would extend past the end of the tapscript, fail.
  1. 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 (stack size, and size of the elements in the stack; see “Resource Limits” below), fail. Note that this check can be bypassed using OP_SUCCESSx.
  2. The tapscript is executed according to the rules in the following section, with the initial stack as input.
    1. If execution fails for any reason, fail.
    2. If the execution results in anything but exactly one element on the stack which evaluates to true with CastToBool(), fail.
  3. If this step is reached without encountering a failure, validation succeeds.

Script execution

The execution rules for tapscript are based on those for P2WSH according to BIP141, including the OP_CHECKLOCKTIMEVERIFY and OP_CHECKSEQUENCEVERIFY opcodes defined in BIP65 and BIP112, but with the following modifications:

Rules for signature opcodes

The following rules apply to OP_CHECKSIG, OP_CHECKSIGVERIFY, and OP_CHECKSIGADD.

Common Signature Message Extension

We define the tapscript message extension ext to BIP341 Common Signature Message, indicated by ext_flag = 1:

Signature validation

To validate a signature sig with public key p:

In summary, the semantics of signature validation is identical to BIP340, except the following:

  1. The signature message includes the tapscript-specific data key_version.Why does the signature message commit to the key_version? This is for future extensions that define unknown public key types, making sure signatures can’t be moved from one key type to another.
  2. The signature message commits to the executed script through the tapleaf_hash which includes the leaf version and script instead of scriptCode. This implies that this commitment is unaffected by OP_CODESEPARATOR.
  3. The signature message includes the opcode position of the last executed OP_CODESEPARATOR.Why does the signature message include the position of the last executed OP_CODESEPARATOR? This allows continuing to use OP_CODESEPARATOR to sign the executed path of the script. Because the codeseparator_position is the last input to the hash, the SHA256 midstate can be efficiently cached for multiple OP_CODESEPARATORs in a single script. In contrast, the BIP143 handling of OP_CODESEPARATOR is to commit to the executed script only from the last executed OP_CODESEPARATOR onwards which requires unnecessary rehashing of the script. It should be noted that the one known OP_CODESEPARATOR use case of saving a second public key push in a script by sharing the first one between two code branches can be most likely expressed even cheaper by moving each branch into a separate taproot leaf.

Resource limits

In addition to changing the semantics of a number of opcodes, there are also some changes to the resource limitations:

Rationale

Deployment

This proposal is deployed identically to Taproot (BIP341).

Examples

The Taproot (BIP341) test vectors also contain examples for Tapscript execution.

Acknowledgements

This document is the result of many discussions and contains contributions by a number of people. The authors wish to thank all those who provided valuable feedback and reviews, including the participants of the structured reviews.