Skip to content

Commit

Permalink
Merge pull request #178 from 0xPolygonHermez/feature/fix-jump
Browse files Browse the repository at this point in the history
fix jump PC
  • Loading branch information
krlosMata authored Nov 25, 2022
2 parents e0632bf + 3516182 commit f9fafbd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
11 changes: 10 additions & 1 deletion main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ opCALLDATALOAD:
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)

SP - 1 => SP :JMPN(stackUnderflow)
$ => E :MLOAD(SP)
$ => B :MLOAD(SP)
$ => A :MLOAD(txCalldataLen)
$ :LT,JMPC(CALLDATALOADreturn0)
B => E
; Div operation with Arith
E :MSTORE(arithA)
32 :MSTORE(arithB)
Expand Down Expand Up @@ -41,6 +44,12 @@ opCALLDATALOAD2:
GAS - 3 => GAS :JMPN(outOfGas)
:JMP(readCode)

CALLDATALOADreturn0:
0 :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
GAS - 3 => GAS :JMPN(outOfGas)
:JMP(readCode)

opCALLDATASIZE:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
Expand Down
11 changes: 6 additions & 5 deletions main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ opJUMP:
; check out-of-gas
GAS - 8 => GAS :JMPN(outOfGas)

$ => PC :MLOAD(SP); [counter => PC]
$ => B :MLOAD(SP); [counter => PC]
; Check PC is a JUMPDEST
:JMP(checkJumpDest)
/**
Expand All @@ -42,8 +42,7 @@ opJUMPI:
0 => A
$ :EQ, JMPC(readCode)
SP + 1 => SP
$ => PC :MLOAD(SP--); [counter => PC]
PC => B
$ => B :MLOAD(SP--); [counter => PC]
; Check PC is a JUMPDEST
:JMP(checkJumpDest)

Expand All @@ -52,10 +51,10 @@ checkJumpDest:
; if it is a deploy we have to check the data from the calldata (not the bytecode)
$ => A :MLOAD(isCreateContract)
-A :JMPN(checkJumpDestDeployment)
PC => HASHPOS
HASHPOS => B
$ => A :MLOAD(bytecodeLength)
$ :LT,JMPC(invalidJump)
B => PC
B => HASHPOS
; set number of bytes to hashP at D
1 => D
; get hashP address pointer where contract bytecode is stored
Expand All @@ -72,6 +71,7 @@ checkJumpDestDeployment:
; 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
B => PC
HASHPOS + PC => HASHPOS
; get memory pointer for hashing
$ => E :MLOAD(batchHashDataId)
Expand All @@ -93,6 +93,7 @@ checkJumpDestDeploymentCreate:
; get offset call position
$ => E :MLOAD(argsOffsetCall)
; increase current program counter (PC) to offset for getting pushed bytes to read
B => PC
E + PC => E
; set bytes to read from memory at C
1 => C
Expand Down
11 changes: 9 additions & 2 deletions main/opcodes/stack-operations.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,19 @@ opAuxPUSHA:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 6 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)


$ => B :MLOAD(bytecodeLength)
PC + D => A
$ :LT,JMPC(opAuxPUSHA2)
PC => A
B - A => D

opAuxPUSHA2:
; check out-of-gas
GAS - 3 => GAS :JMPN(outOfGas)
; read bytes from calldata
:CALL(readPush); in: [D: bytes to read] out: [E: value read]
; store stack output
; store stack output
E :MSTORE(SP++); [pushed_value(E) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
Expand Down

0 comments on commit f9fafbd

Please sign in to comment.