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

Boundaries check fixes #228

Merged
merged 4 commits into from
Jan 17, 2023
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
4 changes: 2 additions & 2 deletions counters/tests/opCREATE2.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ operation:
:JMP(opCREATE2)
checkCounters:
500 - STEP:JMPN(failedCounters)
30 - CNT_BINARY :JMPNZ(failedCounters)
28 - CNT_BINARY :JMPNZ(failedCounters)
5 - CNT_ARITH :JMPNZ(failedCounters)
%OPCREATE2_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters)
2 - CNT_MEM_ALIGN :JMPNZ(failedCounters)
%OPCREATE2_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters)
33 - CNT_POSEIDON_G :JMPNZ(failedCounters)
27 - CNT_POSEIDON_G :JMPNZ(failedCounters)

0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0
finalizeExecution:
Expand Down
4 changes: 2 additions & 2 deletions counters/tests/opREVERT.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ operation:
:JMP(opREVERT)
checkCounters:
800 - STEP:JMPN(failedCounters)
41 - CNT_BINARY :JMPNZ(failedCounters)
39 - CNT_BINARY :JMPNZ(failedCounters)
11 - CNT_ARITH :JMPNZ(failedCounters)
%OPREVERT_CNT_KECCAK_F - CNT_KECCAK_F :JMPNZ(failedCounters)
4 - CNT_MEM_ALIGN :JMPNZ(failedCounters)
%OPREVERT_CNT_PADDING_PG - CNT_PADDING_PG :JMPNZ(failedCounters)
6 - CNT_POSEIDON_G :JMPNZ(failedCounters)
%OPREVERT_CNT_POSEIDON_G - CNT_POSEIDON_G :JMPNZ(failedCounters)

0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0
finalizeExecution:
Expand Down
1 change: 0 additions & 1 deletion main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
CONSTL %ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2 = 0xAE4bB80bE56B819606589DE61d5ec3b522EEB032n
CONSTL %ADDRESS_SYSTEM = 0x000000000000000000000000000000005ca1ab1en
CONST %CALLDATA_OFFSET = 1024
CONST %MEMORY_LENGTH = 0x400000
CONST %BATCH_DIFFICULTY = 0
CONST %TX_GAS_LIMIT = 30000000
CONST %GLOBAL_EXIT_ROOT_STORAGE_POS = 0
Expand Down
36 changes: 19 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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offset => B -> offset => A

$ => 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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offset => B -> offset => A

$ => A :MLOAD(SP+1); [offset => B]
$ => C :MLOAD(SP); [size => C]
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset)
Expand All @@ -104,24 +104,26 @@ 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)
A + C => A
; if offset + size is lower then calldata size => length
$ => B :MLOAD(txCalldataLen)
A => D
; if offset is not lower than calldata length, return 0
$ :LT,JMPNC(opCALLDATACOPY0)
B + C => A
D => B
; if offset + size is lower than calldata size => length
$ :LT,JMPC(opCALLDATACOPYX0, opCALLDATACOPYloop)

opCALLDATACOPYX0:
Expand Down
3 changes: 1 addition & 2 deletions main/opcodes/stack-operations.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ opAuxPUSHB:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
; check is a create/create2
$ => A :MLOAD(isCreate)
0 - A :JMPN(opAuxPUSHBcreate)
$ => A :MLOAD(isCreate), JMPNZ(opAuxPUSHBcreate)
; set bytes length to read to C
D - 1 => C
0 => A
Expand Down
88 changes: 0 additions & 88 deletions main/precompiled/modexp.zkasm

This file was deleted.

2 changes: 1 addition & 1 deletion main/touched.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ checkpointTouched:
; @info Back to initial touched root
revertTouched:
$ => A :MLOAD(initTouchedSR)
A :MSTORE(touchedSR)
A :MSTORE(touchedSR), RETURN

; @info Check if an address is cold. If it is, add it to the touched tree
; @in A => address
Expand Down
33 changes: 16 additions & 17 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ MSTORE32:
D :MSTORE(tmpVarDmstore)
; check offset is lower than max memory
E => A
%MEMORY_LENGTH => B
%MAX_MEM_EXPANSION_BYTES => B
$ :LT,JMPC(initMSTORE, errorMLOADMSTORE)

initMSTORE:
Expand Down 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 Expand Up @@ -312,7 +311,7 @@ MLOAD32:
D :MSTORE(tmpVarDmload)
; check offset is lower than max memory
E => A
%MEMORY_LENGTH => B
%MAX_MEM_EXPANSION_BYTES => B
$ :LT,JMPC(initMLOAD, errorMLOADMSTORE)

initMLOAD:
Expand Down Expand Up @@ -354,7 +353,7 @@ endMLOAD:
$ => D :MLOAD(tmpVarDmload), RETURN

errorMLOADMSTORE:
:JMP(handleError)
:JMP(outOfGas)

VAR GLOBAL tmpVarAemptyAcc
VAR GLOBAL tmpVarBemptyAcc
Expand All @@ -378,7 +377,7 @@ isEmptyAccount:

E => A
; read balance
; set key for smt balance query
; set key for smt balance query.
%SMT_KEY_BALANCE => B
0 => C
$ => B :SLOAD
Expand Down Expand Up @@ -900,24 +899,24 @@ firstContextInvalid:
VAR GLOBAL tmpVarAoffsetUtil
VAR GLOBAL tmpVarBoffsetUtil
;Get offset/32 & offset%32
;@in A offset
;@in A offset (offset is assumed to be less than %MAX_MEM_EXPANSION_BYTES)
;@out E offset/32
;@out C offset%32
offsetUtil:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
A :MSTORE(tmpVarAoffsetUtil)
B :MSTORE(tmpVarAoffsetUtil)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
A :MSTORE(tmpVarAoffsetUtil)
B :MSTORE(tmpVarBoffsetUtil)

${A >> 5} => E
${A & 0x1F} => C
E * 32 + C :ASSERT
E * 32 + C :ASSERT
C => A
0x20 => B
$ :LT,JMPNC(failAssert)
0x0FFFF - E :JMPN(stackUnderflow)
31 - C :JMPN(stackUnderflow)
$ => A :MLOAD(tmpVarAoffsetUtil)
$ => B :MLOAD(tmpVarBoffsetUtil), RETURN
$ :LT,JMPNC(failAssert)

$ => A :MLOAD(tmpVarAoffsetUtil)
$ => B :MLOAD(tmpVarBoffsetUtil), RETURN

;@info: move balances between two accounts
;@in: txSrcAddr: source address
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