Skip to main content

Computation Cost

Since Klaytn aims to maintain 1-second block time, the execution time of transactions has to be managed. Here are three approaches to achieve that:

  1. Limiting the gas limit of a transaction
  2. Limiting the execution time of a transaction
  3. Limiting the computation cost of a transaction

Limiting the gas limit of a transaction was not a feasible solution because the concept of the gas represents the current exchange value of the various resources in the blockchain platform such as computation, storage, network bandwidth, and so on. It is not suitable as a metric for the transaction execution time.

Limiting the execution time of a transaction was not feasible either because the execution time can vary between nodes on the blockchain platform. For example, consider the case in which we limit the execution time of a transaction to be 100 milli-second. If a node executes a transaction in 90 ms and another node executes it in 110 ms, the two nodes cannot reach a consensus. Hence, this solution is not appropriate.

The last approach is to limit the computation cost of a transaction. We modelled the computation cost of each EVM opcode based on its actual execution time and limit the sum of computation cost of a transaction. With this approach, we eliminate other factors and only count the normalized execution time unit, and nodes can reach a consensus as well.

Therefore, we chose the third option for Klaytn. The computation cost limit was 100,000,000, but as CPU computing performance has increased, the limit has been raised to 150,000,000 after Cancun EVM hardfork. This limit value is determined by the platform, so the developers should be aware of the computation cost of a transaction. To calculate the computation cost of a transaction, Klaytn provides klay_estimateComputationCost. The usage is almost the same as klay_estimateGas.

note

Computation cost related hardfork changes can be found at the bottom of this page. Go to Hardfork Changes.

Computation Cost Limit

A series of opcodes or precompiled contracts are executed sequentially when executing a transaction. To limit the execution time of a transaction, we have made a deterministic execution time calculation model for opcodes and precompiled contracts based on real execution time.

Based on this model, predetermined computation cost values for opcodes and precompiled contracts are added to the total computation cost. If the total value exceeds computation cost limit, transaction execution is aborted and returns ComputationCostLimitReached(0x0a) error.

When setting the computation cost limit value, we set --opcode-computation-cost-limit flag value as a limit if it is set as a non-zero value. If it's zero, the limit is set to the default computation cost limit defined for each specific hardfork. Exceptionally, the limit for call/estimateGas/estimateComputationCost is always set to unlimited and is not influenced by flag or hardfork values. However, execution still can be aborted due to other limits such as gas cap.

Computation Cost of Opcodes

The below table shows the computation cost of EVM opcodes. The computation cost was determined based on experiments.

OpcodeComputationCost
STOP0
ADD150
MUL200
SUB219
DIV404
SDIV360
MOD320
SMOD560
ADDMOD360
MULMOD700
EXP720
SIGNEXTEND481
LT201
GT264
SLT176
SGT222
EQ220
ISZERO165
AND288
OR160
XOR454
NOT364
BYTE589
SHL478
SHR498
SAR834
SHA3560
ADDRESS284
BALANCE1407
ORIGIN210
CALLER188
CALLVALUE149
CALLDATALOAD596
CALLDATASIZE194
CALLDATACOPY100
CODESIZE145
CODECOPY898
GASPRICE131
EXTCODESIZE1481
EXTCODECOPY1000
RETURNDATASIZE10
RETURNDATACOPY40
EXTCODEHASH1000
BLOCKHASH500
COINBASE189
TIMESTAMP265
NUMBER202
PREVRANDAO1498
GASLIMIT166
CHAINID120
SELFBALANCE374
POP140
MLOAD376
MSTORE288
MSTORE8230
SLOAD2550
SSTORE2510
JUMP253
JUMPI176
PC147
MSIZE137
GAS230
JUMPDEST10
PUSH080
PUSH1120
PUSH2120
PUSH3120
PUSH4120
PUSH5120
PUSH6120
PUSH7120
PUSH8120
PUSH9120
PUSH10120
PUSH11120
PUSH12120
PUSH13120
PUSH14120
PUSH15120
PUSH16120
PUSH17120
PUSH18120
PUSH19120
PUSH20120
PUSH21120
PUSH22120
PUSH23120
PUSH24120
PUSH25120
PUSH26120
PUSH27120
PUSH28120
PUSH29120
PUSH30120
PUSH31120
PUSH32120
DUP1190
DUP2190
DUP3176
DUP4142
DUP5177
DUP6165
DUP7147
DUP8157
DUP9138
DUP10174
DUP11141
DUP12144
DUP13157
DUP14143
DUP15237
DUP16149
SWAP1141
SWAP2156
SWAP3145
SWAP4135
SWAP5115
SWAP6146
SWAP7199
SWAP8130
SWAP9160
SWAP10134
SWAP11147
SWAP12128
SWAP13121
SWAP14114
SWAP15197
SWAP16128
LOG0100
LOG1500
LOG2500
LOG3500
LOG4500
PUSH0
DUP0
SWAP0
CREATE2094
CALL5000
CALLCODE4000
RETURN0
DELEGATECALL696
CREATE210000
STATICCALL10000
REVERT0
SELFDESTRUCT0
BASEFEE198
BLOBBASEFEE120
BLOBHASH165
TSTORE280
TLOAD220
MCOPY250

Precompiled contracts computation cost table

Input is a byte array input of a precompiled contract.

AddressPrecompiled contractsComputation Cost
0x01ecrecover113,150
0x02sha256hashnumOfWords(input) / 32 * 100 + 1,000
0x03ripemd160hashnumOfWords(input) / 32 * 10 + 100
0x04dataCopy0
0x05bigModExpsee the code here
0x06bn256Add8,000
0x07bn256ScalarMul100,000
0x08bn256PairingnumOfPairings(input) * 1,000,000 + 2,000,000
0x09blake2fbigEndian(getRounds(input[0:4])) * 10 + 10,000
0x0Akzg2,200,000
0x3FDvmLog10
0x3FEfeePayer10
0x3FFvalidateSendernumOfSigs(input) * 180,000 + 10,000

Hardfork Changes

HardforkNew itemsChanges
Cancun EVMBLOBBASEFEE (0x49)
BLOBHASH (0x50)
TSTORE (0x5c) opcode
TLOAD (0x5d)
MCOPY (0x5e)
kzg (0x0a) precompiled contract
increase the computation cost limit
from 100,000,000 to 150,000,000

reduce the computation cost of some opcodes
due to cpu performance increase
-Sdiv (0x05): 739 -> 360
-Mod (0x06): 812 -> 320
-Addmod (0x08): 1410 -> 360
-Mulmod (0x09): 1760 -> 700
-Exp (0x0A): 5000 -> 720
-Sha3 (0x20): 2465 -> 560
-Mstore8 (0x53): 5142 -> 230
-Log1, Log2, Log3, Log4 (0xA1-0xA4): 1000 -> 500

increase the computation cost of some opcodes
due to increased database size
-SLOAD (0x54): 835 -> 2550
-SSTORE (0x55): 1548 -> 2510
Shanghai EVMPUSH0 (0x5f) opcode
KoremodExp (0x05) precompiled contract
use new gas calculation logic.
Computation cost also affected.
Become more accurate.
London EVMBaseFee (0x48) opcode
Istanbul EVMCHAINID (0x46) opcode
SELFBALANCE (0x47) opcode
blake2f (0x09) precompiled contract
reduce the computation cost of over-priced opcodes
- ADDMOD (0x08): 3349 -> 1410
- MULMOD (0x09): 4757 -> 1760
- XOR (0x18): 657 -> 454
- NOT (0x19): 1289 -> 364
- SHL (0x1B): 1603 -> 478
- SHR (0x1C): 1815 -> 834
Make this page better