Skip to content

Commit 1646da3

Browse files
committed
fix proper ts and pc counting
1 parent abd650b commit 1646da3

File tree

4 files changed

+54
-37
lines changed

4 files changed

+54
-37
lines changed

ceno_zkvm/src/chip_handler.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ pub trait RegisterChipOperations<E: ExtensionField> {
3030
fn register_read(
3131
&mut self,
3232
register_id: &WitIn,
33-
prev_ts: &TSUInt,
34-
ts: &TSUInt,
33+
prev_ts: &mut TSUInt,
34+
ts: &mut TSUInt,
3535
values: &UInt64,
36-
) -> Result<(), ZKVMError>;
36+
) -> Result<TSUInt, ZKVMError>;
3737

3838
fn register_write(
3939
&mut self,
4040
register_id: &WitIn,
41-
prev_ts: &TSUInt,
42-
ts: &TSUInt,
41+
prev_ts: &mut TSUInt,
42+
ts: &mut TSUInt,
4343
prev_values: &UInt64,
4444
values: &UInt64,
45-
) -> Result<(), ZKVMError>;
45+
) -> Result<TSUInt, ZKVMError>;
4646
}

ceno_zkvm/src/chip_handler/register.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ff_ext::ExtensionField;
22
use singer_utils::structs::RAMType;
33

44
use crate::{
5-
circuit_builder::CircuitBuilder,
5+
circuit_builder::{self, CircuitBuilder},
66
error::ZKVMError,
77
expression::{Expression, ToExpr, WitIn},
88
structs::{TSUInt, UInt64},
@@ -14,10 +14,10 @@ impl<E: ExtensionField> RegisterChipOperations<E> for CircuitBuilder<E> {
1414
fn register_read(
1515
&mut self,
1616
register_id: &WitIn,
17-
prev_ts: &TSUInt,
18-
ts: &TSUInt,
17+
prev_ts: &mut TSUInt,
18+
ts: &mut TSUInt,
1919
values: &UInt64,
20-
) -> Result<(), ZKVMError> {
20+
) -> Result<TSUInt, ZKVMError> {
2121
// READ (a, v, t)
2222
let read_record = self.rlc_chip_record(
2323
[
@@ -44,17 +44,23 @@ impl<E: ExtensionField> RegisterChipOperations<E> for CircuitBuilder<E> {
4444
);
4545
self.read_record(read_record)?;
4646
self.write_record(write_record)?;
47-
Ok(())
47+
48+
// assert prev_ts < current_ts
49+
let is_lt = prev_ts.lt(self, ts)?;
50+
self.require_one(is_lt)?;
51+
let next_ts = ts.add_const(self, 1.into())?;
52+
53+
Ok(next_ts)
4854
}
4955

5056
fn register_write(
5157
&mut self,
5258
register_id: &WitIn,
53-
prev_ts: &TSUInt,
54-
ts: &TSUInt,
59+
prev_ts: &mut TSUInt,
60+
ts: &mut TSUInt,
5561
prev_values: &UInt64,
5662
values: &UInt64,
57-
) -> Result<(), ZKVMError> {
63+
) -> Result<TSUInt, ZKVMError> {
5864
// READ (a, v, t)
5965
let read_record = self.rlc_chip_record(
6066
[
@@ -81,6 +87,12 @@ impl<E: ExtensionField> RegisterChipOperations<E> for CircuitBuilder<E> {
8187
);
8288
self.read_record(read_record)?;
8389
self.write_record(write_record)?;
84-
Ok(())
90+
91+
// assert prev_ts < current_ts
92+
let is_lt = prev_ts.lt(self, ts)?;
93+
self.require_one(is_lt)?;
94+
let next_ts = ts.add_const(self, 1.into())?;
95+
96+
Ok(next_ts)
8597
}
8698
}

ceno_zkvm/src/instructions/riscv/add.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::{
1313
structs::{PCUInt, TSUInt, UInt64},
1414
};
1515

16+
use super::constants::RISCV64_PC_STEP_SIZE;
17+
1618
pub struct AddInstruction;
1719

1820
pub struct InstructionConfig<E: ExtensionField> {
@@ -40,16 +42,13 @@ impl<E: ExtensionField> Instruction<E> for AddInstruction {
4042
circuit_builder: &mut CircuitBuilder<E>,
4143
) -> Result<InstructionConfig<E>, ZKVMError> {
4244
let pc = PCUInt::new(circuit_builder);
43-
let memory_ts = TSUInt::new(circuit_builder);
45+
let mut memory_ts = TSUInt::new(circuit_builder);
4446
let clk = circuit_builder.create_witin();
4547

4648
// state in
4749
circuit_builder.state_in(&pc, &memory_ts, clk.expr())?;
4850

49-
let next_pc = pc.add_const(circuit_builder, 1.into())?;
50-
let next_memory_ts = memory_ts.add_const(circuit_builder, 1.into())?;
51-
52-
circuit_builder.state_out(&next_pc, &next_memory_ts, clk.expr() + 1.into())?;
51+
let next_pc = pc.add_const(circuit_builder, RISCV64_PC_STEP_SIZE.into())?;
5352

5453
// Execution result = addend0 + addend1, with carry.
5554
let prev_rd_memory_value = UInt64::new(circuit_builder);
@@ -60,38 +59,43 @@ impl<E: ExtensionField> Instruction<E> for AddInstruction {
6059
let computed_outcome = addend_0.add(circuit_builder, &addend_1)?;
6160
outcome.eq(circuit_builder, &computed_outcome)?;
6261

63-
// TODO rs1_id, rs2_id, rd_id should be byte code lookup
62+
// TODO rs1_id, rs2_id, rd_id should be bytecode lookup
6463
let rs1_id = circuit_builder.create_witin();
6564
let rs2_id = circuit_builder.create_witin();
6665
let rd_id = circuit_builder.create_witin();
6766
circuit_builder.assert_u5(rs1_id.expr())?;
6867
circuit_builder.assert_u5(rs2_id.expr())?;
6968
circuit_builder.assert_u5(rd_id.expr())?;
70-
let prev_rs1_memory_ts = TSUInt::new(circuit_builder);
71-
let prev_rs2_memory_ts = TSUInt::new(circuit_builder);
72-
let prev_rd_memory_ts = TSUInt::new(circuit_builder);
73-
74-
let is_lt_0 = prev_rs1_memory_ts.lt(circuit_builder, &memory_ts)?;
75-
let is_lt_1 = prev_rs2_memory_ts.lt(circuit_builder, &memory_ts)?;
76-
let is_lt_2 = prev_rd_memory_ts.lt(circuit_builder, &memory_ts)?;
7769

78-
// less than = true
79-
circuit_builder.require_one(is_lt_0)?;
80-
circuit_builder.require_one(is_lt_1)?;
81-
circuit_builder.require_one(is_lt_2)?;
70+
let mut prev_rs1_memory_ts = TSUInt::new(circuit_builder);
71+
let mut prev_rs2_memory_ts = TSUInt::new(circuit_builder);
72+
let mut prev_rd_memory_ts = TSUInt::new(circuit_builder);
8273

83-
circuit_builder.register_read(&rs1_id, &prev_rs1_memory_ts, &memory_ts, &addend_0)?;
74+
let mut memory_ts = circuit_builder.register_read(
75+
&rs1_id,
76+
&mut prev_rs1_memory_ts,
77+
&mut memory_ts,
78+
&addend_0,
79+
)?;
8480

85-
circuit_builder.register_read(&rs2_id, &prev_rs2_memory_ts, &memory_ts, &addend_1)?;
81+
let mut memory_ts = circuit_builder.register_read(
82+
&rs2_id,
83+
&mut prev_rs2_memory_ts,
84+
&mut memory_ts,
85+
&addend_1,
86+
)?;
8687

87-
circuit_builder.register_write(
88+
let memory_ts = circuit_builder.register_write(
8889
&rd_id,
89-
&prev_rd_memory_ts,
90-
&memory_ts,
90+
&mut prev_rd_memory_ts,
91+
&mut memory_ts,
9192
&prev_rd_memory_value,
9293
&computed_outcome,
9394
)?;
9495

96+
let next_memory_ts = memory_ts.add_const(circuit_builder, 1.into())?;
97+
circuit_builder.state_out(&next_pc, &next_memory_ts, clk.expr() + 1.into())?;
98+
9599
Ok(InstructionConfig {
96100
pc,
97101
memory_ts,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod add;
2+
mod constants;

0 commit comments

Comments
 (0)