Polygon zkEVM zkROM代码解析

Posted mutourend

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Polygon zkEVM zkROM代码解析相关的知识,希望对你有一定的参考价值。

1. 引言

Polygon zkEVM zkROM代码库为:

zkROM的基本流程为:

  • 1)A:加载输入变量;
  • 2)B:设置batch storage state-tree:batchHash (oldStateRoot) & globalExitRoot;
  • 3)C:循环解析RLP交易;
  • 4)D:循环处理交易;
  • 5)E:batch asserts:localExitRoot、transactions size、batchHashData & globalHash;
  • 6)F:finalize execution

前序博客为:

本文重点包含E/F步骤。

2. E:batch asserts:localExitRoot、transactions size、batchHashData & globalHash

zkROM第五步为batch asserts,即包含对localExitRoot、transactions size、batchHashData & globalHash的断言:

processTxsEnd:

;;;;;;;;;;;;;;;;;;
;; E - Batch asserts: localExitRoot, transactions size, batchHashData & globalHash
;;;;;;;;;;;;;;;;;;

;; Assert local exit root
        ; Read 'localExitRoot' variable from GLOBAL_EXIT_ROOT_MANAGER_L2 and check
        ; it is equal to the 'newLocalExitRoot' input
        ; 常量值CONSTL %ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2 = 0xAE4bB80bE56B819606589DE61d5ec3b522EEB032n
        %ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2  => A
        ; 常量值CONST %SMT_KEY_SC_STORAGE = 3
        %SMT_KEY_SC_STORAGE => B
        ; 常量值CONST %LOCAL_EXIT_ROOT_STORAGE_POS = 1
        %LOCAL_EXIT_ROOT_STORAGE_POS => C
        ; 从Storage中读取以A/B/C为key的value值,给A
        $ => A                                          :SLOAD
        ; 将Storage中读取的值存入newLocalExitRoot
        A                                               :MSTORE(newLocalExitRoot)
assertNewLocalExitRoot:
		; 断言ctx.input.newLocalExitRoot值 与 Storage中读取的值 相等
        $getNewLocalExitRoot()                        :ASSERT


;; Transactions size verification
        ; Ensure bytes added to compute the 'batchHashData' matches the number of bytes loaded from input
        ; 全局变量batchHashPos表示hash batchHashData position
        $ => A                          :MLOAD(batchHashPos)
        ; 全局变量Transactions bytes read from the input表示 Transactions bytes read from the input
        ; 断言batchL2DataLength 与 batchHashPos 相等
        $                               :MLOAD(batchL2DataLength), ASSERT

;; Compute and check 'batchHashData'
        ; Compute 'batchHashData'
        ; Ensure hash result 'batchHashData' mathes the input
        ; 此时A为batchHashPos
        ; 全局变量batchHashPos表示hash batchHashData position
        A => HASHPOS
        ; 全局变量batchHashDataId表示 hash address used when adding bytes to batchHashData
        $ => E                          :MLOAD(batchHashDataId)

        32 => D
        ; 全局变量globalExitRoot表示 Global exit-tree root
        $ => A                          :MLOAD(globalExitRoot)
        ; 附加32个字节的globalExitRoot,为Keccak(transactions | globalExitRoot
        A                               :HASHK(E)

        20 => D
        $ => A                          :MLOAD(sequencerAddr)
        ; 附加20个字节的sequencerAddr,为Keccak(transactions | globalExitRoot | sequencerAddr
        A                               :HASHK(E)
		; 执行哈希运算 Keccak(transactions | globalExitRoot | sequencerAddr)
        HASHPOS                         :HASHKLEN(E)
        ; 全局变量batchHashData表示 ; batchHashData = H_keccak( transactions | globalExitRoot | sequencerAddr )
        $ => A                          :MLOAD(batchHashData)
        ; 断言2个哈希值相等
        $                               :HASHKDIGEST(E), ASSERT

;; Compute and check 'globalHash'
		; 全局变量 globalHash = H_keccak(oldStateRoot | oldLocalStateRoot | newStateRoot | newLocalExitRoot | batchHashData | numBatch | timestamp )
		; 全局变量oldHashPos表示 Save hash data position for globalHash
        $ => HASHPOS                    :MLOAD(oldHashPos) ; Retrieve 'globalHash' hash postion

        32 => D
        ; 附加32字节的newStateRoot SR,addr为0,Keccak(oldStateRoot | oldLocalExitRoot | SR
        SR                              :HASHK(0) ; add 'newStateRoot' to `globalHash`
		; 全局变量newLocalExitRoot表示 Updated local exit-tree root
        $ => A                          :MLOAD(newLocalExitRoot)
        ; 附加32字节的newLocalExitRoot,addr为0,Keccak(oldStateRoot | oldLocalExitRoot | SR | newLocalExitRoot
        A                               :HASHK(0)
		; 全局变量batchHashData表示 batchHashData = H_keccak( transactions | globalExitRoot | sequencerAddr )
        $ => A                          :MLOAD(batchHashData)
        ; 附加32字节的batchHashData,addr为0,Keccak(oldStateRoot | oldLocalExitRoot | SR | newLocalExitRoot | batchHashData
        A                               :HASHK(0)

        8 => D
        ; 全局变量numBatch表示 Current batch to process
        $ => A                          :MLOAD(numBatch)
        ; 附加8字节的numBatch,addr为0,Keccak(oldStateRoot | oldLocalExitRoot | SR | newLocalExitRoot | batchHashData | numBatch
        A                               :HASHK(0)
		; 全局变量timestamp表示 Current batch timestamp
        $ => A                          :MLOAD(timestamp)
        ; 附加8字节的timestamp,addr为0,Keccak(oldStateRoot | oldLocalExitRoot | SR | newLocalExitRoot | batchHashData | numBatch | timestamp
        A                               :HASHK(0)
		; 执行哈希运算,Keccak(oldStateRoot | oldLocalExitRoot | SR | newLocalExitRoot | batchHashData | numBatch | timestamp)
        HASHPOS                         :HASHKLEN(0)
		; 全局变量 globalHash = H_keccak(oldStateRoot | oldLocalStateRoot | newStateRoot | newLocalExitRoot | batchHashData | numBatch | timestamp )
        $ => A                          :MLOAD(globalHash)
assertNewStateRoot:
		; 断言2个哈希值相等
        $                               :HASHKDIGEST(0), ASSERT
		; 打印日志,表示当前batch处理完毕
        $eventLog(onFinishBatch)

3. F:finalize execution

zkROM第六步为finalize execution,即将各寄存器初始化为0:

;;;;;;;;;;;;;;;;;;
;; F - Finalize execution
;;;;;;;;;;;;;;;;;;
finalizeExecution:
		; 寄存器清零
        0 => A,B,C,D,E,CTX, SP, PC, GAS, MAXMEM, SR, HASHPOS, RR ; Set all registers to 0
                                                                        :JMP(finalWait)
; 在主业务流程和最后清零操作之后,进行补零到execution trace表的倒数第二行。
finalWait:
    $beforeLast()     :JMPN(finalWait)
                        :JMP(start)

附录:Polygon Hermez 2.0 zkEVM系列博客

以上是关于Polygon zkEVM zkROM代码解析的主要内容,如果未能解决你的问题,请参考以下文章

Polygon zkEVM zkROM代码解析

Polygon zkEVM zkROM代码解析

Polygon zkEVM zkROM代码解析

Polygon zkEVM zkROM代码解析

Polygon zkEVM节点代码解析

Polygon zkEVM PIL编译器——pilcom 代码解析