Skip to content

Commit

Permalink
Merge pull request #235 from 0xPolygonHermez/feature/fix-lt-counters
Browse files Browse the repository at this point in the history
fix zk-counters checks with :LT
  • Loading branch information
krlosMata authored Jan 24, 2023
2 parents b7c0876 + 5c78f75 commit e488704
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion counters/tests/opSHA3.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ operation:
:JMP(opSHA3)
checkCounters:
3300 - STEP:JMPN(failedCounters)
146 - CNT_BINARY :JMPNZ(failedCounters)
145 - CNT_BINARY :JMPNZ(failedCounters)
9 - CNT_ARITH :JMPNZ(failedCounters)
15 - CNT_KECCAK_F :JMPNZ(failedCounters)
63 - CNT_MEM_ALIGN :JMPNZ(failedCounters)
Expand Down
7 changes: 3 additions & 4 deletions main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ start: ; main zkROM entry point
${getGlobalExitRoot()} :MSTORE(globalExitRoot)
${getSequencerAddr()} :MSTORE(sequencerAddr)
${getTimestamp()} :MSTORE(timestamp)
${getTxsLen()} :MSTORE(batchL2DataLength)
${getTxsLen()} :MSTORE(batchL2DataLength) ; less than 300.000 bytes. Enforced by the smart contract

B => SR ;set initial state root

Expand Down Expand Up @@ -81,9 +81,8 @@ skipSetGlobalExitRoot:
136 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => B :MLOAD(arithRes1)
; Compute minimum necessary keccaks to finish the batch
B + 1 + %MIN_CNT_KECCAK_BATCH :MSTORE(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F => A
$ :LT, JMPC(outOfCountersKeccak)
B + 1 + %MIN_CNT_KECCAK_BATCH => B :MSTORE(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - B :JMPN(handleOOCKatRLP)

;;;;;;;;;;;;;;;;;;
;; C - Loop parsing RLP transactions
Expand Down
13 changes: 6 additions & 7 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,12 @@ opEXTCODECOPYLoadBytecode:

; check poseidon counters
; 56 is the value used by the prover to increment poseidon counters depending on the hash length
RR :MSTORE(tmpZkPCext)
B + 1 :MSTORE(arithA)
56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => RR :MLOAD(tmpZkPCext)
$ => B :MLOAD(arithRes1)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 - 1 => A
$ :LT, JMPC(outOfCountersPoseidon)
RR :MSTORE(tmpZkPCext)
B + 1 :MSTORE(arithA)
56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => RR :MLOAD(tmpZkPCext)
$ => B :MLOAD(arithRes1)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 - 1 - B :JMPN(outOfCountersPoseidon)

; set key for smt smart contract code query
E => A
Expand Down
3 changes: 1 addition & 2 deletions main/opcodes/crypto.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ opSHA3:
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
; checks keccak counters
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 => A
$ :LT, JMPC(outOfCountersKeccak)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 - B :JMPN(outOfCountersKeccak)

; new hash id
$ => B :MLOAD(lastHashKIdUsed)
Expand Down
14 changes: 6 additions & 8 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,14 @@ nonceIs0:

;; compute new contract address as CREATE2 spec: keccak256(0xff ++ address ++ salt ++ keccak256(init_code))[12:] (https://eips.ethereum.org/EIPS/eip-1014)
create2:
$ => C :MLOAD(txCalldataLen)
$ => C :MLOAD(txCalldataLen) ; less than 2**32 bytes. Enforced by memory expansion gas cost & smart contract batchL2DataHash
; Check keccak counters
C + 1 :MSTORE(arithA)
136 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
; -2 because we will use one more keccack for generating contract address
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 2 => A
$ :LT, JMPC(outOfCountersKeccak)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 2 - B :JMPN(outOfCountersKeccak)

$ => CTX :MLOAD(originCTX)
$ => B :MLOAD(argsOffsetCall)
Expand Down Expand Up @@ -393,11 +392,10 @@ callContract:

; check poseidon counters
; 56 is the value used by the prover to increment poseidon counters depending on the hash length
B :MSTORE(arithA)
56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => B :MLOAD(arithRes1)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A
$ :LT, JMPC(outOfCountersPoseidon)
B :MSTORE(arithA)
56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => B :MLOAD(arithRes1)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 - B :JMPN(outOfCountersPoseidon)

; get hash contract
$ => A :MLOAD(txDestAddr)
Expand Down
9 changes: 4 additions & 5 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -1190,11 +1190,10 @@ hashPoseidonLinearFromMemory:
C :JMPZ(hashPoseidonReturn)
; check poseidon counters
; 56 is the value used by the prover to increment poseidon counters depending on the hash length
C :MSTORE(arithA)
56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => B :MLOAD(arithRes1)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A
$ :LT, JMPC(outOfCountersPoseidon)
C :MSTORE(arithA)
56 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => B :MLOAD(arithRes1)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 - B :JMPN(outOfCountersPoseidon)

; get a new hashPId
$ => B :MLOAD(nextHashPId)
Expand Down

0 comments on commit e488704

Please sign in to comment.