Skip to content

Commit

Permalink
Improve comments + small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Apr 4, 2024
1 parent cd9c6d4 commit 347de78
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 37 deletions.
3 changes: 1 addition & 2 deletions main/load-change-l2-block-utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 7 additions & 12 deletions main/load-tx-rlp-utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
28 changes: 17 additions & 11 deletions main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -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
;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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
;;;;;;;;;;;;;;;;;;
Expand All @@ -120,7 +123,7 @@ txLoopRLP:
C - A :JMPN(loadTx_rlp)

;;;;;;;;;;;;;;;;;;
;; D - Loop processing transactions
;; E - Loop processing transactions
;; - Load transaction data and interpret it
;;;;;;;;;;;;;;;;;;

Expand Down Expand Up @@ -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
;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
14 changes: 4 additions & 10 deletions main/opcodes/storage-memory.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions main/process-change-l2-block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 347de78

Please sign in to comment.