Skip to content

Commit

Permalink
Boundaries check fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Jan 16, 2023
1 parent af786a8 commit 147ba77
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
34 changes: 17 additions & 17 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
SP - 1 => SP :JMPN(stackUnderflow)
; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
$ => B :MLOAD(SP); [offset => B]
$ => A :MLOAD(txCalldataLen)
; if offset is lower than calldata length, return 0
$ :LT,JMPC(CALLDATALOADreturn0)
B => E
$ => A :MLOAD(SP); [offset => B]
$ => B :MLOAD(txCalldataLen)
; if offset is not lower than calldata length, return 0
$ :LT,JMPNC(CALLDATALOADreturn0)
A => E
; Div operation with Arith
E :MSTORE(arithA)
32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
Expand Down Expand Up @@ -94,7 +94,7 @@ opCALLDATACOPY:
; check stack underflow
SP - 3 => SP :JMPN(stackUnderflow)
$ => E :MLOAD(SP+2); [destOffset => E]
$ => B :MLOAD(SP+1); [offset => B]
$ => A :MLOAD(SP+1); [offset => B]
$ => C :MLOAD(SP); [size => C]
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset)
Expand All @@ -104,25 +104,25 @@ opCALLDATACOPY:
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
;${3*((C+31)/32)}
;(C+31)/32 => A
C+31 => A
A :MSTORE(arithA)
C+31 => B
B :MSTORE(arithA)
32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => A :MLOAD(arithRes1)
$ => B :MLOAD(arithRes1)
; Mul operation with Arith
; 3*A
3 :MSTORE(arithA)
A :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB]
$ => A :MLOAD(arithRes1)
GAS - A => GAS :JMPN(outOfGas)
B :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB]
$ => B :MLOAD(arithRes1)
GAS - B => GAS :JMPN(outOfGas)
:CALL(saveMem); in: [lastMemOffset, lastMemLength]
; save current stack pointer
SP :MSTORE(SPw)
$ => A :MLOAD(txCalldataLen)
; if offset is lower than calldata length, return 0
$ :LT,JMPC(opCALLDATACOPY0)
$ => B :MLOAD(txCalldataLen)
; if offset is not lower than calldata length, return 0
$ :LT,JMPNC(opCALLDATACOPY0)
A + C => A
; if offset + size is lower then calldata size => length
$ :LT,JMPC(opCALLDATACOPYX0, opCALLDATACOPYloop)
; if offset + size is lower than calldata size => length
$ :LT,JMPC(opCALLDATACOPYloop, opCALLDATACOPYX0)

opCALLDATACOPYX0:
$ => C :MLOAD(txCalldataLen)
Expand Down
3 changes: 1 addition & 2 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ initMSTORE:
0 => C
0 :MSTORE(isMSTOREX), JMP(finalMSTORE)

; used if memory to load is allocated in two different slots
; used if memory to store is allocated in two different slots
MSTOREX2:
$ => D :MLOAD(tmpVarCmstore)
C + D => D
Expand Down Expand Up @@ -239,7 +239,6 @@ MSTOREX2:

finalMSTORE:
C :JMPNZ(memAlignOptionMSTORE)
; (*) NOTE: only using E0 on address indirection, ignores E1..E7
$ => A :MLOAD(bytesToStore)
A :MSTORE(MEM:E)
E*32 => E :JMP(offsetMSTORE32)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"yargs": "^17.5.1"
},
"devDependencies": {
"@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/rlp-checks",
"@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/read-write-addresses",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#develop",
"chai": "^4.3.6",
"chalk": "^3.0.0",
"eslint": "^8.25.0",
Expand Down

0 comments on commit 147ba77

Please sign in to comment.