Skip to content

Commit

Permalink
Merge pull request #207 from 0xPolygonHermez/feature/jmpnz-optimization
Browse files Browse the repository at this point in the history
Feature/jmpnz optimization
  • Loading branch information
krlosMata authored Dec 15, 2022
2 parents a488581 + 82ed040 commit 2c9227c
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 211 deletions.
7 changes: 2 additions & 5 deletions main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ setGlobalExitRoot:
%SMT_KEY_SC_STORAGE => B

; read timestamp given the globalExitRoot
$ => D :SLOAD

; skip overwrite timestamp if it is different than 0
-D :JMPN(skipSetGlobalExitRoot)
$ => D :SLOAD, JMPNZ(skipSetGlobalExitRoot)

$ => D :MLOAD(timestamp)
$ => SR :SSTORE ; Store 'timestamp' in storage position 'keccak256(globalExitRoot, 0)'
Expand Down Expand Up @@ -118,8 +116,7 @@ endCheckRLP:

txLoop:
$ => A :MLOAD(pendingTxs)
A-1 => A :MSTORE(pendingTxs)
A :JMPN(processTxsEnd)
A-1 => A :MSTORE(pendingTxs), JMPN(processTxsEnd)

$ => A :MLOAD(ctxTxToUse) ; Load first context used by transaction
A+1 => CTX :MSTORE(ctxTxToUse),JMP(processTx)
Expand Down
27 changes: 10 additions & 17 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
E :MSTORE(arithA)
32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(arithRes2)
; if data not aligned (remainder > 0), shift
0 - A :JMPN(opCALLDATALOAD2)
$ => A :MLOAD(arithRes2), JMPNZ(opCALLDATALOAD2)
SP => C
; add %CALLDATA_OFFSET to point calldata in the memory
%CALLDATA_OFFSET + B => SP
Expand Down Expand Up @@ -70,8 +69,7 @@ opCALLDATASIZE:
; check out-of-gas
GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas)
; return 0 for contract creation
$ => A :MLOAD(isCreateContract)
0 - A :JMPN(opCALLDATASIZEdep)
$ => A :MLOAD(isCreateContract), JMPNZ(opCALLDATASIZEdep)
$ => B :MLOAD(txCalldataLen)
B :MSTORE(SP++); [size => SP]
; check stack overflow
Expand Down Expand Up @@ -142,7 +140,7 @@ opCALLDATACOPYinit:
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
; finish loop
C - 1 :JMPN(opCALLDATACOPYcheckLen)
C :JMPZ(opCALLDATACOPYcheckLen)
; copy last bytes
C - 32 :JMPN(opCALLDATACOPYfinal)
B :MSTORE(arithA)
Expand Down Expand Up @@ -190,8 +188,7 @@ opCALLDATACOPYfinal:
$ => C :MLOAD(SP); [size => C]
; point to next memory slot
B + 1 => SP
C - 32 + D => D
D :JMPN(opCALLDATACOPYxor)
C - 32 + D => D :JMPN(opCALLDATACOPYxor)
A => B
$ => A :MLOAD(SP); [calldata => C]
32 - D => D :CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
Expand Down Expand Up @@ -246,8 +243,7 @@ opCODESIZE:
; check out-of-gas
GAS-%GAS_QUICK_STEP => GAS :JMPN(outOfGas)
; if is create, get size from calldata
$ => A :MLOAD(isCreateContract)
0 - A :JMPN(opCODESIZEdep)
$ => A :MLOAD(isCreateContract), JMPNZ(opCODESIZEdep)
; else, from storage
$ => A :MLOAD(txDestAddr)
; set key for smt bytecode length query
Expand Down Expand Up @@ -283,9 +279,7 @@ VAR GLOBAL memInteger
*/
opCODECOPY:
; if is a create, copy from calldata
$ => A :MLOAD(isCreateContract)
0 - A :JMPN(opCALLDATACOPY)

$ => A :MLOAD(isCreateContract), JMPNZ(opCALLDATACOPY)
; check stack underflow
SP - 3 => SP :JMPN(stackUnderflow)
$ => C :MLOAD(SP+2); [destOffset => C]
Expand Down Expand Up @@ -343,8 +337,7 @@ opCODECOPYinit:

%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon)

$ => B :MLOAD(remainingBytes)
B - 1 :JMPN(readCode)
$ => B :MLOAD(remainingBytes), JMPZ(readCode)
$ => A :MLOAD(memOffset), CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
E :MSTORE(memInteger)
; read M0 previous value
Expand Down Expand Up @@ -516,7 +509,7 @@ opEXTCODECOPYCheckHash:
opEXTCODECOPYCheckHashLoop:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
; finish reading bytecode
B - 1 :JMPN(opEXTCODECOPYCheckHashLoopEnd)
B :JMPZ(opEXTCODECOPYCheckHashLoopEnd)
; add bytes to hash contract bytecode
${getBytecode(A, HASHPOS, 1)} :HASHP1(E)
B - 1 => B :JMP(opEXTCODECOPYCheckHashLoop)
Expand All @@ -532,7 +525,7 @@ opEXTCODECOPYCheckHashEnd:
opEXTCODECOPYinit:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
C - 1 :JMPN(readCode)
C :JMPZ(readCode)
C - 32 :JMPN(opEXTCODECOPYfinal)
${getBytecode(A,B,32)} => D
; set bytesToStore with value to use in MSTORE
Expand Down Expand Up @@ -645,7 +638,7 @@ opRETURNDATACOPY:

opRETURNDATACOPYinit:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
C - 1 :JMPN(readCode)
C :JMPZ(readCode)
C - 32 :JMPN(opRETURNDATACOPYfinal)
B => E
; Load memory from last context used
Expand Down
4 changes: 2 additions & 2 deletions main/opcodes/comparison.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ opSAR:

; check sign negative or positive
; if sign positive, return shifted result. sign negative, return -(shifted result)]
E - 1 :JMPN(finishOpSAR)
E :JMPZ(finishOpSAR)

; sign negative
A => B ; [shifted result => B]
Expand All @@ -447,7 +447,7 @@ maxShiftSAR:
0 => D ; [sign positive: 0 => D]

; check sign negative or positive
E - 1 :JMPN(endMaxShiftSAR)
E :JMPZ(endMaxShiftSAR)
%MAX_UINT_256 => D ; [sign negative: MAX_UINT_256 => D]

endMaxShiftSAR:
Expand Down
3 changes: 1 addition & 2 deletions main/opcodes/context-information.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ opADDRESS:
; check out-of-gas
GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas)
; check is a create
$ => A :MLOAD(isCreateContract)
0 - A :JMPN(opADDRESSdeploy)
$ => A :MLOAD(isCreateContract), JMPNZ(opADDRESSdeploy)
; load current storage address
$ => A :MLOAD(storageAddr), JMP(opADDRESSend)

Expand Down
36 changes: 13 additions & 23 deletions main/opcodes/create-terminate-context.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
*/
opSTOP:
; check if first context
$ => B :MLOAD(originCTX)
; if origin ctx is 0, end tx
B - 1 :JMPN(handleGas)
$ => B :MLOAD(originCTX), JMPZ(handleGas)
; else handle finish ctx
$ => A :MLOAD(gasRefund)
; check if is a create call
$ => C :MLOAD(isCreate)
; set 1 to D, default stack return in stop code
1 => D
C - 1 :JMPN(opSTOPend)
C :JMPZ(opSTOPend)
; if is a create, return created contract address
$ => D :MLOAD(createContractAddress)

Expand Down Expand Up @@ -70,8 +69,7 @@ opCREATE:
; check stack underflow
SP - 3 :JMPN(stackUnderflow)
; check is static
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => D :MLOAD(SP--); [value => D]
D :MSTORE(valueCall)
Expand Down Expand Up @@ -194,9 +192,7 @@ opCALL:
$ => A :MLOAD(SP--); [value => A]
A :MSTORE(valueCall)
; if is static and value is not 0 -> revert
$ => B :MLOAD(isStaticCall)
B - 1 :JMPN(opCALL2)

$ => B :MLOAD(isStaticCall), JMPZ(opCALL2)
0 => B
$ :EQ,JMPC(opCALL2, invalidStaticTx)

Expand Down Expand Up @@ -439,12 +435,10 @@ opRETURN:
; compute memory expansion gas cost
C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
; check is a create
$ => D :MLOAD(isCreateContract)
0 - D :JMPN(opRETURNdeploy)
$ => D :MLOAD(isCreateContract), JMPNZ(opRETURNdeploy)
; reduce CTX
$ => B :MLOAD(originCTX)
; if origin ctx is 0, end tx
B - 1 :JMPN(handleGas)
$ => B :MLOAD(originCTX), JMPZ(handleGas)
; handle gas refund
$ => B :MLOAD(gasRefund)
$ => A :MLOAD(currentCTX)
Expand Down Expand Up @@ -473,7 +467,7 @@ opRETURN32:
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon)

C - 1 :JMPN(opRETURNend)
C :JMPZ(opRETURNend)
C - 32 :JMPN(opRETURNfinal)
; load first 32 bytes from memory at current CTX
:CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
Expand Down Expand Up @@ -517,8 +511,7 @@ opRETURNdeploy:
;code_deposit_cost = 200 * returned_code_size
GAS - %RETURN_GAS_COST * C => GAS :JMPN(outOfGas)
; check if first context
$ => B :MLOAD(originCTX)
0 - B :JMPN(opRETURNcreate, endDeploy)
$ => B :MLOAD(originCTX), JMPNZ(opRETURNcreate, endDeploy)

opRETURNcreate:
; checks zk-counters
Expand Down Expand Up @@ -703,8 +696,7 @@ opCREATE2:
; check stack underflow
SP - 4 :JMPN(stackUnderflow)
; check is static
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => A :MLOAD(SP--); [value => D]
A :MSTORE(valueCall)
Expand Down Expand Up @@ -920,7 +912,7 @@ opREVERT:
$${eventLog(onError, revert)}
0 :MSTORE(gasRefund)
; if origin ctx is 0, end tx
B - 1 :JMPN(handleGas)
B :JMPZ(handleGas)
; decrease CTX
$ => A :MLOAD(currentCTX)
B => CTX
Expand All @@ -945,7 +937,7 @@ opREVERT32:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)

C - 1 :JMPN(opREVERTend)
C :JMPZ(opREVERTend)
; load first 32 bytes from memory at current CTX
C - 32 :JMPN(opREVERTfinal)
:CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
Expand Down Expand Up @@ -1004,9 +996,7 @@ opSENDALL:
SP - 1 => SP :JMPN(stackUnderflow)

; check is static
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)

$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
$ => A :MLOAD(storageAddr)
; set key for smt balance query
%SMT_KEY_BALANCE => B
Expand Down Expand Up @@ -1060,7 +1050,7 @@ opSENDALL2:
$ => B :MLOAD(gasRefund)
$ => C :MLOAD(isCreate)
1 => D
C - 1 :JMPN(opSENDALLend)
C :JMPZ(opSENDALLend)
$ => D :MLOAD(createContractAddress)

opSENDALLend:
Expand Down
2 changes: 1 addition & 1 deletion main/opcodes/crypto.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ opSHA3Loop:
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

C - 1 :JMPN(opSHA3End)
C :JMPZ(opSHA3End)
C - 32 :JMPN(opSHA3Final)
; load next 32 bytes from memory
:CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
Expand Down
6 changes: 2 additions & 4 deletions main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ opJUMPI:

checkJumpDest:
; if it is a deploy we have to check the data from the calldata (not the bytecode)
$ => A :MLOAD(isCreateContract)
-A :JMPN(checkJumpDestDeployment)
$ => A :MLOAD(isCreateContract), JMPNZ(checkJumpDestDeployment)
$ => A :MLOAD(bytecodeLength)
$ :LT,JMPC(invalidJump)
B => PC
Expand All @@ -60,8 +59,7 @@ checkJumpDest:
$ :EQ, JMPC(readCode, invalidJump)
checkJumpDestDeployment:
; check jumpDest from calldata
$ => A :MLOAD(isCreate)
-A :JMPN(checkJumpDestDeploymentCreate)
$ => A :MLOAD(isCreate), JMPNZ(checkJumpDestDeploymentCreate)
; get position where data starts in the tx
$ => HASHPOS :MLOAD(dataStarts)
; add PC to data starts to point the bytes to read for the push
Expand Down
19 changes: 7 additions & 12 deletions main/opcodes/logs.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ opLOG0:
GAS - %LOG_GAS => GAS :JMPN(outOfGas)

; check is static
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)

SP - 1 => SP
$ => E :MLOAD(SP--) ; [offset => E]
Expand Down Expand Up @@ -48,8 +47,7 @@ opLOG1:

SP - 3 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
Expand All @@ -72,8 +70,7 @@ opLOG2:

SP - 4 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
Expand All @@ -96,8 +93,7 @@ opLOG3:

SP - 5 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
Expand All @@ -120,8 +116,7 @@ opLOG4:

SP - 6 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall)
0 - A :JMPN(invalidStaticTx)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
Expand All @@ -143,7 +138,7 @@ opLOGLoop:
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

C - 1 :JMPN(opSaveTopicsInit)
C :JMPZ(opSaveTopicsInit)
; load next 32 bytes
C - 32 :JMPN(opLOGFinal)
:CALL(MLOAD32); in: [E: offset] out: [A: value]
Expand All @@ -164,7 +159,7 @@ opSaveTopicsLoop:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

A - 1 :JMPN(readCode)
A :JMPZ(readCode)
; check stack underflow
SP - 1 => SP
; check out-of-gas
Expand Down
Loading

0 comments on commit 2c9227c

Please sign in to comment.