openvm_rv32im_circuit/adapters/
branch.rsuse std::{
borrow::{Borrow, BorrowMut},
cell::RefCell,
marker::PhantomData,
};
use openvm_circuit::{
arch::{
AdapterAirContext, AdapterRuntimeContext, BasicAdapterInterface, ExecutionBridge,
ExecutionBus, ExecutionState, ImmInstruction, Result, VmAdapterAir, VmAdapterChip,
VmAdapterInterface,
},
system::{
memory::{
offline_checker::{MemoryBridge, MemoryReadAuxCols},
MemoryAddress, MemoryAuxColsFactory, MemoryController, MemoryControllerRef,
MemoryReadRecord,
},
program::ProgramBus,
},
};
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{instruction::Instruction, riscv::RV32_REGISTER_AS};
use openvm_stark_backend::{
interaction::InteractionBuilder,
p3_air::BaseAir,
p3_field::{AbstractField, Field, PrimeField32},
};
use super::RV32_REGISTER_NUM_LIMBS;
#[derive(Debug)]
pub struct Rv32BranchAdapterChip<F: Field> {
pub air: Rv32BranchAdapterAir,
_marker: PhantomData<F>,
}
impl<F: PrimeField32> Rv32BranchAdapterChip<F> {
pub fn new(
execution_bus: ExecutionBus,
program_bus: ProgramBus,
memory_controller: MemoryControllerRef<F>,
) -> Self {
let memory_controller = RefCell::borrow(&memory_controller);
let memory_bridge = memory_controller.memory_bridge();
Self {
air: Rv32BranchAdapterAir {
execution_bridge: ExecutionBridge::new(execution_bus, program_bus),
memory_bridge,
},
_marker: PhantomData,
}
}
}
#[derive(Debug)]
pub struct Rv32BranchReadRecord<F: Field> {
pub rs1: MemoryReadRecord<F, RV32_REGISTER_NUM_LIMBS>,
pub rs2: MemoryReadRecord<F, RV32_REGISTER_NUM_LIMBS>,
}
#[derive(Debug)]
pub struct Rv32BranchWriteRecord {
pub from_state: ExecutionState<u32>,
}
#[repr(C)]
#[derive(AlignedBorrow)]
pub struct Rv32BranchAdapterCols<T> {
pub from_state: ExecutionState<T>,
pub rs1_ptr: T,
pub rs2_ptr: T,
pub reads_aux: [MemoryReadAuxCols<T, RV32_REGISTER_NUM_LIMBS>; 2],
}
#[derive(Clone, Copy, Debug, derive_new::new)]
pub struct Rv32BranchAdapterAir {
pub(super) execution_bridge: ExecutionBridge,
pub(super) memory_bridge: MemoryBridge,
}
impl<F: Field> BaseAir<F> for Rv32BranchAdapterAir {
fn width(&self) -> usize {
Rv32BranchAdapterCols::<F>::width()
}
}
impl<AB: InteractionBuilder> VmAdapterAir<AB> for Rv32BranchAdapterAir {
type Interface =
BasicAdapterInterface<AB::Expr, ImmInstruction<AB::Expr>, 2, 0, RV32_REGISTER_NUM_LIMBS, 0>;
fn eval(
&self,
builder: &mut AB,
local: &[AB::Var],
ctx: AdapterAirContext<AB::Expr, Self::Interface>,
) {
let local: &Rv32BranchAdapterCols<_> = local.borrow();
let timestamp = local.from_state.timestamp;
let mut timestamp_delta: usize = 0;
let mut timestamp_pp = || {
timestamp_delta += 1;
timestamp + AB::F::from_canonical_usize(timestamp_delta - 1)
};
self.memory_bridge
.read(
MemoryAddress::new(AB::F::from_canonical_u32(RV32_REGISTER_AS), local.rs1_ptr),
ctx.reads[0].clone(),
timestamp_pp(),
&local.reads_aux[0],
)
.eval(builder, ctx.instruction.is_valid.clone());
self.memory_bridge
.read(
MemoryAddress::new(AB::F::from_canonical_u32(RV32_REGISTER_AS), local.rs2_ptr),
ctx.reads[1].clone(),
timestamp_pp(),
&local.reads_aux[1],
)
.eval(builder, ctx.instruction.is_valid.clone());
self.execution_bridge
.execute_and_increment_or_set_pc(
ctx.instruction.opcode,
[
local.rs1_ptr.into(),
local.rs2_ptr.into(),
ctx.instruction.immediate,
AB::Expr::from_canonical_u32(RV32_REGISTER_AS),
AB::Expr::from_canonical_u32(RV32_REGISTER_AS),
],
local.from_state,
AB::F::from_canonical_usize(timestamp_delta),
(4, ctx.to_pc),
)
.eval(builder, ctx.instruction.is_valid);
}
fn get_from_pc(&self, local: &[AB::Var]) -> AB::Var {
let cols: &Rv32BranchAdapterCols<_> = local.borrow();
cols.from_state.pc
}
}
impl<F: PrimeField32> VmAdapterChip<F> for Rv32BranchAdapterChip<F> {
type ReadRecord = Rv32BranchReadRecord<F>;
type WriteRecord = Rv32BranchWriteRecord;
type Air = Rv32BranchAdapterAir;
type Interface = BasicAdapterInterface<F, ImmInstruction<F>, 2, 0, RV32_REGISTER_NUM_LIMBS, 0>;
fn preprocess(
&mut self,
memory: &mut MemoryController<F>,
instruction: &Instruction<F>,
) -> Result<(
<Self::Interface as VmAdapterInterface<F>>::Reads,
Self::ReadRecord,
)> {
let Instruction { a, b, d, e, .. } = *instruction;
debug_assert_eq!(d.as_canonical_u32(), RV32_REGISTER_AS);
debug_assert_eq!(e.as_canonical_u32(), RV32_REGISTER_AS);
let rs1 = memory.read::<RV32_REGISTER_NUM_LIMBS>(d, a);
let rs2 = memory.read::<RV32_REGISTER_NUM_LIMBS>(e, b);
Ok(([rs1.data, rs2.data], Self::ReadRecord { rs1, rs2 }))
}
fn postprocess(
&mut self,
memory: &mut MemoryController<F>,
_instruction: &Instruction<F>,
from_state: ExecutionState<u32>,
output: AdapterRuntimeContext<F, Self::Interface>,
_read_record: &Self::ReadRecord,
) -> Result<(ExecutionState<u32>, Self::WriteRecord)> {
let timestamp_delta = memory.timestamp() - from_state.timestamp;
debug_assert!(
timestamp_delta == 2,
"timestamp delta is {}, expected 2",
timestamp_delta
);
Ok((
ExecutionState {
pc: output.to_pc.unwrap_or(from_state.pc + 4),
timestamp: memory.timestamp(),
},
Self::WriteRecord { from_state },
))
}
fn generate_trace_row(
&self,
row_slice: &mut [F],
read_record: Self::ReadRecord,
write_record: Self::WriteRecord,
aux_cols_factory: &MemoryAuxColsFactory<F>,
) {
let row_slice: &mut Rv32BranchAdapterCols<_> = row_slice.borrow_mut();
row_slice.from_state = write_record.from_state.map(F::from_canonical_u32);
row_slice.rs1_ptr = read_record.rs1.pointer;
row_slice.rs2_ptr = read_record.rs2.pointer;
row_slice.reads_aux = [
aux_cols_factory.make_read_aux_cols(read_record.rs1),
aux_cols_factory.make_read_aux_cols(read_record.rs2),
]
}
fn air(&self) -> &Self::Air {
&self.air
}
}