Skip to content

Commit

Permalink
chore: remove magic number 4
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-sun committed Jan 11, 2025
1 parent 70dc0b1 commit a9f0454
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions extensions/native/circuit/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use openvm_instructions::{
use openvm_native_compiler::{
FieldArithmeticOpcode, FieldExtensionOpcode, FriOpcode, NativeBranchEqualOpcode,
NativeJalOpcode, NativeLoadStoreOpcode, NativePhantom, BLOCK_LOAD_STORE_OPCODES,
SINGLE_LOAD_STORE_OPCODES,
BLOCK_LOAD_STORE_SIZE, SINGLE_LOAD_STORE_OPCODES,
};
use openvm_poseidon2_air::Poseidon2Config;
use openvm_rv32im_circuit::BranchEqualCoreChip;
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<F: PrimeField32> VmExtension<F> for Native {
.map(|&opcode| VmOpcode::with_default_offset(opcode)),
)?;

let mut block_load_store_chip = NativeLoadStoreChip::<F, 4>::new(
let mut block_load_store_chip = NativeLoadStoreChip::<F, BLOCK_LOAD_STORE_SIZE>::new(
NativeLoadStoreAdapterChip::new(
execution_bus,
program_bus,
Expand Down
41 changes: 22 additions & 19 deletions extensions/native/circuit/src/loadstore/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use openvm_circuit::arch::{
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::instruction::Instruction;
use openvm_native_compiler::{
NativeLoadStoreOpcode, BLOCK_LOAD_STORE_OPCODES, SINGLE_LOAD_STORE_OPCODES,
NativeLoadStoreOpcode, BLOCK_LOAD_STORE_OPCODES, BLOCK_LOAD_STORE_SIZE,
SINGLE_LOAD_STORE_OPCODES,
};
use openvm_stark_backend::{
interaction::InteractionBuilder,
Expand Down Expand Up @@ -87,12 +88,10 @@ where

let expected_opcode = flags
.iter()
.zip(if NUM_CELLS == 1 {
SINGLE_LOAD_STORE_OPCODES
} else if NUM_CELLS == 4 {
BLOCK_LOAD_STORE_OPCODES
} else {
panic!("Unsupported number of cells: {}", NUM_CELLS);
.zip(match NUM_CELLS {
1 => SINGLE_LOAD_STORE_OPCODES,
BLOCK_LOAD_STORE_SIZE => BLOCK_LOAD_STORE_OPCODES,
_ => panic!("Unsupported number of cells: {}", NUM_CELLS),
})
.fold(AB::Expr::ZERO, |acc, (flag, opcode)| {
acc + (*flag).into() * AB::Expr::from_canonical_usize(opcode.as_usize())
Expand Down Expand Up @@ -154,7 +153,8 @@ where
let (pointer_read, data_read) = reads.into();

let data_write = if (NUM_CELLS == 1 && local_opcode == NativeLoadStoreOpcode::HINT_STOREW)
|| (NUM_CELLS == 4 && local_opcode == NativeLoadStoreOpcode::HINT_STOREW4)
|| (NUM_CELLS == BLOCK_LOAD_STORE_SIZE
&& local_opcode == NativeLoadStoreOpcode::HINT_STOREW4)
{
let mut streams = self.streams.get().unwrap().lock().unwrap();
if streams.hint_stream.len() < NUM_CELLS {
Expand Down Expand Up @@ -184,17 +184,20 @@ where

fn generate_trace_row(&self, row_slice: &mut [F], record: Self::Record) {
let cols: &mut NativeLoadStoreCoreCols<_, NUM_CELLS> = row_slice.borrow_mut();
if NUM_CELLS == 1 {
cols.is_loadw = F::from_bool(record.opcode == NativeLoadStoreOpcode::LOADW);
cols.is_storew = F::from_bool(record.opcode == NativeLoadStoreOpcode::STOREW);
cols.is_hint_storew = F::from_bool(record.opcode == NativeLoadStoreOpcode::HINT_STOREW);
} else if NUM_CELLS == 4 {
cols.is_loadw = F::from_bool(record.opcode == NativeLoadStoreOpcode::LOADW4);
cols.is_storew = F::from_bool(record.opcode == NativeLoadStoreOpcode::STOREW4);
cols.is_hint_storew =
F::from_bool(record.opcode == NativeLoadStoreOpcode::HINT_STOREW4);
} else {
panic!("Unsupported number of cells: {}", NUM_CELLS);
match NUM_CELLS {
1 => {
cols.is_loadw = F::from_bool(record.opcode == NativeLoadStoreOpcode::LOADW);
cols.is_storew = F::from_bool(record.opcode == NativeLoadStoreOpcode::STOREW);
cols.is_hint_storew =
F::from_bool(record.opcode == NativeLoadStoreOpcode::HINT_STOREW);
}
BLOCK_LOAD_STORE_SIZE => {
cols.is_loadw = F::from_bool(record.opcode == NativeLoadStoreOpcode::LOADW4);
cols.is_storew = F::from_bool(record.opcode == NativeLoadStoreOpcode::STOREW4);
cols.is_hint_storew =
F::from_bool(record.opcode == NativeLoadStoreOpcode::HINT_STOREW4);
}
_ => panic!("Unsupported number of cells: {}", NUM_CELLS),
}

cols.pointer_read = record.pointer_read;
Expand Down
2 changes: 2 additions & 0 deletions extensions/native/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub const BLOCK_LOAD_STORE_OPCODES: [NativeLoadStoreOpcode; 3] = [
NativeLoadStoreOpcode::HINT_STOREW4,
];

pub const BLOCK_LOAD_STORE_SIZE: usize = 4;

#[derive(Copy, Clone, Debug, UsizeOpcode)]
#[opcode_offset = 0x110]
pub struct NativeBranchEqualOpcode(pub BranchEqualOpcode);
Expand Down

0 comments on commit a9f0454

Please sign in to comment.