This document proposes new opcodes to be added to the elements network along with the taproot upgrade. The new tapscript OP_SUCCESS
opcodes allow introducing new opcodes more cleanly than through OP_NOP
. In this document, we propose modifying the following OP_SUCCESS
to have the additional semantics. We use opcodes serially OP_SUCCESS196
, 197
... in order
to avoid conflict with bitcoin potentially using OP_SUCESSSx
(assuming bitcoin uses those serially based on availability). The initial version of this document had additional opcodes(OP_FOR
, multi-byte opcodes) has since been updated to this current version in favor of application complexity.
Taproot already increases a lot of resource limitations from segwitv0, so there is no additional need to alter any of those. In particular, from BIP 342
- Script size limit: the maximum script size of 10000 bytes does not apply. Their size is only implicitly bounded by the block weight limit.
- Non-push opcodes limit: The maximum non-push opcodes limit of 201 per script does not apply.
- Sigops limit: 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 CompactSize prefix). Executing a signature opcode (
OP_CHECKSIG
,OP_CHECKSIGVERIFY
, orOP_CHECKSIGADD
) with a non-empty signature decrements the budget by 50. If that brings the budget below zero, the script fails immediately. - Stack + altstack element count limit: 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 the initial stack.
- Stack element size limit: The existing limit of maximum 520 bytes per stack element remains, during the stack machine operations. There is an additional policy rule limiting the initial push size to
80
bytes.
-
Streaming Opcodes for streaming hashes: There is an existing limitation of
MAX_SCRIPT_ELEMENT_SIZE
(520 bytes) because of which we cannot operate hash functions likeOP_SHA256
on messages more than 520 bytes. This allows hashing on more than 520 bytes while still preserving the existing security against resource exhaustion attacks. The proposal for this by Russell O'Connor can be found in the description here.- Define
OP_SUCCESS196
asOP_SHA256INITIALIZE
which pops a bytestring and push SHA256 context creating by adding the bytestring to the initial SHA256 context. - Define
OP_SUCCESS197
asOP_SHA256UPDATE
which first pops a bytestring followed by another pop for SHA256 context and pushes an updated context by adding the bytestring to the data stream being hashed. - Define
OP_SUCCESS198
asOP_SHA256FINALIZE
which first pops a pops a bytestring followed by another pop for SHA256 context and finally pushes a SHA256 hash value after adding the bytestring and completing the padding.
- Define
-
Transaction Introspection codes: Transaction introspection is already possible in elements script by use of
OP_CHECKSIGFROMSTACKVERIFY
, however the current solutions are really expensive in applications like covenants. Therefore, we are not adding any new functionality by supporting introspection, only making it easier to use. The warning still remains the same as with covenants, if the user is inspecting data from parts of the transaction that are not signed, the script can cause unexpected behavior. For opcodes that inspect data that is not committed in sighash, introspection is safe because any changes to the witness data would cause wtxid to change and it would revalidate the tx again. For pegin inputs, the asset/value/script information will be one from the parent chain.- Transaction input introspection opcodes:
- Define
OP_SUCCESS199
asOP_INSPECTINPUTOUTPOINT
: Pop aCScriptNum
input indexidx
and push the outpoint as a tuple. First push thetxid
(32) of theprev_out
, followed by a 4 byte push ofvout
followed by a push for the outpoint_flag(1) as defined in Modified BIP-341 SigMsg for Elements. - Define
OP_SUCCESS200
asOP_INSPECTINPUTASSET
: Pop aCScriptNum
input indexidx
and push thenAsset
onto the stack as two elements. The first push the assetID(32), followed by the prefix(1) - Define
OP_SUCCESS201
asOP_INSPECTINPUTVALUE
: Pop aCScriptNum
input indexidx
and push thenValue
as a tuple, value(8 byte LE, 32) followed by prefix(1), - Define
OP_SUCCESS202
asOP_INSPECTINPUTSCRIPTPUBKEY
: Pop aCScriptNum
input indexidx
and push the following depending the type of scriptPubkey:- If the scriptPubKey is not a native segwit program, push a single sha256 hash of the scriptPubKey on stack top. Next, push a
CScriptNum(-1)
to indicate a non-native segwit scriptPubKey. - If the scriptPubKey is a native segwit program, push the witness program(2-40) followed by a push for segwit version(0-1).
- If the scriptPubKey is not a native segwit program, push a single sha256 hash of the scriptPubKey on stack top. Next, push a
- Define
OP_SUCCESS203
asOP_INSPECTINPUTSEQUENCE
: Pop aCScriptNum
input indexidx
and push thenSequence
(4) as little-endian number. - Define
OP_SUCCESS204
asOP_INSPECTINPUTISSUANCE
: Pop aCScriptNum
input indexidx
and push the assetIssuance information if the asset has issuance, otherwise push an empty vector. Asset Issuance information is pushed as follows- Push
nInflationKeys
as tuple, value(8 byte LE, 32) followed by push for prefix(1). In casenInflationKeys
is null, push a 8 byte LE0
followed by a push for explicit prefix(1). - Push
nAmount
as a tuple, value(8 byte LE, 32) followed by a push for prefix(1). In casenAmount
is null, push a 8 byte LE0
followed by a push for explicit prefix(1). - Push 32 byte
assetEntropy
- Push 32 byte
assetBlindingNonce
- Push
- Define
- Define
OP_SUCCESS205
asOP_PUSHCURRENTINPUTINDEX
that pushes the current input index asCScriptNum
. This can be used in conjunction with input introspection opcodes for inspecting current input. - Output introspection opcodes:
- Define
OP_SUCCESS206
asOP_INSPECTOUTPUTASSET
: Pop aCScriptNum
input indexidx
and push thenAsset
as a tuple, first push the assetID(32), followed by the prefix(1) - Define
OP_SUCCESS207
asOP_INSPECTOUTPUTVALUE
: Pop aCScriptNum
input indexidx
and push thenValue
as a tuple, value(8 byte LE, 32) followed by prefix - Define
OP_SUCCESS208
asOP_INSPECTOUTPUTNONCE
: Pop aCScriptNum
input indexidx
and push thenNonce
(33) onto the stack. If the nonce is null, push an empty vector onto the stack. - Define
OP_SUCCESS209
asOP_INSPECTOUTPUTSCRIPTPUBKEY
: Pop aCScriptNum
input indexidx
and push the scriptPubkey onto the stack.- If the scriptPubKey is not a native segwit program, push a single sha256 hash of the scriptPubKey on stack top. Next, push a
CScriptNum(-1)
to indicate a non-native segwit scriptPubKey. - If the scriptPubKey is a native segwit program, push the witness program(2-40) followed by a push for segwit version(0-1).
- If the scriptPubKey is not a native segwit program, push a single sha256 hash of the scriptPubKey on stack top. Next, push a
- Define
- Transaction introspection opcodes:
- Define
OP_SUCCESS210
asOP_INSPECTVERSION
: Push the nVersion(4) as little-endian. - Define
OP_SUCCESS211
asOP_INSPECTLOCKTIME
: Push the nLockTime(4) as little-endian. - Define
OP_SUCCESS212
asOP_INSPECTNUMINPUTS
: Push the number of inputs as CScriptNum - Define
OP_SUCCESS213
asOP_INSPECTNUMOUTPUTS
: Push the number of outputs as CScriptNum - Define
OP_SUCCESS214
asOP_TXWEIGHT
: Push the transaction weight (8) as little-endian
- Define
- Transaction input introspection opcodes:
-
Signed 64-bit arithmetic opcodes: Current operations on
CScriptNum
as limited to 4 bytes and are difficult to compose because of minimality rules. having a fixed width little operations with 8 byte signed operations helps doing calculations on amounts which are encoded as 8 byte little endian.- When dealing with overflows, we explicitly return the success bit as a
CScriptNum
at the top of the stack and the result being the second element from the top. If the operation overflows, first the operands are pushed onto the stack followed by success bit. [a_second
a_top
] overflows, the stack state after the operation is [a_second
a_top
0
] and if the operation does not overflow, the stack state is [res
1
]. - This gives the user flexibility to deal if they script to have overflows using
OP_IF\OP_ELSE
orOP_VERIFY
the success bit if they expect that operation would never fail. When defining the opcodes which can fail, we only define the success path, and assume the overflow behavior as stated above.
- Define
OP_SUCCESS215
asOP_ADD64
: pop the first number(8 byte LE) asb
followed another pop fora
(8 byte LE). Push a + b onto the stack. Push 1CScriptNum
if there is no overflow. Overflow behavior defined above. - Define
OP_SUCCESS216
asOP_SUB64
: pop the first number(8 byte LE) asb
followed another pop fora
(8 byte LE). Push a - b onto the stack. Push 1CScriptNum
if there is no overflow. Overflow behavior defined above. - Define
OP_SUCCESS217
asOP_MUL64
: pop the first number(8 byte LE) asb
followed another pop fora
(8 byte LE). Pusha*b
onto the stack. Push 1CScriptNum
if there is no overflow. Overflow behavior defined above. - Define
OP_SUCCESS218
asOP_DIV64
: pop the first number(8 byte LE) asb
followed another pop fora
(8 byte LE). First push remaindera%b
(must be non-negative and less than |b|) onto the stack followed by quotient(a//b
) onto the stack. Ifb==0
ora = -2<sup>63</sup> && b = -1
, treat as overflow as defined above. Push 1CScriptNum
if there is no overflow. - Define
OP_SUCCESS219
asOP_NEG64
: pop the first number(8 byte LE) asa
and pushes-a
on the stack top. If the number is-2<sup>63</sup>
(int64_min
) treat as overflow, otherwise pushCScriptNum
1 to indicate no overflow. - Define
OP_SUCCESS220
asOP_LESSTHAN64
(cannot fail!): pop the first number(8 byte LE) asb
followed another pop fora
(8 byte LE). Pusha < b
. - Define
OP_SUCCESS221
asOP_LESSTHANOREQUAL64
(cannot fail!): pop the first number(8 byte LE) asb
followed another pop fora
(8 byte LE). Pusha <= b
. - Define
OP_SUCCESS222
asOP_GREATERTHAN64
(cannot fail!): pop the first number(8 byte LE) asb
followed another pop fora
(8 byte LE). Pusha > b
. - Define
OP_SUCCESS223
asOP_GREATERTHANOREQUAL64
(cannot fail!): pop the first number(8 byte LE) asb
followed another pop fora
(8 byte LE). Pusha >= b
. - Support for binary operations is already available via
OP_AND
,OP_OR
,OP_INVERT
andOP_XOR
- When dealing with overflows, we explicitly return the success bit as a
-
Conversion opcodes: Methods for conversion from
CScriptNum
to8-byte LE
,4-byte LE
.- Define
OP_SUCCESS224
asOP_SCIPTNUMTOLE64
: pop the stack as minimalCSciptNum
, push 8 byte signed LE corresponding to that number. - Define
OP_SUCCESS225
asOP_LE64TOSCIPTNUM
: pop the stack as a 8 byte signed LE. Convert toCScriptNum
and push it, abort on fail. - Define
OP_SUCCESS226
asOP_LE32TOLE64
: pop the stack as a 4 byte unsigned LE. Push the corresponding 8 byte signed LE number. Cannot fail, useful for operating of version, locktime, sequence, number of inputs, number of outputs, weight etc.
- Define
-
Crypto: In order to allow more complex operations on elements, we introduce the following new crypto-operators. Each opcode counts as 50 towards the sigops budget.
- Define
OP_SUCCESS227
asOP_ECMULSCALARVERIFY
which pops three elements from stack as described below: 1) a 32 byte big endian, unsigned scalark
. 2) Compressed EC pointP
, and 3) compressed EC pointQ
. Abort ifP
,Q
is invalid ork
is not 32 bytes and outside of secp256k1 curve order. Abort ifQ != k*P
. - Define
OP_SUCCESS228
asOP_TWEAKVERIFY
with the following semantics: Pop the three elements as: 1) 32 byte X-only internal keyP
, 2) a 32 byte big endian, unsigned scalark
, and 3) 33 byte compressed pointQ
. Abort ifP
,Q
is invalid ork
is not 32 bytes and outside of secp256k1 curve order. Abort ifQ != P + k*G
whereG
is the generator for secp256k1.
- Define
-
Changes to existing Opcodes:
- Add
OP_CHECKSIGFROMSTACK
andOP_CHECKSIGFROMSTACKVERIFY
to follow the semantics from bip340 when witness program is v1. In more detail, the opcodes pops three elements stack 1) 32 bytepk
Xonly public key 2) Variable length messagemsg
and 3) 64 byte Schnorr signaturesig
. Letres = BIP340_verify(pk, msg, sig)
whereBIP340_verify
is defined for elements here. If opcode isOP_CHECKSIGFROMSTACKVERIFY
, abort if the verification fails. - If the opcode is
OP_CHECKSIGFROMSTACK
, push0
if an empty sig is provided and push1
if the verification succeeds. Abort if provided with a non-empty signature that fails the verification. BothOP_CHECKSIGFROMSTACK
andOP_CHECKSIGFROMSTACKVERIFY
count as 50 towards the sigops budget. - Abort if the pubkey is empty, but allow success for non-empty non-32byte keys for future extension of tagged keys.
- Add
- In order to inspect the current input,
OP_PUSHCURRENTINPUTINDEX
can be used in combination withOP_INSPECTINPUTXX
to obtain information about input being spent on stack - The input nNonce field is not consistently stored in elements UTXO database. Therefore, it is not covered in sighash or
wtxid
and hence introspecting it is not possible. - Conversion opcodes can be used be used to convert ScriptNums/LE32 nums to LE64 for operations.