Skip to content

Commit b7b3ae1

Browse files
committed
extract shard id from cycle
1 parent 2d5ed66 commit b7b3ae1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

ceno_zkvm/src/e2e.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ pub struct RAMRecord {
106106
pub ram_type: RAMType,
107107
pub id: u64,
108108
pub addr: WordAddr,
109+
// prev_cycle and cycle are global cycle
109110
pub prev_cycle: Cycle,
110111
pub cycle: Cycle,
111112
pub prev_value: Option<Word>,
112113
pub value: Word,
114+
// for global reads, `shard_id` refers to the shard that previously produced this value.
115+
// for global write, `shard_id` refers to current shard.
116+
pub shard_id: usize,
113117
}
114118

115119
#[derive(Clone, Debug)]
@@ -155,6 +159,7 @@ pub struct ShardContext<'a> {
155159
write_thread_based_record_storage:
156160
Either<Vec<BTreeMap<WordAddr, RAMRecord>>, &'a mut BTreeMap<WordAddr, RAMRecord>>,
157161
pub cur_shard_cycle_range: std::ops::Range<usize>,
162+
pub expected_inst_per_shard: usize,
158163
}
159164

160165
impl<'a> Default for ShardContext<'a> {
@@ -177,6 +182,7 @@ impl<'a> Default for ShardContext<'a> {
177182
.collect::<Vec<_>>(),
178183
),
179184
cur_shard_cycle_range: Tracer::SUBCYCLES_PER_INSN as usize..usize::MAX,
185+
expected_inst_per_shard: usize::MAX,
180186
}
181187
}
182188
}
@@ -223,6 +229,7 @@ impl<'a> ShardContext<'a> {
223229
.collect::<Vec<_>>(),
224230
),
225231
cur_shard_cycle_range,
232+
expected_inst_per_shard,
226233
}
227234
}
228235

@@ -244,6 +251,7 @@ impl<'a> ShardContext<'a> {
244251
read_thread_based_record_storage: Either::Right(read),
245252
write_thread_based_record_storage: Either::Right(write),
246253
cur_shard_cycle_range: self.cur_shard_cycle_range.clone(),
254+
expected_inst_per_shard: self.expected_inst_per_shard,
247255
})
248256
.collect_vec(),
249257
_ => panic!("invalid type"),
@@ -279,6 +287,14 @@ impl<'a> ShardContext<'a> {
279287
self.cur_shard_cycle_range.contains(&(cycle as usize))
280288
}
281289

290+
#[inline(always)]
291+
pub fn extract_prev_shard_id(&self, cycle: Cycle) -> usize {
292+
let subcycle_per_insn = Tracer::SUBCYCLES_PER_INSN;
293+
let per_shard_cycles =
294+
(self.expected_inst_per_shard as u64).saturating_mul(subcycle_per_insn);
295+
((cycle.saturating_sub(subcycle_per_insn)) / per_shard_cycles) as usize
296+
}
297+
282298
#[inline(always)]
283299
pub fn aligned_prev_ts(&self, prev_cycle: Cycle) -> Cycle {
284300
let mut ts = prev_cycle.saturating_sub(self.current_shard_offset_cycle());
@@ -311,6 +327,7 @@ impl<'a> ShardContext<'a> {
311327
&& self.is_current_shard_cycle(cycle)
312328
&& !self.is_first_shard()
313329
{
330+
let prev_shard_id = self.extract_prev_shard_id(prev_cycle);
314331
let ram_record = self
315332
.read_thread_based_record_storage
316333
.as_mut()
@@ -326,6 +343,7 @@ impl<'a> ShardContext<'a> {
326343
cycle,
327344
prev_value,
328345
value,
346+
shard_id: prev_shard_id,
329347
},
330348
);
331349
}
@@ -363,6 +381,7 @@ impl<'a> ShardContext<'a> {
363381
cycle,
364382
prev_value,
365383
value,
384+
shard_id: self.shards.shard_id,
366385
},
367386
);
368387
}

ceno_zkvm/src/instructions/riscv/rv32im/mmu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<E: ExtensionField> MmuConfig<'_, E> {
187187
addr,
188188
ram_type: record.ram_type,
189189
value: record.value,
190-
shard: record.prev_cycle, // TODO: extract shard id properly
190+
shard: record.shard_id as u64,
191191
local_clk: local_clk,
192192
global_clk: global_clk,
193193
is_write: is_write,

0 commit comments

Comments
 (0)