Skip to content

Commit

Permalink
Refactor Tx circuit and use challenge api (scroll-tech#803)
Browse files Browse the repository at this point in the history
* feat: refactor tx circuit and use challenge api for it

* feat: remove `#[ignore]` for tests of tx circuit

* fix: apply suggestion and clean up

* fix: apply suggestion from @ChihChengLiang
  • Loading branch information
han0110 authored and StefanosChaliasos committed Oct 7, 2022
1 parent 5b003de commit 4c87317
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 512 deletions.
26 changes: 6 additions & 20 deletions circuit-benchmarks/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod tests {
use halo2_proofs::poly::kzg::multiopen::{ProverSHPLONK, VerifierSHPLONK};
use halo2_proofs::poly::kzg::strategy::SingleStrategy;
use halo2_proofs::{
arithmetic::FieldExt,
halo2curves::bn256::{Bn256, Fr, G1Affine},
poly::commitment::ParamsProver,
transcript::{
Expand All @@ -20,10 +19,7 @@ mod tests {
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use std::marker::PhantomData;
use zkevm_circuits::tx_circuit::{
sign_verify::{SignVerifyChip, POW_RAND_SIZE, VERIF_HEIGHT},
Curve, TxCircuit,
};
use zkevm_circuits::tx_circuit::{sign_verify::SignVerifyChip, Curve, TxCircuit};
use zkevm_circuits::tx_circuit::{Group, Secp256k1Affine};

use crate::bench_params::DEGREE;
Expand All @@ -42,23 +38,17 @@ mod tests {

let aux_generator =
<Secp256k1Affine as CurveAffine>::CurveExt::random(&mut rng).to_affine();
let chain_id: u64 = 1337;
let chain_id: u64 = mock::MOCK_CHAIN_ID.low_u64();

let txs = vec![mock::CORRECT_MOCK_TXS[0].clone().into()];

let randomness = Fr::from(0xcafeu64);
let mut instance: Vec<Vec<Fr>> = (1..POW_RAND_SIZE + 1)
.map(|exp| vec![randomness.pow(&[exp as u64, 0, 0, 0]); MAX_TXS * VERIF_HEIGHT])
.collect();
// SignVerifyChip -> ECDSAChip -> MainGate instance column
instance.push(vec![]);
let circuit = TxCircuit::<Fr, MAX_TXS, MAX_CALLDATA> {
sign_verify: SignVerifyChip {
aux_generator,
window_size: 2,
_marker: PhantomData,
},
randomness,
txs,
chain_id,
};
Expand All @@ -75,13 +65,9 @@ mod tests {
let pk = keygen_pk(&general_params, vk, &circuit).expect("keygen_pk should not fail");
// Create a proof
let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);
let instance_slices: Vec<&[Fr]> = instance.iter().map(|v| &v[..]).collect();

// Bench proof generation time
let proof_message = format!(
"Packed Multi-Keccak Proof generation with degree = {}",
DEGREE
);
let proof_message = format!("Tx Circuit Proof generation with degree = {}", DEGREE);
let start2 = start_timer!(|| proof_message);
create_proof::<
KZGCommitmentScheme<Bn256>,
Expand All @@ -94,7 +80,7 @@ mod tests {
&general_params,
&pk,
&[circuit],
&[&instance_slices],
&[&[&[]]],
rng,
&mut transcript,
)
Expand All @@ -103,7 +89,7 @@ mod tests {
end_timer!(start2);

// Bench verification time
let start3 = start_timer!(|| "Packed Multi-Keccak Proof verification");
let start3 = start_timer!(|| "Tx Circuit Proof verification");
let mut verifier_transcript = Blake2bRead::<_, G1Affine, Challenge255<_>>::init(&proof[..]);
let strategy = SingleStrategy::new(&general_params);

Expand All @@ -117,7 +103,7 @@ mod tests {
&verifier_params,
pk.get_vk(),
strategy,
&[&instance_slices],
&[&[&[]]],
&mut verifier_transcript,
)
.expect("failed to verify bench circuit");
Expand Down
15 changes: 3 additions & 12 deletions integration-tests/tests/circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bus_mapping::circuit_input_builder::BuilderClient;
use bus_mapping::operation::OperationContainer;
use eth_types::geth_types;
use halo2_proofs::{
arithmetic::{CurveAffine, Field, FieldExt},
arithmetic::CurveAffine,
dev::MockProver,
halo2curves::{
bn256::Fr,
Expand All @@ -23,9 +23,7 @@ use zkevm_circuits::copy_circuit::dev::test_copy_circuit;
use zkevm_circuits::evm_circuit::witness::RwMap;
use zkevm_circuits::evm_circuit::{test::run_test_circuit, witness::block_convert};
use zkevm_circuits::state_circuit::StateCircuit;
use zkevm_circuits::tx_circuit::{
sign_verify::SignVerifyChip, Secp256k1Affine, TxCircuit, POW_RAND_SIZE, VERIF_HEIGHT,
};
use zkevm_circuits::tx_circuit::{sign_verify::SignVerifyChip, Secp256k1Affine, TxCircuit};

lazy_static! {
pub static ref GEN_DATA: GenDataOutput = GenDataOutput::load();
Expand Down Expand Up @@ -89,24 +87,17 @@ async fn test_tx_circuit_block(block_num: u64) {
let mut rng = ChaCha20Rng::seed_from_u64(2);
let aux_generator = <Secp256k1Affine as CurveAffine>::CurveExt::random(&mut rng).to_affine();

let randomness = Fr::random(&mut rng);
let mut instance: Vec<Vec<Fr>> = (1..POW_RAND_SIZE + 1)
.map(|exp| vec![randomness.pow(&[exp as u64, 0, 0, 0]); txs.len() * VERIF_HEIGHT])
.collect();

instance.push(vec![]);
let circuit = TxCircuit::<Fr, 4, { 4 * (4 + 32 + 32) }> {
sign_verify: SignVerifyChip {
aux_generator,
window_size: 2,
_marker: PhantomData,
},
randomness,
txs,
chain_id: CHAIN_ID,
};

let prover = MockProver::run(DEGREE, &circuit, instance).unwrap();
let prover = MockProver::run(DEGREE, &circuit, vec![vec![]]).unwrap();

prover.verify().expect("tx_circuit verification failed");
}
Expand Down
26 changes: 12 additions & 14 deletions zkevm-circuits/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use crate::evm_circuit::{table::FixedTableTag, EvmCircuit};
use crate::keccak_circuit::keccak_packed_multi::KeccakPackedConfig as KeccakConfig;
use crate::state_circuit::StateCircuitConfig;
use crate::table::{BlockTable, BytecodeTable, CopyTable, MptTable, RwTable, TxTable};
use crate::tx_circuit::{sign_verify::POW_RAND_SIZE, TxCircuit, TxCircuitConfig};
use crate::tx_circuit::{TxCircuit, TxCircuitConfig};
use crate::util::{power_of_randomness_from_instance, Challenges};
use crate::witness::{block_convert, Block};

Expand Down Expand Up @@ -146,27 +146,24 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize> Circuit<F>
let q_copy_table = meta.fixed_column();
let copy_table = CopyTable::construct(meta, q_copy_table);

let power_of_randomness = power_of_randomness_from_instance(meta);
let power_of_randomness: [_; 31] = power_of_randomness_from_instance(meta);

let keccak_circuit = KeccakConfig::configure(meta, power_of_randomness[0].clone());
let keccak_table = keccak_circuit.keccak_table.clone();

let evm_circuit = EvmCircuit::configure(
meta,
power_of_randomness[..31].to_vec().try_into().unwrap(),
power_of_randomness.clone(),
&tx_table,
&rw_table,
&bytecode_table,
&block_table,
&copy_table,
&keccak_table,
);
let state_circuit = StateCircuitConfig::configure(
meta,
power_of_randomness[..31].to_vec().try_into().unwrap(),
&rw_table,
&mpt_table,
);
let state_circuit =
StateCircuitConfig::configure(meta, power_of_randomness.clone(), &rw_table, &mpt_table);
let challenges = Challenges::mock(power_of_randomness[0].clone());

Self::Config {
tx_table: tx_table.clone(),
Expand All @@ -188,15 +185,15 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize> Circuit<F>
),
tx_circuit: TxCircuitConfig::new(
meta,
power_of_randomness.clone(),
tx_table,
keccak_table.clone(),
challenges.clone(),
),
bytecode_circuit: BytecodeConfig::configure(
meta,
bytecode_table,
keccak_table,
Challenges::mock(power_of_randomness[0].clone()),
challenges,
),
keccak_circuit,
}
Expand Down Expand Up @@ -238,7 +235,8 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize> Circuit<F>
)?;
// --- Tx Circuit ---
config.tx_circuit.load(&mut layouter)?;
self.tx_circuit.assign(&config.tx_circuit, &mut layouter)?;
self.tx_circuit
.assign(&config.tx_circuit, &mut layouter, &challenges)?;
// --- Bytecode Circuit ---
let bytecodes: Vec<UnrolledBytecode<F>> = self
.block
Expand Down Expand Up @@ -319,14 +317,14 @@ impl<const MAX_TXS: usize, const MAX_CALLDATA: usize> SuperCircuit<Fr, MAX_TXS,
let k = k + 1;
log::debug!("super circuit uses k = {}", k);

let mut instance: Vec<Vec<Fr>> = (1..POW_RAND_SIZE + 1)
let mut instance: Vec<Vec<Fr>> = (1..32)
.map(|exp| vec![block.randomness.pow_vartime(&[exp as u64, 0, 0, 0]); (1 << k) - 64])
.collect();
// SignVerifyChip -> ECDSAChip -> MainGate instance column
instance.push(vec![]);

let chain_id = block.context.chain_id;
let tx_circuit = TxCircuit::new(aux_generator, block.randomness, chain_id.as_u64(), txs);
let tx_circuit = TxCircuit::new(aux_generator, chain_id.as_u64(), txs);

let circuit = SuperCircuit::<_, MAX_TXS, MAX_CALLDATA> {
block,
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl TxTable {
tx_id: meta.advice_column(),
tag: meta.advice_column(),
index: meta.advice_column(),
value: meta.advice_column(),
value: meta.advice_column_in(SecondPhase),
}
}

Expand Down
Loading

0 comments on commit 4c87317

Please sign in to comment.