Skip to content

Commit

Permalink
Deduct min steps to finish tx from total steps
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Nov 28, 2022
1 parent c965a39 commit eaa0cac
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 128 deletions.
2 changes: 1 addition & 1 deletion main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CONST %SSTORE_RESET_GAS = 2900
CONST %SSTORE_CLEARS_SCHEDULE = 15000

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

CONST %MAX_CNT_ARITH = %MAX_CNT_STEPS / 32
CONST %MAX_CNT_BINARY = %MAX_CNT_STEPS / 32
Expand Down
20 changes: 10 additions & 10 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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

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

%MAX_CNT_ARITH - CNT_ARITH - 1 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - MIN_STEPS_FINISH_TX - STEP - 80 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 80 :JMPN(outOfCountersStep)

GAS-8 => GAS :JMPN(outOfGas)

Expand Down Expand Up @@ -255,7 +255,7 @@ opMULMOD:
opEXP:

%MAX_CNT_BINARY - CNT_BINARY - 5 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

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

opCOINBASE:

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

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

opTIMESTAMP:

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

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

opNUMBER:

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

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

opDIFFICULTY:

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

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

opGASLIMIT:

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

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

opCHAINID:

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

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

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

0 :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
Expand Down
32 changes: 16 additions & 16 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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 200 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :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 - MIN_STEPS_FINISH_TX - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

GAS - 3 => GAS :JMPN(outOfGas)
GAS - ${3*((E+31)/32)} => GAS :JMPN(outOfGas)
Expand All @@ -262,7 +262,7 @@ opCODECOPY2:
opCODECOPYinit:
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - MIN_STEPS_FINISH_TX - STEP - 100 :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 @@ -308,7 +308,7 @@ readValueBytecode:
opEXTCODESIZE:

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

SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
Expand All @@ -334,7 +334,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 - MIN_STEPS_FINISH_TX - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

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

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


Expand Down Expand Up @@ -408,7 +408,7 @@ opEXTCODECOPYCheckHash:
; TODO: it could be improved by computing how many 32 bytes slots are needed
opEXTCODECOPYCheckHashLoop:

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

B - 1 :JMPN(opEXTCODECOPYCheckHashLoopEnd) ; finish reading bytecode
1 => D
Expand All @@ -426,7 +426,7 @@ opEXTCODECOPYCheckHashEnd:

opEXTCODECOPYinit:

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

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

%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - MIN_STEPS_FINISH_TX - STEP - 100 :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 @@ -480,7 +480,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 - MIN_STEPS_FINISH_TX - STEP - 400 :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 @@ -521,7 +521,7 @@ opRETURNDATACOPY:
GAS - A => GAS :JMPN(outOfGas)

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

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

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

SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
Expand Down
16 changes: 8 additions & 8 deletions main/opcodes/context-information.zkasm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
opADDRESS:
%MAX_CNT_STEPS - MIN_STEPS_FINISH_TX - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

$ => A :MLOAD(isCreateContract)
0 - A :JMPN(opADDRESSdeploy)
Expand All @@ -18,7 +18,7 @@ opADDRESSend:
opSELFBALANCE:

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

$ => A :MLOAD(storageAddr)
0 => B,C ; balance key smt
Expand All @@ -30,7 +30,7 @@ opSELFBALANCE:
opBALANCE:

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

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

opORIGIN:

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

$ => A :MLOAD(txSrcOriginAddr)
A :MSTORE(SP++)
Expand All @@ -55,7 +55,7 @@ opORIGIN:

opCALLER:

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

$ => A :MLOAD(txSrcAddr)
A :MSTORE(SP++)
Expand All @@ -65,7 +65,7 @@ opCALLER:

opCALLVALUE:

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

$ => A :MLOAD(txValue)
A :MSTORE(SP++)
Expand All @@ -75,7 +75,7 @@ opCALLVALUE:

opGASPRICE:

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

$ => A :MLOAD(txGasPrice)
A :MSTORE(SP++)
Expand All @@ -84,7 +84,7 @@ opGASPRICE:
:JMP(readCode)
opGAS:

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

GAS - 2 => GAS :JMPN(outOfGas)
GAS :MSTORE(SP++)
Expand Down
Loading

0 comments on commit eaa0cac

Please sign in to comment.