Skip to content

Commit

Permalink
chore: replace 4 with DEFAULT_PC_STEP (#1140)
Browse files Browse the repository at this point in the history
* chore: replace 4 with DEFAULT_PC_STEP in rv32im

* fix: import

* chore: replace 4 with DEFAULT_PC_STEP in rv32-adapters

* chore: replace 4 with DEFAULT_PC_STEP in rv32-adapters
  • Loading branch information
yi-sun authored Dec 26, 2024
1 parent 74fac04 commit 9a7fc45
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 40 deletions.
5 changes: 3 additions & 2 deletions extensions/rv32-adapters/src/eq_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use openvm_circuit_primitives::bitwise_op_lookup::{
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{
instruction::Instruction,
program::DEFAULT_PC_STEP,
riscv::{RV32_MEMORY_AS, RV32_REGISTER_AS},
};
use openvm_rv32im_circuit::adapters::{
Expand Down Expand Up @@ -215,7 +216,7 @@ impl<
],
cols.from_state,
AB::F::from_canonical_usize(timestamp_delta),
(4, ctx.to_pc),
(DEFAULT_PC_STEP, ctx.to_pc),
)
.eval(builder, ctx.instruction.is_valid.clone());
}
Expand Down Expand Up @@ -377,7 +378,7 @@ impl<

Ok((
ExecutionState {
pc: from_state.pc + 4,
pc: from_state.pc + DEFAULT_PC_STEP,
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state, rd },
Expand Down
3 changes: 2 additions & 1 deletion extensions/rv32-adapters/src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use openvm_circuit_primitives::bitwise_op_lookup::{
};
use openvm_instructions::{
instruction::Instruction,
program::DEFAULT_PC_STEP,
riscv::{RV32_CELL_BITS, RV32_MEMORY_AS, RV32_REGISTER_AS, RV32_REGISTER_NUM_LIMBS},
};
use openvm_rv32im_circuit::adapters::read_rv32_register;
Expand Down Expand Up @@ -208,7 +209,7 @@ impl<F: PrimeField32, const NUM_READS: usize, const READ_SIZE: usize, const WRIT

Ok((
ExecutionState {
pc: from_state.pc + 4,
pc: from_state.pc + DEFAULT_PC_STEP,
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state, writes },
Expand Down
5 changes: 3 additions & 2 deletions extensions/rv32-adapters/src/heap_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use openvm_circuit_primitives::bitwise_op_lookup::{
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{
instruction::Instruction,
program::DEFAULT_PC_STEP,
riscv::{RV32_MEMORY_AS, RV32_REGISTER_AS},
};
use openvm_rv32im_circuit::adapters::{
Expand Down Expand Up @@ -156,7 +157,7 @@ impl<AB: InteractionBuilder, const NUM_READS: usize, const READ_SIZE: usize> VmA
],
cols.from_state,
AB::F::from_canonical_usize(timestamp_delta),
(4, ctx.to_pc),
(DEFAULT_PC_STEP, ctx.to_pc),
)
.eval(builder, ctx.instruction.is_valid);
}
Expand Down Expand Up @@ -267,7 +268,7 @@ impl<F: PrimeField32, const NUM_READS: usize, const READ_SIZE: usize> VmAdapterC

Ok((
ExecutionState {
pc: output.to_pc.unwrap_or(from_state.pc + 4),
pc: output.to_pc.unwrap_or(from_state.pc + DEFAULT_PC_STEP),
timestamp: memory.timestamp(),
},
from_state,
Expand Down
5 changes: 3 additions & 2 deletions extensions/rv32-adapters/src/vec_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use openvm_circuit_primitives::bitwise_op_lookup::{
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{
instruction::Instruction,
program::DEFAULT_PC_STEP,
riscv::{RV32_MEMORY_AS, RV32_REGISTER_AS},
};
use openvm_rv32im_circuit::adapters::{
Expand Down Expand Up @@ -322,7 +323,7 @@ impl<
],
cols.from_state,
AB::F::from_canonical_usize(timestamp_delta),
(4, ctx.to_pc),
(DEFAULT_PC_STEP, ctx.to_pc),
)
.eval(builder, ctx.instruction.is_valid.clone());
}
Expand Down Expand Up @@ -437,7 +438,7 @@ impl<

Ok((
ExecutionState {
pc: from_state.pc + 4,
pc: from_state.pc + DEFAULT_PC_STEP,
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state, writes },
Expand Down
5 changes: 3 additions & 2 deletions extensions/rv32-adapters/src/vec_heap_two_reads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use openvm_circuit_primitives::bitwise_op_lookup::{
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{
instruction::Instruction,
program::DEFAULT_PC_STEP,
riscv::{RV32_MEMORY_AS, RV32_REGISTER_AS},
};
use openvm_rv32im_circuit::adapters::{
Expand Down Expand Up @@ -353,7 +354,7 @@ impl<
],
cols.from_state,
AB::F::from_canonical_usize(timestamp_delta),
(4, ctx.to_pc),
(DEFAULT_PC_STEP, ctx.to_pc),
)
.eval(builder, ctx.instruction.is_valid.clone());
}
Expand Down Expand Up @@ -470,7 +471,7 @@ impl<

Ok((
ExecutionState {
pc: from_state.pc + 4,
pc: from_state.pc + DEFAULT_PC_STEP,
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state, writes },
Expand Down
5 changes: 3 additions & 2 deletions extensions/rv32im/circuit/src/adapters/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use openvm_circuit_primitives::utils::not;
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{
instruction::Instruction,
program::DEFAULT_PC_STEP,
riscv::{RV32_IMM_AS, RV32_REGISTER_AS},
};
use openvm_stark_backend::{
Expand Down Expand Up @@ -184,7 +185,7 @@ impl<AB: InteractionBuilder> VmAdapterAir<AB> for Rv32BaseAluAdapterAir {
],
local.from_state,
AB::F::from_canonical_usize(timestamp_delta),
(4, ctx.to_pc),
(DEFAULT_PC_STEP, ctx.to_pc),
)
.eval(builder, ctx.instruction.is_valid);
}
Expand Down Expand Up @@ -267,7 +268,7 @@ impl<F: PrimeField32> VmAdapterChip<F> for Rv32BaseAluAdapterChip<F> {

Ok((
ExecutionState {
pc: from_state.pc + 4,
pc: from_state.pc + DEFAULT_PC_STEP,
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state, rd },
Expand Down
8 changes: 5 additions & 3 deletions extensions/rv32im/circuit/src/adapters/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use openvm_circuit::{
},
};
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{instruction::Instruction, riscv::RV32_REGISTER_AS};
use openvm_instructions::{
instruction::Instruction, program::DEFAULT_PC_STEP, riscv::RV32_REGISTER_AS,
};
use openvm_stark_backend::{
interaction::InteractionBuilder,
p3_air::BaseAir,
Expand Down Expand Up @@ -137,7 +139,7 @@ impl<AB: InteractionBuilder> VmAdapterAir<AB> for Rv32BranchAdapterAir {
],
local.from_state,
AB::F::from_canonical_usize(timestamp_delta),
(4, ctx.to_pc),
(DEFAULT_PC_STEP, ctx.to_pc),
)
.eval(builder, ctx.instruction.is_valid);
}
Expand Down Expand Up @@ -190,7 +192,7 @@ impl<F: PrimeField32> VmAdapterChip<F> for Rv32BranchAdapterChip<F> {

Ok((
ExecutionState {
pc: output.to_pc.unwrap_or(from_state.pc + 4),
pc: output.to_pc.unwrap_or(from_state.pc + DEFAULT_PC_STEP),
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state },
Expand Down
5 changes: 3 additions & 2 deletions extensions/rv32im/circuit/src/adapters/hintstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use openvm_circuit_primitives::var_range::{VariableRangeCheckerBus, VariableRang
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{
instruction::Instruction,
program::DEFAULT_PC_STEP,
riscv::{RV32_MEMORY_AS, RV32_REGISTER_AS},
};
use openvm_stark_backend::{
Expand Down Expand Up @@ -200,7 +201,7 @@ impl<AB: InteractionBuilder> VmAdapterAir<AB> for Rv32HintStoreAdapterAir {

let to_pc = ctx
.to_pc
.unwrap_or(local_cols.from_state.pc + AB::F::from_canonical_u32(4));
.unwrap_or(local_cols.from_state.pc + AB::F::from_canonical_u32(DEFAULT_PC_STEP));
self.execution_bridge
.execute(
ctx.instruction.opcode,
Expand Down Expand Up @@ -295,7 +296,7 @@ impl<F: PrimeField32> VmAdapterChip<F> for Rv32HintStoreAdapterChip<F> {

Ok((
ExecutionState {
pc: output.to_pc.unwrap_or(from_state.pc + 4),
pc: output.to_pc.unwrap_or(from_state.pc + DEFAULT_PC_STEP),
timestamp: memory.timestamp(),
},
Self::WriteRecord {
Expand Down
8 changes: 5 additions & 3 deletions extensions/rv32im/circuit/src/adapters/jalr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use openvm_circuit::{
};
use openvm_circuit_primitives::utils::not;
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{instruction::Instruction, riscv::RV32_REGISTER_AS};
use openvm_instructions::{
instruction::Instruction, program::DEFAULT_PC_STEP, riscv::RV32_REGISTER_AS,
};
use openvm_stark_backend::{
interaction::InteractionBuilder,
p3_air::{AirBuilder, BaseAir},
Expand Down Expand Up @@ -146,7 +148,7 @@ impl<AB: InteractionBuilder> VmAdapterAir<AB> for Rv32JalrAdapterAir {

let to_pc = ctx
.to_pc
.unwrap_or(local_cols.from_state.pc + AB::F::from_canonical_u32(4));
.unwrap_or(local_cols.from_state.pc + AB::F::from_canonical_u32(DEFAULT_PC_STEP));

// regardless of `needs_write`, must always execute instruction when `is_valid`.
self.execution_bridge
Expand Down Expand Up @@ -223,7 +225,7 @@ impl<F: PrimeField32> VmAdapterChip<F> for Rv32JalrAdapterChip<F> {

Ok((
ExecutionState {
pc: output.to_pc.unwrap_or(from_state.pc + 4),
pc: output.to_pc.unwrap_or(from_state.pc + DEFAULT_PC_STEP),
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state, rd },
Expand Down
5 changes: 3 additions & 2 deletions extensions/rv32im/circuit/src/adapters/loadstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use openvm_circuit_primitives::{
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{
instruction::Instruction,
program::DEFAULT_PC_STEP,
riscv::{RV32_IMM_AS, RV32_REGISTER_AS},
UsizeOpcode,
};
Expand Down Expand Up @@ -301,7 +302,7 @@ impl<AB: InteractionBuilder> VmAdapterAir<AB> for Rv32LoadStoreAdapterAir {

let to_pc = ctx
.to_pc
.unwrap_or(local_cols.from_state.pc + AB::F::from_canonical_u32(4));
.unwrap_or(local_cols.from_state.pc + AB::F::from_canonical_u32(DEFAULT_PC_STEP));
self.execution_bridge
.execute(
ctx.instruction.opcode,
Expand Down Expand Up @@ -446,7 +447,7 @@ impl<F: PrimeField32> VmAdapterChip<F> for Rv32LoadStoreAdapterChip<F> {

Ok((
ExecutionState {
pc: output.to_pc.unwrap_or(from_state.pc + 4),
pc: output.to_pc.unwrap_or(from_state.pc + DEFAULT_PC_STEP),
timestamp: memory.timestamp(),
},
Self::WriteRecord {
Expand Down
8 changes: 5 additions & 3 deletions extensions/rv32im/circuit/src/adapters/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use openvm_circuit::{
},
};
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{instruction::Instruction, riscv::RV32_REGISTER_AS};
use openvm_instructions::{
instruction::Instruction, program::DEFAULT_PC_STEP, riscv::RV32_REGISTER_AS,
};
use openvm_stark_backend::{
interaction::InteractionBuilder,
p3_air::BaseAir,
Expand Down Expand Up @@ -154,7 +156,7 @@ impl<AB: InteractionBuilder> VmAdapterAir<AB> for Rv32MultAdapterAir {
],
local.from_state,
AB::F::from_canonical_usize(timestamp_delta),
(4, ctx.to_pc),
(DEFAULT_PC_STEP, ctx.to_pc),
)
.eval(builder, ctx.instruction.is_valid);
}
Expand Down Expand Up @@ -216,7 +218,7 @@ impl<F: PrimeField32> VmAdapterChip<F> for Rv32MultAdapterChip<F> {

Ok((
ExecutionState {
pc: from_state.pc + 4,
pc: from_state.pc + DEFAULT_PC_STEP,
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state, rd },
Expand Down
10 changes: 6 additions & 4 deletions extensions/rv32im/circuit/src/adapters/rdwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use openvm_circuit::{
};
use openvm_circuit_primitives::utils::not;
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{instruction::Instruction, riscv::RV32_REGISTER_AS};
use openvm_instructions::{
instruction::Instruction, program::DEFAULT_PC_STEP, riscv::RV32_REGISTER_AS,
};
use openvm_stark_backend::{
interaction::InteractionBuilder,
p3_air::{AirBuilder, BaseAir},
Expand Down Expand Up @@ -167,7 +169,7 @@ impl Rv32RdWriteAdapterAir {

let to_pc = ctx
.to_pc
.unwrap_or(local_cols.from_state.pc + AB::F::from_canonical_u32(4));
.unwrap_or(local_cols.from_state.pc + AB::F::from_canonical_u32(DEFAULT_PC_STEP));
// regardless of `needs_write`, must always execute instruction when `is_valid`.
self.execution_bridge
.execute(
Expand Down Expand Up @@ -274,7 +276,7 @@ impl<F: PrimeField32> VmAdapterChip<F> for Rv32RdWriteAdapterChip<F> {

Ok((
ExecutionState {
pc: output.to_pc.unwrap_or(from_state.pc + 4),
pc: output.to_pc.unwrap_or(from_state.pc + DEFAULT_PC_STEP),
timestamp: memory.timestamp(),
},
Self::WriteRecord {
Expand Down Expand Up @@ -338,7 +340,7 @@ impl<F: PrimeField32> VmAdapterChip<F> for Rv32CondRdWriteAdapterChip<F> {

Ok((
ExecutionState {
pc: output.to_pc.unwrap_or(from_state.pc + 4),
pc: output.to_pc.unwrap_or(from_state.pc + DEFAULT_PC_STEP),
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state, rd },
Expand Down
4 changes: 2 additions & 2 deletions extensions/rv32im/circuit/src/branch_lt/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use openvm_circuit_primitives::{
utils::not,
};
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{instruction::Instruction, UsizeOpcode};
use openvm_instructions::{instruction::Instruction, program::DEFAULT_PC_STEP, UsizeOpcode};
use openvm_rv32im_transpiler::BranchLessThanOpcode;
use openvm_stark_backend::{
interaction::InteractionBuilder,
Expand Down Expand Up @@ -157,7 +157,7 @@ where

let to_pc = from_pc
+ cols.cmp_result * cols.imm
+ not(cols.cmp_result) * AB::Expr::from_canonical_u8(4);
+ not(cols.cmp_result) * AB::Expr::from_canonical_u32(DEFAULT_PC_STEP);

AdapterAirContext {
to_pc: Some(to_pc),
Expand Down
21 changes: 14 additions & 7 deletions extensions/rv32im/circuit/src/jal_lui/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ use openvm_circuit_primitives::bitwise_op_lookup::{
BitwiseOperationLookupBus, BitwiseOperationLookupChip,
};
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{instruction::Instruction, program::PC_BITS, UsizeOpcode};
use openvm_instructions::{
instruction::Instruction,
program::{DEFAULT_PC_STEP, PC_BITS},
UsizeOpcode,
};
use openvm_rv32im_transpiler::Rv32JalLuiOpcode::{self, *};
use openvm_stark_backend::{
interaction::InteractionBuilder,
Expand Down Expand Up @@ -103,11 +107,12 @@ where
);

let intermed_val = rd[0] + intermed_val * AB::Expr::from_canonical_u32(1 << RV32_CELL_BITS);
builder
.when(is_jal)
.assert_eq(intermed_val, from_pc + AB::F::from_canonical_u32(4));
builder.when(is_jal).assert_eq(
intermed_val,
from_pc + AB::F::from_canonical_u32(DEFAULT_PC_STEP),
);

let to_pc = from_pc + is_lui * AB::F::from_canonical_u32(4) + is_jal * imm;
let to_pc = from_pc + is_lui * AB::F::from_canonical_u32(DEFAULT_PC_STEP) + is_jal * imm;

let expected_opcode = is_lui * AB::F::from_canonical_u32(LUI as u32)
+ is_jal * AB::F::from_canonical_u32(JAL as u32)
Expand Down Expand Up @@ -243,7 +248,9 @@ pub(super) fn run_jal_lui(
) -> (u32, [u32; RV32_REGISTER_NUM_LIMBS]) {
match opcode {
JAL => {
let rd_data = array::from_fn(|i| ((pc + 4) >> (8 * i)) & ((1 << RV32_CELL_BITS) - 1));
let rd_data = array::from_fn(|i| {
((pc + DEFAULT_PC_STEP) >> (8 * i)) & ((1 << RV32_CELL_BITS) - 1)
});
let next_pc = pc as i32 + imm;
assert!(next_pc >= 0);
(next_pc as u32, rd_data)
Expand All @@ -253,7 +260,7 @@ pub(super) fn run_jal_lui(
let rd = imm << 12;
let rd_data =
array::from_fn(|i| (rd >> (RV32_CELL_BITS * i)) & ((1 << RV32_CELL_BITS) - 1));
(pc + 4, rd_data)
(pc + DEFAULT_PC_STEP, rd_data)
}
}
}
Loading

0 comments on commit 9a7fc45

Please sign in to comment.