跳到主要内容

操作码

详细介绍比特币的操作码。

比特币使用一种脚本系统进行交易。类似于Forth语言,比特币的脚本系统简单、基于堆栈且从左到右处理。

脚本本质上是记录在每笔交易中的一系列指令,描述了想要花费被转移的比特币的下一个人如何获得访问权限。将比特币转移到目标比特币地址的典型脚本只是用两件事来限制未来比特币的花费:花费者必须提供:

  1. 一个公钥,当被哈希时,得到嵌入在脚本中的目标地.
  2. 一个证明对应于刚提供的公钥的私钥所有权的签名。

MVC完善并拓展了比特币的脚本系统,使其更加强大和灵活。

操作码列表

操作码请参考:https://en.bitcoin.it/wiki/Script

常量

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.)
N/A1-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.

流程控制

WordOpcodeHexInputOutputDescription
OP_NOP970x61NothingNothingDoes nothing.
OP_IF990x63<expression>if [statements] [else [statements]]* endifIf the top stack value is not False, the statements are executed. The top stack value is removed.
OP_NOTIF1000x64<expression>notif [statements] [else [statements]]* endifIf the top stack value is False, the statements are executed. The top stack value is removed.
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 OP_IF earlier is also invalid.
OP_VERIFY1050x69True / falseNothing / failMarks transaction as invalid if top stack value is not true. The top stack value is removed.
OP_RETURN1060x6aNothingfailMarks transaction as invalid. Since bitcoin 0.9, a standard way of attaching extra data to transactions is to add a zero-value output with a scriptPubKey consisting of OP_RETURN followed by data. Such outputs are provably unspendable and specially discarded from storage in the UTXO set, reducing their cost to the network. Since 0.12, standard relay rules allow a single output with OP_RETURN, that contains any sequence of push statements (or OP_RESERVED[1]) after the OP_RETURN provided the total scriptPubKey length is at most 83 bytes.

堆栈操作

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_IFDUP1150x73xx / x xIf the top stack value is not 0, duplicate it.
OP_DEPTH1160x74Nothing<Stack size>Puts the number of stack items onto the stack.
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 3rd item down the stack is moved to the top.
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.
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.

拼接操作

如果脚本中存在任何标记为禁用的操作码,则必须中止并失败。

WordOpcodeHexInputOutputDescription
OP_CAT1260x7ex1 x2out连接两个字符串。禁用。
OP_SUBSTR1270x7fin begin sizeout返回字符串的一部分。禁用。
OP_LEFT1280x80in sizeout仅保留字符串中指定点左侧的字符。禁用。
OP_RIGHT1290x81in sizeout仅保留字符串中指定点右侧的字符。禁用。
OP_SIZE1300x82inin size将堆栈顶部元素的字符串长度压入堆栈(不弹出该元素)。

位逻辑操作

如果脚本中存在任何标记为禁用的操作码,则必须中止并失败。

WordOpcodeHexInputOutputDescription
OP_INVERT1310x83inout翻转输入的所有位。禁用。
OP_AND1320x84x1 x2out对输入的每一位进行布尔与操作。禁用。
OP_OR1330x85x1 x2out对输入的每一位进行布尔或操作。禁用。
OP_XOR1340x86x1 x2out对输入的每一位进行布尔异或操作。禁用。
OP_EQUAL1350x87x1 x2True / false如果输入完全相等,则返回1,否则返回0。
OP_EQUALVERIFY1360x88x1 x2Nothing / fail与OP_EQUAL相同,但随后运行OP_VERIFY。

数值操作

WordOpcodeHexInputOutputDescription
OP_1ADD1390x8binout1 is added to the input.
OP_1SUB1400x8cinout1 is subtracted from the input.
OP_2MUL1410x8dinoutThe input is multiplied by 2. (disabled)
OP_2DIV1420x8einoutThe input is divided by 2. (disabled)
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. (disabled)
OP_DIV1500x96a bouta is divided by b. (disabled)
OP_MOD1510x97a boutReturns the remainder after dividing a by b. (disabled)
OP_LSHIFT1520x98a boutShifts a left b bits, preserving sign. (disabled)
OP_RSHIFT1530x99a boutShifts a right b bits, preserving sign. (disabled)
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.

密码学操作

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_CODESEPARATOR1710xabNoneNoneAll 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 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.
OP_CHECKSIGADD1860xbasig n puboutThree values are popped from the stack. The integer n is incremented by one and returned to the stack if the signature is valid for the public key and transaction. The integer n is returned to the stack unchanged if the signature is the empty vector (OP_0). In any other case, the script is invalid. This opcode is only available in tapscript.

时间锁定

WordOpcodeHexInputOutputDescription
OP_CHECKLOCKTIMEVERIFY1770xb1xx / failMarks 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_CHECKSEQUENCEVERIFY1780xb2xx / failMarks 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.