Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SP optimitzations #206

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ opCALLDATACOPY:
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
; check stack underflow
SP - 3 :JMPN(stackUnderflow)
SP - 1 => SP
$ => E :MLOAD(SP--); [destOffset => E]
$ => B :MLOAD(SP--); [offset => B]
SP - 3 => SP :JMPN(stackUnderflow)
$ => E :MLOAD(SP+2); [destOffset => E]
$ => B :MLOAD(SP+1); [offset => B]
$ => C :MLOAD(SP); [size => C]
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset)
Expand Down Expand Up @@ -283,14 +282,14 @@ VAR GLOBAL memInteger
* - stack output: []
*/
opCODECOPY:
; check stack underflow
SP - 3 :JMPN(stackUnderflow)
; if is a create, copy from calldata
$ => A :MLOAD(isCreateContract)
0 - A :JMPN(opCALLDATACOPY)
SP - 1 => SP
$ => C :MLOAD(SP--); [destOffset => C]
$ => D :MLOAD(SP--); [offset => D]

; check stack underflow
SP - 3 => SP :JMPN(stackUnderflow)
$ => C :MLOAD(SP+2); [destOffset => C]
$ => D :MLOAD(SP+1); [offset => D]
$ => E :MLOAD(SP); [size => E]
; store lastMemOffset for memory expansion gas cost
C :MSTORE(lastMemOffset)
Expand Down Expand Up @@ -432,22 +431,35 @@ opEXTCODECOPY:
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
; check stack underflow
SP - 4 :JMPN(stackUnderflow)
SP - 1 => SP
$ => A :MLOAD(SP--), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address]
SP - 4 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP+3), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address]
:CALL(isColdAddress); in: [A: address] out: [D: 0 if warm, 1 if cold]
; check out-of-gas
GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas)
:CALL(opEXTCODECOPYCheckHash)
$ => C :MLOAD(SP--); [destOffset => C]
$ => D :MLOAD(SP--); [offset => D]
$ => C :MLOAD(SP+2); [destOffset => C]
$ => D :MLOAD(SP+1); [offset => D]
$ => E :MLOAD(SP); [size => E]
; store lastMemOffset for memory expansion gas cost
C :MSTORE(lastMemOffset)
; store lastMemLength for memory expansion gas cost
E :MSTORE(lastMemLength)
; check out-of-gas
GAS - ${3*((E+31)/32)} => GAS :JMPN(outOfGas)
;${3*((E+31)/32)}
E+31 => A
;(E+31)/32
A :MSTORE(arithA)
32 :MSTORE(arithB)
:CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => A :MLOAD(arithRes1)
; Mul operation with Arith
; 3*((E+31)/32)
3 :MSTORE(arithA)
A :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB]
$ => A :MLOAD(arithRes1)
; check out-of-gas
GAS - A => GAS :JMPN(outOfGas)

; compute memory expansion gas cost
:CALL(saveMem); in: [lastMemOffset, lastMemLength]
; if offset is above data len, length => offset
Expand Down Expand Up @@ -588,12 +600,12 @@ opRETURNDATACOPY:
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign)
; check stack underflow
SP - 3 :JMPN(stackUnderflow)
SP - 3 => SP :JMPN(stackUnderflow)
; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
SP - 1 => SP
$ => D :MLOAD(SP--); [destOffset => D]
$ => E :MLOAD(SP--); [offset => E]

$ => D :MLOAD(SP+2); [destOffset => D]
$ => E :MLOAD(SP+1); [offset => E]
$ => C :MLOAD(SP); [size => C]
; store lastMemOffset for memory expansion gas cost
D :MSTORE(lastMemOffset)
Expand Down
12 changes: 6 additions & 6 deletions main/opcodes/comparison.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,18 @@ opISZERO:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)
SP - 1 :JMPN(stackUnderflow)

; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)

; read one item from stack
$ => A :MLOAD(SP) ; [a => B]
$ => A :MLOAD(SP-1) ; [a => B]
0 => B

; call binary:Equal state machine
; and push the result into the stack
$ :EQ, MSTORE(SP++), JMP(readCode) ; [(a == 0) ==> SP]
$ :EQ, MSTORE(SP-1), JMP(readCode) ; [(a == 0) ==> SP]

/**
* @link [https://www.evm.codes/#16?fork=berlin]
Expand Down Expand Up @@ -273,16 +273,16 @@ opNOT:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)
SP - 1 :JMPN(stackUnderflow)

; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)

; read one item from the stack
$ => A :MLOAD(SP)
$ => A :MLOAD(SP-1)

0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn => B ; 2**226 - 1 =>
$ => A :XOR,MSTORE(SP++), JMP(readCode) ; [ NOT a => SP]
$ => A :XOR,MSTORE(SP-1), JMP(readCode) ; [ NOT a => SP]

/**
* @link [https://www.evm.codes/#1a?fork=berlin]
Expand Down
6 changes: 3 additions & 3 deletions main/opcodes/context-information.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ opBALANCE:
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 252 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)
SP - 1 :JMPN(stackUnderflow)
; Mask address to 20 bytes
$ => A :MLOAD(SP), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address]
$ => A :MLOAD(SP-1), CALL(maskAddress); [address => A]; in: [A: address] out: [A: masked address]
; set key for smt balance query
0 => B,C
; balance in D
$ => D :SLOAD
D :MSTORE(SP++), CALL(isColdAddress); [balance(D) => SP]; in: [A: address] out: [D: 0 if warm, 1 if cold]
D :MSTORE(SP-1), CALL(isColdAddress); [balance(D) => SP]; in: [A: address] out: [D: 0 if warm, 1 if cold]
; check out-of-gas
GAS - %WARM_STORGE_READ_GAS - D * %COLD_ACCOUNT_ACCESS_COST_RED => GAS :JMPN(outOfGas, readCode)

Expand Down
11 changes: 5 additions & 6 deletions main/opcodes/create-terminate-context.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,8 @@ opCALLCODEend:
*/
opRETURN:
; check stack underflow
SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
$ => E :MLOAD(SP--); [offset => E]
SP - 2 => SP :JMPN(stackUnderflow)
$ => E :MLOAD(SP+1); [offset => E]
$ => C :MLOAD(SP); [size => C]

; checks zk-counters
Expand Down Expand Up @@ -902,12 +901,12 @@ opREVERT:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
; check stack underflow
SP - 2 :JMPN(stackUnderflow)
SP - 2 => SP :JMPN(stackUnderflow)
; load initSR to revert al state changes
; revert touched accounts
$ => SR :MLOAD(initSR), CALL(revertTouched)
SP - 1 => SP
$ => E :MLOAD(SP--); [offset => E]

$ => E :MLOAD(SP+1); [offset => E]
$ => C :MLOAD(SP); [size => C]
E :MSTORE(retDataOffset)
C :MSTORE(retDataLength)
Expand Down
3 changes: 1 addition & 2 deletions main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ opJUMPI:
$ => B :MLOAD(SP); [value => B]
0 => A
$ :EQ, JMPC(readCode)
SP + 1 => SP
; Check PC is a JUMPDEST
$ => B :MLOAD(SP--), JMP(checkJumpDest); [counter => PC]
$ => B :MLOAD(SP+1), JMP(checkJumpDest); [counter => PC]

checkJumpDest:
; if it is a deploy we have to check the data from the calldata (not the bytecode)
Expand Down
Loading