比特币(BSV)知识库:脚本-比特币脚本中的操作码(Opcodes used in Bitcoin Script)

BSV知识库 专栏收录该内容
73 篇文章 3 订阅

特别提示:

比特币(BSV)知识库-Bitcoin wiki-目前为全英文内容,暂无中文译文,并且仍在持续编写和补充中。欢迎中国的开发者在文章底部评论,进行阐述和探讨。

Opcodes used in Bitcoin Script

This is a list of all Script words, also known as opcodes, commands, or functions.

OP_NOP1-OP_NOP10 were originally set aside to be used when HASH and other security functions become insecure due to improvements in computing.

False is zero or negative zero (using any number of bytes) or an empty array, and True is anything else.

Constants

When talking about scripts, these value-pushing words are usually omitted.

WordOpcodeHexInputOutputDescription
OP_0, OP_FALSE00x00Nothing.(empty value)An empty array of bytes is pushed onto the stack. (This is not a no-op: an item is added to the stack.)
Pushdata Bytelength1-750x01-0x4b(special)dataThe next opcode bytes is data to be pushed onto the stack
OP_PUSHDATA1760x4c(special)dataThe next byte contains the number of bytes to be pushed onto the stack.
OP_PUSHDATA2770x4d(special)dataThe next two bytes contain the number of bytes to be pushed onto the stack in little endian order.
OP_PUSHDATA4780x4e(special)dataThe next four bytes contain the number of bytes to be pushed onto the stack in little endian order.
OP_1NEGATE790x4fNothing.-1The number -1 is pushed onto the stack.
OP_1, OP_TRUE810x51Nothing.1The number 1 is pushed onto the stack.
OP_2-OP_1682-960x52-0x60Nothing.2-16The number in the word name (2-16) is pushed onto the stack.

Flow control

WordOpcodeHexInputOutputDescription
OP_NOP970x61NothingNothingDoes nothing.
OP_VER DISABLED980x62NothingProtocol versionPuts the version of the protocol under which this transaction will be evaluated onto the stack.

(This opcode is scheduled to be re-enabled in the Chronicle update)

OP_IF990x63<expression> IF
   [statements>

ELSE

   [statements]

ENDIF

If the top stack value is not FALSE, the statements between IF and ELSE are executed.

If the top stack value is FALSE the statements between ELSE and ENDIF are executed.
The top stack value is removed.

OP_NOTIF1000x64<expression> NOTIF
   [statements]

ELSE

   [statements]

ENDIF

If the top stack value is FALSE, the statements between IF and ELSE are executed.

If the top stack value is not FALSE the statements between ELSE and ENDIF are executed.
The top stack value is removed.

OP_VERIF DISABLED1010x65<version> VERIF
   [statements]

ELSE

   [statements]

ENDIF

If the top stack value is EQUAL to the version of the protocol under which this transaction will be evaluated, the statements between IF and ELSE are executed.

If the top stack value is NOT EQUAL to the version of the protocol under which this transaction will be evaluated, the statements between ELSE and ENDIF are executed.
The top stack value is removed. (This opcode is scheduled to be re-enabled in the Chronicle update)

OP_VERNOTIF DISABLED1020x66<version> VERNOTIF
   [statements]

ELSE

   [statements]

ENDIF

If the top stack value is NOT EQUAL to the version of the protocol under which this transaction will be evaluated, the statements between IF and ELSE are executed.

If the top stack value is EQUAL to the version of the protocol under which this transaction will be evaluated, the statements between ELSE and ENDIF are executed.
The top stack value is removed. (This opcode is scheduled to be re-enabled in the Chronicle update)

OP_ELSE1030x67<expression> if [statements] [else [statements]]* endifIf the preceding OP_IF or OP_NOTIF or OP_ELSE was not executed then these statements are and if the preceding OP_IF or OP_NOTIF or OP_ELSE was executed then these statements are not.
OP_ENDIF1040x68<expression> if [statements] [else [statements]]* endifEnds an if/else block. All blocks must end, or the transaction is invalid. An OP_ENDIF without a prior matching OP_IF or OP_NOTIF is also invalid.
OP_VERIFY1050x69True / falseNothing / failMarks transaction as invalid if top stack value is not true. The top stack value is removed.
OP_RETURN1060x6aNothingEnds script with top value on stack as final resultOP_RETURN can also be used to create "False Return" outputs with a scriptPubKey consisting of OP_FALSE OP_RETURN followed by data. Such outputs are provably unspendable and should be given a value of zero Satoshis. These outputs can be pruned from storage in the UTXO set, reducing its size. Currently the BitcoinSV network supports multiple FALSE RETURN outputs in a given transaction with each one capable of holding up to 100kB of data. After the Genesis upgrade in 2020 miners will be free to mine transactions containing FALSE RETURN outputs of any size.

Stack

WordOpcodeHexInputOutputDescription
OP_TOALTSTACK1070x6bx1(alt)x1Puts the input onto the top of the alt stack. Removes it from the main stack.
OP_FROMALTSTACK1080x6c(alt)x1x1Puts the input onto the top of the main stack. Removes it from the alt stack.
OP_2DROP1090x6dx1 x2NothingRemoves the top two stack items.
OP_2DUP1100x6ex1 x2x1 x2 x1 x2Duplicates the top two stack items.
OP_3DUP1110x6fx1 x2 x3x1 x2 x3 x1 x2 x3Duplicates the top three stack items.
OP_2OVER1120x70x1 x2 x3 x4x1 x2 x3 x4 x1 x2Copies the pair of items two spaces back in the stack to the front.
OP_2ROT1130x71x1 x2 x3 x4 x5 x6x3 x4 x5 x6 x1 x2The fifth and sixth items back are moved to the top of the stack.
OP_2SWAP1140x72x1 x2 x3 x4x3 x4 x1 x2Swaps the top two pairs of items.
OP_IFDUP1150x73xx / x xIf the top stack value is not 0, duplicate it.
OP_DEPTH1160x74Nothing<Stack size>Counts the number of stack items onto the stack and places the value on the top
OP_DROP1170x75xNothingRemoves the top stack item.
OP_DUP1180x76xx xDuplicates the top stack item.
OP_NIP1190x77x1 x2x2Removes the second-to-top stack item.
OP_OVER1200x78x1 x2x1 x2 x1Copies the second-to-top stack item to the top.
OP_PICK1210x79xn ... x2 x1 x0 <n>xn ... x2 x1 x0 xnThe item n back in the stack is copied to the top.
OP_ROLL1220x7axn ... x2 x1 x0 <n>... x2 x1 x0 xnThe item n back in the stack is moved to the top.
OP_ROT1230x7bx1 x2 x3x2 x3 x1The top three items on the stack are rotated to the left.
OP_SWAP1240x7cx1 x2x2 x1The top two items on the stack are swapped.
OP_TUCK1250x7dx1 x2x2 x1 x2The item at the top of the stack is copied and inserted before the second-to-top item.

Data Manipulation

WordOpcodeHexInputOutputDescription
OP_CAT1260x7ex1 x2outConcatenates two strings.
OP_SPLIT1270x7fx nx1 x2Splits byte sequence x at position n.
OP_NUM2BIN1280x80a boutConverts numeric value a into byte sequence of length b.
OP_BIN2NUM1290x81xoutConverts byte sequence x into a numeric value.
OP_SIZE1300x82inin sizePushes the string length of the top element of the stack (without popping it).

Bitwise logic

WordOpcodeHexInputOutputDescription
OP_INVERT1310x83inoutFlips all of the bits in the input.
OP_AND1320x84x1 x2outBoolean and between each bit in the inputs.
OP_OR1330x85x1 x2outBoolean or between each bit in the inputs.
OP_XOR1340x86x1 x2outBoolean exclusive or between each bit in the inputs.
OP_EQUAL1350x87x1 x2True / falseReturns 1 if the inputs are exactly equal, 0 otherwise.
OP_EQUALVERIFY1360x88x1 x2Nothing / failSame as OP_EQUAL, but runs OP_VERIFY afterward.

Arithmetic

Note: Arithmetic inputs are limited to signed 32-bit integers, but may overflow their output.

If any input value for any of these commands is longer than 4 bytes, the script must abort and fail.

WordOpcodeHexInputOutputDescription
OP_1ADD1390x8binout1 is added to the input.
OP_1SUB1400x8cinout1 is subtracted from the input.
OP_2MUL DISABLED1410x8dinoutThe input is multiplied by 2. (This opcode is scheduled to be re-enabled in the Chronicle update)
OP_2DIV DISABLED1420x8einoutThe input is divided by 2. (This opcode is scheduled to be re-enabled in the Chronicle update)
OP_NEGATE1430x8finoutThe sign of the input is flipped.
OP_ABS1440x90inoutThe input is made positive.
OP_NOT1450x91inoutIf the input is 0 or 1, it is flipped. Otherwise the output will be 0.
OP_0NOTEQUAL1460x92inoutReturns 0 if the input is 0. 1 otherwise.
OP_ADD1470x93a bouta is added to b.
OP_SUB1480x94a boutb is subtracted from a.
OP_MUL1490x95a bouta is multiplied by b.
OP_DIV1500x96a bouta is divided by b.
OP_MOD1510x97a boutReturns the remainder after dividing a by b.
OP_LSHIFT1520x98a boutLogical left shift b bits. Sign data is discarded
OP_RSHIFT1530x99a boutLogical right shift b bits. Sign data is discarded
OP_BOOLAND1540x9aa boutIf both a and b are not 0, the output is 1. Otherwise 0.
OP_BOOLOR1550x9ba boutIf a or b is not 0, the output is 1. Otherwise 0.
OP_NUMEQUAL1560x9ca boutReturns 1 if the numbers are equal, 0 otherwise.
OP_NUMEQUALVERIFY1570x9da bNothing / failSame as OP_NUMEQUAL, but runs OP_VERIFY afterward.
OP_NUMNOTEQUAL1580x9ea boutReturns 1 if the numbers are not equal, 0 otherwise.
OP_LESSTHAN1590x9fa boutReturns 1 if a is less than b, 0 otherwise.
OP_GREATERTHAN1600xa0a boutReturns 1 if a is greater than b, 0 otherwise.
OP_LESSTHANOREQUAL1610xa1a boutReturns 1 if a is less than or equal to b, 0 otherwise.
OP_GREATERTHANOREQUAL1620xa2a boutReturns 1 if a is greater than or equal to b, 0 otherwise.
OP_MIN1630xa3a boutReturns the smaller of a and b.
OP_MAX1640xa4a boutReturns the larger of a and b.
OP_WITHIN1650xa5x min maxoutReturns 1 if x is within the specified range (left-inclusive), 0 otherwise.

Cryptography

WordOpcodeHexInputOutputDescription
OP_RIPEMD1601660xa6inhashThe input is hashed using RIPEMD-160.
OP_SHA11670xa7inhashThe input is hashed using SHA-1.
OP_SHA2561680xa8inhashThe input is hashed using SHA-256.
OP_HASH1601690xa9inhashThe input is hashed twice: first with SHA-256 and then with RIPEMD-160.
OP_HASH2561700xaainhashThe input is hashed two times with SHA-256.
OP_CODESEPARATOR1710xabNothingNothingAll of the signature checking words will only match signatures to the data after the most recently-executed OP_CODESEPARATOR.
OP_CHECKSIG1720xacsig pubkeyTrue / falseThe entire transaction's outputs, inputs, and script (from the most recently-executed OP_CODESEPARATOR to the end) are hashed. The signature used by OP_CHECKSIG must be a valid signature for this hash and public key. If it is, 1 is returned, 0 otherwise.
OP_CHECKSIGVERIFY1730xadsig pubkeyNothing / failSame as OP_CHECKSIG, but OP_VERIFY is executed afterward.
OP_CHECKMULTISIG1740xaex sig1 sig2 ... <number of signatures> pub1 pub2 <number of public keys>True / FalseCompares the first signature against each public key until it finds an ECDSA match. Starting with the subsequent public key, it compares the second signature against each remaining public key until it finds an ECDSA match. The process is repeated until all signatures have been checked or not enough public keys remain to produce a successful result. All signatures need to match a public key. Because public keys are not checked again if they fail any signature comparison, signatures must be placed in the scriptSig using the same order as their corresponding public keys were placed in the scriptPubKey or redeemScript. If all signatures are valid, 1 is returned, 0 otherwise. Due to a bug, one extra unused value is removed from the stack.
OP_CHECKMULTISIGVERIFY1750xafx sig1 sig2 ... <number of signatures> pub1 pub2 ... <number of public keys>Nothing / failSame as OP_CHECKMULTISIG, but OP_VERIFY is executed afterward.

Used NOP opcode identifiers

In Bitcoin's history, new opcodes were added that used reserved NO_OP opcode identifiers. These opcodes have been reverted to the original OP_NOP functionality.

WordOpcodeHexInputOutputDescription
OP_NOP2

(previously OP_CHECKLOCKTIMEVERIFY)

1770xb1Nothing

(Previously: x)

Nothing

(Previously: x or fail)

NO OPERATION

Evaluation process for UTXOs that pre-date genesis: Mark transaction as invalid if the top stack item is greater than the transaction's nLockTime field, otherwise script evaluation continues as though an OP_NOP was executed. Transaction is also invalid if 1. the stack is empty; or 2. the top stack item is negative; or 3. the top stack item is greater than or equal to 500000000 while the transaction's nLockTime field is less than 500000000, or vice versa; or 4. the input's nSequence field is equal to 0xffffffff. The precise semantics are described in BIP 0065.

OP_NOP3

(previously OP_CHECKSEQUENCEVERIFY)

1780xb2Nothing

(Previously: x)

Nothing

(Previously: x or fail)

NO OPERATION

Evaluation process for UTXOs that pre-date genesis: Mark transaction as invalid if the relative lock time of the input (enforced by BIP 0068 with nSequence) is not equal to or longer than the value of the top stack item. The precise semantics are described in BIP 0112.

Pseudo-words

These words are used internally for assisting with transaction matching. They are invalid if used in actual scripts.

WordOpcodeHexDescription
OP_PUBKEYHASH2530xfdRepresents a public key hashed with OP_HASH160.
OP_PUBKEY2540xfeRepresents a public key compatible with OP_CHECKSIG.
OP_INVALIDOPCODE2550xffMatches any opcode that is not yet assigned.

Reserved words

Any opcode not assigned is also reserved. Using an unassigned opcode makes the transaction invalid.

WordOpcodeHexWhen used...
OP_RESERVED800x50Transaction is invalid unless occuring in an unexecuted OP_IF branch
OP_RESERVED11370x89Transaction is invalid unless occuring in an unexecuted OP_IF branch
OP_RESERVED21380x8aTransaction is invalid unless occuring in an unexecuted OP_IF branch
OP_NOP1, OP_NOP4-OP_NOP10176, 179-1850xb0, 0xb3-0xb9The word is ignored. Does not mark transaction as invalid.

Examples

For examples of common Bitcoin transaction scripts please see Bitcoin Transactions

Attribution

This content is based on content sourced from https://en.bitcoin.it/wiki/Script under Creative Commons Attribution 3.0. Although it may have been extensively revised and updated we acknowledge the original authors.

声明:

比特币(BSV)知识库项目由比特币协会(Bitcoin Association)发起并支持,更多信息请参见知识库官网:https://wiki.bitcoinsv.io/


  • 对比特币区块链开发感兴趣的朋友可以通过CSDN站内私信联系我们,申请加入BSV开发者交流群。
  • 同时,您也可以扫描下方二维码,关注比特币协会官方微信公众号——BA资讯,了解更多区块链领域的实时资讯。

 

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值