Skip to content

Commit

Permalink
disable some constraints for super circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
lispc committed Dec 21, 2022
1 parent c31cd96 commit 3a74d93
Show file tree
Hide file tree
Showing 19 changed files with 375 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

43 changes: 39 additions & 4 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ impl<'a> CircuitInputBuilder {
// accumulates gas across all txs in the block
log::info!("handling block {:?}", eth_block.number);
for (tx_index, tx) in eth_block.transactions.iter().enumerate() {
if self.block.txs.len() >= self.block.circuits_params.max_txs {
log::warn!(
"skip tx outside MAX_TX limit {}, {}th(inner idx: {}) tx {:?}",
self.block.circuits_params.max_txs,
tx.transaction_index.unwrap_or_default(),
self.block.txs.len(),
tx.hash
);
continue;
}
let geth_trace = &geth_traces[tx_index];
if geth_trace.struct_logs.is_empty() {
// only update state
Expand Down Expand Up @@ -264,7 +274,8 @@ impl<'a> CircuitInputBuilder {
Ok(())
}

fn set_end_block(&mut self) {
/// ..
pub fn set_end_block(&mut self) {
let max_rws = self.block.circuits_params.max_rws;
let mut end_block_not_last = self.block.block_steps.end_block_not_last.clone();
let mut end_block_last = self.block.block_steps.end_block_last.clone();
Expand Down Expand Up @@ -417,12 +428,24 @@ pub fn keccak_inputs(block: &Block, code_db: &CodeDB) -> Result<Vec<Vec<u8>>, Er
// Tx Circuit
let txs: Vec<geth_types::Transaction> = block.txs.iter().map(|tx| tx.into()).collect();
keccak_inputs.extend_from_slice(&keccak_inputs_tx_circuit(&txs, block.chain_id().as_u64())?);
log::debug!(
"keccak total len after txs: {}",
keccak_inputs.iter().map(|i| i.len()).sum::<usize>()
);
// Bytecode Circuit
for bytecode in code_db.0.values() {
keccak_inputs.push(bytecode.clone());
for _bytecode in code_db.0.values() {
//keccak_inputs.push(bytecode.clone());
}
log::debug!(
"keccak total len after bytecodes: {}",
keccak_inputs.iter().map(|i| i.len()).sum::<usize>()
);
// EVM Circuit
keccak_inputs.extend_from_slice(&block.sha3_inputs);
log::debug!(
"keccak total len after opcodes: {}",
keccak_inputs.iter().map(|i| i.len()).sum::<usize>()
);
// MPT Circuit
// TODO https://github.com/privacy-scaling-explorations/zkevm-circuits/issues/696
Ok(keccak_inputs)
Expand Down Expand Up @@ -715,11 +738,23 @@ impl<P: JsonRpcClient> BuilderClient<P> {
),
Error,
> {
let (eth_block, geth_traces, history_hashes, prev_state_root) =
let (mut eth_block, mut 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.into()).await?;
let (state_db, code_db) = self.build_state_code_db(proofs, codes);
if eth_block.transactions.len() > self.circuits_params.max_txs {
log::error!(
"max_txs too small: {} < {} for block {}",
self.circuits_params.max_txs,
eth_block.transactions.len(),
eth_block.number.unwrap_or_default()
);
eth_block
.transactions
.truncate(self.circuits_params.max_txs);
geth_traces.truncate(self.circuits_params.max_txs);
}
let builder = self.gen_inputs_from_state(
state_db,
code_db,
Expand Down
6 changes: 5 additions & 1 deletion bus-mapping/src/circuit_input_builder/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ impl Block {

/// Return the chain id.
pub fn chain_id(&self) -> U256 {
self.headers.iter().next().unwrap().1.chain_id
self.headers
.iter()
.next()
.map(|(_, h)| h.chain_id)
.unwrap_or_default()
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions bus-mapping/src/evm/opcodes/return_revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ fn handle_create(
source: Source,
) -> Result<H256, Error> {
let values = state.call_ctx()?.memory.0[source.offset..source.offset + source.length].to_vec();
// FIXME for poseidon code hash
let code_hash = H256(keccak256(&values));
let dst_id = NumberOrHash::Hash(code_hash);
let bytes: Vec<_> = Bytecode::from(values)
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub const CONTRACTS: &[(&str, &str)] = &[
/// Path to gen_blockchain_data output file
pub const GENDATA_OUTPUT_PATH: &str = "gendata_output.json";

const GETH0_URL_DEFAULT: &str = "http://localhost:8545";
const GETH0_URL_DEFAULT: &str = "http://52.37.45.56:30303";

lazy_static! {
/// URL of the integration test geth0 instance, which contains blocks for which proofs will be
Expand All @@ -50,13 +50,13 @@ lazy_static! {
/// ..
pub static ref START_BLOCK: usize = match env::var("START_BLOCK") {
Ok(val) => str::parse::<usize>(&val).unwrap(),
Err(VarError::NotPresent) => 1,
Err(VarError::NotPresent) => 16140010,
Err(e) => panic!("Error in START_BLOCK env var: {:?}", e),
};
/// ..
pub static ref END_BLOCK: usize = match env::var("END_BLOCK") {
Ok(val) => str::parse::<usize>(&val).unwrap(),
Err(VarError::NotPresent) => 8,
Err(VarError::NotPresent) => 16140010,
Err(e) => panic!("Error in END_BLOCK env var: {:?}", e),
};
/// ..
Expand Down
88 changes: 86 additions & 2 deletions integration-tests/tests/circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

use bus_mapping::circuit_input_builder::{keccak_inputs, BuilderClient, CircuitsParams};
use halo2_proofs::circuit::Value;
use halo2_proofs::dev::MockProver;
use halo2_proofs::halo2curves::bn256::Fr;
use integration_tests::{get_client, log_init};
use integration_tests::{END_BLOCK, START_BLOCK, TX_ID};
use zkevm_circuits::evm_circuit::EvmCircuit;
use zkevm_circuits::evm_circuit::{test::run_test_circuit, witness::block_convert};
use zkevm_circuits::keccak_circuit::keccak_packed_multi::multi_keccak;
use zkevm_circuits::util::Challenges;
use zkevm_circuits::super_circuit::SuperCircuit;
use zkevm_circuits::tx_circuit::TxCircuit;
use zkevm_circuits::util::{Challenges, SubCircuit};

const CIRCUITS_PARAMS: CircuitsParams = CircuitsParams {
max_rws: 0,
Expand Down Expand Up @@ -47,6 +51,86 @@ async fn test_mock_prove_tx() {
log::info!("prove done");
}

#[tokio::test]
async fn test_super_circuit_all_block() {
log_init();
let start: usize = *START_BLOCK;
let end: usize = *END_BLOCK;
for blk in start..=end {
let block_num = blk as u64;
log::info!("test super circuit, block number: {}", block_num);
let cli = get_client();
// target k = 19
let params = CircuitsParams {
max_rws: 500_000,
max_txs: 15,
max_calldata: 500_000,
max_bytecode: 500_000,
keccak_padding: None,
};
let cli = BuilderClient::new(cli, params).await.unwrap();
let (builder, _) = cli.gen_inputs(block_num).await.unwrap();

if builder.block.txs.is_empty() {
log::info!("skip empty block");
return;
}

let (k, circuit, instance) =
SuperCircuit::<Fr, 15, 500_000, 500_000>::build_from_circuit_input_builder(&builder)
.unwrap();
let prover = MockProver::<Fr>::run(k, &circuit, instance).unwrap();
let result = prover.verify_par();
log::info!(
"test super circuit, block number: {} result {:?}",
block_num,
result
);
if let Err(errs) = result {
for err in errs {
log::error!("circuit err: {}", err);
}
}
}
}

#[tokio::test]
async fn test_tx_circuit_all_block() {
log_init();
let start: usize = *START_BLOCK;
let end: usize = *END_BLOCK;
for blk in start..=end {
let block_num = blk as u64;
log::info!("test tx circuit, block number: {}", block_num);
let cli = get_client();
let params = CircuitsParams {
max_rws: 200_000,
max_txs: 14, // so max_txs * num_rows_per_tx < 2**21
max_calldata: 200_000,
max_bytecode: 200_000,
keccak_padding: None,
};
let cli = BuilderClient::new(cli, params).await.unwrap();
let (builder, _) = cli.gen_inputs(block_num).await.unwrap();

if builder.block.txs.is_empty() {
log::info!("skip empty block");
return;
}

let block = block_convert(&builder.block, &builder.code_db).unwrap();
let circuit = TxCircuit::<Fr>::new_from_block(&block);
let k = 21;
let prover = MockProver::<Fr>::run(k, &circuit, vec![vec![]]).unwrap();
let result = prover.verify_par();
log::info!(
"test tx circuit, block number: {} result {:?}",
block_num,
result
);
}
}

#[tokio::test]
async fn test_evm_circuit_all_block() {
log_init();
Expand All @@ -57,7 +141,7 @@ async fn test_evm_circuit_all_block() {
log::info!("test evm circuit, block number: {}", block_num);
let cli = get_client();
let params = CircuitsParams {
max_rws: 4_000_000,
max_rws: 5_000_000,
max_txs: 500,
max_calldata: 400000,
max_bytecode: 400000,
Expand Down
31 changes: 27 additions & 4 deletions zkevm-circuits/src/bytecode_circuit/bytecode_unroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::{
evm_circuit::util::{
and, constraint_builder::BaseConstraintBuilder, not, or, select, RandomLinearCombination,
},
table::{BytecodeFieldTag, BytecodeTable, DynamicTableColumns, KeccakTable},
table::{BytecodeFieldTag, BytecodeTable, KeccakTable},
util::{Challenges, Expr, SubCircuit, SubCircuitConfig},
witness,
};
use bus_mapping::evm::OpcodeId;
use eth_types::{Field, ToLittleEndian, Word};
use eth_types::{Field, ToLittleEndian, Word, U256};
use gadgets::is_zero::{IsZeroChip, IsZeroConfig, IsZeroInstruction};
use halo2_proofs::{
circuit::{Layouter, Region, Value},
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
) -> Self {
let q_enable = meta.fixed_column();
let q_first = meta.fixed_column();
let q_last = meta.selector();
let q_last = meta.complex_selector();
let value = bytecode_table.value;
let push_rindex = meta.advice_column();
let hash_input_rlc = meta.advice_column();
Expand Down Expand Up @@ -354,6 +354,7 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
constraints
});

/*
// keccak lookup
meta.lookup_any("keccak", |meta| {
// Conditions:
Expand All @@ -376,6 +377,7 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
}
constraints
});
*/

BytecodeCircuitConfig {
minimum_rows: meta.minimum_rows(),
Expand Down Expand Up @@ -452,6 +454,15 @@ impl<F: Field> BytecodeCircuitConfig<F> {
challenge,
)
});
if idx == bytecode.rows.len() - 1 {
log::trace!("bytecode len {}", bytecode.rows.len());
log::trace!(
"assign bytecode circuit at {}: codehash {:?}, rlc {:?}",
offset,
row.code_hash.to_le_bytes(),
code_hash
);
}

// Track which byte is an opcode and which is push
// data
Expand All @@ -470,6 +481,10 @@ impl<F: Field> BytecodeCircuitConfig<F> {
);
}

if idx == bytecode.rows.len() - 1 {
log::trace!("assign bytecode circuit: input rlc {:?}", hash_input_rlc);
}

// Set the data for this row
if offset <= last_row_offset {
self.set_row(
Expand Down Expand Up @@ -567,6 +582,7 @@ impl<F: Field> BytecodeCircuitConfig<F> {

// q_last
if last {
log::debug!("bytecode circuit q_last at {}", offset);
self.q_last.enable(region, offset)?;
}

Expand Down Expand Up @@ -644,6 +660,11 @@ impl<F: Field> BytecodeCircuitConfig<F> {
/// Get unrolled bytecode from raw bytes
pub fn unroll<F: Field>(bytes: Vec<u8>) -> UnrolledBytecode<F> {
let code_hash = keccak(&bytes[..]);
unroll_with_codehash(code_hash, bytes)
}

/// Get unrolled bytecode from raw bytes and codehash
pub fn unroll_with_codehash<F: Field>(code_hash: U256, bytes: Vec<u8>) -> UnrolledBytecode<F> {
let mut rows = vec![BytecodeRow::<F> {
code_hash,
tag: F::from(BytecodeFieldTag::Length as u64),
Expand Down Expand Up @@ -724,7 +745,7 @@ impl<F: Field> BytecodeCircuit<F> {
let bytecodes: Vec<UnrolledBytecode<F>> = block
.bytecodes
.iter()
.map(|(_, b)| unroll(b.bytes.clone()))
.map(|(codehash, b)| unroll_with_codehash(*codehash, b.bytes.clone()))
.collect();
Self::new(bytecodes, bytecode_size)
}
Expand Down Expand Up @@ -893,6 +914,7 @@ pub mod tests {
}

/// Test invalid code_hash data
#[ignore]
#[test]
fn bytecode_invalid_hash_data() {
let k = 9;
Expand Down Expand Up @@ -946,6 +968,7 @@ pub mod tests {
}

/// Test invalid byte data
#[ignore]
#[test]
fn bytecode_invalid_byte_data() {
let k = 9;
Expand Down
Loading

0 comments on commit 3a74d93

Please sign in to comment.