Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
zkevm-circuits
zkevm-circuits
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Packages & Registries
    • Packages & Registries
    • Package Registry
  • Analytics
    • Analytics
    • CI / CD
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar

新注册的用户请输入邮箱并保存,随后登录邮箱激活账号。后续可直接使用邮箱登录!

  • zkp
  • zkevm-circuitszkevm-circuits
  • Wiki
    • Zkevm docs
  • 7 copy

Last edited by 桂忠 Aug 07, 2024
Page history
This is an old version of this page. You can view the most recent version or browse the history.

7 copy

简介

Copy类操作指的是在EVM中进行不定长的一段数据数据拷贝,例如CALLDATACOPY是一种将calldata中一段数据拷贝到memory中的操作。拷贝以byte作为数据长度单位,注意和栈的U256不同。

由于其长度的不定性,即无法在编写电路时就获知数据拷贝的长度,我们难以在不引入新子电路、子表格的情况下处理此类操作。

我们的做法是,定义了copy子电路,对于Copy类操作,假设其长度为len,在此子电路中,使用len行来处理。每一行都要加上相应的约束来证明是从来源拷贝到去向。对于每次操作,生成的Witness就会包含len行的copy子电路的Row。

具体的,如下操作是Copy类操作:

  • CODECOPY
  • CALLDATACOPY
  • RETURN
  • RETURNDATACOPY
  • LOG
  • 调用开始(开始处理一笔交易或者CALL)时,CALLDATA要被写入STATE子电路所维持的状态中。分为两种:
    • CALLDATA_FROMPUBLIC: 外界(交易、公开数据)的CALLDATA输入
    • CALLDATA_FROMCALL: 合约调用另一个合约的CALLDATA输入

如下图,数据的位置可能是:state子电路中的memory、calldata、returndata,bytecode子电路中的bytecode,public子电路中的calldata和log。

Figure 1

  • CODECOPY:从bytecode到memory
  • CALLDATACOPY:从calldata(state中的)到memory
  • RETURN:从memory到returndata
  • RETURNDATACOPY:从returndata到memory
  • LOG:从memory到log
  • 调用开始(开始处理一笔交易或者CALL)时,CALLDATA要被写入STATE子电路所维持的状态中。分为两种:
  • CALLDATA_FROMPUBLIC: 从public的CALLDATA到state中的calldata
  • CALLDATA_FROMCALL: 从memory到state中的calldata

设计

Witness、Column设计

共使用9列,参见代码。

pub struct Row {
    /// The byte value that is copied
    pub byte: U256,
    /// The source type, one of PublicCalldata, Memory, Bytecode, Calldata, Returndata
    pub src_type: Type,
    /// The source id, tx_idx for PublicCalldata, contract_addr for Bytecode, call_id for Memory, Calldata, Returndata
    pub src_id: U256,
    /// The source pointer, for PublicCalldata, Bytecode, Calldata, Returndata means the index, for Memory means the address
    pub src_pointer: U256,
    /// The source stamp, state stamp for Memory, Calldata, Returndata. None for PublicCalldata and Bytecode
    pub src_stamp: Option<U256>,
    /// The destination type, one of Memory, Calldata, Returndata, PublicLog
    pub dst_type: Type,
    /// The destination id, tx_idx for PublicLog, call_id for Memory, Calldata, Returndata
    pub dst_id: U256,
    /// The destination pointer, for Calldata, Returndata, PublicLog means the index, for Memory means the address
    pub dst_pointer: U256,
    /// The destination stamp, state stamp for Memory, Calldata, Returndata. As for PublicLog it means the log_stamp
    pub dst_stamp: U256,
    /// The counter for one copy operation
    pub cnt: U256,
    /// The length for one copy operation
    pub len: U256,
}

例子:CODECOPY,从bytecode拷贝到memory。例子里长度为8,被拷贝的数据为0xabcd......见下表。

byte src type dst type src id src pointer src stamp dst id dst pointer dst stamp cnt len
0xab bytecode=5 memory=0 contract addr some offset nil callid some mem addr some stamp 0 8
0xcd bytecode=5 memory=0 contract addr some offset nil callid some mem addr some stamp 1 8
... bytecode=5 memory=0 contract addr some offset nil callid some mem addr some stamp 2 8
bytecode=5 memory=0 contract addr some offset nil callid some mem addr some stamp 3 8
bytecode=5 memory=0 contract addr some offset nil callid some mem addr some stamp 4 8
bytecode=5 memory=0 contract addr some offset nil callid some mem addr some stamp 5 8
bytecode=5 memory=0 contract addr some offset nil callid some mem addr some stamp 6 8
bytecode=5 memory=0 contract addr some offset nil callid some mem addr some stamp 7 8

门约束

TODO

Lookup

每一行,都要进行两个约束,向Copy的来源和Copy的去向进行查找表。查找表的来源都是此子表格的一行(的某些列),去向是“Copy的来源或Copy的去向”。

以上面为例,每一行,要进行:

  • 去向为bytecode的查找表,含义是确定拷贝的数据没错
  • 去向为state(具体tag为memory)的查找表,含义是确定拷贝的数据确实写进去了
Clone repository
  • basics
    • evm
    • halo2
  • code notes
    • binary_number_with_real_selector
    • how to use macro
    • simple_lt
    • simple_lt_word
  • Home
  • image
  • zkevm docs
    • 1 introduction
    • 10 public
    • 11 fixed
    • 12 exp
    • 13 keccak
    • 14 comparisons
    • 15 differences
View All Pages

Copyright © 2024 ChainWeaver Org. All Rights Reserved. 版权所有。

京ICP备2023035722号-3

京公网安备 11010802044225号