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

[proof-chunk] Instantiate circuits with Chunk from bus-mapping #1690

Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
a6e7fdd
connect prev and cur fingerprint generated frm rw tables
CeciliaZ030 Nov 27, 2023
1e3eb82
put steps in chunk
CeciliaZ030 Nov 30, 2023
34d6926
non-optional chunk_ctx
CeciliaZ030 Nov 30, 2023
a1ef120
set_begin_chunk & set_end_chunk
CeciliaZ030 Dec 1, 2023
5caa81d
total_chunk & dry_run
CeciliaZ030 Dec 1, 2023
2f0db02
ChunkContext<C: CircuitsParams>
CeciliaZ030 Dec 2, 2023
0d931be
fixed_param in Chunk
CeciliaZ030 Dec 2, 2023
1f2e467
comput param
CeciliaZ030 Dec 2, 2023
8ef6efe
simplify CircuitParams
CeciliaZ030 Dec 2, 2023
0602d0b
Revert "simplify CircuitParams"
CeciliaZ030 Dec 2, 2023
c3d784c
fix
CeciliaZ030 Dec 2, 2023
a14f9a9
no more run_with_chunk cuz chunk is mandatory
CeciliaZ030 Dec 2, 2023
77ba0a6
fix test ctx
CeciliaZ030 Dec 2, 2023
960f03e
single chunk works
CeciliaZ030 Dec 3, 2023
ab2d23c
SubCircuit APi changes with chunk in min_num_rows_block
CeciliaZ030 Dec 3, 2023
6b39462
change CircuitTestBuilder modifiers for block and chunk
CeciliaZ030 Dec 3, 2023
2972ada
revert some wired name change
CeciliaZ030 Dec 3, 2023
5f750ed
revert more wired name change
CeciliaZ030 Dec 3, 2023
3fca09a
rws in Chunk
CeciliaZ030 Dec 4, 2023
b8f64ba
fixed param of last chunk
CeciliaZ030 Dec 4, 2023
872c34f
update
CeciliaZ030 Dec 4, 2023
918650f
test_single_chunk work again
CeciliaZ030 Dec 4, 2023
efde650
Builder pattern in CircuitInputBuilder<C>
CeciliaZ030 Dec 8, 2023
822674b
fmt & fix
CeciliaZ030 Dec 8, 2023
8c9eb38
clippy
CeciliaZ030 Dec 8, 2023
be91432
documentation
CeciliaZ030 Dec 8, 2023
86deb51
set_total_chunk
CeciliaZ030 Dec 8, 2023
f58e308
set_total_chunk & fix clippy
CeciliaZ030 Dec 8, 2023
46907bb
distinguish use of block.rws and chunk.rws, chunk test pass
CeciliaZ030 Dec 8, 2023
d5b3cdc
total_rws < max_rws to pass CI
CeciliaZ030 Dec 8, 2023
5722425
Squashed commit of the following:
CeciliaZ030 Dec 10, 2023
ac527ff
fixed merge errors
CeciliaZ030 Dec 12, 2023
ecadfb3
apply comments
CeciliaZ030 Dec 13, 2023
f353c1c
Merge remote-tracking branch 'Cecilia/bus-mapping-chunk-' into bus-ma…
CeciliaZ030 Dec 13, 2023
a0e1151
fix unit tests & simplify RwMap api for test builder
CeciliaZ030 Dec 14, 2023
f47f0b2
circuit-benchmarks
CeciliaZ030 Dec 15, 2023
afa05ba
twist root circuit api to support multi-proof
hero78119 Dec 11, 2023
42ed0f3
apply c-caller-control principle
hero78119 Dec 11, 2023
c341d5c
fmt & clippy
CeciliaZ030 Dec 15, 2023
5810e73
introduce Padding virtual step
hero78119 Dec 19, 2023
8749544
Merge remote-tracking branch 'ming/padding_step' into bus-mapping-chunk-
CeciliaZ030 Dec 23, 2023
853624b
padding step integrated
CeciliaZ030 Dec 23, 2023
b27114a
no BlockSteps anymore
CeciliaZ030 Dec 23, 2023
4c62c53
look ahead in static rws
CeciliaZ030 Dec 24, 2023
8b5d59f
fix errors
CeciliaZ030 Dec 24, 2023
c37977e
fmt & clippy
CeciliaZ030 Dec 24, 2023
e4390bf
padding window
CeciliaZ030 Dec 28, 2023
b971cdd
fix clippy
CeciliaZ030 Dec 28, 2023
8d17fbe
typo
CeciliaZ030 Dec 28, 2023
ecce427
fix rws
CeciliaZ030 Jan 6, 2024
11fa062
record initial_tx, end_tx within chunk, fixed assign_block for execut…
CeciliaZ030 Jan 7, 2024
ef8250b
fmt & clippy
CeciliaZ030 Jan 7, 2024
e3b1368
fixed yaml
CeciliaZ030 Jan 9, 2024
c2376f1
fixed chunked copy events, CopyCircuit test success
CeciliaZ030 Jan 9, 2024
079a730
fmt & clippy
CeciliaZ030 Jan 9, 2024
63d2baf
prev chunk last rw continuation
CeciliaZ030 Jan 10, 2024
95cac76
table_assignments_padding inconsistant with padding start id
CeciliaZ030 Jan 10, 2024
84d5a6f
fix light test
CeciliaZ030 Jan 10, 2024
f839393
fmt & clippy
CeciliaZ030 Jan 11, 2024
5825c2a
pass lints
CeciliaZ030 Jan 11, 2024
1c0bf68
apply ming's suggestion
CeciliaZ030 Jan 11, 2024
45a1501
fmt
CeciliaZ030 Jan 11, 2024
e3905bb
clippy
CeciliaZ030 Jan 11, 2024
4e4420b
reproduce heavy test evm circuit error
hero78119 Jan 12, 2024
0709577
Merge pull request #24 from hero78119/error_reproduce
CeciliaZ030 Jan 13, 2024
4a72c5f
chores: fix evm end_row assignment bug
hero78119 Jan 23, 2024
5c915bd
fix wrong field
hero78119 Jan 23, 2024
445481f
Merge pull request #25 from hero78119/fix_bug
CeciliaZ030 Jan 23, 2024
58e66c3
refactor and bug fix
hero78119 Jan 23, 2024
698cfd6
Merge pull request #26 from hero78119/fix_bug
CeciliaZ030 Jan 23, 2024
3fc283d
chores: bug fix
hero78119 Jan 23, 2024
33060f4
Merge pull request #27 from hero78119/fix_bug
CeciliaZ030 Jan 24, 2024
e104614
fix bug: add back missing constraints
hero78119 Jan 24, 2024
cf897c8
Merge pull request #28 from hero78119/fix_bug
CeciliaZ030 Jan 24, 2024
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
618 changes: 368 additions & 250 deletions bus-mapping/src/circuit_input_builder.rs

Large diffs are not rendered by default.

54 changes: 11 additions & 43 deletions bus-mapping/src/circuit_input_builder/block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//! Block-related utility module

use super::{
chunk::ChunkContext, execution::ExecState, transaction::Transaction, CopyEvent, ExecStep,
ExpEvent,
};
use super::{execution::ExecState, transaction::Transaction, CopyEvent, ExecStep, ExpEvent};
use crate::{
operation::{OperationContainer, RWCounter},
Error,
Expand All @@ -16,7 +13,7 @@ use std::collections::HashMap;
pub struct BlockContext {
/// Used to track the global counter in every operation in the block.
/// Contains the next available value.
pub(crate) rwc: RWCounter,
pub rwc: RWCounter,
/// Map call_id to (tx_index, call_index) (where tx_index is the index used
/// in Block.txs and call_index is the index used in Transaction.
/// calls).
Expand All @@ -42,24 +39,9 @@ impl BlockContext {
}
}

/// Block-wise execution steps that don't belong to any Transaction.
#[derive(Debug)]
pub struct BlockSteps {
/// EndBlock step that is repeated after the last transaction and before
/// reaching the last EVM row.
pub end_block_not_last: ExecStep,
/// Last EndBlock step that appears in the last EVM row.
pub end_block_last: ExecStep,
/// TODO Define and move chunk related step to Chunk struct
/// Begin op of a chunk
pub begin_chunk: ExecStep,
/// End op of a chunk
pub end_chunk: Option<ExecStep>,
}

// TODO: Remove fields that are duplicated in`eth_block`
/// Circuit Input related to a block.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Block {
/// chain id
pub chain_id: Word,
Expand All @@ -84,10 +66,11 @@ pub struct Block {
pub container: OperationContainer,
/// Transactions contained in the block
pub txs: Vec<Transaction>,
/// Block-wise steps
pub block_steps: BlockSteps,
/// Chunk context
pub chunk_context: ChunkContext,
/// End block step
pub end_block: ExecStep,

// /// Chunk context
// pub chunk_context: ChunkContext,
/// Copy events in this block.
pub copy_events: Vec<CopyEvent>,
/// Inputs to the SHA3 opcode
Expand Down Expand Up @@ -131,25 +114,10 @@ impl Block {
prev_state_root,
container: OperationContainer::new(),
txs: Vec::new(),
block_steps: BlockSteps {
begin_chunk: ExecStep {
exec_state: ExecState::BeginChunk,
..ExecStep::default()
},
end_block_not_last: ExecStep {
exec_state: ExecState::EndBlock,
..ExecStep::default()
},
end_block_last: ExecStep {
exec_state: ExecState::EndBlock,
..ExecStep::default()
},
end_chunk: Some(ExecStep {
exec_state: ExecState::EndChunk,
..ExecStep::default()
}),
end_block: ExecStep {
exec_state: ExecState::EndBlock,
..ExecStep::default()
},
chunk_context: ChunkContext::new(0, 1),
copy_events: Vec::new(),
exp_events: Vec::new(),
sha3_inputs: Vec::new(),
Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/circuit_input_builder/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Call {
}

/// Context of a [`Call`].
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct CallContext {
/// Index of call
pub index: usize,
Expand Down Expand Up @@ -151,7 +151,7 @@ impl CallContext {
/// [`Operation::reversible`](crate::operation::Operation::reversible) that
/// happened in them, that will be reverted at once when the call that initiated
/// this reversion group eventually ends with failure (and thus reverts).
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct ReversionGroup {
/// List of `index` and `reversible_write_counter_offset` of calls belong to
/// this group. `reversible_write_counter_offset` is the number of
Expand Down
61 changes: 46 additions & 15 deletions bus-mapping/src/circuit_input_builder/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,88 @@
use super::{ExecStep, FixedCParams};
use crate::operation::RWCounter;

/// Context of a [`ChunkContext`].
#[derive(Debug, Default, Clone)]
pub struct Chunk {
/// current context
pub ctx: ChunkContext,
/// fixed param for the chunk
pub fixed_param: FixedCParams,
/// Begin op of a chunk
pub begin_chunk: Option<ExecStep>,
/// End op of a chunk
pub end_chunk: Option<ExecStep>,
/// Padding step that is repeated after the last transaction and before
/// reaching the last EVM row.
pub padding: Option<ExecStep>,
}

/// Context of chunking, used to track the current chunk index and inner rw counter
/// also the global rw counter from start to end.
#[derive(Debug, Clone)]
pub struct ChunkContext {
/// Index of current chunk, start from 0
pub idx: usize,
/// Used to track the inner chunk counter in every operation in the chunk.
/// Contains the next available value.
pub rwc: RWCounter,
/// index of current chunk, start from 0
pub chunk_index: usize,
/// number of chunks
/// Number of chunks
pub total_chunks: usize,
/// initial rw counter
/// Initial global rw counter
pub initial_rwc: usize,
/// end rw counter
/// End global rw counter
pub end_rwc: usize,
/// If this block is chunked dynamically
pub is_dynamic: bool,
}

impl Default for ChunkContext {
fn default() -> Self {
Self::new(0, 1)
Self::new(1, false)
}
}

impl ChunkContext {
/// Create a new Self
pub fn new(chunk_index: usize, total_chunks: usize) -> Self {
pub fn new(total_chunks: usize, is_dynamic: bool) -> Self {
Self {
rwc: RWCounter::new(),
chunk_index,
idx: 0,
total_chunks,
initial_rwc: 1, // rw counter start from 1
end_rwc: 0, // end_rwc should be set in later phase
is_dynamic,
}
}

/// new Self with one chunk
/// New chunking context with one chunk
pub fn new_one_chunk() -> Self {
Self {
rwc: RWCounter::new(),
chunk_index: 0,
idx: 0,
total_chunks: 1,
initial_rwc: 1, // rw counter start from 1
end_rwc: 0, // end_rwc should be set in later phase
is_dynamic: false,
}
}

/// is first chunk
/// Proceed the context to next chunk, record the initial rw counter
/// update the chunk idx and reset the inner rw counter
pub fn bump(&mut self, initial_rwc: usize) {
assert!(self.idx + 1 < self.total_chunks, "Exceed total chunks");
self.idx += 1;
self.rwc = RWCounter::new();
self.initial_rwc = initial_rwc;
self.end_rwc = 0;
}

/// Is first chunk
pub fn is_first_chunk(&self) -> bool {
self.chunk_index == 0
self.idx == 0
}

/// is last chunk
/// Is last chunk
pub fn is_last_chunk(&self) -> bool {
self.total_chunks - self.chunk_index - 1 == 0
self.total_chunks - self.idx - 1 == 0
}
}
2 changes: 2 additions & 0 deletions bus-mapping/src/circuit_input_builder/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ pub enum ExecState {
BeginTx,
/// Virtual step End Tx
EndTx,
/// Virtual step Padding
Padding,
/// Virtual step End Block
EndBlock,
/// Virtual step End Chunk
Expand Down
32 changes: 9 additions & 23 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
exec_trace::OperationRef,
operation::{
AccountField, AccountOp, CallContextField, CallContextOp, MemoryOp, Op, OpEnum, Operation,
RWCounter, StackOp, Target, TxAccessListAccountOp, TxLogField, TxLogOp, TxReceiptField,
TxReceiptOp, RW,
StackOp, Target, TxAccessListAccountOp, TxLogField, TxLogOp, TxReceiptField, TxReceiptOp,
RW,
},
state_db::{CodeDB, StateDB},
Error,
Expand All @@ -37,7 +37,7 @@ pub struct CircuitInputStateRef<'a> {
/// Block Context
pub block_ctx: &'a mut BlockContext,
/// Chunk Context
pub chunk_ctx: Option<&'a mut ChunkContext>,
pub chunk_ctx: &'a mut ChunkContext,
/// Transaction
pub tx: &'a mut Transaction,
/// Transaction Context
Expand All @@ -52,9 +52,7 @@ impl<'a> CircuitInputStateRef<'a> {
geth_step,
call_ctx,
self.block_ctx.rwc,
self.chunk_ctx
.as_ref()
.map_or_else(RWCounter::new, |chunk_ctx| chunk_ctx.rwc),
self.chunk_ctx.rwc,
call_ctx.reversible_write_counter,
self.tx_ctx.log_id,
))
Expand All @@ -66,10 +64,7 @@ impl<'a> CircuitInputStateRef<'a> {
exec_state: ExecState::BeginTx,
gas_left: self.tx.gas(),
rwc: self.block_ctx.rwc,
rwc_inner_chunk: self
.chunk_ctx
.as_ref()
.map_or_else(RWCounter::new, |chunk_ctx| chunk_ctx.rwc),
rwc_inner_chunk: self.chunk_ctx.rwc,
..Default::default()
}
}
Expand Down Expand Up @@ -105,10 +100,7 @@ impl<'a> CircuitInputStateRef<'a> {
0
},
rwc: self.block_ctx.rwc,
rwc_inner_chunk: self
.chunk_ctx
.as_ref()
.map_or_else(RWCounter::new, |chunk_ctx| chunk_ctx.rwc),
rwc_inner_chunk: self.chunk_ctx.rwc,
// For tx without code execution
reversible_write_counter: if let Some(call_ctx) = self.tx_ctx.calls().last() {
call_ctx.reversible_write_counter
Expand All @@ -132,9 +124,7 @@ impl<'a> CircuitInputStateRef<'a> {
}
let op_ref = self.block.container.insert(Operation::new(
self.block_ctx.rwc.inc_pre(),
self.chunk_ctx
.as_mut()
.map_or_else(RWCounter::new, |chunk_ctx| chunk_ctx.rwc.inc_pre()),
self.chunk_ctx.rwc.inc_pre(),
rw,
op,
));
Expand Down Expand Up @@ -200,9 +190,7 @@ impl<'a> CircuitInputStateRef<'a> {
self.check_apply_op(&op.clone().into_enum());
let op_ref = self.block.container.insert(Operation::new_reversible(
self.block_ctx.rwc.inc_pre(),
self.chunk_ctx
.as_mut()
.map_or_else(RWCounter::new, |chunk_ctx| chunk_ctx.rwc.inc_pre()),
self.chunk_ctx.rwc.inc_pre(),
RW::WRITE,
op,
));
Expand Down Expand Up @@ -1005,9 +993,7 @@ impl<'a> CircuitInputStateRef<'a> {
self.check_apply_op(&op);
let rev_op_ref = self.block.container.insert_op_enum(
self.block_ctx.rwc.inc_pre(),
self.chunk_ctx
.as_mut()
.map_or_else(RWCounter::new, |chunk_ctx| chunk_ctx.rwc.inc_pre()),
self.chunk_ctx.rwc.inc_pre(),
RW::WRITE,
false,
op,
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/circuit_input_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{

use super::{call::ReversionGroup, Call, CallContext, CallKind, CodeSource, ExecStep};

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
/// Context of a [`Transaction`] which can mutate in an [`ExecStep`].
pub struct TransactionContext {
/// Unique identifier of transaction of the block. The value is `index + 1`.
Expand Down
11 changes: 9 additions & 2 deletions bus-mapping/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ impl BlockData<FixedCParams> {
}

impl BlockData<DynamicCParams> {
/// Create a new block from the given Geth data with default CircuitsParams.
/// Create a new block with one chunk
/// from the given Geth data with default CircuitsParams.
pub fn new_from_geth_data(geth_data: GethData) -> Self {
Self::new_from_geth_data_chunked(geth_data, 1)
}

/// Create a new block with given number of chunks
/// from the given Geth data with default CircuitsParams.
pub fn new_from_geth_data_chunked(geth_data: GethData, total_chunks: usize) -> Self {
let (sdb, code_db) = Self::init_dbs(&geth_data);

Self {
Expand All @@ -98,7 +105,7 @@ impl BlockData<DynamicCParams> {
history_hashes: geth_data.history_hashes,
eth_block: geth_data.eth_block,
geth_traces: geth_data.geth_traces,
circuits_params: DynamicCParams {},
circuits_params: DynamicCParams { total_chunks },
}
}
}
2 changes: 1 addition & 1 deletion circuit-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[features]
default = []
default = ["benches"]
benches = []
Loading
Loading