Skip to content

Commit

Permalink
Merge pull request #217 from 0xPolygonHermez/revert-commit
Browse files Browse the repository at this point in the history
Revert "Merge pull request #186 from 0xPolygonHermez/hotfix/hashk-check"
  • Loading branch information
krlosMata authored Dec 22, 2022
2 parents 59aa750 + 33007ea commit 0180fff
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 80 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
name: Test executor inputs

on:
push:
branches: [main]
pull_request:
branches: [main, develop]

jobs:
build:
runs-on: [self-hosted, linux, X64, hc]
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
uses: actions/checkout@v2
- name: setup zkevm-rom N = 23
run: |
npm i
Expand Down
13 changes: 7 additions & 6 deletions main/ecrecover/ecrecover.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ VAR GLOBAL ecrecover_k2
VAR GLOBAL ecrecover_RR
VAR GLOBAL ecrecover_v_parity

; ecrecover_lastHashIdUsed was a madatory parameter
VAR GLOBAL ecrecover_lastHashIdUsed

INCLUDE "constEc.zkasm"

; ERROR CODES
Expand Down Expand Up @@ -166,14 +169,10 @@ ecrecover_v_y2_same_parity:


; generate keccak of public key to obtain ethereum address
$ => E :MLOAD(lastHashKIdUsed)
E + 1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(ecrecover_lastHashIdUsed)
0 => HASHPOS
32 => D
; check keccak counters
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 :JMPN(outOfCounters)


$ => A :MLOAD(mulPointEc_p3_x)

A :HASHK(E)
Expand All @@ -183,6 +182,8 @@ ecrecover_v_y2_same_parity:
A :HASHK(E)

64 :HASHKLEN(E)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 :JMPN(outOfCounters)
$ => A :HASHKDIGEST(E)

; for address take only last 20 bytes
Expand Down
19 changes: 3 additions & 16 deletions main/load-tx-rlp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,7 @@ shortList:
A - 0xc0 => A

endList:
A + C => B :MSTORE(txRLPLength)
; Check enough zk counters to digest tx hash
B + 1 :MSTORE(arithA)
136 :MSTORE(arithB)
:CALL(divARITH)
$ => B :MLOAD(arithRes1)
$ => D :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - B - D - 1:JMPN(handleOOCatRLP)

A + C :MSTORE(txRLPLength)

;; Read RLP 'nonce'
; 64 bits max
Expand Down Expand Up @@ -319,18 +311,13 @@ vREADTx:
;; increase number of transaction to process
$ => A :MLOAD(pendingTxs)
A + 1 => A :MSTORE(pendingTxs)
;; computte signature
$ => A :HASHKDIGEST(E)
A :MSTORE(txHash)
HASHPOS :MSTORE(sigDataSize) ; save bytes length added to ethereum transaction hash
:JMP(txLoopRLP)

;;;;;;;;;
;; E - Handler error RLP fields
;;;;;;;;;
handleOOCatRLP:
${eventLog(onError, OOC)}
$ => SR :MLOAD(batchSR)


invalidTxRLP:
;; Append all missing 'batchL2Data' to 'batchDataHash' bytes
$ => B :MLOAD(batchL2DataLength)
Expand Down
1 change: 1 addition & 0 deletions main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ start: ; main zkROM entry point
E+1 => E :MSTORE(lastHashKIdUsed)
0 :MSTORE(batchHashPos)
E :MSTORE(batchHashDataId)
E :MSTORE(lastTxHashId) ; Points at first hash address to be used when processing transactions
$ => A :MLOAD(lastCtxUsed)
A :MSTORE(ctxTxToUse) ; Points at first context to be used when processing transactions

Expand Down
31 changes: 12 additions & 19 deletions main/opcodes.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ opSHA3:
%MAX_CNT_ARITH - CNT_ARITH - 192 :JMPN(outOfCounters)
%MAX_CNT_BINARY - CNT_BINARY - 193 :JMPN(outOfCounters)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCounters)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 2 :JMPN(outOfCounters)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 10 :JMPN(outOfCounters)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCounters)

Expand Down Expand Up @@ -674,14 +676,6 @@ opSHA3:
:CALL(mulARITH)
$ => A :MLOAD(arithRes1)
GAS - A => GAS :JMPN(outOfGas) ; dynamic_gas = dynamic_gas = 6 * minimum_word_size + memory_expansion_cost
; 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)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 => A
$ :LT, JMPC(outOfCounters)
; new hash id
$ => B :MLOAD(lastHashKIdUsed)
B + 1 => B :MSTORE(lastHashKIdUsed)
Expand Down Expand Up @@ -714,6 +708,16 @@ opSHA3Final:
opSHA3End:
$ => E :MLOAD(lastHashKIdUsed)
HASHPOS :HASHKLEN(E)

; Check keccak counters
HASHPOS :MSTORE(arithA)
136 :MSTORE(arithB)
:CALL(divARITH)
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A => A
$ :LT, JMPC(outOfCounters)

$ => A :HASHKDIGEST(E)
A :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
Expand Down Expand Up @@ -1173,17 +1177,6 @@ opEXTCODECOPYCheckHash:
0 => A
D => B
$ :EQ, JMPC(opEXTCODECOPYCheckHashEnd)
; check poseidon counters
; 56 is the value used by the prover to increment poseidon counters depending on the hash length
RR :MSTORE(tmpZkPC2)
B + 1 :MSTORE(arithA)
56 :MSTORE(arithB)
:CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => RR :MLOAD(tmpZkPC2)
$ => B :MLOAD(arithRes1)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 1 => A
$ :LT, JMPC(outOfCounters)

E => A
; get hash contract
%SMT_KEY_SC_CODE => B
Expand Down
3 changes: 3 additions & 0 deletions main/precompiled/pre-ecrecover.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ funcECRECOVER:
; read data stored in SP + 1024
; hash [32 bytes], v [32 bytes], r [32 bytes], s [32 bytes]
1024 => SP
$ => A :MLOAD(lastHashKIdUsed)
A + 1 :MSTORE(lastHashKIdUsed)
A + 1 :MSTORE(ecrecover_lastHashIdUsed)
$ => E :MLOAD(SP++) ; hash
$ => A :MLOAD(SP++) ; v
; check v range [27, 28]
Expand Down
54 changes: 30 additions & 24 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,27 @@ processTx:
${eventLog(onProcessTx)}
; Minimum of 100000 steps left to process a tx
%MAX_CNT_STEPS - STEP - 100000 :JMPN(outOfCounters)
; check from address
$ => A :MLOAD(txHash)
; Get sigDataSize
$ => HASHPOS :MLOAD(sigDataSize)

; Check keccak counters
HASHPOS :MSTORE(arithA)
136 :MSTORE(arithB)
:CALL(divARITH)
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A => A
$ :LT, JMPC(outOfCounters)

; Get hash address previously stored in RLP parsing
$ => E :MLOAD(lastTxHashId)
E+1 => E :MSTORE(lastTxHashId)

; Check the signature
$ => A :MLOAD(lastHashKIdUsed)
A + 1 :MSTORE(lastHashKIdUsed)
A + 1 :MSTORE(ecrecover_lastHashIdUsed)
$ => A :HASHKDIGEST(E)
$ => B :MLOAD(txR)
$ => C :MLOAD(txS)
$ => D :MLOAD(txV)
Expand Down Expand Up @@ -211,9 +229,6 @@ getContractAddress:
; Check if create is with CREATE2 opcode
$ => A :MLOAD(isCreate2)
0 - A :JMPN(create2)
; Check keccak counters
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1:JMPN(outOfCounters)
$ => A :MLOAD(txNonce)
0x80 => B
$ :LT,JMPC(nonce1byte)
Expand Down Expand Up @@ -250,6 +265,8 @@ nonceIs0:
endContractAddress:
; end contract address hash and get the 20 first bytes
HASHPOS :HASHKLEN(E)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 :JMPN(outOfCounters)
$ => A :HASHKDIGEST(E)
:CALL(maskAddress) ; Mask address to 20 bytes
A :MSTORE(createContractAddress)
Expand All @@ -261,16 +278,6 @@ endContractAddress:
;; 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)
; Check keccak counters
C + 1 :MSTORE(arithA)
136 :MSTORE(arithB)
:CALL(divARITH)
$ => 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(outOfCounters)

$ => CTX :MLOAD(originCTX)
$ => B :MLOAD(argsOffsetCall)

Expand Down Expand Up @@ -301,6 +308,14 @@ endloopCreate2:
create2end:
$ => CTX :MLOAD(currentCTX)
HASHPOS :HASHKLEN(E)
; Check keccak counters
HASHPOS :MSTORE(arithA)
136 :MSTORE(arithB)
:CALL(divARITH)
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A => A
$ :LT, JMPC(outOfCounters)
$ => C :HASHKDIGEST(E)
; new hash with position 0 is started
0 => HASHPOS
Expand Down Expand Up @@ -434,15 +449,6 @@ callContract:
0 => A
$ :EQ, JMPC(defaultOpCode) ;no bytecode

; 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(outOfCounters)

$ => A :MLOAD(txDestAddr)
; get hash contract
%SMT_KEY_SC_CODE => B
Expand Down
9 changes: 0 additions & 9 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -903,15 +903,6 @@ hashPoseidonLinearFromMemory:
$ => C :MLOAD(memSizeLinearPoseidon)
0 => D
C - 1 :JMPN(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(outOfCounters)

; get a new hashPId
$ => B :MLOAD(nextHashPId)
B :MSTORE(tmpContractHashId)
Expand Down
3 changes: 2 additions & 1 deletion main/vars.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ VAR GLOBAL batchL2DataParsed ; Number of bytes read when decoding RLP transactio
VAR GLOBAL pendingTxs ; Number of transactions decoded in RLP block
VAR GLOBAL lastCtxUsed ; Last context that has been used
VAR GLOBAL ctxTxToUse ; First context to be used when processing transactions
VAR GLOBAL lastTxHashId ; First hash address to be used when processing transactions
VAR GLOBAL lastHashKIdUsed ; Last hash address used
VAR GLOBAL nextHashPId ; Next posidon hash address available

Expand Down Expand Up @@ -50,7 +51,6 @@ VAR CTX txS ; transaction parameter: ecdsa signature S
VAR CTX txR ; transaction parameter: ecdsa signature R
VAR CTX txV ; transaction parameter: ecdsa signature V
VAR CTX txSrcAddr ; address that sends a transaction 'message.sender'
VAR CTX txHash ; signed tx hash
VAR CTX txCalldataLen ; calldata length
VAR CTX isCreateContract ; flag to determine if a transaction will create a new contract
VAR CTX createContractAddress ; address computed of a new contract
Expand Down Expand Up @@ -81,5 +81,6 @@ VAR CTX isDelegateCall ; flag to determine if a new context comes from a DELEGAT
VAR CTX isCreate2 ; flag to determine if a new context comes from a CREATE2 opcode
VAR CTX salt ; CREATE2 parameter 'salt' used to compute new contract address
VAR CTX gasCTX ; remaining gas in the origin CTX when a new context is created
VAR CTX sigDataSize ; hash position for the ethereum transaction hash
VAR CTX dataStarts; hash position where de transaction 'data' starts in the batchHashData
VAR CTX SPw ; aux variable to store Stack poimnter 'SP'

0 comments on commit 0180fff

Please sign in to comment.