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

Commit fe61a14

Browse files
committed
feat: refactor tx circuit and use challenge api for it
1 parent 77c5b62 commit fe61a14

File tree

6 files changed

+477
-492
lines changed

6 files changed

+477
-492
lines changed

circuit-benchmarks/src/tx_circuit.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod tests {
1010
use halo2_proofs::poly::kzg::multiopen::{ProverSHPLONK, VerifierSHPLONK};
1111
use halo2_proofs::poly::kzg::strategy::SingleStrategy;
1212
use halo2_proofs::{
13-
arithmetic::FieldExt,
1413
halo2curves::bn256::{Bn256, Fr, G1Affine},
1514
poly::commitment::ParamsProver,
1615
transcript::{
@@ -20,10 +19,7 @@ mod tests {
2019
use rand::SeedableRng;
2120
use rand_chacha::ChaCha20Rng;
2221
use std::marker::PhantomData;
23-
use zkevm_circuits::tx_circuit::{
24-
sign_verify::{SignVerifyChip, POW_RAND_SIZE, VERIF_HEIGHT},
25-
Curve, TxCircuit,
26-
};
22+
use zkevm_circuits::tx_circuit::{sign_verify::SignVerifyChip, Curve, TxCircuit};
2723
use zkevm_circuits::tx_circuit::{Group, Secp256k1Affine};
2824

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

4339
let aux_generator =
4440
<Secp256k1Affine as CurveAffine>::CurveExt::random(&mut rng).to_affine();
45-
let chain_id: u64 = 1337;
41+
let chain_id: u64 = mock::MOCK_CHAIN_ID.low_u64();
4642

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

49-
let randomness = Fr::from(0xcafeu64);
50-
let mut instance: Vec<Vec<Fr>> = (1..POW_RAND_SIZE + 1)
51-
.map(|exp| vec![randomness.pow(&[exp as u64, 0, 0, 0]); MAX_TXS * VERIF_HEIGHT])
52-
.collect();
5345
// SignVerifyChip -> ECDSAChip -> MainGate instance column
54-
instance.push(vec![]);
5546
let circuit = TxCircuit::<Fr, MAX_TXS, MAX_CALLDATA> {
5647
sign_verify: SignVerifyChip {
5748
aux_generator,
5849
window_size: 2,
5950
_marker: PhantomData,
6051
},
61-
randomness,
6252
txs,
6353
chain_id,
6454
};
@@ -75,10 +65,9 @@ mod tests {
7565
let pk = keygen_pk(&general_params, vk, &circuit).expect("keygen_pk should not fail");
7666
// Create a proof
7767
let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);
78-
let instance_slices: Vec<&[Fr]> = instance.iter().map(|v| &v[..]).collect();
7968

8069
// Bench proof generation time
81-
let proof_message = format!("Packed Multi-Keccak Proof generation with {} rows", DEGREE);
70+
let proof_message = format!("Tx Circuit Proof generation with {} rows", DEGREE);
8271
let start2 = start_timer!(|| proof_message);
8372
create_proof::<
8473
KZGCommitmentScheme<Bn256>,
@@ -91,7 +80,7 @@ mod tests {
9180
&general_params,
9281
&pk,
9382
&[circuit],
94-
&[&instance_slices],
83+
&[&[&[]]],
9584
rng,
9685
&mut transcript,
9786
)
@@ -100,7 +89,7 @@ mod tests {
10089
end_timer!(start2);
10190

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

@@ -114,7 +103,7 @@ mod tests {
114103
&verifier_params,
115104
pk.get_vk(),
116105
strategy,
117-
&[&instance_slices],
106+
&[&[&[]]],
118107
&mut verifier_transcript,
119108
)
120109
.expect("failed to verify bench circuit");

integration-tests/tests/circuits.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bus_mapping::circuit_input_builder::BuilderClient;
44
use bus_mapping::operation::OperationContainer;
55
use eth_types::geth_types;
66
use halo2_proofs::{
7-
arithmetic::{CurveAffine, Field, FieldExt},
7+
arithmetic::CurveAffine,
88
dev::MockProver,
99
halo2curves::{
1010
bn256::Fr,
@@ -23,9 +23,7 @@ use zkevm_circuits::copy_circuit::dev::test_copy_circuit;
2323
use zkevm_circuits::evm_circuit::witness::RwMap;
2424
use zkevm_circuits::evm_circuit::{test::run_test_circuit, witness::block_convert};
2525
use zkevm_circuits::state_circuit::StateCircuit;
26-
use zkevm_circuits::tx_circuit::{
27-
sign_verify::SignVerifyChip, Secp256k1Affine, TxCircuit, POW_RAND_SIZE, VERIF_HEIGHT,
28-
};
26+
use zkevm_circuits::tx_circuit::{sign_verify::SignVerifyChip, Secp256k1Affine, TxCircuit};
2927

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

92-
let randomness = Fr::random(&mut rng);
93-
let mut instance: Vec<Vec<Fr>> = (1..POW_RAND_SIZE + 1)
94-
.map(|exp| vec![randomness.pow(&[exp as u64, 0, 0, 0]); txs.len() * VERIF_HEIGHT])
95-
.collect();
96-
97-
instance.push(vec![]);
9890
let circuit = TxCircuit::<Fr, 4, { 4 * (4 + 32 + 32) }> {
9991
sign_verify: SignVerifyChip {
10092
aux_generator,
10193
window_size: 2,
10294
_marker: PhantomData,
10395
},
104-
randomness,
10596
txs,
10697
chain_id: CHAIN_ID,
10798
};
10899

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

111102
prover.verify().expect("tx_circuit verification failed");
112103
}

zkevm-circuits/src/super_circuit.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ use halo2_proofs::{
6868
use super::copy_circuit::CopyCircuit;
6969
use crate::{
7070
keccak_circuit::keccak_bit::{KeccakBitCircuit, KeccakBitConfig},
71-
tx_circuit::sign_verify::POW_RAND_SIZE,
7271
witness::block_convert,
7372
};
7473
use bus_mapping::mock::BlockData;
@@ -153,23 +152,20 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize> Circuit<F>
153152
let keccak_circuit = KeccakBitCircuit::configure(meta);
154153
let keccak_table = keccak_circuit.keccak_table.clone();
155154

156-
let power_of_randomness = power_of_randomness_from_instance(meta);
155+
let power_of_randomness: [_; 31] = power_of_randomness_from_instance(meta);
157156
let evm_circuit = EvmCircuit::configure(
158157
meta,
159-
power_of_randomness[..31].to_vec().try_into().unwrap(),
158+
power_of_randomness.clone(),
160159
&tx_table,
161160
&rw_table,
162161
&bytecode_table,
163162
&block_table,
164163
&copy_table,
165164
&keccak_table,
166165
);
167-
let state_circuit = StateCircuitConfig::configure(
168-
meta,
169-
power_of_randomness[..31].to_vec().try_into().unwrap(),
170-
&rw_table,
171-
&mpt_table,
172-
);
166+
let state_circuit =
167+
StateCircuitConfig::configure(meta, power_of_randomness.clone(), &rw_table, &mpt_table);
168+
let challenges = Challenges::mock(power_of_randomness[0].clone());
173169

174170
Self::Config {
175171
tx_table: tx_table.clone(),
@@ -191,15 +187,15 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize> Circuit<F>
191187
),
192188
tx_circuit: TxCircuitConfig::new(
193189
meta,
194-
power_of_randomness.clone(),
195190
tx_table,
196191
keccak_table.clone(),
192+
challenges.clone(),
197193
),
198194
bytecode_circuit: BytecodeConfig::configure(
199195
meta,
200196
bytecode_table,
201197
keccak_table,
202-
Challenges::mock(power_of_randomness[0].clone()),
198+
challenges,
203199
),
204200
keccak_circuit,
205201
}
@@ -241,7 +237,8 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize> Circuit<F>
241237
)?;
242238
// --- Tx Circuit ---
243239
config.tx_circuit.load(&mut layouter)?;
244-
self.tx_circuit.assign(&config.tx_circuit, &mut layouter)?;
240+
self.tx_circuit
241+
.assign(&config.tx_circuit, &mut layouter, &challenges)?;
245242
// --- Bytecode Circuit ---
246243
let bytecodes: Vec<UnrolledBytecode<F>> = self
247244
.block
@@ -322,14 +319,14 @@ impl<const MAX_TXS: usize, const MAX_CALLDATA: usize> SuperCircuit<Fr, MAX_TXS,
322319
let k = k + 1;
323320
log::debug!("super circuit uses k = {}", k);
324321

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

331328
let chain_id = block.context.chain_id;
332-
let tx_circuit = TxCircuit::new(aux_generator, block.randomness, chain_id.as_u64(), txs);
329+
let tx_circuit = TxCircuit::new(aux_generator, chain_id.as_u64(), txs);
333330

334331
let circuit = SuperCircuit::<_, MAX_TXS, MAX_CALLDATA> {
335332
block,

zkevm-circuits/src/table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl TxTable {
108108
tx_id: meta.advice_column(),
109109
tag: meta.advice_column(),
110110
index: meta.advice_column(),
111-
value: meta.advice_column(),
111+
value: meta.advice_column_in(SecondPhase),
112112
}
113113
}
114114

0 commit comments

Comments
 (0)