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

Commit fdd6e4a

Browse files
committed
chores: address review feedback
1 parent 820f0b0 commit fdd6e4a

8 files changed

Lines changed: 127 additions & 149 deletions

File tree

bus-mapping/src/circuit_input_builder.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ use std::{
4848
pub use transaction::{Transaction, TransactionContext};
4949
pub use withdrawal::{Withdrawal, WithdrawalContext};
5050

51+
/// number of execution state fields
52+
pub const N_EXEC_STATE: usize = 10;
53+
5154
/// Runtime Config
5255
///
5356
/// Default to mainnet block
@@ -341,6 +344,8 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
341344
}
342345
}
343346

347+
// chunking and mutable bumping chunk_ctx once condition match
348+
// return true on bumping to next chunk
344349
fn check_and_chunk(
345350
&mut self,
346351
geth_trace: &GethExecTrace,
@@ -493,15 +498,14 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
493498
rw: RW,
494499
tx: Option<&Transaction>,
495500
) {
496-
let STEP_STATE_LEN = 10;
497501
let mut dummy_tx = Transaction::default();
498502
let mut dummy_tx_ctx = TransactionContext::default();
499503

500-
let rw_counters = (0..STEP_STATE_LEN)
504+
let rw_counters = (0..N_EXEC_STATE)
501505
.map(|_| self.block_ctx.rwc.inc_pre())
502506
.collect::<Vec<RWCounter>>();
503507
// just bump rwc in chunk_ctx as block_ctx rwc to assure same delta apply
504-
let rw_counters_inner_chunk = (0..STEP_STATE_LEN)
508+
let rw_counters_inner_chunk = (0..N_EXEC_STATE)
505509
.map(|_| self.chunk_ctx.rwc.inc_pre())
506510
.collect::<Vec<RWCounter>>();
507511

@@ -537,7 +541,7 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
537541
]
538542
};
539543

540-
debug_assert_eq!(STEP_STATE_LEN, tags.len());
544+
debug_assert_eq!(N_EXEC_STATE, tags.len());
541545
let state = self.state_ref(&mut dummy_tx, &mut dummy_tx_ctx);
542546

543547
tags.iter()
@@ -865,15 +869,15 @@ fn push_op<T: Op>(
865869
impl<C: CircuitsParams> CircuitInputBuilder<C> {
866870
///
867871
pub fn rws_reserve(&self) -> usize {
868-
// This is the last chunk of a block, reserve for EndBlock, not EndChunk
872+
// rw ops reserved for EndBlock
869873
let end_block_rws = if self.chunk_ctx.is_last_chunk() && self.chunk_rws() > 0 {
870874
1
871875
} else {
872876
0
873877
};
874-
// This is not the last chunk, reserve for EndChunk
878+
// rw ops reserved for EndChunk
875879
let end_chunk_rws = if !self.chunk_ctx.is_last_chunk() {
876-
10
880+
N_EXEC_STATE
877881
} else {
878882
0
879883
};

bus-mapping/src/operation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ pub enum Target {
116116
TxReceipt,
117117
/// Means the target of the operation is the TxLog.
118118
TxLog,
119-
/// StepState
120-
StepState,
121119

122-
/// padding operation.
120+
/// Chunking: StepState
121+
StepState,
122+
/// Chunking: padding operation.
123123
Padding,
124124
}
125125

@@ -916,7 +916,7 @@ pub enum StepStateField {
916916
LogID,
917917
}
918918

919-
/// Represents an CallContext read/write operation.
919+
/// StepStateOp represents exec state store and load
920920
#[derive(Clone, PartialEq, Eq)]
921921
pub struct StepStateOp {
922922
/// field of CallContext

zkevm-circuits/src/root_circuit.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ where
286286
config.aggregate::<M, As>(ctx, &key.clone(), &self.snark_witnesses)?;
287287

288288
// aggregate user challenge for rwtable permutation challenge
289-
let (_alpha, _gamma) = {
290-
let mut challenges = config.aggregate_user_challenges::<M, As>(
289+
let (alpha, gamma) = {
290+
let (mut challenges, _) = config.aggregate_user_challenges::<M, As>(
291291
loader.clone(),
292292
self.user_challenges,
293293
proofs,
@@ -330,20 +330,6 @@ where
330330
(zero_const, one_const, total_chunk_const)
331331
};
332332

333-
// TODO remove me
334-
let (_hardcode_alpha, _hardcode_gamma) = {
335-
(
336-
loader
337-
.scalar_chip()
338-
.assign_constant(&mut loader.ctx_mut(), M::Fr::from(101))
339-
.unwrap(),
340-
loader
341-
.scalar_chip()
342-
.assign_constant(&mut loader.ctx_mut(), M::Fr::from(103))
343-
.unwrap(),
344-
)
345-
};
346-
347333
// `first.sc_rwtable_row_prev_fingerprint ==
348334
// first.ec_rwtable_row_prev_fingerprint` will be checked inside circuit
349335
vec![
@@ -354,17 +340,11 @@ where
354340
(first_chunk.initial_rwc.assigned(), &one_const),
355341
// constraint permutation fingerprint
356342
// challenge: alpha
357-
// TODO remove hardcode
358-
(first_chunk.sc_permu_alpha.assigned(), &_hardcode_alpha),
359-
(first_chunk.ec_permu_alpha.assigned(), &_hardcode_alpha),
360-
// (first_chunk.sc_permu_alpha.assigned(), &alpha.assigned()),
361-
// (first_chunk.ec_permu_alpha.assigned(), &alpha.assigned()),
343+
(first_chunk.sc_permu_alpha.assigned(), &alpha.assigned()),
344+
(first_chunk.ec_permu_alpha.assigned(), &alpha.assigned()),
362345
// challenge: gamma
363-
// TODO remove hardcode
364-
(first_chunk.sc_permu_gamma.assigned(), &_hardcode_gamma),
365-
(first_chunk.ec_permu_gamma.assigned(), &_hardcode_gamma),
366-
// (first_chunk.sc_permu_gamma.assigned(), &gamma.assigned()),
367-
// (first_chunk.ec_permu_gamma.assigned(), &gamma.assigned()),
346+
(first_chunk.sc_permu_gamma.assigned(), &gamma.assigned()),
347+
(first_chunk.ec_permu_gamma.assigned(), &gamma.assigned()),
368348
// fingerprint
369349
(
370350
first_chunk.ec_rwtable_prev_fingerprint.assigned(),

zkevm-circuits/src/root_circuit/aggregation.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use snark_verifier::{
1515
self,
1616
halo2::{
1717
halo2_wrong_ecc::{self, integer::rns::Rns, maingate::*, EccConfig},
18-
Scalar,
18+
EcPoint, Scalar,
1919
},
2020
native::NativeLoader,
2121
},
@@ -233,7 +233,13 @@ impl AggregationConfig {
233233
loader: Rc<Halo2Loader<'c, M::G1Affine>>,
234234
user_challenges: Option<&UserChallenge>,
235235
proofs: Vec<PlonkProof<M::G1Affine, Rc<Halo2Loader<'c, M::G1Affine>>, As>>,
236-
) -> Result<Vec<LoadedScalar<'c, M::G1Affine>>, Error>
236+
) -> Result<
237+
(
238+
Vec<LoadedScalar<'c, M::G1Affine>>,
239+
Vec<EcPoint<'c, M::G1Affine, EccChip<M::G1Affine>>>,
240+
),
241+
Error,
242+
>
237243
where
238244
M: MultiMillerLoop,
239245
M::Fr: Field,
@@ -253,8 +259,6 @@ impl AggregationConfig {
253259
type PoseidonTranscript<'a, C, S> =
254260
transcript::halo2::PoseidonTranscript<C, Rc<Halo2Loader<'a, C>>, S, T, RATE, R_F, R_P>;
255261

256-
// Verify the cheap part and get accumulator (left-hand and right-hand side of
257-
// pairing) of individual proof.
258262
let witnesses = proofs
259263
.iter()
260264
.flat_map(|proof| {
@@ -279,9 +283,11 @@ impl AggregationConfig {
279283
.map(|user_challenges| user_challenges.num_challenges)
280284
.unwrap_or_default();
281285

282-
Ok((0..num_challenges)
283-
.map(|_| transcript.squeeze_challenge())
284-
.collect_vec())
286+
let witnesses = witnesses
287+
.into_iter()
288+
.cloned()
289+
.collect::<Vec<EcPoint<'c, M::G1Affine, EccChip<M::G1Affine>>>>();
290+
Ok((transcript.squeeze_n_challenges(num_challenges), witnesses))
285291
}
286292

287293
/// Aggregate snarks into a single accumulator and decompose it into
@@ -333,6 +339,7 @@ impl AggregationConfig {
333339
.iter()
334340
.map(|snark| {
335341
let protocol = snark.protocol.loaded(&loader);
342+
336343
let instances = snark.loaded_instances(&loader);
337344
let mut transcript = PoseidonTranscript::new(&loader, snark.proof());
338345
let proof = PlonkSuccinctVerifier::<As>::read_proof(

zkevm-circuits/src/root_circuit/dev.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ use std::{iter, marker::PhantomData, rc::Rc};
2222

2323
/// Aggregation circuit for testing purpose.
2424
#[derive(Clone)]
25+
#[allow(clippy::type_complexity)]
2526
pub struct TestAggregationCircuit<'a, M: MultiMillerLoop, As>
2627
where
2728
M::G1Affine: CurveAffine,
2829
{
2930
svk: KzgSvk<M>,
3031
snarks: Vec<SnarkWitness<'a, M::G1Affine>>,
31-
user_challenge: Option<(UserChallenge, Vec<M::Fr>)>,
32+
user_challenge: Option<(UserChallenge, Vec<M::G1Affine>, Vec<M::Fr>)>,
3233
instances: Vec<M::Fr>,
3334
_marker: PhantomData<As>,
3435
}
@@ -53,10 +54,11 @@ where
5354
{
5455
/// Create an Aggregation circuit with aggregated accumulator computed.
5556
/// Returns `None` if any given snark is invalid.
57+
#[allow(clippy::type_complexity)]
5658
pub fn new(
5759
params: &ParamsKZG<M>,
5860
snarks: impl IntoIterator<Item = Snark<'a, M::G1Affine>>,
59-
user_challenge: Option<(UserChallenge, Vec<M::Fr>)>,
61+
user_challenge: Option<(UserChallenge, Vec<M::G1Affine>, Vec<M::Fr>)>,
6062
) -> Result<Self, snark_verifier::Error> {
6163
let snarks = snarks.into_iter().collect_vec();
6264

@@ -156,16 +158,43 @@ where
156158
config.aggregate::<M, As>(ctx, &self.svk, &self.snarks)?;
157159

158160
// aggregate user challenge for rwtable permutation challenge
159-
let user_challenge = self.user_challenge.as_ref().map(|(challenge, _)| challenge);
160-
let challenges = config.aggregate_user_challenges::<M, As>(
161+
let user_challenge = self
162+
.user_challenge
163+
.as_ref()
164+
.map(|(challenge, _, _)| challenge);
165+
let (challenges, commitments) = config.aggregate_user_challenges::<M, As>(
161166
loader.clone(),
162167
user_challenge,
163168
proofs,
164169
)?;
165170
if !challenges.is_empty() {
166-
let Some((_, expected_challenges)) = self.user_challenge.as_ref() else {
171+
let Some((_, expected_commitments, expected_challenges)) =
172+
self.user_challenge.as_ref()
173+
else {
167174
return Err(InvalidInstances);
168175
};
176+
// check commitment equality
177+
let expected_commitments_loaded = expected_commitments
178+
.iter()
179+
.map(|expected_commitment| {
180+
loader.ecc_chip().assign_point(
181+
&mut loader.ctx_mut(),
182+
Value::known(*expected_commitment),
183+
)
184+
})
185+
.collect::<Result<Vec<_>, Error>>()?;
186+
expected_commitments_loaded
187+
.iter()
188+
.zip(commitments.iter())
189+
.try_for_each(|(expected_commitment, commitment)| {
190+
loader.ecc_chip().assert_equal(
191+
&mut loader.ctx_mut(),
192+
expected_commitment,
193+
&commitment.assigned(),
194+
)
195+
})?;
196+
197+
// check challenge equality
169198
let expected_challenges_loaded = expected_challenges
170199
.iter()
171200
.map(|value| loader.assign_scalar(Value::known(*value)))

0 commit comments

Comments
 (0)