Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

[proof chunk] refactor evm_circuit/state_circuit to support proof chunk #1641

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7a2d655
permutation circuit to support permutation fingerprint on rw_table
hero78119 Oct 3, 2023
043b70e
permutation chip implemetation
hero78119 Oct 4, 2023
554789c
simplify pi by exposing to pre/next instance columns
hero78119 Oct 4, 2023
43ed90e
reserve rw_table first row to be constraints by pi
hero78119 Oct 4, 2023
69680b8
compute next_fingerprints logic in witness block
hero78119 Oct 5, 2023
dd5c9bb
Padding step in RwMap
hero78119 Oct 5, 2023
f2e8a4a
debug variadic_size_check
hero78119 Oct 6, 2023
feab0b1
fix evm circuit lookup logic
hero78119 Oct 9, 2023
aab44aa
rw table sort chronological by rw counter
hero78119 Oct 9, 2023
79ba525
permutation gadget trade more columns with degree
hero78119 Oct 11, 2023
e5af744
fix unittest under statecircuit
hero78119 Oct 11, 2023
a186485
rename by_address_rw_table
hero78119 Oct 11, 2023
1595089
create begin/endchunk step
hero78119 Oct 13, 2023
fe73637
enable begin/end chunk virtual steps in single chunk
hero78119 Oct 16, 2023
d1508a0
add chunk_context and inner chunk rw_counter
hero78119 Oct 16, 2023
ce3c850
revamp constraints on first/last chunk
hero78119 Oct 17, 2023
c47ee95
code cosmetics
hero78119 Oct 17, 2023
b5a9ca1
fix end_block single chunk logic
hero78119 Oct 17, 2023
785a471
remove first chunk first step constraints
hero78119 Oct 17, 2023
6d4a780
chores: variable renaming
hero78119 Oct 18, 2023
a5542f0
add chunkctx table to support lookup chunkctx in execstep
hero78119 Oct 18, 2023
561f5ea
chores: revamp comments
hero78119 Oct 18, 2023
de55034
condense rw_table continuous across chunk via fingerprints
hero78119 Oct 19, 2023
d6be453
refactor global rw_counter to chunkctx_table & code cosmetics
hero78119 Oct 19, 2023
c9a1695
address comments by fixing gamme position bug
hero78119 Oct 20, 2023
1078587
refactor global rw_counter to chunkctx_table lookup
hero78119 Oct 23, 2023
88a6aeb
add testing for multiple chunk
hero78119 Oct 23, 2023
d6e5dbe
chores: make chunk_context as option, first attemp for multiple chunk…
hero78119 Oct 24, 2023
69570e4
set default chunkctx as one chunk
hero78119 Oct 24, 2023
47b4d06
more comment
hero78119 Oct 25, 2023
d263037
optimize state_circuit checking row range
hero78119 Oct 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 45 additions & 25 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use self::access::gen_state_access_trace;
use crate::{
error::Error,
evm::opcodes::{gen_associated_ops, gen_associated_steps},
operation::{CallContextField, Operation, RWCounter, StartOp, RW},
operation::{CallContextField, Op, Operation, OperationContainer, RWCounter, RW},
rpc::GethClient,
state_db::{self, CodeDB, StateDB},
};
Expand Down Expand Up @@ -293,11 +293,6 @@ impl CircuitInputBuilder<FixedCParams> {
);
}

let mut push_op = |step: &mut ExecStep, rwc: RWCounter, rw: RW, op: StartOp| {
let op_ref = state.block.container.insert(Operation::new(rwc, rw, op));
step.bus_mapping_instance.push(op_ref);
};

// rwc index start from 1
let total_rws = state.block_ctx.rwc.0 - 1;
// We need at least 1 extra Start row
Expand All @@ -310,27 +305,51 @@ impl CircuitInputBuilder<FixedCParams> {
max_rws
);
}
let (padding_start, padding_end) = (1, max_rws - total_rws); // rw counter start from 1
push_op(
&mut end_block_last,
RWCounter(padding_start),
RW::READ,
StartOp {},
);
if padding_end != padding_start {
push_op(
&mut end_block_last,
RWCounter(padding_end),
RW::READ,
StartOp {},
);
}
// TODO only push StartOp in first chunk
// push_op(
// &mut state.block.container,
// &mut end_block_last,
// RWCounter(1),
// RW::READ,
// StartOp {},
// );
// if max_rws - total_rws > 1 {
// let (padding_start, padding_end) = (max_rws - total_rws, max_rws); // rw counter
// start from 1 push_op(
// &mut state.block.container,
// &mut end_block_last,
// RWCounter(padding_start),
// RW::READ,
// PaddingOp {},
// );
// if padding_end != padding_start {
// push_op(
// &mut state.block.container,
// &mut end_block_last,
// RWCounter(padding_end),
// RW::READ,
// PaddingOp {},
// );
// }
// }

self.block.block_steps.end_block_not_last = end_block_not_last;
self.block.block_steps.end_block_last = end_block_last;
}
}

#[allow(dead_code)]
fn push_op<T: Op>(
container: &mut OperationContainer,
step: &mut ExecStep,
rwc: RWCounter,
rw: RW,
op: T,
) {
let op_ref = container.insert(Operation::new(rwc, rw, op));
step.bus_mapping_instance.push(op_ref);
}

impl<C: CircuitsParams> CircuitInputBuilder<C> {
/// First part of handle_block, common for dynamic and static circuit parameters.
pub fn begin_handle_block(
Expand Down Expand Up @@ -393,11 +412,12 @@ impl CircuitInputBuilder<DynamicCParams> {
* 2
+ 4; // disabled and unused rows.

let total_rws_before_padding: usize =
// TODO fix below logic for multiple rw_table chunks
let total_rws_before_end_block: usize =
<RWCounter as Into<usize>>::into(self.block_ctx.rwc) - 1; // -1 since rwc start from index `1`
let max_rws_after_padding = total_rws_before_padding
+ 1 // fill 1 to have exactly one StartOp padding in below `set_end_block`
+ if total_rws_before_padding > 0 { 1 /*end_block -> CallContextFieldTag::TxId lookup*/ } else { 0 };
let max_rws_after_padding = total_rws_before_end_block
+ 1 // +1 for RW::Start padding in offset 0
+ if total_rws_before_end_block > 0 { 1 /*end_block -> CallContextFieldTag::TxId lookup*/ } else { 0 };
// Computing the number of rows for the EVM circuit requires the size of ExecStep,
// which is determined in the code of zkevm-circuits and cannot be imported here.
// When the evm circuit receives a 0 value it dynamically computes the minimum
Expand Down
1 change: 1 addition & 0 deletions bus-mapping/src/exec_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl fmt::Debug for OperationRef {
"OperationRef{{ {}, {} }}",
match self.0 {
Target::Start => "Start",
ed255 marked this conversation as resolved.
Show resolved Hide resolved
Target::Padding => "Padding",
Target::Memory => "Memory",
Target::Stack => "Stack",
Target::Storage => "Storage",
Expand Down
32 changes: 31 additions & 1 deletion bus-mapping/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl RWCounter {
/// Enum used to differenciate between EVM Stack, Memory and Storage operations.
#[derive(Debug, Clone, PartialEq, Eq, Copy, EnumIter, Hash)]
pub enum Target {
/// Start is a padding operation.
/// Start operation in the first row
Start = 1,
/// Means the target of the operation is the Memory.
Memory,
Expand All @@ -115,6 +115,8 @@ pub enum Target {
TxReceipt,
/// Means the target of the operation is the TxLog.
TxLog,
/// padding operation.
Padding,
}

impl_expr!(Target);
Expand Down Expand Up @@ -885,6 +887,32 @@ impl Op for StartOp {
}
}

/// Represent a Padding padding operation
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PaddingOp {}

impl PartialOrd for PaddingOp {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for PaddingOp {
fn cmp(&self, _other: &Self) -> Ordering {
Ordering::Equal
}
}

impl Op for PaddingOp {
fn into_enum(self) -> OpEnum {
OpEnum::Padding(self)
}

fn reverse(&self) -> Self {
unreachable!("Padding can't be reverted")
}
}

/// Represents TxReceipt read/write operation.
#[derive(Clone, PartialEq, Eq)]
pub struct TxReceiptOp {
Expand Down Expand Up @@ -955,6 +983,8 @@ pub enum OpEnum {
TxLog(TxLogOp),
/// Start
Start(StartOp),
/// Start
Padding(PaddingOp),
}

/// Operation is a Wrapper over a type that implements Op with a RWCounter.
Expand Down
13 changes: 10 additions & 3 deletions bus-mapping/src/operation/container.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
AccountOp, CallContextOp, MemoryOp, Op, OpEnum, Operation, RWCounter, StackOp, StartOp,
StorageOp, Target, TxAccessListAccountOp, TxAccessListAccountStorageOp, TxLogOp, TxReceiptOp,
TxRefundOp, RW,
AccountOp, CallContextOp, MemoryOp, Op, OpEnum, Operation, PaddingOp, RWCounter, StackOp,
StartOp, StorageOp, Target, TxAccessListAccountOp, TxAccessListAccountStorageOp, TxLogOp,
TxReceiptOp, TxRefundOp, RW,
};
use crate::exec_trace::OperationRef;
use itertools::Itertools;
Expand Down Expand Up @@ -44,6 +44,8 @@ pub struct OperationContainer {
pub tx_log: Vec<Operation<TxLogOp>>,
/// Operations of Start
pub start: Vec<Operation<StartOp>>,
/// Operations of Padding
pub padding: Vec<Operation<PaddingOp>>,
}

impl Default for OperationContainer {
Expand All @@ -68,6 +70,7 @@ impl OperationContainer {
tx_receipt: Vec::new(),
tx_log: Vec::new(),
start: Vec::new(),
padding: Vec::new(),
}
}

Expand Down Expand Up @@ -164,6 +167,10 @@ impl OperationContainer {
self.start.push(Operation::new(rwc, rw, op));
OperationRef::from((Target::Start, self.start.len() - 1))
}
OpEnum::Padding(op) => {
self.padding.push(Operation::new(rwc, rw, op));
OperationRef::from((Target::Padding, self.padding.len() - 1))
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion circuit-benchmarks/src/state_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ mod tests {
.parse()
.expect("Cannot parse DEGREE env var as u32");

let empty_circuit = StateCircuit::<Fr>::new(RwMap::default(), 1 << 16);
let empty_circuit = StateCircuit::<Fr>::new(
RwMap::default(),
1 << 16,
Fr::from(1),
Fr::from(1),
Fr::from(1),
Fr::from(1),
0,
);

// Initialize the polynomial commitment parameters
let mut rng = XorShiftRng::from_seed([
Expand Down
1 change: 1 addition & 0 deletions gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sha3 = "0.7.2"
eth-types = { path = "../eth-types" }
digest = "0.7.6"
strum = "0.24"
itertools = "0.10"

[dev-dependencies]
rand_xorshift = "0.3"
Expand Down
1 change: 1 addition & 0 deletions gadgets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod binary_number;
pub mod is_zero;
pub mod less_than;
pub mod mul_add;
pub mod permutation;
pub mod util;

use eth_types::Field;
Expand Down
Loading
Loading