From cd9c6d4c9330388f8e33e33b0a55b0beb90861f3 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Wed, 3 Apr 2024 16:26:06 +0200 Subject: [PATCH] Improve hashp and hashk usage --- main/l2-tx-hash.zkasm | 3 +-- main/main.zkasm | 9 +++----- main/process-change-l2-block-utils.zkasm | 28 +++++++++--------------- main/process-change-l2-block.zkasm | 5 ++--- main/process-tx.zkasm | 12 ++++------ 5 files changed, 20 insertions(+), 37 deletions(-) diff --git a/main/l2-tx-hash.zkasm b/main/l2-tx-hash.zkasm index 397c6d29..f2403818 100644 --- a/main/l2-tx-hash.zkasm +++ b/main/l2-tx-hash.zkasm @@ -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 diff --git a/main/main.zkasm b/main/main.zkasm index 35014e5c..441d04c5 100644 --- a/main/main.zkasm +++ b/main/main.zkasm @@ -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) diff --git a/main/process-change-l2-block-utils.zkasm b/main/process-change-l2-block-utils.zkasm index 8c876906..ee433aac 100644 --- a/main/process-change-l2-block-utils.zkasm +++ b/main/process-change-l2-block-utils.zkasm @@ -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 @@ -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 \ No newline at end of file diff --git a/main/process-change-l2-block.zkasm b/main/process-change-l2-block.zkasm index 8fee1a69..dd09c1ac 100644 --- a/main/process-change-l2-block.zkasm +++ b/main/process-change-l2-block.zkasm @@ -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) @@ -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) diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index ae9c4c9a..1a8fb504 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -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) @@ -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)