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

Commit 31bf030

Browse files
committed
fix various logic error in index and padding
1 parent f714c7e commit 31bf030

File tree

8 files changed

+68
-45
lines changed

8 files changed

+68
-45
lines changed

bus-mapping/src/circuit_input_builder.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,16 +556,17 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
556556
pub fn commit_chunk(
557557
&mut self,
558558
to_next: bool,
559-
end_tx: usize,
560-
end_copy: usize,
559+
next_tx_index: usize,
560+
next_copy_index: usize,
561561
last_call: Option<Call>,
562562
) {
563563
self.chunk_ctx.end_rwc = self.block_ctx.rwc.0;
564-
self.chunk_ctx.end_tx = end_tx;
565-
self.chunk_ctx.end_copy = end_copy;
564+
self.chunk_ctx.end_tx_index = next_tx_index;
565+
self.chunk_ctx.end_copy_index = next_copy_index;
566566
self.chunks[self.chunk_ctx.idx].ctx = self.chunk_ctx.clone();
567567
if to_next {
568-
self.chunk_ctx.bump(self.block_ctx.rwc.0, end_tx, end_copy);
568+
self.chunk_ctx
569+
.bump(self.block_ctx.rwc.0, next_tx_index, next_copy_index);
569570
self.cur_chunk_mut().prev_last_call = last_call;
570571
}
571572
}

bus-mapping/src/circuit_input_builder/chunk.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ pub struct ChunkContext {
3333
pub initial_rwc: usize,
3434
/// End global rw counter
3535
pub end_rwc: usize,
36+
/// tx range in block: [initial_tx_index, end_tx_index)
37+
pub initial_tx_index: usize,
3638
///
37-
pub initial_tx: usize,
39+
pub end_tx_index: usize,
40+
/// copy range in block: [initial_copy_index, end_copy_index)
41+
pub initial_copy_index: usize,
3842
///
39-
pub end_tx: usize,
40-
///
41-
pub initial_copy: usize,
42-
///
43-
pub end_copy: usize,
43+
pub end_copy_index: usize,
4444
/// Druing dry run, chuncking is desabled
4545
pub enable: bool,
4646
}
@@ -60,10 +60,10 @@ impl ChunkContext {
6060
total_chunks,
6161
initial_rwc: 1, // rw counter start from 1
6262
end_rwc: 0, // end_rwc should be set in later phase
63-
initial_tx: 1,
64-
end_tx: 0,
65-
initial_copy: 0,
66-
end_copy: 0,
63+
initial_tx_index: 0,
64+
end_tx_index: 0,
65+
initial_copy_index: 0,
66+
end_copy_index: 0,
6767
enable: true,
6868
}
6969
}
@@ -76,10 +76,10 @@ impl ChunkContext {
7676
total_chunks: 1,
7777
initial_rwc: 1, // rw counter start from 1
7878
end_rwc: 0, // end_rwc should be set in later phase
79-
initial_tx: 1,
80-
end_tx: 0,
81-
initial_copy: 0,
82-
end_copy: 0,
79+
initial_tx_index: 0,
80+
end_tx_index: 0,
81+
initial_copy_index: 0,
82+
end_copy_index: 0,
8383
enable: true,
8484
}
8585
}
@@ -91,11 +91,11 @@ impl ChunkContext {
9191
self.idx += 1;
9292
self.rwc = RWCounter::new();
9393
self.initial_rwc = initial_rwc;
94-
self.initial_tx = initial_tx;
95-
self.initial_copy = initial_copy;
94+
self.initial_tx_index = initial_tx;
95+
self.initial_copy_index = initial_copy;
9696
self.end_rwc = 0;
97-
self.end_tx = 0;
98-
self.end_copy = 0;
97+
self.end_tx_index = 0;
98+
self.end_copy_index = 0;
9999
}
100100

101101
/// Is first chunk

zkevm-circuits/src/copy_circuit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ impl<F: Field> SubCircuit<F> for CopyCircuit<F> {
849849
fn new_from_block(block: &witness::Block<F>, chunk: &Chunk<F>) -> Self {
850850
let chunked_copy_events = block
851851
.copy_events
852-
.get(chunk.chunk_context.initial_copy..chunk.chunk_context.end_copy)
852+
.get(chunk.chunk_context.initial_copy_index..chunk.chunk_context.end_copy_index)
853853
.unwrap_or_default();
854854
Self::new_with_external_data(
855855
chunked_copy_events.to_owned(),

zkevm-circuits/src/copy_circuit/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn test_copy_circuit_from_block<F: Field>(
4747
) -> Result<(), Vec<VerifyFailure>> {
4848
let chunked_copy_events = block
4949
.copy_events
50-
.get(chunk.chunk_context.initial_copy..chunk.chunk_context.end_copy)
50+
.get(chunk.chunk_context.initial_copy_index..chunk.chunk_context.end_copy_index)
5151
.unwrap_or_default();
5252
test_copy_circuit::<F>(
5353
k,

zkevm-circuits/src/evm_circuit/execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ impl<F: Field> ExecutionConfig<F> {
11271127
let dummy_tx = Transaction::default();
11281128
let chunk_txs = block
11291129
.txs
1130-
.get(chunk.chunk_context.initial_tx - 1..chunk.chunk_context.end_tx)
1130+
.get(chunk.chunk_context.initial_tx_index..chunk.chunk_context.end_tx_index)
11311131
.unwrap_or_default();
11321132

11331133
let last_call = chunk_txs

zkevm-circuits/src/state_circuit/test.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn test_state_circuit_ok(
4545
storage: storage_ops,
4646
..Default::default()
4747
});
48-
let chunk = Chunk::new_from_rw_map(&rw_map);
48+
let chunk = Chunk::new_from_rw_map(&rw_map, None, None);
4949

5050
let circuit = StateCircuit::<Fr>::new(&chunk);
5151
let instance = circuit.instance();
@@ -69,15 +69,19 @@ fn verifying_key_independent_of_rw_length() {
6969

7070
let no_rows = StateCircuit::<Fr>::new(&chunk);
7171

72-
chunk = Chunk::new_from_rw_map(&RwMap::from(&OperationContainer {
73-
memory: vec![Operation::new(
74-
RWCounter::from(1),
75-
RWCounter::from(1),
76-
RW::WRITE,
77-
MemoryOp::new(1, MemoryAddress::from(0), 32),
78-
)],
79-
..Default::default()
80-
}));
72+
chunk = Chunk::new_from_rw_map(
73+
&RwMap::from(&OperationContainer {
74+
memory: vec![Operation::new(
75+
RWCounter::from(1),
76+
RWCounter::from(1),
77+
RW::WRITE,
78+
MemoryOp::new(1, MemoryAddress::from(0), 32),
79+
)],
80+
..Default::default()
81+
}),
82+
None,
83+
None,
84+
);
8185
let one_row = StateCircuit::<Fr>::new(&chunk);
8286

8387
let vk_no_rows = keygen_vk(&params, &no_rows).unwrap();
@@ -944,7 +948,11 @@ fn variadic_size_check() {
944948
},
945949
];
946950
// let rw_map: RwMap = rows.clone().into();
947-
let circuit = StateCircuit::new(&Chunk::new_from_rw_map(&RwMap::from(rows.clone())));
951+
let circuit = StateCircuit::new(&Chunk::new_from_rw_map(
952+
&RwMap::from(rows.clone()),
953+
None,
954+
None,
955+
));
948956
let power_of_randomness = circuit.instance();
949957
let prover1 = MockProver::<Fr>::run(17, &circuit, power_of_randomness).unwrap();
950958

@@ -965,7 +973,7 @@ fn variadic_size_check() {
965973
},
966974
]);
967975

968-
let circuit = StateCircuit::new(&Chunk::new_from_rw_map(&rows.into()));
976+
let circuit = StateCircuit::new(&Chunk::new_from_rw_map(&rows.into(), None, None));
969977
let power_of_randomness = circuit.instance();
970978
let prover2 = MockProver::<Fr>::run(17, &circuit, power_of_randomness).unwrap();
971979

zkevm-circuits/src/witness/chunk.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,21 @@ impl<F: Field> Default for Chunk<F> {
6868

6969
impl<F: Field> Chunk<F> {
7070
#[allow(dead_code)]
71-
pub(crate) fn new_from_rw_map(rws: &RwMap) -> Self {
71+
pub(crate) fn new_from_rw_map(
72+
rws: &RwMap,
73+
padding_start_rw: Option<Rw>,
74+
chrono_padding_start_rw: Option<Rw>,
75+
) -> Self {
7276
let (alpha, gamma) = get_permutation_randomness();
7377
let mut chunk = Chunk::default();
7478
let rw_fingerprints = get_permutation_fingerprint_of_rwmap(
7579
rws,
7680
chunk.fixed_param.max_rws,
77-
alpha, // TODO
81+
alpha,
7882
gamma,
7983
F::from(1),
8084
false,
85+
padding_start_rw,
8186
);
8287
let chrono_rw_fingerprints = get_permutation_fingerprint_of_rwmap(
8388
rws,
@@ -86,6 +91,7 @@ impl<F: Field> Chunk<F> {
8691
gamma,
8792
F::from(1),
8893
true,
94+
chrono_padding_start_rw,
8995
);
9096
chunk.rws = rws.clone();
9197
chunk.rw_fingerprints = rw_fingerprints;
@@ -121,6 +127,9 @@ pub fn chunk_convert<F: Field>(
121127

122128
for (i, chunk) in builder.chunks.iter().enumerate() {
123129
// Get the Rws in the i-th chunk
130+
131+
// TODO this is just chronological rws, not by address sorted rws
132+
// need another sorted method
124133
let cur_rws =
125134
RwMap::from_chunked(&block.container, chunk.ctx.initial_rwc, chunk.ctx.end_rwc);
126135
cur_rws.check_value();
@@ -164,7 +173,7 @@ pub fn chunk_convert<F: Field>(
164173
}
165174

166175
// TODO(Cecilia): if we chunk across blocks then need to store the prev_block
167-
let chunck = Chunk {
176+
let chunk = Chunk {
168177
permu_alpha: challenges[idx][0],
169178
permu_gamma: challenges[idx][1],
170179
rw_fingerprints: rw_fingerprints[idx].clone(),
@@ -179,7 +188,7 @@ pub fn chunk_convert<F: Field>(
179188
prev_chunk_last_rw,
180189
};
181190

182-
Ok(chunck)
191+
Ok(chunk)
183192
}
184193

185194
///
@@ -219,13 +228,15 @@ pub fn get_permutation_fingerprint_of_rwmap<F: Field>(
219228
gamma: F,
220229
prev_continuous_fingerprint: F,
221230
is_chrono: bool,
231+
padding_start_rw: Option<Rw>,
222232
) -> RwFingerprints<F> {
223233
get_permutation_fingerprint_of_rwvec(
224234
&rwmap.table_assignments(is_chrono),
225235
max_row,
226236
alpha,
227237
gamma,
228238
prev_continuous_fingerprint,
239+
padding_start_rw,
229240
)
230241
}
231242

@@ -236,6 +247,7 @@ pub fn get_permutation_fingerprint_of_rwvec<F: Field>(
236247
alpha: F,
237248
gamma: F,
238249
prev_continuous_fingerprint: F,
250+
padding_start_rw: Option<Rw>,
239251
) -> RwFingerprints<F> {
240252
get_permutation_fingerprint_of_rwrowvec(
241253
&rwvec
@@ -246,6 +258,7 @@ pub fn get_permutation_fingerprint_of_rwvec<F: Field>(
246258
alpha,
247259
gamma,
248260
prev_continuous_fingerprint,
261+
padding_start_rw.map(|r| r.table_assignment()),
249262
)
250263
}
251264

@@ -256,8 +269,9 @@ pub fn get_permutation_fingerprint_of_rwrowvec<F: Field>(
256269
alpha: F,
257270
gamma: F,
258271
prev_continuous_fingerprint: F,
272+
padding_start_rwrow: Option<RwRow<Value<F>>>,
259273
) -> RwFingerprints<F> {
260-
let (rows, _) = RwRow::padding(rwrowvec, max_row, true);
274+
let (rows, _) = RwRow::padding(rwrowvec, max_row, padding_start_rwrow);
261275
let x = rows.to2dvec();
262276
let fingerprints = get_permutation_fingerprints(
263277
&x,

zkevm-circuits/src/witness/rw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl RwMap {
133133
pub fn table_assignments_padding(
134134
rows: &[Rw],
135135
target_len: usize,
136-
prev_chunk_last_rw: Option<Rw>,
136+
padding_start_rw: Option<Rw>,
137137
) -> (Vec<Rw>, usize) {
138138
// Remove Start/Padding rows as we will add them from scratch.
139139
let rows_trimmed: Vec<Rw> = rows
@@ -162,7 +162,7 @@ impl RwMap {
162162
.map(|rw_counter| Rw::Padding { rw_counter });
163163
(
164164
iter::empty()
165-
.chain([prev_chunk_last_rw.unwrap_or(Rw::Start { rw_counter: 1 })])
165+
.chain([padding_start_rw.unwrap_or(Rw::Start { rw_counter: 1 })])
166166
.chain(rows_trimmed.into_iter())
167167
.chain(padding.into_iter())
168168
.collect(),

0 commit comments

Comments
 (0)