From 347de78c5001d6de6c26d7bead66053e561a7cb2 Mon Sep 17 00:00:00 2001 From: Ignasi Date: Thu, 4 Apr 2024 16:19:38 +0200 Subject: [PATCH] Improve comments + small optimizations --- main/load-change-l2-block-utils.zkasm | 3 +-- main/load-tx-rlp-utils.zkasm | 19 +++++++----------- main/main.zkasm | 28 ++++++++++++++++----------- main/opcodes/flow-control.zkasm | 1 + main/opcodes/storage-memory.zkasm | 14 ++++---------- main/process-change-l2-block.zkasm | 3 +-- 6 files changed, 31 insertions(+), 37 deletions(-) diff --git a/main/load-change-l2-block-utils.zkasm b/main/load-change-l2-block-utils.zkasm index b85a92d9..92a89eee 100644 --- a/main/load-change-l2-block-utils.zkasm +++ b/main/load-change-l2-block-utils.zkasm @@ -3,9 +3,8 @@ ;@in C: current data parsed pointer ;@out A: D bytes from batch data at offset C getChangeL2TxBytes: - $ => A :MLOAD(batchL2DataLength) $ => B :MLOAD(batchL2DataParsed) - A - B - C - D :JMPN(invalidDecodeChangeL2Block) + $ - B - C - D :F_MLOAD(batchL2DataLength), JMPN(invalidDecodeChangeL2Block) $ => E :MLOAD(batchHashDataPointer) $ => HASHPOS :MLOAD(batchHashPos) $ => A :HASHP(E) diff --git a/main/load-tx-rlp-utils.zkasm b/main/load-tx-rlp-utils.zkasm index d4593b51..24ae98e6 100644 --- a/main/load-tx-rlp-utils.zkasm +++ b/main/load-tx-rlp-utils.zkasm @@ -3,28 +3,24 @@ ;@in C: current data parsed pointer ;@out A: D bytes from batchL2Data at offset C getBatchL2DataBytes: - $ => A :MLOAD(batchL2DataLength) $ => B :MLOAD(batchL2DataParsed) - A - B - C - D :JMPN(invalidTxRLP) + $ - B - C - D :F_MLOAD(batchL2DataLength), JMPN(invalidTxRLP) $ => E :MLOAD(batchHashDataPointer) $ => HASHPOS :MLOAD(batchHashPos) $ => A :HASHP(E) HASHPOS :MSTORE(batchHashPos) C => HASHPOS - $ => E :MLOAD(txHashPointer) - :RETURN + $ => E :MLOAD(txHashPointer), RETURN ;; Add bytes to generate ethereum signed message ;; - legacy transaction: signedMessage = H_keccak(rlp(nonce, gasprice, gaslimit, to, value, data, chainId, 0, 0)) ;; - pre EIP-155: signedMessage = H_keccak(rlp(nonce, gasprice, gaslimit, to, value, data)) ; REVIEW: is it necessary?? (first two steps) addHashTx: - $ => A :MLOAD(txRLPLength) - A - HASHPOS - D :JMPN(invalidTxRLP) + $ - HASHPOS - D :F_MLOAD(txRLPLength), JMPN(invalidTxRLP) addHashTxBegin: - $ => A :MLOAD(batchL2DataLength) $ => B :MLOAD(batchL2DataParsed) - A - B - C - D :JMPN(invalidTxRLP) + $ - B - C - D :F_MLOAD(batchL2DataLength), JMPN(invalidTxRLP) $ => E :MLOAD(batchHashDataPointer) $ => HASHPOS :MLOAD(batchHashPos) $ => A :HASHP(E) @@ -41,12 +37,11 @@ addHashTxBegin: ; @in D: number of bytes to add ; @in C: current tx data parsed pointer addHashTxByteByByte: - $ => A :MLOAD(txRLPLength) - A - HASHPOS - D :JMPN(invalidTxRLP) - $ => A :MLOAD(batchL2DataLength) + $ - HASHPOS - D :F_MLOAD(txRLPLength), JMPN(invalidTxRLP) $ => B :MLOAD(batchL2DataParsed) ; Save current registers - A - B - C - D :JMPN(invalidTxRLP), SAVE(B,C,D,E,RR,RCX) + ; CHECK: Is correct to save here in same line as JMPN? + $ - B - C - D :F_MLOAD(batchL2DataLength), JMPN(invalidTxRLP), SAVE(B,C,D,E,RR,RCX) $ => HASHPOS :MLOAD(batchHashPos) D => E readHash: diff --git a/main/main.zkasm b/main/main.zkasm index 441d04c5..197d2976 100644 --- a/main/main.zkasm +++ b/main/main.zkasm @@ -2,12 +2,13 @@ INCLUDE "constants.zkasm" INCLUDE "vars.zkasm" ; Blocks zkROM -; A - Load initial registers into memory: oldStateRoot (B), oldBatchAccInputHash (C), & chainID (GAS) -; B - Compute keccaks needed to finish the batch -; C - Loop parsing RLP transactions -; D - Load blockNum variable & Loop processing transactions -; E - Batch computations: get newLocalExitRoot, assert transactions size, compute batchHashData & compute newBatchAccInputHash -; F - Finalize execution +; A - Load initial registers into memory: oldStateRoot (SR), oldBatchAccInputHash (C), forkID (CTX), previousL1InfoTreeRoot (D), previousL1InfoTreeIndex (RCX), chainID (GAS) +; B - Compute forcedHashData +; C - Compute newBatchAccInputHash, load timestamp and blockNum +; D - Loop parsing RLP transactions +; E - Loop processing transactions +; F - FinalizeBatch +; G - Finalize execution start: ; main zkROM entry point ;;;;;;;;;;;;;;;;;; @@ -40,7 +41,9 @@ start: ; main zkROM entry point ; If forcedHashData ist no zero, is a forced batch ; Set forced batch flag 1 :MSTORE(isForced) - ; Compute forcedHashData +;;;;;;;;;;;;;;;;;; +;; B - Compute forcedHashData +;;;;;;;;;;;;;;;;;; $ => E :MLOAD(nextHashKId) E + 1 :MSTORE(nextHashKId) 32 => D @@ -53,7 +56,7 @@ start: ; main zkROM entry point $ => A :HASHKDIGEST(E) $ :MLOAD(forcedHashData), ASSERT ;;;;;;;;;;;;;;;;; -;; B - Compute newBatchAccInputHash, load newLocalExitRoot and timestamp +;; C - Compute newBatchAccInputHash, load newLocalExitRoot and timestamp ;;;;;;;;;;;;;;;;;; computeNewBatchAccInputHash: ; newBatchAccInputHash = LinearPoseidon(oldBatchAccInputHash, batchHashData, sequencerAddress, forcedHashData)) @@ -103,7 +106,7 @@ setBlockNum: $ => A :SLOAD,MSTORE(blockNum) ;;;;;;;;;;;;;;;;;; -;; C - Loop parsing RLP transactions +;; D - Loop parsing RLP transactions ;; - Load transaction RLP data and ensure it has correct RLP encoding ;; - If an error is found in any transaction, the batch will not process any transaction ;;;;;;;;;;;;;;;;;; @@ -120,7 +123,7 @@ txLoopRLP: C - A :JMPN(loadTx_rlp) ;;;;;;;;;;;;;;;;;; -;; D - Loop processing transactions +;; E - Loop processing transactions ;; - Load transaction data and interpret it ;;;;;;;;;;;;;;;;;; @@ -161,7 +164,7 @@ processTxsEnd: finalizeBatch: ;;;;;;;;;;;;;;;;;; -;; E - Check all save/restore have been consumed +;; F - Check all save/restore have been consumed ;; - Retrieve newLocalExitRoot ;; - Finalize execution: set output values at corresponding registers ;;;;;;;;;;;;;;;;;; @@ -189,6 +192,9 @@ clearPendingRestores_end: $ => A :SLOAD A :MSTORE(newLocalExitRoot) +;;;;;;;;;;;;;;;;;; +;; G - Finalize execution +;;;;;;;;;;;;;;;;;; SR :MSTORE(auxNewSR) ; Store final SR to recover at output setting phase ; Set registers to its initials values ; Save inputs to RESTORE at last - 1 step diff --git a/main/opcodes/flow-control.zkasm b/main/opcodes/flow-control.zkasm index c59c056c..12ea3a3a 100644 --- a/main/opcodes/flow-control.zkasm +++ b/main/opcodes/flow-control.zkasm @@ -112,5 +112,6 @@ opPC: * - stack output: none */ opJUMPDEST: + ; CHECK: counters check removed, is it safe? Check max bytecode/calldata length ; check out-of-gas GAS - %JUMP_DEST_GAS => GAS :JMPN(outOfGas, readCode) diff --git a/main/opcodes/storage-memory.zkasm b/main/opcodes/storage-memory.zkasm index c57f69bc..1e1024f5 100644 --- a/main/opcodes/storage-memory.zkasm +++ b/main/opcodes/storage-memory.zkasm @@ -122,16 +122,10 @@ opMSIZE: ; check out-of-gas GAS - %GAS_QUICK_STEP => GAS :JMPN(outOfGas) - ; load current memory length - $ => E :MLOAD(memLength) - ; MSIZE should be multiple of a word (32 bytes) - ; Div operation with Arith - E :MSTORE(arithA) - 32 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB] - $ => C :MLOAD(arithRes1) - ; check arithRes2 is 0, no need to round in this case - $ :MLOAD(arithRes2), JMPZ(MSIZEend) - + ; load current memory length, less than 32 bits + $ => A :MLOAD(memLength), CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32] + ; MSIZE should be multiple of a word (32 bytes), if mod is zero, return size, otherwise round up to next word + C :JMPZ(MSIZEend) ; Round size to 32bytes multiple C * 32 + 32 => E diff --git a/main/process-change-l2-block.zkasm b/main/process-change-l2-block.zkasm index dd09c1ac..fc20cacc 100644 --- a/main/process-change-l2-block.zkasm +++ b/main/process-change-l2-block.zkasm @@ -74,8 +74,7 @@ verifyTimestampAndL1InfoRoot: ; check indexL1InfoTree != 0 to verify data L1InfoTree $ => A :MLOAD(indexL1InfoTree), JMPZ(skipSetGERL1InfoTree) ; Verify indexL1InfoTree > currentL1InfoTreeIndex - $ => B :MLOAD(currentL1InfoTreeIndex) - A - B - 1 :JMPN(invalidL1InfoTreeIndex) + A - $ - 1 :F_MLOAD(currentL1InfoTreeIndex), JMPN(invalidL1InfoTreeIndex) ${getL1InfoGER(mem.indexL1InfoTree)} :MSTORE(gerL1InfoTree) ${getL1InfoBlockHash(mem.indexL1InfoTree)} :MSTORE(blockHashL1InfoTree) ${getL1InfoMinTimestamp(mem.indexL1InfoTree)} => B :MSTORE(timestampL1InfoTree)