Files
CHIP-VM-Limits/operation-costs.md
2024-09-02 13:34:42 -04:00

22 KiB

Operation Cost by Operation

This table is provided for clarity, but it is not necessary for correct implementation of the proposal. The provided Operation Cost formulas are derived from the proposal's technical specification. The test vectors are also designed to ensure that all implementations correctly account for the cost of each operation.

The full, standard operation cost1 for each executed operation2 is provided below, accounting for Measurement of Stack-Pushed Bytes, Arithmetic Operation Cost, Hash Digest Iteration Cost, and Signature Checking Operation Cost.

Opcode(s) Codepoint(s) Notes Operation Cost
OP_0 0x00 100
OP_PUSHBYTES_N (1-75) 0x01-0x4b OP_PUSHBYTES_2 is 102, etc. 100 + N
OP_PUSHDATA_N 0x4c-0x4e OP_PUSHDATA_2 of 521 is 621, etc. 100 + length
OP_1NEGATE 0x4f 100 + 1
OP_1-OP_16 0x51-OP_16 100 + 1
OP_NOP 0x61 100
OP_IF 0x63 100
OP_NOTIF 0x64 100
OP_ELSE 0x67 100
OP_ENDIF 0x68 100
OP_VERIFY 0x69 100
OP_RETURN 0x6a 100
OP_TOALTSTACK 0x6b 100
OP_FROMALTSTACK 0x6c Stack: ; alt: a → stack: a; alt: 100 + a.length
OP_2DROP 0x6d 100
OP_2DUP 0x6e a ba b a b 100 + a.length + b.length
OP_3DUP 0x6f a b ca b c a b c 100 + a.length + b.length + c.length
OP_2OVER 0x70 a b c da b c d a b 100 + a.length + b.length
OP_2ROT 0x71 a b c d e fc d e f a b 100 + a.length + b.length
OP_2SWAP 0x72 100
OP_IFDUP 0x73 aa (false) OR aa a (duplicate) 100 (for false) OR 100 + a.length (for duplication)
OP_DEPTH 0x74 a b c ...a b c ... d 100 + d.length
OP_DROP 0x75 100
OP_DUP 0x76 aa a 100 + a.length
OP_NIP 0x77 100
OP_OVER 0x78 a ba b a 100 + a.length
OP_PICK 0x79 E.g. a b c d 3a b c d a (N=3) 100 + a.length
OP_ROLL 0x7a E.g. a b c d 3b c d a (N=3) 100 + a.length + N (where N is depth)
OP_ROT 0x7b 100
OP_SWAP 0x7c 100
OP_TUCK 0x7d a bb a b 100 + b.length
OP_CAT 0x7e a bab 100 + a.length + b.length
OP_SPLIT 0x7f ab na b 100 + a.length + b.length
OP_NUM2BIN 0x80 a nb 100 + b.length
OP_BIN2NUM 0x81 ab 100 + b.length
OP_SIZE 0x82 aa b 100 + b.length
OP_AND 0x84 a bc 100 + c.length
OP_OR 0x85 a bc 100 + c.length
OP_XOR 0x86 a bc 100 + c.length
OP_EQUAL 0x87 a b0 OR 1 100 (for 0) OR 100 + 1 (for 1)
OP_EQUALVERIFY 0x88 100 + 1
OP_1ADD 0x8b ab 100 + (2 * b.length)
OP_1SUB 0x8c ab 100 + (2 * b.length)
OP_NEGATE 0x8f ab 100 + (2 * b.length)
OP_ABS 0x90 ab 100 + (2 * b.length)
OP_NOT 0x91 a0 OR 1 100 (for 0), 100 + 1 for 1
OP_0NOTEQUAL 0x92 a0 OR 1 100 (for 0), 100 + 1 for 1
OP_ADD 0x93 a bc 100 + (2 * c.length)
OP_SUB 0x94 a bc 100 + (2 * c.length)
OP_MUL 0x95 a bc 100 + (2 * c.length) + (a.length * b.length)
OP_DIV 0x96 a bc 100 + (2 * c.length) + (a.length * b.length)
OP_MOD 0x97 a bc 100 + (2 * c.length) + (a.length * b.length)
OP_BOOLAND 0x9a a b0 OR 1 100 (for 0), 100 + 1 for 1
OP_BOOLOR 0x9b a b0 OR 1 100 (for 0), 100 + 1 for 1
OP_NUMEQUAL 0x9c a b0 OR 1 100 (for 0), 100 + 1 for 1
OP_NUMEQUALVERIFY 0x9d a b → (none) 100 + 1
OP_NUMNOTEQUAL 0x9e a b0 OR 1 100 (for 0), 100 + 1 for 1
OP_LESSTHAN 0x9f a b0 OR 1 100 (for 0), 100 + 1 for 1
OP_GREATERTHAN 0xa0 a b0 OR 1 100 (for 0), 100 + 1 for 1
OP_LESSTHANOREQUAL 0xa1 a b0 OR 1 100 (for 0), 100 + 1 for 1
OP_GREATERTHANOREQUAL 0xa2 a b0 OR 1 100 (for 0), 100 + 1 for 1
OP_MIN 0xa3 a bc (where c is minimum of a and b) 100 + (2 * c.length)
OP_MAX 0xa4 a bc (where c is maximum of a and b) 100 + (2 * c.length)
OP_WITHIN 0xa5 a b c0 OR 1 100 (for 0), 100 + 1 for 1
OP_RIPEMD160 0xa6 ab (Showing standard cost1) 100 + (iterations * 192) + b.length
OP_SHA1 0xa7 ab (Showing standard cost1) 100 + (iterations * 192) + b.length
OP_SHA256 0xa8 ab (Showing standard cost1) 100 + (iterations * 192) + b.length
OP_HASH160 0xa9 ab (Showing standard cost1) 100 + ((iterations + 1) * 192) + b.length
OP_HASH256 0xaa ab (Showing standard cost1) 100 + ((iterations + 1) * 192) + b.length
OP_CODESEPARATOR 0xab 100
OP_CHECKSIG 0xac Null signature checks are 100 (Showing standard cost1) 100 (for 0) OR 100 + 26000 + (iterations * 192) + 1
OP_CHECKSIGVERIFY 0xad (Showing standard cost1) 100 + 26000 + (iterations * 192) + 1
OP_CHECKMULTISIG 0xae M-of-N; K=M for Schnorr, N for legacy (Showing standard cost1) 100 (for 0) OR 100 + (26000 * K) + (iterations * 192) + 1
OP_CHECKMULTISIGVERIFY 0xaf M-of-N; K=M for Schnorr, N for legacy (Showing standard cost1) 100 + (26000 * K) + (iterations * 192) + 1
OP_NOP1 0xb0 100
OP_CHECKLOCKTIMEVERIFY 0xb1 100
OP_CHECKSEQUENCEVERIFY 0xb2 100
OP_NOP4 0xb3 100
OP_NOP5 0xb4 100
OP_NOP6 0xb5 100
OP_NOP7 0xb6 100
OP_NOP8 0xb7 100
OP_NOP9 0xb8 100
OP_NOP10 0xb9 100
OP_CHECKDATASIG 0xba Null signature check is 100 (Showing standard cost1) 100 (for 0) OR 100 + 26000 + (iterations * 192) + 1
OP_CHECKDATASIGVERIFY 0xbb (Showing standard cost1) 100 + 26000 + (iterations * 192) + 1
OP_REVERSEBYTES 0xbc ab 100 + b.length
OP_INPUTINDEX 0xc0 a 100 + a.length
OP_ACTIVEBYTECODE 0xc1 a 100 + a.length
OP_TXVERSION 0xc2 a 100 + a.length
OP_TXINPUTCOUNT 0xc3 a 100 + a.length
OP_TXOUTPUTCOUNT 0xc4 a 100 + a.length
OP_TXLOCKTIME 0xc5 a 100 + a.length
OP_UTXOVALUE 0xc6 ab 100 + b.length
OP_UTXOBYTECODE 0xc7 ab 100 + b.length
OP_OUTPOINTTXHASH 0xc8 ab 100 + b.length
OP_OUTPOINTINDEX 0xc9 ab 100 + b.length
OP_INPUTBYTECODE 0xca ab 100 + b.length
OP_INPUTSEQUENCENUMBER 0xcb ab 100 + b.length
OP_OUTPUTVALUE 0xcc ab 100 + b.length
OP_OUTPUTBYTECODE 0xcd ab 100 + b.length
OP_UTXOTOKENCATEGORY 0xce ab 100 + b.length
OP_UTXOTOKENCOMMITMENT 0xcf ab 100 + b.length
OP_UTXOTOKENAMOUNT 0xd0 ab 100 + b.length
OP_OUTPUTTOKENCATEGORY 0xd1 ab 100 + b.length
OP_OUTPUTTOKENCOMMITMENT 0xd2 ab 100 + b.length
OP_OUTPUTTOKENAMOUNT 0xd3 ab 100 + b.length

Notes

  1. Standard and consensus ("nonstandard") operation costs differ only in the cost of hash digest iterations: 192 per iteration in standard validation, 64 per iteration in consensus validation.
  2. All unexecuted operations cost the Base Instruction Cost of 100 per evaluation.