Skip to content

Commit

Permalink
Refactor hashkUsed
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Mar 8, 2024
1 parent d2fba54 commit 4ff9967
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 54 deletions.
4 changes: 2 additions & 2 deletions main/ecrecover/ecrecover.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ k1_calculated:

; [steps: 6280, bin: 522, arith: 527]
; generate keccak of public key to obtain ethereum address
$ => E :MLOAD(lastHashKIdUsed)
E + 1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)
0 => HASHPOS
32 => D

Expand Down
2 changes: 1 addition & 1 deletion main/load-change-l2-block-utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ getChangeL2TxBytes:
$ => A :HASHP(E)
HASHPOS :MSTORE(batchHashPos)
C => HASHPOS
$ => E :MLOAD(lastHashKIdUsed), RETURN
$ => E :MLOAD(txHashPointer), RETURN
12 changes: 7 additions & 5 deletions main/load-tx-rlp-utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ getBatchL2DataBytes:
$ => A :HASHP(E)
HASHPOS :MSTORE(batchHashPos)
C => HASHPOS
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(txHashPointer)
:RETURN

;; Add bytes to generate ethereum signed message
Expand All @@ -28,7 +28,7 @@ addHashTxBegin:
$ => HASHPOS :MLOAD(batchHashPos)
$ => A :HASHP(E)
HASHPOS :MSTORE(batchHashPos)
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(txHashPointer)
C => HASHPOS
A :HASHK(E)
C + D => C :RETURN
Expand All @@ -46,7 +46,7 @@ addHashTxFromFreeInput:
B + C => A
; Read data from free input
${getTxs(A,D)} => A, B
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(txHashPointer)
C => HASHPOS
; Insert data into txhash (hashk)
A :HASHK(E)
Expand All @@ -57,11 +57,13 @@ addHashTxFromFreeInput:
$ => HASHPOS :MLOAD(batchHashPos)
1 => C ; Current pointer
D => RCX ; total bytes
; Set default remainder to 0, in case SHRarith is not using arithDiv because is shifting zero bytes
0 :MSTORE(arithRes2)
; Validate free input
validateFreeInputLoop:
RCX - C :JMPN(validateFreeInputLoopEnd)
RCX - C :JMPN(validateFreeInputLoopEnd)
; Get free input byte by byte
RCX - C => D :CALL(SHRarith) ; in: [A: value, D: #bytes to right shift] out: [A: shifted result]
RCX - C => D :CALL(SHRarith) ; in: [A: value, D: #bytes to right shift] out: [A: shifted result]
$ :HASHP1(E), ASSERT
; The remainder of divArith used at SHRarith is the following bytes I want to check
$ => A :MLOAD(arithRes2)
Expand Down
9 changes: 5 additions & 4 deletions main/load-tx-rlp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ loadTx_rlp_continue:
$ => E :MLOAD(nextHashPId)
E :MSTORE(l2TxHashPointer)
E + 1 :MSTORE(nextHashPId)
$ => E :MLOAD(lastHashKIdUsed)
E + 1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E :MSTORE(txHashPointer)
E + 1 :MSTORE(nextHashKId)
;;;;;;;;;;;;;;;;;;
;; B - Read and check RLP fields. Fill 'batchHashData' and Ethereum signed transaction bytes
;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -218,7 +219,7 @@ veryShortData:
$ => E :MLOAD(globalCalldataMemoryOffset)
A :MSTORE(MEM:E)
E + 1 :MSTORE(globalCalldataMemoryOffset)
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(txHashPointer)
; Restore current CTX
B => CTX :JMP(endData)

Expand Down Expand Up @@ -305,7 +306,7 @@ endChainId:

setPreEIP155Flag:
1 :MSTORE(isPreEIP155)
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(txHashPointer)
;; size verification
; checks RLP length read at the RLP header with bytes read during RLP parsing
sizeVerification:
Expand Down
18 changes: 10 additions & 8 deletions main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,37 @@ start: ; main zkROM entry point
;; B - Compute newAccInputHash, load newLocalExitRoot and timestamp
;;;;;;;;;;;;;;;;;;
computeNewAccInputHash:
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)
$${eventLog(onStartBatch, C)}
0 => HASHPOS

32 => D
$ => A :MLOAD(oldAccInputHash)
A :HASHK(0)
A :HASHK(E)

$ => A :MLOAD(batchHashData)
A :HASHK(0)
A :HASHK(E)

$ => A :MLOAD(l1InfoRoot)
A :HASHK(0)
A :HASHK(E)

8 => D
$ => A :MLOAD(timestampLimit)
A :HASHK(0)
A :HASHK(E)

20 => D
$ => A :MLOAD(sequencerAddr)
A :HASHK(0)
A :HASHK(E)

32 => D
$ => A :MLOAD(forcedBlockHashL1)
A :HASHK(0)
A :HASHK(E)

; finish accInputHash
HASHPOS :HASHKLEN(0)
HASHPOS :HASHKLEN(E)

$ => C :HASHKDIGEST(0)
$ => C :HASHKDIGEST(E)
C :MSTORE(newAccInputHash)
; Compute batchHashData (loaded at smt from executor)
$ => E :MLOAD(nextHashPId)
Expand Down
4 changes: 2 additions & 2 deletions main/opcodes/block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ opBLOCKHASH:
32 => D
; A new hash with position 0 is started
0 => HASHPOS
$ => E :MLOAD(lastHashKIdUsed)
E+1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)
B :HASHK(E)
%STATE_ROOT_STORAGE_POS :HASHK(E)
HASHPOS :HASHKLEN(E)
Expand Down
11 changes: 6 additions & 5 deletions main/opcodes/crypto.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ opSHA3:
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - 1 - B :JMPN(outOfCountersKeccak)

; new hash id
$ => B :MLOAD(lastHashKIdUsed)
B + 1 => B :MSTORE(lastHashKIdUsed)
$ => B :MLOAD(nextHashKId)
B :MSTORE(sha3Pointer)
B + 1 :MSTORE(nextHashKId)
; set bytes to hash at D
32 => D
; A new hash with position 0 is started
Expand All @@ -65,7 +66,7 @@ opSHA3Loop:
; save new offset at B
E => B
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(sha3Pointer)
; append A to hash pointer E
A :HASHK(E)
; restore new offset at E
Expand All @@ -78,15 +79,15 @@ opSHA3Final:
; set #bytes to right shift
32 - C => D :CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(sha3Pointer)
; set remaining bytes length to hash at D
C => D
; append A to hash pointer E
A :HASHK(E)

opSHA3End:
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(sha3Pointer)
; append A to hash pointer E
HASHPOS :HASHKLEN(E)
; compute hash
Expand Down
8 changes: 4 additions & 4 deletions main/process-change-l2-block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ continueProcessChangeL2Block:
; Update state root mapping
32 => D
0 => HASHPOS ; A new hash with position 0 is started
$ => E :MLOAD(lastHashKIdUsed)
E + 1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)

%ADDRESS_SYSTEM => A
%SMT_KEY_SC_STORAGE => B
Expand Down Expand Up @@ -114,8 +114,8 @@ setGERL1InfoTree:
; Do not set if zero
$ :EQ, JMPC(skipSetGERL1InfoTree)
0 => HASHPOS
$ => E :MLOAD(lastHashKIdUsed)
E + 1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)

32 => D
A :HASHK(E)
Expand Down
17 changes: 8 additions & 9 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ addDeploymentGasCost:
%BASE_TX_DEPLOY_GAS => B ; 53000 gas if transaction is a create

getCalldataGasCost:
$ => A :MLOAD(txCalldataLen)
0 => B
$ :EQ,JMPC(endCalldataIntrinsicGas)
$ => A :MLOAD(txCalldataLen), JMPZ(endCalldataIntrinsicGas) ; less than 32 bits. Enforced by memory expansion gas cost & smart contract batchL2DataHash

addGas:
$ => HASHPOS :MLOAD(dataStarts)
Expand Down Expand Up @@ -225,8 +223,9 @@ getContractAddress:
; A new hash with position 0 is started
0 => HASHPOS
; We get a new hashId
$ => E :MLOAD(lastHashKIdUsed)
E+1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E :MSTORE(createAddressPointer)
E + 1 :MSTORE(nextHashKId)
; Check if create is with CREATE2 opcode
$ => A :MLOAD(isCreate2), JMPNZ(create2)
; Check keccak counters
Expand Down Expand Up @@ -285,15 +284,15 @@ loopCreate2:
B => E :CALL(MLOAD32)
E => B
32 => D
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(createAddressPointer)
A :HASHK(E)
C - 32 => C :JMP(loopCreate2)

endloopCreate2:
B => E :CALL(MLOADX)
32 - C => D :CALL(SHRarith)
C => D
$ => E :MLOAD(lastHashKIdUsed)
$ => E :MLOAD(createAddressPointer)
A :HASHK(E)

create2end:
Expand All @@ -303,8 +302,8 @@ create2end:

; new hash with position 0 is started
0 => HASHPOS
$ => E :MLOAD(lastHashKIdUsed)
E+1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)
0xff :HASHK1(E)
20 => D
$ => A :MLOAD(txSrcAddr)
Expand Down
10 changes: 5 additions & 5 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -1633,8 +1633,8 @@ verifyMerkleProof:
;;;;;;;;;;;;;;;;;;;;;

; new keccak address
$ => E :MLOAD(lastHashKIdUsed)
E + 1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)

; add gerL1InfoTree (32 bytes)
0 => HASHPOS
Expand All @@ -1658,8 +1658,8 @@ verifyMerkleProof:
$ => B :MLOAD(indexL1InfoTree)

; prepare new hash
$ => E :MLOAD(lastHashKIdUsed)
E + 1 => E :MSTORE(lastHashKIdUsed)
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)
32 => D

; - Start loop 32 levels:
Expand Down Expand Up @@ -2213,7 +2213,7 @@ hashBranchEnd31:

; verify linear combination of 'indexL1InfoTree'
B :ASSERT
E + 31 => E :MSTORE(lastHashKIdUsed)
E + 32 => E :MSTORE(nextHashKId)


verifyMerkleProofEnd:
Expand Down
6 changes: 4 additions & 2 deletions main/vars.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ 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 lastHashKIdUsed ; Last hash address used
VAR GLOBAL nextHashKId ; Last hash address used
VAR GLOBAL nextHashPId ; Next poseidon hash address available

VAR GLOBAL createAddressPointer ; E pointer for create contract address computation
VAR GLOBAL sha3Pointer ; E pointer for sha3 computation
VAR GLOBAL txHashPointer ; E pointer for tx hash computation
VAR GLOBAL batchL2DataLength ; Transactions bytes read from the input
VAR GLOBAL batchHashDataPointer ; hash address used when adding bytes to batchHashData
VAR GLOBAL batchHashPos ; hash batchHashData position
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
"url": "https://github.com/0xPolygonHermez/zkevm-rom.git"
},
"dependencies": {
"@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#v5.0.0-fork.8",
"@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#develop-feijoa",
"yargs": "^17.5.1"
},
"devDependencies": {
"@0xpolygonhermez/zkevm-commonjs": "github:0xPolygonHermez/zkevm-commonjs#develop-feijoa",
"@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/batch-hash-data",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/batch-hash-data",
"@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#feature/feijoa-batch",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/feijoa-batch",
"chai": "^4.3.6",
"chalk": "^3.0.0",
"eslint": "^8.25.0",
Expand Down
9 changes: 5 additions & 4 deletions test/ecrecover.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
;; ecrecover_tx
;;;;;;;;;;;;
INCLUDE "../main/constants.zkasm"
VAR GLOBAL lastHashKIdUsed
VAR GLOBAL nextHashKId
VAR GLOBAL originalCTX
start:

STEP => A
0 :ASSERT

CTX :MSTORE(originalCTX)
-1 :MSTORE(lastHashKIdUsed)
; :JMP(repeat_ecrecover_test)
:JMP(edge_cases)
:JMP(repeat_ecrecover_test)
; :JMP(edge_cases)
; :JMP(worst_case)
; :JMP(point_arith_tests)

INCLUDE "../main/ecrecover/ecrecover.zkasm"

Expand Down

0 comments on commit 4ff9967

Please sign in to comment.