区块链中文技术社区

ZK-COUNTERS TABLE

ZK-COUNTERS TABLE

opcodenamecnt_arithcnt_binarycnt_mem_aligncnt_keccak_fcnt_padding_pgcnt_poseidon_gis_dynamic
0x00STOP000000false
0x01ADD010000false
0x02MUL100000false
0x03SUB010000false
0x04DIV120000false
0x05SDIV180000false
0x06MOD120000false
0x07SMOD180000false
0x08ADDMOD130000false
0x09MULMOD220000false
0x0aEXP51210250000true
0x0bSIGNEXTEND060000false
0x10LT010000false
0x11GT010000false
0x12SLT010000false
0x13SGT010000false
0x14EQ010000false
0x15ISZERO010000false
0x16AND010000false
0x17OR010000false
0x18XOR010000false
0x19NOT010000false
0x1aBYTE240000false
0x1bSHL120000false
0x1cSHR130000false
0x1dSAR2100000false
0x20SHA319219322010true
0x30ADDRESS000000false
0x31BALANCE000009false
0x32ORIGIN000000false
0x33CALLER000000false
0x34CALLVALUE000000false
0x35CALLDATALOAD64660000true
0x36CALLDATASIZE000000false
0x37CALLDATACOPY---000true
0x38CODESIZE00000252true
0x39CODECOPY0--00255true
0x3aGASPRICE000000false
0x3bEXTCODESIZE00000255true
0x3cEXTCODECOPY0--011510true
0x3dRETURNDATASIZE010000false
0x3eRETURNDATACOPY--2000true
0x3fEXTCODEHASH00000255true
0x40BLOCKHASH000109false
0x41COINBASE000000false
0x42TIMESTAMP000000false
0x43NUMBER000000false
0x44DIFFICULTY000000false
0x45GASLIMIT000000false
0x46CHAINID000000false
0x47SELFBALANCE00000255true
0x50POP000000false
0x51MLOAD3232100255true
0x52MSTORE3232100255true
0x53MSTORE8321100255false
0x54SLOAD00000255true
0x55SSTORE0-000255true
0x56JUMP0-0000true
0x57JUMPI0-0000true
0x59MSIZE130000false
0x5aGAS000000false
0x5bJUMPDEST000000false
0x60PUSH1030000true
0x61PUSH2040000true
0x62PUSH3050000false
0x63PUSH4020000false
0x64PUSH5040000false
0x65PUSH6050000false
0x66PUSH7060000false
0x67PUSH8030000false
0x68PUSH9050000false
0x69PUSH10060000false
0x6aPUSH11070000false
0x6bPUSH12040000false
0x6cPUSH13060000false
0x6dPUSH14070000false
0x6ePUSH15080000false
0x6fPUSH16050000false
0x70PUSH17070000false
0x71PUSH18080000false
0x72PUSH19090000false
0x73PUSH20060000false
0x74PUSH21080000false
0x75PUSH22090000false
0x76PUSH230100000false
0x77PUSH24070000false
0x78PUSH25090000false
0x79PUSH260100000false
0x7aPUSH270110000false
0x7bPUSH28080000false
0x7cPUSH290100000false
0x7dPUSH300110000false
0x7ePUSH310120000false
0x7fPUSH32090000false
0x80DUP1000000false
0x81DUP2000000false
0x82DUP3000000false
0x83DUP4000000false
0x84DUP5000000false
0x85DUP6000000false
0x86DUP7000000false
0x87DUP8000000false
0x88DUP9000000false
0x90SWAP1000000false
0x91SWAP2000000false
0x92SWAP3000000false
0x93SWAP4000000false
0x94SWAP5000000false
0x95SWAP6000000false
0x96SWAP7000000false
0xa0LOG00-0000true
0xa1LOG10-0000true
0xa2LOG20-0000true
0xa3LOG30-0000true
0xa4LOG40-0000true
0xf0CREATE--0-0-true
0xf1CALL--00--true
0xf2CALLCODE--00--true
0xf3RETURN000000false
0xf4DELEGATECALL--00--true
0xf5CREATE2--0-0-true
0xfaSTATICCALL--00--true
0xfdREVERT000000false
0xfeINVALID010000false

Dynamic zk-counters

In the following doc, we calculate the cost of processing the opcode. It's important to also add the cost of calculating the cost

EXP

Inputs:
1- a: integer base.
2- exponent: integer exponent.

dynamic_gas = 50 * exponent_byte_size
We need to calculate the exponent byte size to get the gas cost. The counters cost is dynamic but we can't calculate the cost without consuming counters in a dynamic way. We need to find a way to get the size of the exponent in a constant manner or handle the counters limitations from the zkasm.
Maximum setted: Maxmimun byte syze = 256 bytes. Max counters = 256 * (2A + 4B) + 1B = 512A + 1025B

SHA3

L = input length
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0

cnt_arith = 2 + B*6
cnt_binary = 2 + A + B*9
cnt_keccak = 1

CALLDATALOAD

L = byte offset in the calldata.
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0

counters = divARITH + B*(SHLarith + SHRarith)

CALLDATACOPY

L = Length to copy.
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0

counters = A(divARITH + SHLarith + SHRarith + MSTORE32) + SHLarith2 + SHRarith + MSTOREX

CODESIZE

counters = SLOAD

CODECOPY

L = bytes to copy.
if is createContract -> counters = CALLDATACOPY
else -> counters = LT + L*(LT + MEM_ALIGN_WR8)

EXTCODESIZE

counters = SLOAD

EXTCODECOPY

L = bytes to copy.
if is createContract -> counters = CALLDATACOPY
else -> counters = LT + L*(LT + MEM_ALIGN_WR8)

RETURNDATACOPY

L = Length to copy.
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0
counters = 2EQ + LT + divARITH + mulARITH + A(MLOAD32 + MSTORE32) + B*(MLOADX + MSTOREX)

EXTCODEHASH

counters = SLOAD

SELFBALANCE

counters = SLOAD

MLOAD

counters = MLOAD32

MSTORE

counters = MSTORE32

SLOAD

counters = SLOAD

SSTORE

Cant calculate

JUMPI

isCreateContract ? true -> A = 1, false -> A = 0
isCreate ? true -> B = 1, false -> B = 0

counters = EQ + A(B(MLOADX + SHRarith) + (1-B)(EQ)) + (1-A)(EQ)

JUMP

isCreateContract ? true -> A = 1, false -> A = 0
isCreate ? true -> B = 1, false -> B = 0

counters = EQ + A(B(MLOADX + SHRarith) + (1-B)(EQ)) + (1-A)(EQ)

LOG

L = byte size to copy.
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0
counters = AMLOAD32 + BMLOADX

CREATE

counters = computeGasSendCall + copySP + SLOAD + SSTORE + getLenBytes

CALL

argsLengthCall + retLength == 0 ? true -> A = 1, false -> A = 0
argsOffsetCall > memLength ? true -> B = 1, false -> B = 0
counters = addARITH + EQ + (1-A)(LT +BsaveMem ) + LT + isEmptyAccount + computeGasSendCall + copySP

CALLCODE

counters = 2EQ + LT2 + computeGasSendCall + copySP

DELEGATECALL

counters = 2EQ + LT2 + computeGasSendCall + copySP

CREATE2

counters = computeGasSendCall + copySP + SLOAD + SSTORE + getLenBytes

STATICCALL

counters = 2EQ + LT2 + computeGasSendCall + copySP

REGS TABLE

REG Namecnt_arithcnt_binarycnt_mem_aligncnt_keccak_fcnt_padding_pgcnt_poseidon_gis_dynamic
LT010000false
EQ010000false
ARITH100000false
SLOAD0000011true
SSTORE0000011true
MEM_ALIGN_WR8001000false

Dynamic regs

SSTORE

SLOAD

Should check how SLOAD is implemented

Functions TABLE

FUNC Namecnt_arithcnt_binarycnt_mem_aligncnt_keccak_fcnt_padding_pgcnt_poseidon_gis_dynamic
addARITH010000false
divARITH120000false
subARITH010000false
mulARITH100000false
saveMem000000false
computeGasSendCall010000false
copySP--0000true
MLOAD32000000true
MLOADX000000true
MSTORE32000000true
MSTOREX000000true
sliceA000000false
SHRarith--0000true
SHLarith--0000true
opCODECOPYLoadBytes010000false
isEmptyAccount--0000true

Dynamic functions

copySP

It depends on the stack size.
L = stack length
L/32 = A
L % 32 > 0 ? true -> B = 1, false -> B = 0
counters = MLOAD32 * (A + B)

MLOAD32

MSTORE32

MSTOREX

MLOADX

L = bytes length
L > 0 ? true -> A = 1, false -> A = 0
isMSTOREX ? true -> B = 1, false -> B = 0
counters = LT + B(2SHRarith + 2SHLarith) + (1-B)(C(2SHLarith + 4SHRarith) + (1-C)(2SHRarith + 2SHLarith) + MEM_ALIGN_WR)
MAX:
counters = 192A + 193B + 2MA

SHRarith

SHLarith

A -> bytes to shift
D -> times to shift (A << D)
E -> D > 256? true = 1, false = 0
counters= ARITH + EQ + (1-E) (D(LT + ARITH)) = 1A + 1B + (1-E)(D(1A + 1B)
MAX:
counters = 32A + 32B

isEmptyAccount

isNotPrecompiled ? true -> A = 1, false -> A = 0
zeroBalance ? true -> B = 1, false -> B = 0
zeroNonce ? true -> C = 1, false -> C = 0

counters = LT + A(SLOAD + LT + B(SLOAD + LT + C*(SLOAD + LT)))

https://github.com/0xPolygonHermez/zkevm-rom/blob/main/docs/opcode-cost-zk-counters.md

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »