Skip to content

Commit

Permalink
Merge pull request #197 from 0xPolygonHermez/feature/last-fixes
Browse files Browse the repository at this point in the history
Fixes solved
  • Loading branch information
krlosMata authored Dec 13, 2022
2 parents 5fc5e82 + 100fcdd commit 415e73a
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 67 deletions.
15 changes: 8 additions & 7 deletions main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ CONST %COLD_SLOAD_COST_RED = 2000 ; Cost of using a cols memory slot reduced
CONST %SSTORE_DYNAMIC_GAS = 100
; COUNTERS
CONST %MIN_STEPS_FINISH_BATCH = 200 ; min steps to finish tx
CONST %MAX_CNT_STEPS = 2**23 - %MIN_STEPS_FINISH_BATCH
CONST %TOTAL_STEPS = 2**23
CONST %MAX_CNT_STEPS = %TOTAL_STEPS - %MIN_STEPS_FINISH_BATCH

CONST %MAX_CNT_ARITH = %MAX_CNT_STEPS / 32
CONST %MAX_CNT_BINARY = %MAX_CNT_STEPS / 16
CONST %MAX_CNT_MEM_ALIGN = %MAX_CNT_STEPS / 32
CONST %MAX_CNT_KECCAK_F = (%MAX_CNT_STEPS / 155286) * 44
CONST %MAX_CNT_PADDING_PG = (%MAX_CNT_STEPS / 56)
CONST %MAX_CNT_POSEIDON_G = (%MAX_CNT_STEPS / 30)
CONST %MAX_CNT_ARITH = %TOTAL_STEPS / 32
CONST %MAX_CNT_BINARY = %TOTAL_STEPS / 16
CONST %MAX_CNT_MEM_ALIGN = %TOTAL_STEPS / 32
CONST %MAX_CNT_KECCAK_F = (%TOTAL_STEPS / 155286) * 44
CONST %MAX_CNT_PADDING_PG = (%TOTAL_STEPS / 56)
CONST %MAX_CNT_POSEIDON_G = (%TOTAL_STEPS / 30)
CONST %MIN_CNT_KECCAK_BATCH = 1 ; minimum necessary keccaks to compute global hash

; ETHEREUM CONSTANTS
Expand Down
5 changes: 3 additions & 2 deletions main/ecrecover/ecrecover.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ INCLUDE "constEc.zkasm"
; 100 - fail sqrt, but has solution (!!!)

ecrecover:

%MAX_CNT_BINARY - CNT_BINARY - 10 :JMPN(outOfCountersBinary)

%MAX_CNT_BINARY - CNT_BINARY - 800 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 500 :JMPN(outOfCountersArith)

A :MSTORE(ecrecover_hash)
B :MSTORE(ecrecover_r)
Expand Down
2 changes: 1 addition & 1 deletion main/load-tx-rlp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ chainREAD:

chainId0:
0 => A
:JMPN(endChainId)
:JMP(endChainId)

shortChainId:
A - 0x80 => D
Expand Down
10 changes: 5 additions & 5 deletions main/opcodes/arithmetic.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ opMUL:

; check stack underflow
SP - 2 :JMPN(stackUnderflow)

; check out-of-gas
GAS - %GAS_FAST_STEP => GAS :JMPN(outOfGas)

Expand Down Expand Up @@ -282,14 +282,14 @@ opADDMOD:

AddModJumpCarry:
; Store sumResult on E
A => E
A => E
$ => A :MLOAD(SP); [N => A]
2 => B
$ :LT, JMPC(zeroOneAddMod)
; k: Max should be (2^256 -1) * 2 / 2 --> smaller than 2^256
${_addMod / A} => B
; addModResult
${_addMod % A} => C
; addModResult
${_addMod % A} => C
; k * N + addModResult = D*2^256 + sumResult
; B * A + C = D*2^256 + E
; Check addModResult is less than modulus
Expand Down Expand Up @@ -393,7 +393,7 @@ opSIGNEXTEND: ; following this impl https://github.com/ethereumjs/ethereumjs-mon
$ => B :AND ; check sign bit
0 => A
; If 0 means the sign bit was 0 --> positive
$ :EQ, JMPC(opSIGNEXTENDPositive)
$ :EQ, JMPC(opSIGNEXTENDPositive)
C => A ; mask
%MAX_UINT_256 => B
$ => B :XOR ; not mask
Expand Down
40 changes: 29 additions & 11 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,26 @@ opCODECOPY:
;Check counters
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)


; check out-of-gas
GAS - %GAS_FASTEST_STEP => GAS :JMPN(outOfGas)
GAS - ${3*((E+31)/32)} => GAS :JMPN(outOfGas)
;${3*((E+31)/32)}
E+31 => A
;(E+31)/32
A :MSTORE(arithA)
32 :MSTORE(arithB)
:CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => A :MLOAD(arithRes1)
; Mul operation with Arith
; 3*((E+31)/32)
3 :MSTORE(arithA)
A :MSTORE(arithB)
:CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB]
$ => A :MLOAD(arithRes1)

GAS - A => GAS :JMPN(outOfGas)

; compute memory expansion gas cost
:CALL(saveMem); in: [lastMemOffset, lastMemLength]
; if offset is above data len, length => offset
Expand All @@ -343,16 +360,17 @@ opCODECOPY2:
; set hashId to get bytes from
D :MSTORE(codecopyHashId)
; set contract bytecode length
B :MSTORE(codecopyBytecodeLength)
B :MSTORE(codecopyBytecodeLength)
C :MSTORE(memOffset)
E :MSTORE(remainingBytes)
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 2*E :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 100*E :JMPN(outOfCountersStep)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - E :JMPN(outOfCountersMemalign)

opCODECOPYinit:
; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)

%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign)

$ => B :MLOAD(remainingBytes)
B - 1 :JMPN(readCode)
Expand Down Expand Up @@ -529,10 +547,10 @@ opEXTCODECOPYCheckHash:
opEXTCODECOPYCheckHashLoop:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
; finish reading bytecode
B - 1 :JMPN(opEXTCODECOPYCheckHashLoopEnd)
B - 1 :JMPN(opEXTCODECOPYCheckHashLoopEnd)
1 => D
; add bytes to hash contract bytecode
${getBytecode(A, HASHPOS, 1)} :HASHP(E)
${getBytecode(A, HASHPOS, 1)} :HASHP(E)
B - 1 => B
:JMP(opEXTCODECOPYCheckHashLoop)

Expand Down Expand Up @@ -642,7 +660,7 @@ opRETURNDATACOPY:
$ => E :MLOAD(retDataOffset)
$ => CTX :MLOAD(currentCTX)
; E ret data offset (memory pointer) of last context, B offset in return data that want to retrive
E + B => E
E + B => E
; if retDataSize <= retdataOffset + dataLen -> OOG
;A retDataSize
B + C => B ; total offset (retdataOffset + dataLen)
Expand Down Expand Up @@ -672,7 +690,7 @@ opRETURNDATACOPYinit:
; Load memory from last context used
$ => CTX :MLOAD(retDataCTX)
:CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
; save new offset at B
; save new offset at B
; Save memory to the current context
$ => CTX :MLOAD(currentCTX)
E => B
Expand Down Expand Up @@ -700,7 +718,7 @@ opRETURNDATACOPYfinal:

opRETURNDATACOPYEmpty:
; offset return data + len return data must be 0
E + C => B
E + C => B
$ :EQ, JMPC(readCode)
:JMP(outOfGas)

Expand Down
2 changes: 1 addition & 1 deletion main/opcodes/comparison.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ opNOT:
; read one item from the stack
$ => A :MLOAD(SP)

0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn => B ; 2**226 - 1 =>
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn => B ; 2**226 - 1 =>
$ => A :XOR,MSTORE(SP++) ; [ NOT a => SP]
; check stack overflow
%CALLDATA_OFFSET - SP :JMPN(stackOverflow)
Expand Down
14 changes: 7 additions & 7 deletions main/opcodes/context-information.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ opADDRESSdeploy:
opADDRESSend:
A :MSTORE(SP++); [address(A) => SP]
; check stack overflow
%CALLDATA_OFFSET - SP :JMPN(stackOverflow)
%CALLDATA_OFFSET - SP :JMPN(stackOverflow)
:JMP(readCode)

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ opSELFBALANCE:
* @process-opcode
* - stack input: [address]
* - stack output: [balance]
*/
*/
opBALANCE:
; checks zk-counters
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 252 :JMPN(outOfCountersPoseidon)
Expand Down Expand Up @@ -91,7 +91,7 @@ opBALANCE:
* @process-opcode
* - stack input: []
* - stack output: [address]
*/
*/
opORIGIN:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand All @@ -111,7 +111,7 @@ opORIGIN:
* @process-opcode
* - stack input: []
* - stack output: [address]
*/
*/
opCALLER:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand All @@ -131,7 +131,7 @@ opCALLER:
* @process-opcode
* - stack input: []
* - stack output: [value]
*/
*/
opCALLVALUE:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand All @@ -151,7 +151,7 @@ opCALLVALUE:
* @process-opcode
* - stack input: []
* - stack output: [price]
*/
*/
opGASPRICE:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand All @@ -171,7 +171,7 @@ opGASPRICE:
* @process-opcode
* - stack input: []
* - stack output: [gas]
*/
*/
opGAS:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand Down
14 changes: 7 additions & 7 deletions main/opcodes/create-terminate-context.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ opSTOP:
$ => A :MLOAD(gasRefund)
; check if is a create call
$ => C :MLOAD(isCreate)
; set 1 to D, default stack return in stop code
; set 1 to D, default stack return in stop code
1 => D
C - 1 :JMPN(opSTOPend)
; if is a create, return created contract address
Expand Down Expand Up @@ -63,11 +63,11 @@ opCREATE:
%MAX_CNT_BINARY - CNT_BINARY - 32 :JMPN(outOfCountersBinary)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 510 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)

; check out-of-gas
GAS - %CREATE_GAS => GAS :JMPN(outOfGas)
GAS :MSTORE(gasCall)

; increase depth
$ => A :MLOAD(depth)
A + 1 :MSTORE(depth)
Expand Down Expand Up @@ -182,7 +182,7 @@ opCreateFail:
* - stack output: [success]
*/
opCALL:

; checks zk-counters
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
; increase depth
Expand Down Expand Up @@ -395,7 +395,7 @@ opCALLCODEend:
SP :MSTORE(lastSP)
PC :MSTORE(lastPC)
; increase CTX
$ => A :MLOAD(lastCtxUsed)
$ => A :MLOAD(lastCtxUsed)
A+1 => CTX :MSTORE(lastCtxUsed)
CTX :MSTORE(currentCTX)
$ => A :MLOAD(originAuxCTX)
Expand Down Expand Up @@ -548,7 +548,7 @@ opRETURNdeploy:
; code size limit = 0x6000
0x6000 - C :JMPN(invalidCodeSize)
;code_deposit_cost = 200 * returned_code_size
GAS - %RETURN_GAS_COST * C => GAS :JMPN(outOfGas)
GAS - %RETURN_GAS_COST * C => GAS :JMPN(outOfGas)
; check if first context
$ => B :MLOAD(originCTX)
0 - B :JMPN(opRETURNcreate)
Expand Down Expand Up @@ -742,7 +742,7 @@ opCREATE2:
%MAX_CNT_STEPS - STEP - 400 :JMPN(outOfCountersStep)
; check out-of-gas
GAS - %CREATE_2_GAS => GAS :JMPN(outOfGas)

; increase depth
$ => A :MLOAD(depth)
A + 1 :MSTORE(depth)
Expand Down
10 changes: 5 additions & 5 deletions main/opcodes/crypto.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ opSHA3:

; check stack underflow
SP - 2 :JMPN(stackUnderflow)

; check out-of-gas
GAS - %KECCAK_GAS => GAS :JMPN(outOfGas)
SP - 1 => SP
Expand All @@ -34,7 +34,7 @@ opSHA3:
C :MSTORE(lastMemLength)
; compute memory expansion gas cost
:CALL(saveMem); in: [lastMemOffset, lastMemLength]
; check out-of-gas, dynamic
; check out-of-gas, dynamic
;${6*((C+31)/32)}
C+31 => A
;(C+31)/32
Expand Down Expand Up @@ -77,7 +77,7 @@ opSHA3Loop:
C - 32 :JMPN(opSHA3Final)
; load next 32 bytes from memory
:CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
; save new offset at B
; save new offset at B
E => B
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
Expand All @@ -95,7 +95,7 @@ opSHA3Final:
:CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
; set remaining bytes length to hash at D
; set remaining bytes length to hash at D
C => D
; append A to hash pointer E
A :HASHK(E)
Expand All @@ -104,7 +104,7 @@ opSHA3End:
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
; append A to hash pointer E
HASHPOS :HASHKLEN(E)
HASHPOS :HASHKLEN(E)
; compute hash
$ => A :HASHKDIGEST(E)
; store hash
Expand Down
2 changes: 1 addition & 1 deletion main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ opJUMPDEST:

; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

; check out-of-gas
GAS - %JUMP_DEST_GAS => GAS :JMPN(outOfGas)
:JMP(readCode)
Loading

0 comments on commit 415e73a

Please sign in to comment.