Skip to content

Commit

Permalink
fix sig issue for 1559/2930 in tx circuit (#1269)
Browse files Browse the repository at this point in the history
* fix sig issue for 1559

* add supercircuit test for 1559

* refactor 1559&2930 test helper to single mod

* update tests

* fmt align

* change pub(crate) mod

* use wallet_a as from addr

* restore serial_test_super_circuit_1tx_2max_tx

* increase max_rlp_rows

* add access list para in test helper test_block_2930_trace

* refactor 2930 test with/no access list

* remove debug info

* fmt align

* add empty storage_keys into access list

* disable test serial_test_super_circuit_eip_2930_tx_accesslist for rlp issue
  • Loading branch information
DreamWuGit authored May 22, 2024
1 parent e6cfd7b commit 2f252a3
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 1 deletion.
2 changes: 2 additions & 0 deletions zkevm-circuits/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
//! - [x] Bytecode Circuit
//! - [x] Tx Circuit
//! - [ ] MPT Circuit
#[cfg(feature = "scroll")]
pub(crate) mod eip1559_2930;
pub(crate) mod precompile_block_trace;
#[cfg(any(feature = "test", test))]
pub(crate) mod test;
Expand Down
112 changes: 112 additions & 0 deletions zkevm-circuits/src/super_circuit/eip1559_2930.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//! Helper functions for super circuit tests of EIP-1559

use super::CircuitsParams;
use eth_types::{address, l2_types::BlockTrace, AccessList, AccessListItem, H256};
use ethers_signers::{LocalWallet, Signer};
use mock::{eth, gwei, TestContext, MOCK_CHAIN_ID};
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;

pub(crate) fn test_block_1559_trace() -> BlockTrace {
let mut rng = ChaCha20Rng::seed_from_u64(2);
let wallet_a = LocalWallet::new(&mut rng).with_chain_id(MOCK_CHAIN_ID);

let addr_a = wallet_a.address();
let addr_b = address!("0x0000000000000000000000000000000000001559");

TestContext::<2, 1>::new(
None,
|accs| {
accs[0].address(addr_b).balance(eth(1));
accs[1].address(addr_a).balance(gwei(80_000));
},
|mut txs, _accs| {
txs[0]
.from(wallet_a)
.to(addr_b)
.gas_price(gwei(2))
.gas(30_000.into())
.value(gwei(20_000))
.max_fee_per_gas(gwei(2))
.max_priority_fee_per_gas(gwei(2))
.transaction_type(2); // Set tx type to EIP-1559.
},
|block, _tx| block.number(0xcafe_u64),
)
.unwrap()
.l2_trace()
.clone()
}

pub(crate) fn test_circuits_params(max_txs: usize, max_calldata: usize) -> CircuitsParams {
CircuitsParams {
max_txs,
max_calldata,
max_rws: 256,
max_copy_rows: 256,
max_exp_steps: 256,
max_bytecode: 512,
max_mpt_rows: 2049,
max_poseidon_rows: 512,
max_evm_rows: 0,
max_keccak_rows: 0,
max_inner_blocks: 1,
max_rlp_rows: 1000,
..Default::default()
}
}

/// Helper functions for super circuit tests of EIP-2930
/// param `has_access_list` indicates targeting tx has access list data or not.
pub(crate) fn test_block_2930_trace(has_access_list: bool) -> BlockTrace {
let mut rng = ChaCha20Rng::seed_from_u64(2);
let wallet_a = LocalWallet::new(&mut rng).with_chain_id(MOCK_CHAIN_ID);

let addr_a = wallet_a.address();
let addr_b = address!("0x0000000000000000000000000000000000002930");

let test_access_list = AccessList(vec![
AccessListItem {
address: address!("0x0000000000000000000000000000000000001111"),
storage_keys: [10, 11].map(H256::from_low_u64_be).to_vec(),
},
AccessListItem {
address: address!("0x0000000000000000000000000000000000002222"),
storage_keys: [20, 22].map(H256::from_low_u64_be).to_vec(),
},
AccessListItem {
address: address!("0x0000000000000000000000000000000000003333"),
storage_keys: [30, 33].map(H256::from_low_u64_be).to_vec(),
},
// empty storage_keys
AccessListItem {
address: address!("0x0000000000000000000000000000000000004444"),
storage_keys: Vec::new(),
},
]);

TestContext::<2, 1>::new(
None,
|accs| {
accs[0].address(addr_b).balance(eth(20));
accs[1].address(addr_a).balance(eth(20));
},
|mut txs, _accs| {
txs[0]
.from(wallet_a)
.to(addr_b)
.gas_price(gwei(2))
.gas(1_000_000.into())
.value(eth(2))
.transaction_type(1); // Set tx type to EIP-2930.

if has_access_list {
txs[0].access_list(test_access_list);
}
},
|block, _tx| block.number(0xcafe_u64),
)
.unwrap()
.l2_trace()
.clone()
}
52 changes: 52 additions & 0 deletions zkevm-circuits/src/super_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,55 @@ fn serial_test_super_circuit_precompile_sha256() {

test_super_circuit::<MAX_TXS, MAX_CALLDATA, 1, TEST_MOCK_RANDOMNESS>(block, circuits_params);
}

#[ignore]
#[cfg(feature = "scroll")]
#[test]
fn serial_test_super_circuit_eip_1559_tx() {
const MAX_TXS: usize = 1;
const MAX_CALLDATA: usize = 256;

let block_trace = eip1559_2930::test_block_1559_trace();
let circuits_params = eip1559_2930::test_circuits_params(MAX_TXS, MAX_CALLDATA);

test_super_circuit::<MAX_TXS, MAX_CALLDATA, 1, TEST_MOCK_RANDOMNESS>(
block_trace,
circuits_params,
);
}

#[ignore]
#[cfg(feature = "scroll")]
#[test]
fn serial_test_super_circuit_eip_2930_tx_no_accesslist() {
const MAX_TXS: usize = 1;
const MAX_CALLDATA: usize = 256;

// tx with no access list data
let block_trace = eip1559_2930::test_block_2930_trace(false);
let circuits_params = eip1559_2930::test_circuits_params(MAX_TXS, MAX_CALLDATA);

test_super_circuit::<MAX_TXS, MAX_CALLDATA, 1, TEST_MOCK_RANDOMNESS>(
block_trace,
circuits_params,
);
}

// TODO: disable this test for rlp issue now, will enable it after rlp issue fixed.
// issue tracking here https://github.com/scroll-tech/zkevm-circuits/issues/1138
// #[ignore]
// #[cfg(feature = "scroll")]
// #[test]
// fn serial_test_super_circuit_eip_2930_tx_accesslist() {
// const MAX_TXS: usize = 1;
// const MAX_CALLDATA: usize = 256;

// // tx with access list data
// let block_trace = eip1559_2930::test_block_2930_trace(true);
// let circuits_params = eip1559_2930::test_circuits_params(MAX_TXS, MAX_CALLDATA);

// test_super_circuit::<MAX_TXS, MAX_CALLDATA, 1, TEST_MOCK_RANDOMNESS>(
// block_trace,
// circuits_params,
// );
// }
6 changes: 5 additions & 1 deletion zkevm-circuits/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2883,8 +2883,12 @@ impl<F: Field> TxCircuitConfig<F> {
let sig_s = meta.query_advice(tx_table.value, Rotation(3));
let sv_address = meta.query_advice(sv_address, Rotation::cur());

// include eip1559 and eip2930 type tx, sig_v is 0 or 1.

let v = is_eip155(meta) * (sig_v.expr() - 2.expr() * chain_id - 35.expr())
+ is_pre_eip155(meta) * (sig_v.expr() - 27.expr());
+ is_pre_eip155(meta) * (sig_v.expr() - 27.expr())
+ meta.query_advice(is_eip1559, Rotation::cur()) * sig_v.expr()
+ meta.query_advice(is_eip2930, Rotation::cur()) * sig_v.expr();

let input_exprs = vec![
1.expr(), // q_enable = true
Expand Down

0 comments on commit 2f252a3

Please sign in to comment.