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

Commit

Permalink
Recursion PoC (#102)
Browse files Browse the repository at this point in the history
* Recursion PoC

* Remove unnecessary proof

* fix: use block_table as lookup

* feat: add taiko_super_circuit for our recursion proof

* reset super_circuit

* feat: benchmarks/super_circuit use taiko super circuit

* fix: clippy

* fix: benchmark

* fix: remove unused selectors

* use root circuit to pass instances

* make root circuit support multi sub-circuits

* use new pcd aggregation circuit

* add N->1 aggregation testcase

* add new circuit file

* make PCDAggregationCircuit generic for both GWC & SHPLONK

* make PCDAggregationCircuit generic for both GWC & SHPLONK

* Update zkevm-circuits/src/witness/block.rs

Co-authored-by: Brecht Devos <[email protected]>

* update lib

* fix name

* fix: remove unused selectors

* feat: add anchor circuit into super circuit

* fix: load pi_table

* rename pcd aggregation circuit

* fix: super circuit test cases

* fix: benchmark

* move test in taiko super circuit to test.rs

* remove deprecated function and fix test

* clean compile warnings

* fix(test): test-all relevant to taiko

* feat: export the metahash

* chore: move golden touch wallet into test

* fix: use protocol instance in block witness

* Fix 2tx test issue

* Re-enable PSE PI tests that were temporarily disabled

* Update zkevm-circuits/src/root_circuit/taiko_aggregation.rs

Co-authored-by: Brecht Devos <[email protected]>

* fix: pi-circuit tests

* fix: ignore the anchor tx's fee

* fix: anchor tx without fee

* fix: each params will be up to u256

* fix: gas price non zero

* fix(anchor): calldata with fixed 32B

* feat: add anchor flag and support multi V

* fix: anchor method signature changed

* fix(anchor): error randomness

* fix: clippy complain

* Fix make test-all + Feedback changes

---------

Co-authored-by: johntaiko <[email protected]>
Co-authored-by: smtmfft <[email protected]>
Co-authored-by: smtmfft <[email protected]>
  • Loading branch information
4 people authored Jul 24, 2023
1 parent 7d575e8 commit 5d785b4
Show file tree
Hide file tree
Showing 29 changed files with 1,956 additions and 341 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.png
.DS_Store
.vscode
.idea
.idea
/generated
96 changes: 93 additions & 3 deletions Cargo.lock

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

65 changes: 61 additions & 4 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,27 @@ impl<'a> CircuitInputBuilder {
&mut self,
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
) -> Result<(), Error> {
self.handle_block_with_anchor(eth_block, geth_traces, false)
}

/// Handle a block by handling each transaction to generate all the
/// associated operations.
pub fn handle_block_with_anchor(
&mut self,
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
has_anchor_tx: bool,
) -> Result<(), Error> {
// accumulates gas across all txs in the block
for (tx_index, tx) in eth_block.transactions.iter().enumerate() {
let geth_trace = &geth_traces[tx_index];
self.handle_tx(tx, geth_trace, tx_index + 1 == eth_block.transactions.len())?;
self.handle_tx(
tx,
geth_trace,
has_anchor_tx && tx_index == 0,
tx_index + 1 == eth_block.transactions.len(),
)?;
}
self.set_value_ops_call_context_rwc_eor();
self.set_end_block();
Expand Down Expand Up @@ -265,10 +281,11 @@ impl<'a> CircuitInputBuilder {
&mut self,
eth_tx: &eth_types::Transaction,
geth_trace: &GethExecTrace,
is_anchor_tx: bool,
is_last_tx: bool,
) -> Result<(), Error> {
let mut tx = self.new_tx(eth_tx, !geth_trace.failed)?;
let mut tx_ctx = TransactionContext::new(eth_tx, geth_trace, is_last_tx)?;
let mut tx_ctx = TransactionContext::new(eth_tx, geth_trace, is_anchor_tx, is_last_tx)?;

// Generate BeginTx step
let begin_tx_step = gen_associated_steps(
Expand Down Expand Up @@ -567,6 +584,30 @@ impl<P: JsonRpcClient> BuilderClient<P> {
geth_traces: &[eth_types::GethExecTrace],
history_hashes: Vec<Word>,
prev_state_root: Word,
) -> Result<CircuitInputBuilder, Error> {
self.gen_inputs_from_state_with_anchor(
sdb,
code_db,
eth_block,
geth_traces,
history_hashes,
prev_state_root,
false,
)
}

/// Step 5. For each step in TxExecTraces, gen the associated ops and state
/// circuit inputs
#[allow(clippy::too_many_arguments)]
pub fn gen_inputs_from_state_with_anchor(
&self,
sdb: StateDB,
code_db: CodeDB,
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
history_hashes: Vec<Word>,
prev_state_root: Word,
has_anchor_tx: bool,
) -> Result<CircuitInputBuilder, Error> {
let block = Block::new(
self.chain_id,
Expand All @@ -576,7 +617,7 @@ impl<P: JsonRpcClient> BuilderClient<P> {
self.circuits_params,
)?;
let mut builder = CircuitInputBuilder::new(sdb, code_db, block);
builder.handle_block(eth_block, geth_traces)?;
builder.handle_block_with_anchor(eth_block, geth_traces, has_anchor_tx)?;
Ok(builder)
}

Expand All @@ -590,19 +631,35 @@ impl<P: JsonRpcClient> BuilderClient<P> {
eth_types::Block<eth_types::Transaction>,
),
Error,
> {
self.gen_inputs_with_anchor(block_num, false).await
}

/// Perform all the steps to generate the circuit inputs
pub async fn gen_inputs_with_anchor(
&self,
block_num: u64,
has_anchor_tx: bool,
) -> Result<
(
CircuitInputBuilder,
eth_types::Block<eth_types::Transaction>,
),
Error,
> {
let (eth_block, geth_traces, history_hashes, prev_state_root) =
self.get_block(block_num).await?;
let access_set = Self::get_state_accesses(&eth_block, &geth_traces)?;
let (proofs, codes) = self.get_state(block_num, access_set).await?;
let (state_db, code_db) = Self::build_state_code_db(proofs, codes);
let builder = self.gen_inputs_from_state(
let builder = self.gen_inputs_from_state_with_anchor(
state_db,
code_db,
&eth_block,
&geth_traces,
history_hashes,
prev_state_root,
has_anchor_tx,
)?;
Ok((builder, eth_block))
}
Expand Down
25 changes: 17 additions & 8 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,21 +474,29 @@ impl<'a> CircuitInputStateRef<'a> {
must_create: bool,
value: Word,
fee: Option<Word>,
is_anchor_tx: bool,
) -> Result<(), Error> {
let (found, sender_account) = self.sdb.get_account(&sender);
if !found {
return Err(Error::AccountNotFound(sender));
}
let mut sender_balance_prev = sender_account.balance;
debug_assert!(
sender_account.balance >= value + fee.unwrap_or_default(),
"invalid amount balance {:?} value {:?} fee {:?}",
sender_balance_prev,
value,
fee
);
if !is_anchor_tx {
debug_assert!(
sender_account.balance >= value + fee.unwrap_or_default(),
"invalid amount balance {:?} value {:?} fee {:?}",
sender_balance_prev,
value,
fee
);
}
if let Some(fee) = fee {
let sender_balance = sender_balance_prev - fee;
let sender_balance = if is_anchor_tx {
// anchor tx doesn't need fee
sender_balance_prev
} else {
sender_balance_prev - fee
};
log::trace!(
"sender balance update with fee (not reversible): {:?} {:?}->{:?}",
sender,
Expand Down Expand Up @@ -575,6 +583,7 @@ impl<'a> CircuitInputStateRef<'a> {
must_create,
value,
None,
false,
)
}

Expand Down
1 change: 1 addition & 0 deletions bus-mapping/src/circuit_input_builder/tracer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl CircuitInputBuilderTx {
struct_logs: vec![geth_step.clone()],
},
false,
false,
)
.unwrap();

Expand Down
8 changes: 8 additions & 0 deletions bus-mapping/src/circuit_input_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct TransactionContext {
id: usize,
/// The index of logs made in the transaction.
pub(crate) log_id: usize,
is_anchor_tx: bool,
/// Identifier if this transaction is last one of the block or not.
is_last_tx: bool,
/// Call stack.
Expand All @@ -38,6 +39,7 @@ impl TransactionContext {
pub fn new(
eth_tx: &eth_types::Transaction,
geth_trace: &GethExecTrace,
is_anchor_tx: bool,
is_last_tx: bool,
) -> Result<Self, Error> {
// Iterate over geth_trace to inspect and collect each call's is_success, which
Expand Down Expand Up @@ -74,6 +76,7 @@ impl TransactionContext {
.as_u64() as usize
+ 1,
log_id: 0,
is_anchor_tx,
is_last_tx,
call_is_success,
calls: Vec::new(),
Expand All @@ -89,6 +92,11 @@ impl TransactionContext {
self.id
}

/// Return is_anchor_tx of the this transaction.
pub fn is_anchor_tx(&self) -> bool {
self.is_anchor_tx
}

/// Return is_last_tx of the this transaction.
pub fn is_last_tx(&self) -> bool {
self.is_last_tx
Expand Down
1 change: 1 addition & 0 deletions bus-mapping/src/evm/opcodes/begin_end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
call.is_create(),
call.value,
Some(state.tx.tx.gas_price * state.tx.gas()),
state.tx_ctx.is_anchor_tx(),
)?;

// In case of contract creation we wish to verify the correctness of the
Expand Down
Loading

0 comments on commit 5d785b4

Please sign in to comment.