简介
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输入
设计
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)的查找表,含义是确定拷贝的数据确实写进去了