简介
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。
- 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,
}
其中,Type是
pub enum Type {
#[default]
/// Zero value for padding, under which id, pointer, and stamp are default value
Zero,
/// Memory in state sub-circuit
Memory,
/// Calldata in state sub-circuit
Calldata,
/// Returndata in state sub-circuit
Returndata,
/// Log in public sub-circuit
PublicLog,
/// Calldata in public sub-circuit
PublicCalldata,
/// Bytecode in bytecode sub-circuit
Bytecode,
}
例子:CODECOPY,从bytecode拷贝到memory。例子里长度为8,被拷贝的数据为0xabcd......见下表。
byte | src type | src id | src pointer | src stamp | dst type | dst id | dst pointer | dst stamp | cnt | len |
---|---|---|---|---|---|---|---|---|---|---|
0xab | Bytecode |
contract addr | some offset | nil | Memory |
callid | some mem addr | some stamp | 0 | 8 |
0xcd | Bytecode |
contract addr | some offset | nil | Memory |
callid | some mem addr | some stamp | 1 | 8 |
... | Bytecode |
contract addr | some offset | nil | Memory |
callid | some mem addr | some stamp | 2 | 8 |
Bytecode |
contract addr | some offset | nil | Memory |
callid | some mem addr | some stamp | 3 | 8 | |
Bytecode |
contract addr | some offset | nil | Memory |
callid | some mem addr | some stamp | 4 | 8 | |
Bytecode |
contract addr | some offset | nil | Memory |
callid | some mem addr | some stamp | 5 | 8 | |
Bytecode |
contract addr | some offset | nil | Memory |
callid | some mem addr | some stamp | 6 | 8 | |
Bytecode |
contract addr | some offset | nil | Memory |
callid | some mem addr | some stamp | 7 | 8 |
门约束
- 若 len-cnt-1==0 OR len==0: next cnt=0
- 否则: next cnt=cnt+1; next src type, dst type, src xx, dst xx, len... same as cur
- 若 len==0: 说明此行是pad行,则cur的 src type, dst type, src xx, dst xx, len 全部是nil或者默认值
Lookup
每一行,都要进行两个约束,向Copy的来源和Copy的去向进行查找表。查找表的来源都是此子表格的一行(的某些列),去向是“Copy的来源或Copy的去向”。我们称为Lookup1和Lookup2。
以上面为例,每一行,要进行:
- Lookup1:去向为bytecode的查找表,含义是确定拷贝的数据没错
- Lookup2:去向为state(具体tag为memory)的查找表,含义是确定拷贝的数据确实写进去了
具体的,Lookup1的情况视src type而定:
- Zero:不进行lookup
- Memory、Calldata、Returndata:来源是此子表格的(tag=常数Memory/Calldata/Returndata, src id, src pointer + cnt, src stamp + cnt, byte, is_write=常数0),去向是state table的(tag, call_id, pointer_lo, stamp, value_lo, is_write)
- 未完待续