Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Add test function to report opcode gas cost in state circuit #735

Merged
merged 1 commit into from
Sep 16, 2022

Conversation

ed255
Copy link
Member

@ed255 ed255 commented Sep 6, 2022

This test function prints a table with the implemented opcodes and their gas cost, state circuit cost (height / number of rows), and the ratio between the two. This is similar to #616 but for the State Circuit. The test bytecode does some extra steps so that the opcode avoids extra gas cost due to memory expansions / warming up accounts.

Also add memory expansion for call-like dummy opcodes since the memory check check for CALLCODE, DELEGATECALL and STATICCALL was failing because the memory expansion was not being done.

The resulting table is the following:

state opcode h g h/g
STOP STOP 2 0 inf
ADD_SUB ADD 3 3 1.000
ADD_SUB SUB 3 3 1.000
MUL_DIV_MOD MUL 3 5 0.600
MUL_DIV_MOD DIV 3 5 0.600
MUL_DIV_MOD MOD 3 5 0.600
SDIV_SMOD SDIV 3 5 0.600
SDIV_SMOD SMOD 3 5 0.600
ADDMOD ADDMOD 4 8 0.500
MULMOD MULMOD 4 8 0.500
EXP EXP 3 60 0.050
SIGNEXTEND SIGNEXTEND 3 5 0.600
CMP LT 3 3 1.000
CMP GT 3 3 1.000
CMP EQ 3 3 1.000
SCMP SLT 3 3 1.000
SCMP SGT 3 3 1.000
ISZERO ISZERO 2 3 0.667
BITWISE AND 3 3 1.000
BITWISE OR 3 3 1.000
BITWISE XOR 3 3 1.000
NOT NOT 2 3 0.667
BYTE BYTE 3 3 1.000
SHL SHL 3 3 1.000
SHR SHR 3 3 1.000
SAR SAR 3 3 1.000
SHA3 SHA3 67 42 1.595
ADDRESS ADDRESS 2 2 1.000
BALANCE BALANCE 3 100 0.030
ORIGIN ORIGIN 2 2 1.000
CALLER CALLER 2 2 1.000
CALLVALUE CALLVALUE 2 2 1.000
CALLDATALOAD CALLDATALOAD 4 3 1.333
CALLDATASIZE CALLDATASIZE 2 2 1.000
CALLDATACOPY CALLDATACOPY 53 9 5.889
CODESIZE CODESIZE 1 2 0.500
CODECOPY CODECOPY 51 9 5.667
GASPRICE GASPRICE 2 2 1.000
EXTCODESIZE EXTCODESIZE 3 100 0.030
EXTCODECOPY EXTCODECOPY 5 103 0.049
RETURNDATASIZE RETURNDATASIZE 1 2 0.500
RETURNDATACOPY RETURNDATACOPY 3 9 0.333
EXTCODEHASH EXTCODEHASH 9 100 0.090
BLOCKHASH BLOCKHASH 2 20 0.100
BLOCKCTXU64 TIMESTAMP 1 2 0.500
BLOCKCTXU64 NUMBER 1 2 0.500
BLOCKCTXU64 GASLIMIT 1 2 0.500
BLOCKCTXU160 COINBASE 1 2 0.500
BLOCKCTXU256 DIFFICULTY 1 2 0.500
BLOCKCTXU256 BASEFEE 1 2 0.500
CHAINID CHAINID 1 2 0.500
SELFBALANCE SELFBALANCE 3 5 0.600
POP POP 1 2 0.500
MEMORY MLOAD 34 3 11.333
MEMORY MSTORE 34 3 11.333
MEMORY MSTORE8 3 3 1.000
SLOAD SLOAD 8 2100 0.004
SSTORE SSTORE 10 22100 0.000
JUMP JUMP 3 8 0.375
JUMPI JUMPI 3 10 0.300
PC PC 1 2 0.500
MSIZE MSIZE 1 2 0.500
GAS GAS 1 2 0.500
JUMPDEST JUMPDEST 0 1 0.000
PUSH PUSH1 1 3 0.333
PUSH PUSH2 1 3 0.333
PUSH PUSH3 1 3 0.333
PUSH PUSH4 1 3 0.333
PUSH PUSH5 1 3 0.333
PUSH PUSH6 1 3 0.333
PUSH PUSH7 1 3 0.333
PUSH PUSH8 1 3 0.333
PUSH PUSH9 1 3 0.333
PUSH PUSH10 1 3 0.333
PUSH PUSH11 1 3 0.333
PUSH PUSH12 1 3 0.333
PUSH PUSH13 1 3 0.333
PUSH PUSH14 1 3 0.333
PUSH PUSH15 1 3 0.333
PUSH PUSH16 1 3 0.333
PUSH PUSH17 1 3 0.333
PUSH PUSH18 1 3 0.333
PUSH PUSH19 1 3 0.333
PUSH PUSH20 1 3 0.333
PUSH PUSH21 1 3 0.333
PUSH PUSH22 1 3 0.333
PUSH PUSH23 1 3 0.333
PUSH PUSH24 1 3 0.333
PUSH PUSH25 1 3 0.333
PUSH PUSH26 1 3 0.333
PUSH PUSH27 1 3 0.333
PUSH PUSH28 1 3 0.333
PUSH PUSH29 1 3 0.333
PUSH PUSH30 1 3 0.333
PUSH PUSH31 1 3 0.333
PUSH PUSH32 1 3 0.333
DUP DUP1 2 3 0.667
DUP DUP2 2 3 0.667
DUP DUP3 2 3 0.667
DUP DUP4 2 3 0.667
DUP DUP5 2 3 0.667
DUP DUP6 2 3 0.667
DUP DUP7 2 3 0.667
DUP DUP8 2 3 0.667
DUP DUP9 2 3 0.667
DUP DUP10 3 3 1.000
DUP DUP11 3 3 1.000
DUP DUP12 3 3 1.000
DUP DUP13 3 3 1.000
DUP DUP14 3 3 1.000
DUP DUP15 3 3 1.000
DUP DUP16 3 3 1.000
SWAP SWAP1 4 3 1.333
SWAP SWAP2 4 3 1.333
SWAP SWAP3 4 3 1.333
SWAP SWAP4 4 3 1.333
SWAP SWAP5 4 3 1.333
SWAP SWAP6 4 3 1.333
SWAP SWAP7 4 3 1.333
SWAP SWAP8 4 3 1.333
SWAP SWAP9 3 3 1.000
SWAP SWAP10 3 3 1.000
SWAP SWAP11 3 3 1.000
SWAP SWAP12 3 3 1.000
SWAP SWAP13 3 3 1.000
SWAP SWAP14 3 3 1.000
SWAP SWAP15 3 3 1.000
SWAP SWAP16 3 3 1.000
LOG LOG0 135 887 0.152
LOG LOG1 137 1262 0.109
LOG LOG2 139 1637 0.085
LOG LOG3 141 2012 0.070
LOG LOG4 143 2387 0.060
CREATE CREATE 6 32000 0.000
CALL CALL 44 14273 0.003
CALLCODE CALLCODE 1 14273 0.000
RETURN RETURN 0 0 NaN
DELEGATECALL DELEGATECALL 1 180 0.006
CREATE2 CREATE2 6 32012 0.000
STATICCALL STATICCALL 1 180 0.006
SELFDESTRUCT SELFDESTRUCT 3 5000 0.001

Resolve privacy-scaling-explorations/zkevm-chain#15

@ed255 ed255 requested a review from miha-stopar as a code owner September 6, 2022 14:17
@github-actions github-actions bot added crate-bus-mapping Issues related to the bus-mapping workspace member crate-zkevm-circuits Issues related to the zkevm-circuits workspace member labels Sep 6, 2022
@ed255 ed255 force-pushed the feature/state-circuit-costs branch from c9d7601 to cdd0e9d Compare September 6, 2022 14:17
@ed255 ed255 requested a review from AronisAt79 September 6, 2022 14:18
@ed255 ed255 force-pushed the feature/state-circuit-costs branch 2 times, most recently from 416dc20 to 81290ad Compare September 8, 2022 14:44
Copy link
Contributor

@han0110 han0110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Now we can see more clearly about the latest bottleneck easily!

Copy link
Collaborator

@miha-stopar miha-stopar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Add memory expansion for call-like dummy opcodes
@ed255 ed255 force-pushed the feature/state-circuit-costs branch from 81290ad to bad3680 Compare September 16, 2022 09:49
@ed255 ed255 requested a review from a team as a code owner September 16, 2022 09:49
@ed255 ed255 merged commit 1ee1af9 into main Sep 16, 2022
lispc added a commit that referenced this pull request Aug 28, 2023
…S auditors (#572)

* fix finding 3 (#575)

* Fix zellic finding 4 (#576)

* fix finding 3 (#575)

* fix finding 4

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* add range check on diffs (#586)

* Fix finding 10 (#578)

* fix finding 3 (#575)

* fix finding 10

* Fix finding 13 (#579)

* fix finding 3 (#575)

* fix finding 13

* Fix zellic finding 14 (#580)

* fix finding 3 (#575)

* fix finding 14

* Fix zellic finding 5 (#584)

* fix finding 3 (#575)

* fix finding 5

* refine comments

* fmt

* Fix finding 17 (#602)

* add q_last

* fix

* add more diff range check

* fix finding 7 (#625)

* tx_id = 1 when sm starts

* Fix finding 11 : use length for rlc in rlp table (#719)

* fix: use tag_bytes_rlc and tag_length to copy tag's bytes around

* fix lookup input for Len & RLC & GasCost fields in tx circuit

* refactor

* fix

* refactor

* fix col phase issue

* refactor bytes_rlc type

* Fix the bugs in Tx & PI circuits reported by Zellic & KALOS auditors (#612)

* lookup chain_id to RLP table

* fix finding 22 (#614)

* fix finding 21 (#613)

* fix finding 23 (#618)

* fix finding 26 (#622)

* fix finding 28 (#624)

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* fix finding 29 (#623)

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* enforce is_final is true at the last row and fix RLC related vul (#735)

* Fix finding 30  (#733)

* enforce all txs in a block are included in the tx table

* clippy

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* Fix Zellic / Kalos finding25 (#619)

* fix finding 25

* add comment

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* fix conflicts

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Co-authored-by: Zhang Zhuo <mycinbrin@gmail.com>

* use q_first instead

* fmt

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Co-authored-by: Zhang Zhuo <mycinbrin@gmail.com>
lispc added a commit that referenced this pull request Aug 28, 2023
* add row counting interface for keccak

* add class level capacity calculator for keccak

* remove f capacity from core

* remove capacity calculator in aggregator util

* remove unnecessary imports

* replace max keccak round in core

* replace reference for max keccak

* remove unnecessary keccak imports and constants

* remove max keccak constant

* remove constants in hash cell parsing

* remove constant column sanity check

* add state column usage log

* adjust input bytes column

* add long column padding

* correct fmt

* fix fmt

* minor fixes

* fix

* Fix: allow skipping of L1Msg tx part 2 (calculate num_all_txs in tx circuit) (#778)

* calculate num_l1_msgs and num_l2_txs in tx circuit

* fix

* fmt and clippy

* fix: non-last tx requires next is calldata

* add NumAllTxs in block table and copy it from pi to block table

* add lookup for NumAllTxs in tx circuit

* clippy

* add block num diff check to avoid two real block have same num

* clippy

* address comments

* Fix the bugs in RLP/Tx/PI circuit which are reported by Zellic & KALOS auditors (#572)

* fix finding 3 (#575)

* Fix zellic finding 4 (#576)

* fix finding 3 (#575)

* fix finding 4

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* add range check on diffs (#586)

* Fix finding 10 (#578)

* fix finding 3 (#575)

* fix finding 10

* Fix finding 13 (#579)

* fix finding 3 (#575)

* fix finding 13

* Fix zellic finding 14 (#580)

* fix finding 3 (#575)

* fix finding 14

* Fix zellic finding 5 (#584)

* fix finding 3 (#575)

* fix finding 5

* refine comments

* fmt

* Fix finding 17 (#602)

* add q_last

* fix

* add more diff range check

* fix finding 7 (#625)

* tx_id = 1 when sm starts

* Fix finding 11 : use length for rlc in rlp table (#719)

* fix: use tag_bytes_rlc and tag_length to copy tag's bytes around

* fix lookup input for Len & RLC & GasCost fields in tx circuit

* refactor

* fix

* refactor

* fix col phase issue

* refactor bytes_rlc type

* Fix the bugs in Tx & PI circuits reported by Zellic & KALOS auditors (#612)

* lookup chain_id to RLP table

* fix finding 22 (#614)

* fix finding 21 (#613)

* fix finding 23 (#618)

* fix finding 26 (#622)

* fix finding 28 (#624)

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* fix finding 29 (#623)

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* enforce is_final is true at the last row and fix RLC related vul (#735)

* Fix finding 30  (#733)

* enforce all txs in a block are included in the tx table

* clippy

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* Fix Zellic / Kalos finding25 (#619)

* fix finding 25

* add comment

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* fix conflicts

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Co-authored-by: Zhang Zhuo <mycinbrin@gmail.com>

* use q_first instead

* fmt

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Co-authored-by: Zhang Zhuo <mycinbrin@gmail.com>

* add pi comments

* rename preimage col idx

* add keccak rows check

* rename input bytes col finder fn

* modify keccak row env constaint

* modify keccak row env constaint

* add named constant setup vars

* modify keccak row check

* clippy advised

* add comments on chunk hash

* fmt

* avoid constant lookup table

* avoid repetitive computation of input_bytes_col_idx

---------

Co-authored-by: Zhuo Zhang <mycinbrin@gmail.com>
Co-authored-by: xkx <xiakunxian130@gmail.com>
Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
crate-bus-mapping Issues related to the bus-mapping workspace member crate-zkevm-circuits Issues related to the zkevm-circuits workspace member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calculate the OPCODE circuit cost for the State Circuit
3 participants