Skip to content

Commit

Permalink
Improve hashp and hashk usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Apr 3, 2024
1 parent e15d445 commit cd9c6d4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 37 deletions.
3 changes: 1 addition & 2 deletions main/l2-tx-hash.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ addL2HashTx_dataLength:
$ => E :MLOAD(l2TxHashPointer)
$ => HASHPOS :MLOAD(l2HASHP)
3 => D
$ => A :MLOAD(txCalldataLen)
A :HASHP(E)
${mem.txCalldataLen} :HASHP(E), MLOAD(txCalldataLen)
HASHPOS :MSTORE(l2HASHP)

; load temporary register values
Expand Down
9 changes: 3 additions & 6 deletions main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ start: ; main zkROM entry point
$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)
32 => D
${getForcedGER()} => A :MSTORE(forcedGER)
A :HASHK(E)
${getForcedBlockHashL1()} => A :MSTORE(forcedBlockHashL1InfoTree)
A :HASHK(E)
${getForcedGER()} => A :MSTORE(forcedGER), HASHK(E)
${getForcedBlockHashL1()} => A :MSTORE(forcedBlockHashL1InfoTree), HASHK(E)
8 => D
${getForcedTimestamp()} => A :MSTORE(forcedTimestamp)
A :HASHK(E)
${getForcedTimestamp()} => A :MSTORE(forcedTimestamp), HASHK(E)
HASHPOS :HASHKLEN(E)
; Assert forcedHashData computed matches with forcedHashData obtained from free input
$ => A :HASHKDIGEST(E)
Expand Down
28 changes: 10 additions & 18 deletions main/process-change-l2-block-utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,27 @@ computeL1InfoTreeValue:
E + 1 :MSTORE(nextHashKId)
0 => HASHPOS
32 => D
$ => A :MLOAD(gerL1InfoTree)
A :HASHK(E)
$ => A :MLOAD(blockHashL1InfoTree)
A :HASHK(E)
${mem.gerL1InfoTree} :HASHK(E), MLOAD(gerL1InfoTree)
${mem.blockHashL1InfoTree} :HASHK(E), MLOAD(blockHashL1InfoTree)
8 => D
$ => A :MLOAD(timestampL1InfoTree)
A :HASHK(E)
${mem.timestampL1InfoTree} :HASHK(E), MLOAD(timestampL1InfoTree)
HASHPOS :HASHKLEN(E)
; Info tree data at C
$ => C :HASHKDIGEST(E), MSTORE(computedL1InfoTreeData), RETURN
$ :HASHKDIGEST(E), MSTORE(computedL1InfoTreeData), RETURN

; compute "new" currentL1InfoRoot from historicRoot, currentL1InfoTreeRoot = keccak(HistoricRoot, blockHashL1InfoTree)
; compute "new" currentL1InfoRoot from historicRoot, currentL1InfoTreeRoot = keccak(HistoricRoot, computedL1InfoTreeData)
computeNewCurrentL1InfoRootFromHistoric:
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - 1 :JMPN(outOfCountersKeccak)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - 1 :JMPN(outOfCountersKeccak)

$ => E :MLOAD(nextHashKId)
E + 1 :MSTORE(nextHashKId)
0 => HASHPOS
32 => D
${getL1HistoricRoot(mem.indexL1InfoTree)} :HASHK(E) ; historic root
$ => A :MLOAD(blockHashL1InfoTree)
C :HASHK(E) ; Info tree Data
${mem.computedL1InfoTreeData} :HASHK(E), MLOAD(computedL1InfoTreeData) ; Info tree Data
HASHPOS :HASHKLEN(E)
; compute "new" currentL1InfoRoot
$ => A :HASHKDIGEST(E)
; update currentL1InfoTreeRoot
A :MSTORE(currentL1InfoTreeRoot), RETURN
$ => A :HASHKDIGEST(E), MSTORE(currentL1InfoTreeRoot), RETURN

; compute "new" currentL1InfoRoot from computed merkle root, currentL1InfoTreeRoot = keccak(computedMerkleRoot, computedL1InfoTreeData)
; @in C: computed merkle root
Expand All @@ -43,9 +37,7 @@ computeNewCurrentL1InfoRoot:
0 => HASHPOS
32 => D
C :HASHK(E) ; computed merkle root
$ => C :MLOAD(computedL1InfoTreeData)
C :HASHK(E)
${mem.computedL1InfoTreeData} :HASHK(E), MLOAD(computedL1InfoTreeData)
HASHPOS :HASHKLEN(E)
$ => A :HASHKDIGEST(E)
; update currentL1InfoTreeRoot
A :MSTORE(currentL1InfoTreeRoot), RETURN
$ => A :HASHKDIGEST(E), MSTORE(currentL1InfoTreeRoot), RETURN
5 changes: 2 additions & 3 deletions main/process-change-l2-block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ continueProcessChangeL2Block:

%ADDRESS_SYSTEM => A
%SMT_KEY_SC_STORAGE => B
$ => C :MLOAD(blockNum)
C :HASHK(E)
${mem.blockNum} :HASHK(E), MLOAD(blockNum)
%STATE_ROOT_STORAGE_POS :HASHK(E)
HASHPOS :HASHKLEN(E)
$ => C :HASHKDIGEST(E)
Expand Down Expand Up @@ -85,7 +84,7 @@ verifyTimestampAndL1InfoRoot:
$ :LT, JMPC(invalidChangeL2BlockMinTimestamp)
; Compute infoTreeData
:CALL(computeL1InfoTreeValue)
$ => B :MLOAD(currentL1InfoTreeIndex), JMPNZ(previousL1InfoTreeIndexIsNotZero)
$ :MLOAD(currentL1InfoTreeIndex), JMPNZ(previousL1InfoTreeIndexIsNotZero)
; if previous L1 Info TreeIndex is zero, currentL1InfoTreeRoot = keccak(HistoricRoot, blockHashL1InfoTree)
:CALL(computeNewCurrentL1InfoRootFromHistoric)
$ => A :MLOAD(indexL1InfoTree)
Expand Down
12 changes: 4 additions & 8 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,17 @@ getContractAddress:
0xc0 + 22 + C :HASHK1(E)
0x94 :HASHK1(E)
20 => D
$ => B :MLOAD(txSrcAddr)
B :HASHK(E)
${mem.txSrcAddr} :HASHK(E), MLOAD(txSrcAddr)
0x80 + C :HASHK1(E)
C => D
A :HASHK(E), JMP(endContractAddress)

nonce1byte:
$ => A :MLOAD(txSrcAddr)
$ => B :MLOAD(txNonce)
0xc0 + 22 :HASHK1(E)
0x94 :HASHK1(E)
20 => D
A :HASHK(E)
${mem.txSrcAddr} :HASHK(E), MLOAD(txSrcAddr)
B :JMPZ(nonceIs0)
B :HASHK1(E),JMP(endContractAddress)

Expand Down Expand Up @@ -303,11 +301,9 @@ create2end:
E + 1 :MSTORE(nextHashKId)
0xff :HASHK1(E)
20 => D
$ => A :MLOAD(txSrcAddr)
A :HASHK(E)
${mem.txSrcAddr} :HASHK(E), MLOAD(txSrcAddr)
32 => D
$ => B :MLOAD(salt)
B :HASHK(E)
${mem.salt} :HASHK(E), MLOAD(salt)
32 => D
C :HASHK(E)

Expand Down

0 comments on commit cd9c6d4

Please sign in to comment.