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

Fix/counter steps #180

Merged
merged 5 commits into from
Nov 30, 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
3 changes: 2 additions & 1 deletion main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ CONST %SSTORE_RESET_GAS = 2900
CONST %SSTORE_CLEARS_SCHEDULE = 15000

; COUNTERS
CONST %MAX_CNT_STEPS = 2**23
CONST %MIN_STEPS_FINISH_BATCH = 200 ; min steps to finish tx
CONST %MAX_CNT_STEPS = 2**23 - %MIN_STEPS_FINISH_BATCH

CONST %MAX_CNT_ARITH = %MAX_CNT_STEPS / 32
CONST %MAX_CNT_BINARY = %MAX_CNT_STEPS / 32
Expand Down
18 changes: 9 additions & 9 deletions main/opcodes/arithmetic.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
opADD:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand All @@ -33,7 +33,7 @@ opADD:
opMUL:

%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
Expand All @@ -52,7 +52,7 @@ opMUL:
opSUB:

%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
Expand All @@ -67,7 +67,7 @@ opDIV:

%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
Expand All @@ -87,7 +87,7 @@ opSDIV:

%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 8 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
Expand Down Expand Up @@ -133,7 +133,7 @@ opMOD:

%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
Expand All @@ -153,7 +153,7 @@ opSMOD:

%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 8 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
Expand Down Expand Up @@ -255,7 +255,7 @@ opMULMOD:
opEXP:

%MAX_CNT_BINARY - CNT_BINARY - 5 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
Expand All @@ -272,7 +272,7 @@ opEXP:
opSIGNEXTEND: ; following this impl https://github.com/ethereumjs/ethereumjs-monorepo/blob/master/packages/vm/src/evm/opcodes/functions.ts#L193

%MAX_CNT_ARITH - CNT_ARITH - 6 :JMPN(outOfCountersArith)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 2 :JMPN(stackUnderflow)
SP - 1 => SP
Expand Down
16 changes: 8 additions & 8 deletions main/opcodes/block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ opBLOCKHASH:
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 :JMPN(outOfCountersKeccak)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 1 => SP :JMPN(stackUnderflow)
GAS - 20 => GAS :JMPN(outOfGas)
Expand Down Expand Up @@ -40,7 +40,7 @@ opBLOCKHASHzero:

opCOINBASE:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

$ => A :MLOAD(sequencerAddr)
A :MSTORE(SP++)
Expand All @@ -50,7 +50,7 @@ opCOINBASE:

opTIMESTAMP:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

$ => A :MLOAD(timestamp)
A :MSTORE(SP++)
Expand All @@ -60,7 +60,7 @@ opTIMESTAMP:

opNUMBER:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

; Get current tx count
$ => D :MLOAD(txCount)
Expand All @@ -71,7 +71,7 @@ opNUMBER:

opDIFFICULTY:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

%BATCH_DIFFICULTY => A
A :MSTORE(SP++)
Expand All @@ -81,7 +81,7 @@ opDIFFICULTY:

opGASLIMIT:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

%TX_GAS_LIMIT => A
A :MSTORE(SP++)
Expand All @@ -91,7 +91,7 @@ opGASLIMIT:

opCHAINID:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

$ => A :MLOAD(chainID)
A :MSTORE(SP++)
Expand All @@ -100,7 +100,7 @@ opCHAINID:
:JMP(readCode)
opBASEFEE:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

0 :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
Expand Down
30 changes: 15 additions & 15 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ opCALLDATALOAD:

%MAX_CNT_ARITH - CNT_ARITH - 64 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 66 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 1 => SP :JMPN(stackUnderflow)
$ => B :MLOAD(SP)
Expand Down Expand Up @@ -52,7 +52,7 @@ CALLDATALOADreturn0:

opCALLDATASIZE:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

$ => A :MLOAD(isCreateContract)
0 - A :JMPN(opCALLDATASIZEdep)
Expand All @@ -75,7 +75,7 @@ opCALLDATACOPY:
%MAX_CNT_ARITH - CNT_ARITH - 192 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 192 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 3 :JMPN(stackUnderflow)
SP - 1 => SP
Expand All @@ -102,7 +102,7 @@ opCALLDATACOPYinit:
%MAX_CNT_ARITH - CNT_ARITH - 192 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 192 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 1 :JMPN(outOfCountersMemalign)
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)

C - 1 :JMPN(opCALLDATACOPYcheckLen)
C - 32 :JMPN(opCALLDATACOPYfinal)
Expand Down Expand Up @@ -196,7 +196,7 @@ opCALLDATACOPYend:
opCODESIZE:

%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 252 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

$ => A :MLOAD(isCreateContract)
0 - A :JMPN(opCODESIZEdep)
Expand Down Expand Up @@ -236,7 +236,7 @@ opCODECOPY:

;Check counters
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

GAS - 3 => GAS :JMPN(outOfGas)
GAS - ${3*((E+31)/32)} => GAS :JMPN(outOfGas)
Expand All @@ -258,7 +258,7 @@ opCODECOPY2:
opCODECOPYinit:
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign)

$ => B :MLOAD(remainingBytes)
Expand Down Expand Up @@ -304,7 +304,7 @@ readValueBytecode:
opEXTCODESIZE:

%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
Expand All @@ -330,7 +330,7 @@ opEXTCODECOPY:

%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon)
%MAX_CNT_PADDING_PG - CNT_PADDING_PG - 11 :JMPN(outOfCountersPadding)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 4 :JMPN(stackUnderflow)
SP - 1 => SP
Expand All @@ -347,7 +347,7 @@ opEXTCODECOPY:

;Check counters should be before than do all the operations! TODO
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign)


Expand Down Expand Up @@ -428,7 +428,7 @@ opEXTCODECOPYCheckHashEnd:

opEXTCODECOPYinit:

%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

C - 1 :JMPN(readCode)
C - 32 :JMPN(opEXTCODECOPYfinal)
Expand All @@ -450,7 +450,7 @@ opEXTCODECOPYfinal:
opRETURNDATASIZE:

%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

; Load ret data length from last ctx
; If no retDataCTX(0), retDataLength = 0
Expand Down Expand Up @@ -482,7 +482,7 @@ opRETURNDATACOPY:
%MAX_CNT_ARITH - CNT_ARITH - 32 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 600 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign)

:CALL(saveMem)
Expand Down Expand Up @@ -523,7 +523,7 @@ opRETURNDATACOPY:
GAS - A => GAS :JMPN(outOfGas)

opRETURNDATACOPYinit:
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

C - 1 :JMPN(readCode)
C - 32 :JMPN(opRETURNDATACOPYfinal)
Expand Down Expand Up @@ -562,7 +562,7 @@ opRETURNDATACOPYEmpty:
opEXTCODEHASH:

%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
Expand Down
20 changes: 10 additions & 10 deletions main/opcodes/comparison.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
opLT:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand Down Expand Up @@ -42,7 +42,7 @@ opLT:
opGT:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand Down Expand Up @@ -74,7 +74,7 @@ opGT:
opSLT:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand Down Expand Up @@ -106,7 +106,7 @@ opSLT:
opSGT:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand Down Expand Up @@ -138,7 +138,7 @@ opSGT:
opEQ:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand Down Expand Up @@ -170,7 +170,7 @@ opEQ:
opISZERO:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)
Expand Down Expand Up @@ -201,7 +201,7 @@ opISZERO:
opAND:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand Down Expand Up @@ -233,7 +233,7 @@ opAND:
opOR:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand Down Expand Up @@ -265,7 +265,7 @@ opOR:
opXOR:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)
Expand Down Expand Up @@ -297,7 +297,7 @@ opXOR:
opNOT:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)

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