Skip to content

Commit

Permalink
More optimizations + counters
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Feb 9, 2024
1 parent 56c538b commit a36998b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 42 deletions.
28 changes: 14 additions & 14 deletions counters/countersConstants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ CONST %OPCALLDATALOAD_CNT_PADDING_PG = 0 + %SHLARITH_CNT_PADDING_PG + %SHRARITH_
CONST %OPCALLDATALOAD_CNT_POSEIDON_G = 0 + %SHLARITH_CNT_POSEIDON_G + %SHRARITH_CNT_POSEIDON_G + %DIVARITH_CNT_POSEIDON_G
; opCALLDATACOPY - COMPLEX - hardcoded values at test
CONST %OPCALLDATACOPY_STEP = 500
CONST %OPCALLDATACOPY_CNT_BINARY = 18
CONST %OPCALLDATACOPY_CNT_BINARY = 16
CONST %OPCALLDATACOPY_CNT_ARITH = 8
CONST %OPCALLDATACOPY_CNT_KECCAK_F = 0
CONST %OPCALLDATACOPY_CNT_MEM_ALIGN = 2
Expand All @@ -225,87 +225,87 @@ CONST %OPRETURNDATACOPY_CNT_PADDING_PG = 0
CONST %OPRETURNDATACOPY_CNT_POSEIDON_G = 0
; opCODECOPY - COMPLEX - hardcoded values at test
CONST %OPCODECOPY_STEP = 1700
CONST %OPCODECOPY_CNT_BINARY = 57
CONST %OPCODECOPY_CNT_BINARY = 55
CONST %OPCODECOPY_CNT_ARITH = 4
CONST %OPCODECOPY_CNT_KECCAK_F = 0
CONST %OPCODECOPY_CNT_MEM_ALIGN = 43
CONST %OPCODECOPY_CNT_PADDING_PG = 0
CONST %OPCODECOPY_CNT_POSEIDON_G = 0
; opEXTCODECOPY - COMPLEX - hardcoded values at test
CONST %OPEXTCODECOPY_STEP = 2000
CONST %OPEXTCODECOPY_CNT_BINARY = 61
CONST %OPEXTCODECOPY_CNT_BINARY = 59
CONST %OPEXTCODECOPY_CNT_ARITH = 4
CONST %OPEXTCODECOPY_CNT_KECCAK_F = 0
CONST %OPEXTCODECOPY_CNT_MEM_ALIGN = 43
CONST %OPEXTCODECOPY_CNT_PADDING_PG = 0
CONST %OPEXTCODECOPY_CNT_POSEIDON_G = 8
; opCREATE - COMPLEX - hardcoded values at test
CONST %OPCREATE_STEP = 400
CONST %OPCREATE_CNT_BINARY = 16
CONST %OPCREATE_CNT_BINARY = 13
CONST %OPCREATE_CNT_ARITH = 1
CONST %OPCREATE_CNT_KECCAK_F = 0
CONST %OPCREATE_CNT_MEM_ALIGN = 0
CONST %OPCREATE_CNT_PADDING_PG = 0
CONST %OPCREATE_CNT_POSEIDON_G = 23
; opCREATE2 - COMPLEX - hardcoded values at test
CONST %OPCREATE2_STEP = 400
CONST %OPCREATE2_CNT_BINARY = 21
CONST %OPCREATE2_CNT_BINARY = 18
CONST %OPCREATE2_CNT_ARITH = 2
CONST %OPCREATE2_CNT_KECCAK_F = 0
CONST %OPCREATE2_CNT_MEM_ALIGN = 0
CONST %OPCREATE2_CNT_PADDING_PG = 0
CONST %OPCREATE2_CNT_POSEIDON_G = 27
; opCALL - COMPLEX - hardcoded values at test
CONST %OPCALL_STEP = 600
CONST %OPCALL_CNT_BINARY = 27
CONST %OPCALL_CNT_BINARY = 22
CONST %OPCALL_CNT_ARITH = 3
CONST %OPCALL_CNT_KECCAK_F = 0
CONST %OPCALL_CNT_MEM_ALIGN = 0
CONST %OPCALL_CNT_PADDING_PG = 0
CONST %OPCALL_CNT_POSEIDON_G = 14
; opCALLCODE - COMPLEX - hardcoded values at test
CONST %OPCALLCODE_STEP = 600
CONST %OPCALLCODE_CNT_BINARY = 26
CONST %OPCALLCODE_CNT_BINARY = 21
CONST %OPCALLCODE_CNT_ARITH = 3
CONST %OPCALLCODE_CNT_KECCAK_F = 0
CONST %OPCALLCODE_CNT_MEM_ALIGN = 0
CONST %OPCALLCODE_CNT_PADDING_PG = 0
CONST %OPCALLCODE_CNT_POSEIDON_G = 10
; opRETURN - COMPLEX - hardcoded values at test
CONST %OPRETURN_STEP = 700
CONST %OPRETURN_CNT_BINARY = 27
CONST %OPRETURN_CNT_BINARY = 25
CONST %OPRETURN_CNT_ARITH = 11
CONST %OPRETURN_CNT_KECCAK_F = 0
CONST %OPRETURN_CNT_MEM_ALIGN = 4
CONST %OPRETURN_CNT_PADDING_PG = 0
CONST %OPRETURN_CNT_POSEIDON_G = 0
; opREVERT - COMPLEX - hardcoded values at test
CONST %OPREVERT_STEP = 700
CONST %OPREVERT_CNT_BINARY = 27
CONST %OPREVERT_CNT_BINARY = 25
CONST %OPREVERT_CNT_ARITH = 11
CONST %OPREVERT_CNT_KECCAK_F = 0
CONST %OPREVERT_CNT_MEM_ALIGN = 4
CONST %OPREVERT_CNT_PADDING_PG = 0
CONST %OPREVERT_CNT_POSEIDON_G = 0
; opDELEGATECALL - COMPLEX - hardcoded values at test
CONST %OPDELEGATECALL_STEP = 600
CONST %OPDELEGATECALL_CNT_BINARY = 23
CONST %OPDELEGATECALL_CNT_BINARY = 18
CONST %OPDELEGATECALL_CNT_ARITH = 3
CONST %OPDELEGATECALL_CNT_KECCAK_F = 0
CONST %OPDELEGATECALL_CNT_MEM_ALIGN = 0
CONST %OPDELEGATECALL_CNT_PADDING_PG = 0
CONST %OPDELEGATECALL_CNT_POSEIDON_G = 6
; opSTATICCALL - COMPLEX - hardcoded values at test
CONST %OPSTATICCALL_STEP = 600
CONST %OPSTATICCALL_CNT_BINARY = 23
CONST %OPSTATICCALL_CNT_BINARY = 18
CONST %OPSTATICCALL_CNT_ARITH = 3
CONST %OPSTATICCALL_CNT_KECCAK_F = 0
CONST %OPSTATICCALL_CNT_MEM_ALIGN = 0
CONST %OPSTATICCALL_CNT_PADDING_PG = 0
CONST %OPSTATICCALL_CNT_POSEIDON_G = 6
; opSHA3 - COMPLEX - hardcoded values at test
CONST %OPSHA3_STEP = 3300
CONST %OPSHA3_CNT_BINARY = 83
CONST %OPSHA3_CNT_BINARY = 81
CONST %OPSHA3_CNT_ARITH = 9
CONST %OPSHA3_CNT_KECCAK_F = 15
CONST %OPSHA3_CNT_MEM_ALIGN = 63
Expand Down Expand Up @@ -337,15 +337,15 @@ CONST %ISEMPTYACCOUNT_CNT_PADDING_PG = 0
CONST %ISEMPTYACCOUNT_CNT_POSEIDON_G = 6
; computeGasSendCall
CONST %COMPUTEGASSENDCALL_STEP = 30
CONST %COMPUTEGASSENDCALL_CNT_BINARY = 2
CONST %COMPUTEGASSENDCALL_CNT_BINARY = 1
CONST %COMPUTEGASSENDCALL_CNT_ARITH = 0
CONST %COMPUTEGASSENDCALL_CNT_KECCAK_F = 0
CONST %COMPUTEGASSENDCALL_CNT_MEM_ALIGN = 0
CONST %COMPUTEGASSENDCALL_CNT_PADDING_PG = 0
CONST %COMPUTEGASSENDCALL_CNT_POSEIDON_G = 0
; saveMem
CONST %SAVEMEM_STEP = 100 + %MULARITH_STEP + %DIVARITH_STEP
CONST %SAVEMEM_CNT_BINARY = 5 + %MULARITH_CNT_BINARY + %DIVARITH_CNT_BINARY
CONST %SAVEMEM_CNT_BINARY = 3 + %MULARITH_CNT_BINARY + %DIVARITH_CNT_BINARY
CONST %SAVEMEM_CNT_ARITH = 0 + %MULARITH_CNT_ARITH + %DIVARITH_CNT_ARITH
CONST %SAVEMEM_CNT_KECCAK_F = 0 + %MULARITH_CNT_KECCAK_F + %DIVARITH_CNT_KECCAK_F
CONST %SAVEMEM_CNT_MEM_ALIGN = 0 + %MULARITH_CNT_MEM_ALIGN + %DIVARITH_CNT_MEM_ALIGN
Expand Down
7 changes: 0 additions & 7 deletions counters/tests/opCREATE2.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ operation:
1000 :MSTORE(SP++)
:JMP(opCREATE2)
checkCounters:
%OPCREATE2_STEP - STEP:JMPN(failedCounters)
%OPCREATE2_CNT_BINARY - CNT_BINARY :JMPNZ(failedCounters)
%OPCREATE2_CNT_ARITH - CNT_ARITH :JMPNZ(failedCounters)
%OPCREATE2_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters)
%OPCREATE2_CNT_MEM_ALIGN - CNT_MEM_ALIGN :JMPNZ(failedCounters)
%OPCREATE2_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters)
%OPCREATE2_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters)

0 => A,B,C,D,E,CTX, SP, PC, GAS, SR, HASHPOS, RR ; Set all registers to 0
finalizeExecution:
Expand Down
11 changes: 3 additions & 8 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,9 @@ opRETURNDATASIZE:
; check out-of-gas
GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas)
; Load ret data length from last ctx
$ => B :MLOAD(retDataCTX)
; if no retDataCTX(0), retDataLength = 0
0 => A
$ :EQ, JMPC(endOpRETURNDATASIZE)
B => CTX
$ => A :MLOAD(retDataCTX), JMPZ(endOpRETURNDATASIZE)
A => CTX
$ => A :MLOAD(retDataLength)
; Restore current context
$ => CTX :MLOAD(currentCTX)
Expand Down Expand Up @@ -522,10 +520,7 @@ opRETURNDATACOPY:
; store lastMemLength for memory expansion gas cost
C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
; if retDataCTX is 0, end opcode execution
$ => B :MLOAD(retDataCTX)
0 => A
$ :EQ, JMPC(opRETURNDATACOPYEmpty)

$ => B :MLOAD(retDataCTX), JMPZ(opRETURNDATACOPYEmpty)
; Load ret data length from last ctx
B => CTX
E => B
Expand Down
9 changes: 3 additions & 6 deletions main/opcodes/storage-memory.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,11 @@ opMSIZE:
32 :MSTORE(arithB)
:CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => C :MLOAD(arithRes1)
$ => B :MLOAD(arithRes2)
; check arithRes2 is 0, no need to round in this case
0 => A
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
$ :EQ, JMPC(MSIZEend)
$ :MLOAD(arithRes2), JMPZ(MSIZEend)

; Round size to 32bytes multiple
C + 1 => C
C * 32 => E
C * 32 + 32 => E

MSIZEend:
E :MSTORE(SP++); [size(E) => SP]
Expand Down
6 changes: 2 additions & 4 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,8 @@ callContract:
; get contract length
%SMT_KEY_SC_LENGTH => B
0 => C
$ => B :SLOAD
B :MSTORE(bytecodeLength)
0 => A
$ :EQ, JMPC(defaultOpCode) ;no bytecode
; Max length of a smart contract is 24kb, less than 32 bits, no need to use a binary
$ => B :SLOAD, MSTORE(bytecodeLength), JMPZ(defaultOpCode) ; no bytecode

; check poseidon counters
; 56 is the value used by the prover to increment poseidon counters depending on the hash length
Expand Down
4 changes: 1 addition & 3 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -971,10 +971,8 @@ endMoveBalances:
invalidCall:
; return to origin context
$ => SR :MLOAD(initSR)
$ => A :MLOAD(originCTX)
0 => B
; if last context, finish tx
$ :EQ,JMPC(handleGas)
$ => A :MLOAD(originCTX),JMPZ(handleGas)
A => CTX
0 :MSTORE(retDataCTX)
CTX :MSTORE(currentCTX)
Expand Down

0 comments on commit a36998b

Please sign in to comment.