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

Commit

Permalink
Address some comments from pinkie
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 committed Nov 29, 2022
1 parent 9dcf76e commit cc6a967
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 111 deletions.
15 changes: 14 additions & 1 deletion bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use execution::{
};
pub use input_state_ref::CircuitInputStateRef;
use itertools::Itertools;
use log::warn;
use std::collections::HashMap;
pub use transaction::{Transaction, TransactionContext};

Expand Down Expand Up @@ -319,7 +320,19 @@ pub fn keccak_inputs_tx_circuit(
chain_id: u64,
) -> Result<Vec<Vec<u8>>, Error> {
let mut inputs = Vec::new();
let sign_datas: Vec<SignData> = txs.iter().map(|tx| tx.sign_data(chain_id)).try_collect()?;
let sign_datas: Vec<SignData> = txs
.iter()
.enumerate()
.filter(|(i, tx)| {
if tx.v == 0 && tx.r.is_zero() && tx.s.is_zero() {
warn!("tx {} is not signed, skipping tx circuit keccak input", i);
false
} else {
true
}
})
.map(|(_, tx)| tx.sign_data(chain_id))
.try_collect()?;
// Keccak inputs from SignVerify Chip
let sign_verify_inputs = keccak_inputs_sign_verify(&sign_datas);
inputs.extend_from_slice(&sign_verify_inputs);
Expand Down
8 changes: 2 additions & 6 deletions circuit-benchmarks/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
mod tests {
use ark_std::{end_timer, start_timer};
use env_logger::Env;
use halo2_proofs::halo2curves::CurveAffine;
use halo2_proofs::plonk::{create_proof, keygen_pk, keygen_vk, verify_proof};
use halo2_proofs::poly::kzg::commitment::{KZGCommitmentScheme, ParamsKZG, ParamsVerifierKZG};
use halo2_proofs::poly::kzg::multiopen::{ProverSHPLONK, VerifierSHPLONK};
Expand All @@ -18,8 +17,7 @@ mod tests {
};
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use zkevm_circuits::tx_circuit::{Curve, TxCircuit};
use zkevm_circuits::tx_circuit::{Group, Secp256k1Affine};
use zkevm_circuits::tx_circuit::TxCircuit;

use crate::bench_params::DEGREE;

Expand All @@ -34,12 +32,10 @@ mod tests {
const MAX_CALLDATA: usize = 1024;

let mut rng = ChaCha20Rng::seed_from_u64(42);
let aux_generator =
<Secp256k1Affine as CurveAffine>::CurveExt::random(&mut rng).to_affine();

let chain_id: u64 = mock::MOCK_CHAIN_ID.low_u64();
let txs = vec![mock::CORRECT_MOCK_TXS[0].clone().into()];
let circuit = TxCircuit::<Fr>::new(MAX_TXS, MAX_CALLDATA, aux_generator, chain_id, txs);
let circuit = TxCircuit::<Fr>::new(MAX_TXS, MAX_CALLDATA, chain_id, txs);

// Bench setup generation
let setup_message = format!("Setup generation with degree = {}", DEGREE);
Expand Down
3 changes: 3 additions & 0 deletions testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ pub fn run_test(
nonce: tx.nonce,
gas: tx.gas_limit,
transaction_index: Some(U64::from(index)),
r: tx.r,
s: tx.s,
v: U64::from(tx.v),
..eth_types::Transaction::default()
})
.collect();
Expand Down
7 changes: 7 additions & 0 deletions zkevm-circuits/src/bytecode_circuit/bytecode_unroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,13 @@ impl<F: Field> SubCircuit<F> for BytecodeCircuit<F> {
type Config = BytecodeCircuitConfig<F>;

fn new_from_block(block: &witness::Block<F>) -> Self {
// TODO: Find a nicer way to add the extra `128`. Is this to account for
// unusable rows? Then it could be calculated like this:
// fn unusable_rows<F: Field, C: Circuit<F>>() -> usize {
// let mut cs = ConstraintSystem::default();
// C::configure(&mut cs);
// cs.blinding_factors()
// }
let bytecode_size = block.circuits_params.max_bytecode + 128;
let bytecodes: Vec<UnrolledBytecode<F>> = block
.bytecodes
Expand Down
85 changes: 41 additions & 44 deletions zkevm-circuits/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,47 @@ impl<F: Field> SubCircuit<F> for PiCircuit<F> {
)
}

/// Compute the public inputs for this circuit.
fn instance(&self) -> Vec<Vec<F>> {
let rlc_rpi_col = raw_public_inputs_col::<F>(
self.max_txs,
self.max_calldata,
&self.public_data,
self.randomness,
);
assert_eq!(
rlc_rpi_col.len(),
BLOCK_LEN + 1 + EXTRA_LEN + 3 * (TX_LEN * self.max_txs + 1 + self.max_calldata)
);

// Computation of raw_pulic_inputs
let rlc_rpi = rlc_rpi_col
.iter()
.rev()
.fold(F::zero(), |acc, val| acc * self.rand_rpi + val);

// let block_hash = public_data
// .eth_block
// .hash
// .unwrap_or_else(H256::zero)
// .to_fixed_bytes();
let public_inputs = vec![
self.rand_rpi,
rlc_rpi,
F::from(self.public_data.chain_id.as_u64()),
rlc(
self.public_data.state_root.to_fixed_bytes(),
self.randomness,
),
rlc(
self.public_data.prev_state_root.to_fixed_bytes(),
self.randomness,
),
];

vec![public_inputs]
}

/// Make the assignments to the PiCircuit
fn synthesize_sub(
&self,
Expand Down Expand Up @@ -941,50 +982,6 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize> Circuit<F>
}
}

impl<F: Field> PiCircuit<F> {
/// Compute the public inputs for this circuit.
pub fn instance(&self) -> Vec<Vec<F>> {
let rlc_rpi_col = raw_public_inputs_col::<F>(
self.max_txs,
self.max_calldata,
&self.public_data,
self.randomness,
);
assert_eq!(
rlc_rpi_col.len(),
BLOCK_LEN + 1 + EXTRA_LEN + 3 * (TX_LEN * self.max_txs + 1 + self.max_calldata)
);

// Computation of raw_pulic_inputs
let rlc_rpi = rlc_rpi_col
.iter()
.rev()
.fold(F::zero(), |acc, val| acc * self.rand_rpi + val);

// let block_hash = public_data
// .eth_block
// .hash
// .unwrap_or_else(H256::zero)
// .to_fixed_bytes();

let public_inputs = vec![
self.rand_rpi,
rlc_rpi,
F::from(self.public_data.chain_id.as_u64()),
rlc(
self.public_data.state_root.to_fixed_bytes(),
self.randomness,
),
rlc(
self.public_data.prev_state_root.to_fixed_bytes(),
self.randomness,
),
];

vec![public_inputs]
}
}

/// Compute the raw_public_inputs column from the verifier's perspective.
fn raw_public_inputs_col<F: Field>(
max_txs: usize,
Expand Down
71 changes: 41 additions & 30 deletions zkevm-circuits/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use crate::table::{BlockTable, BytecodeTable, CopyTable, ExpTable, MptTable, RwT
use crate::tx_circuit::{TxCircuit, TxCircuitConfig, TxCircuitConfigArgs};
use crate::util::{Challenges, SubCircuit, SubCircuitConfig};
use crate::witness::{block_convert, Block, MptUpdates};
use bus_mapping::circuit_input_builder::CircuitInputBuilder;
use bus_mapping::circuit_input_builder::{CircuitInputBuilder, CircuitsParams};
use bus_mapping::mock::BlockData;
use eth_types::geth_types::GethData;
use eth_types::Field;
Expand Down Expand Up @@ -177,34 +177,29 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
let keccak_circuit = KeccakCircuitConfig::new(meta, power_of_randomness[0].clone());
let keccak_table = keccak_circuit.keccak_table.clone();

let evm_circuit = EvmCircuitConfig::new(
let pi_circuit = PiCircuitConfig::new(
meta,
EvmCircuitConfigArgs {
power_of_randomness: power_of_randomness.clone(),
tx_table: tx_table.clone(),
rw_table,
bytecode_table: bytecode_table.clone(),
PiCircuitConfigArgs {
max_txs: MAX_TXS,
max_calldata: MAX_CALLDATA,
block_table: block_table.clone(),
copy_table,
keccak_table: keccak_table.clone(),
exp_table,
tx_table: tx_table.clone(),
},
);
let state_circuit = StateCircuitConfig::new(
let tx_circuit = TxCircuitConfig::new(
meta,
StateCircuitConfigArgs {
rw_table,
mpt_table,
TxCircuitConfigArgs {
tx_table: tx_table.clone(),
keccak_table: keccak_table.clone(),
challenges: challenges.clone(),
},
);
let pi_circuit = PiCircuitConfig::new(
let bytecode_circuit = BytecodeCircuitConfig::new(
meta,
PiCircuitConfigArgs {
max_txs: MAX_TXS,
max_calldata: MAX_CALLDATA,
block_table: block_table.clone(),
tx_table: tx_table.clone(),
BytecodeCircuitConfigArgs {
bytecode_table: bytecode_table.clone(),
keccak_table: keccak_table.clone(),
challenges: challenges.clone(),
},
);
let copy_circuit = CopyCircuitConfig::new(
Expand All @@ -218,23 +213,28 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
randomness: power_of_randomness[0].clone(),
},
);
let tx_circuit = TxCircuitConfig::new(
let state_circuit = StateCircuitConfig::new(
meta,
TxCircuitConfigArgs {
tx_table,
keccak_table: keccak_table.clone(),
challenges: challenges.clone(),
StateCircuitConfigArgs {
rw_table,
mpt_table,
challenges,
},
);
let bytecode_circuit = BytecodeCircuitConfig::new(
let exp_circuit = ExpCircuitConfig::new(meta, exp_table);
let evm_circuit = EvmCircuitConfig::new(
meta,
BytecodeCircuitConfigArgs {
EvmCircuitConfigArgs {
power_of_randomness: power_of_randomness.clone(),
tx_table,
rw_table,
bytecode_table,
block_table: block_table.clone(),
copy_table,
keccak_table,
challenges,
exp_table,
},
);
let exp_circuit = ExpCircuitConfig::new(meta, exp_table);

Self::Config {
block_table,
Expand Down Expand Up @@ -272,6 +272,8 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
Value::known(block.randomness),
)?;

self.keccak_circuit
.synthesize_sub(&config.keccak_circuit, &challenges, &mut layouter)?;
self.bytecode_circuit.synthesize_sub(
&config.bytecode_circuit,
&challenges,
Expand Down Expand Up @@ -305,7 +307,16 @@ impl<const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: usize>
pub fn build(
geth_data: GethData,
) -> Result<(u32, Self, Vec<Vec<Fr>>, CircuitInputBuilder), bus_mapping::Error> {
let block_data = BlockData::new_from_geth_data(geth_data.clone());
let mut block_data = BlockData::new_from_geth_data_with_params(
geth_data.clone(),
CircuitsParams {
max_txs: MAX_TXS,
max_calldata: MAX_CALLDATA,
max_rws: MAX_RWS,
max_bytecode: 512,
keccak_padding: None,
},
);
let mut builder = block_data.new_circuit_input_builder();
builder
.handle_block(&geth_data.eth_block, &geth_data.geth_traces)
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn run_test_circuits<const NACC: usize, const NTX: usize>(
config: Option<BytecodeTestConfig>,
) -> Result<(), Vec<VerifyFailure>> {
let block: GethData = test_ctx.into();
dbg!(&block);
let mut builder =
BlockData::new_from_geth_data_with_params(block.clone(), CircuitsParams::default())
.new_circuit_input_builder();
Expand Down
31 changes: 4 additions & 27 deletions zkevm-circuits/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ use eth_types::{
{geth_types::Transaction, Address, Field, ToLittleEndian, ToScalar},
};
use halo2_proofs::{
arithmetic::CurveAffine,
circuit::{AssignedCell, Layouter, Region, SimpleFloorPlanner, Value},
plonk::{Advice, Circuit, Column, ConstraintSystem, Error, Expression, Fixed},
};
use itertools::Itertools;
use log::error;
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use sign_verify::{AssignedSignatureVerify, SignVerifyChip, SignVerifyConfig};
use std::marker::PhantomData;

Expand Down Expand Up @@ -154,22 +151,11 @@ pub struct TxCircuit<F: Field> {

impl<F: Field> TxCircuit<F> {
/// Return a new TxCircuit
pub fn new(
max_txs: usize,
max_calldata: usize,
aux_generator: Secp256k1Affine,
chain_id: u64,
txs: Vec<Transaction>,
) -> Self {
pub fn new(max_txs: usize, max_calldata: usize, chain_id: u64, txs: Vec<Transaction>) -> Self {
TxCircuit::<F> {
max_txs,
max_calldata,
sign_verify: SignVerifyChip {
aux_generator,
window_size: 2,
max_verif: max_txs,
_marker: PhantomData,
},
sign_verify: SignVerifyChip::new(max_txs),
txs,
chain_id,
}
Expand Down Expand Up @@ -321,13 +307,9 @@ impl<F: Field> SubCircuit<F> for TxCircuit<F> {
type Config = TxCircuitConfig<F>;

fn new_from_block(block: &witness::Block<F>) -> Self {
let mut rng = ChaCha20Rng::seed_from_u64(42);
let aux_generator =
<Secp256k1Affine as CurveAffine>::CurveExt::random(&mut rng).to_affine();
Self::new(
block.circuits_params.max_txs,
block.circuits_params.max_calldata,
aux_generator,
block.context.chain_id.as_u64(),
block
.eth_block
Expand Down Expand Up @@ -418,9 +400,8 @@ mod tx_circuit_tests {
use super::*;
use eth_types::address;
use halo2_proofs::{
arithmetic::CurveAffine,
dev::{MockProver, VerifyFailure},
halo2curves::{bn256::Fr, group::Group},
halo2curves::bn256::Fr,
};
use mock::AddrOrWallet;
use pretty_assertions::assert_eq;
Expand All @@ -432,12 +413,8 @@ mod tx_circuit_tests {
max_txs: usize,
max_calldata: usize,
) -> Result<(), Vec<VerifyFailure>> {
let mut rng = ChaCha20Rng::seed_from_u64(2);
let aux_generator =
<Secp256k1Affine as CurveAffine>::CurveExt::random(&mut rng).to_affine();

// SignVerifyChip -> ECDSAChip -> MainGate instance column
let circuit = TxCircuit::<F>::new(max_txs, max_calldata, aux_generator, chain_id, txs);
let circuit = TxCircuit::<F>::new(max_txs, max_calldata, chain_id, txs);

let prover = match MockProver::run(k, &circuit, vec![vec![]]) {
Ok(prover) => prover,
Expand Down
Loading

0 comments on commit cc6a967

Please sign in to comment.