|
| 1 | +use std::{ |
| 2 | + borrow::{Borrow, BorrowMut}, |
| 3 | + marker::PhantomData, |
| 4 | +}; |
| 5 | + |
| 6 | +use openvm_circuit::{ |
| 7 | + arch::{ |
| 8 | + AdapterAirContext, BasicAdapterInterface, ExecutionBridge, ExecutionBus, ExecutionState, |
| 9 | + MinimalInstruction, Result, VmAdapterAir, VmAdapterInterface, |
| 10 | + }, |
| 11 | + system::{ |
| 12 | + memory::{ |
| 13 | + offline_checker::{MemoryBridge, MemoryReadAuxCols, MemoryWriteAuxCols}, |
| 14 | + MemoryController, OfflineMemory, RecordId, |
| 15 | + }, |
| 16 | + program::ProgramBus, |
| 17 | + }, |
| 18 | +}; |
| 19 | +use openvm_circuit_primitives_derive::AlignedBorrow; |
| 20 | +use openvm_instructions::{instruction::Instruction, program::DEFAULT_PC_STEP}; |
| 21 | +use openvm_stark_backend::{ |
| 22 | + interaction::InteractionBuilder, |
| 23 | + p3_air::BaseAir, |
| 24 | + p3_field::{Field, PrimeField32}, |
| 25 | + rap::ColumnsAir, |
| 26 | +}; |
| 27 | +use serde::{Deserialize, Serialize}; |
| 28 | +use struct_reflection::{StructReflection, StructReflectionHelper}; |
| 29 | + |
| 30 | +use crate::{AdapterRuntimeContextWom, FrameBus, FrameState, VmAdapterChipWom}; |
| 31 | + |
| 32 | +use super::{decompose, RV32_REGISTER_NUM_LIMBS}; |
| 33 | + |
| 34 | +#[derive(Debug)] |
| 35 | +pub struct ConstsAdapterChipWom<F: Field> { |
| 36 | + pub air: ConstsAdapterAirWom, |
| 37 | + _marker: PhantomData<F>, |
| 38 | +} |
| 39 | + |
| 40 | +impl<F: PrimeField32> ConstsAdapterChipWom<F> { |
| 41 | + pub fn new( |
| 42 | + execution_bus: ExecutionBus, |
| 43 | + program_bus: ProgramBus, |
| 44 | + frame_bus: FrameBus, |
| 45 | + memory_bridge: MemoryBridge, |
| 46 | + ) -> Self { |
| 47 | + Self { |
| 48 | + air: ConstsAdapterAirWom { |
| 49 | + _execution_bridge: ExecutionBridge::new(execution_bus, program_bus), |
| 50 | + _frame_bus: frame_bus, |
| 51 | + _memory_bridge: memory_bridge, |
| 52 | + }, |
| 53 | + _marker: PhantomData, |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[repr(C)] |
| 59 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 60 | +pub struct ConstsWriteRecord { |
| 61 | + pub from_state: ExecutionState<u32>, |
| 62 | + pub from_frame: FrameState<u32>, |
| 63 | + pub rd: u32, |
| 64 | + pub rd_id: Option<RecordId>, |
| 65 | +} |
| 66 | + |
| 67 | +#[repr(C)] |
| 68 | +#[derive(Debug, Clone, AlignedBorrow, StructReflection)] |
| 69 | +pub struct ConstsAdapterColsWom<T> { |
| 70 | + pub from_state: ExecutionState<T>, |
| 71 | + pub from_frame: FrameState<T>, |
| 72 | + pub offset_within_frame: T, // rd - the offset within the frame |
| 73 | + pub value_reg_ptr: T, // rs1 pointer (register containing value to copy) |
| 74 | + pub value_reg_aux_cols: MemoryReadAuxCols<T>, |
| 75 | + pub frame_ptr_reg_ptr: T, // rs2 pointer (register containing frame pointer) |
| 76 | + pub frame_ptr_reg_aux_cols: MemoryReadAuxCols<T>, |
| 77 | + pub destination_ptr: T, // Where we write: frame_pointer + offset |
| 78 | + pub destination_aux_cols: MemoryWriteAuxCols<T, RV32_REGISTER_NUM_LIMBS>, |
| 79 | + /// 1 if we need to write to destination |
| 80 | + pub needs_write: T, |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(Clone, Copy, Debug, derive_new::new)] |
| 84 | +pub struct ConstsAdapterAirWom { |
| 85 | + pub(super) _memory_bridge: MemoryBridge, |
| 86 | + pub(super) _execution_bridge: ExecutionBridge, |
| 87 | + pub(super) _frame_bus: FrameBus, |
| 88 | +} |
| 89 | + |
| 90 | +impl<F: Field> BaseAir<F> for ConstsAdapterAirWom { |
| 91 | + fn width(&self) -> usize { |
| 92 | + ConstsAdapterColsWom::<F>::width() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl<F: Field> ColumnsAir<F> for ConstsAdapterAirWom { |
| 97 | + fn columns(&self) -> Option<Vec<String>> { |
| 98 | + ConstsAdapterColsWom::<F>::struct_reflection() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<AB: InteractionBuilder> VmAdapterAir<AB> for ConstsAdapterAirWom { |
| 103 | + type Interface = BasicAdapterInterface<AB::Expr, MinimalInstruction<AB::Expr>, 0, 0, 0, 0>; |
| 104 | + |
| 105 | + fn eval( |
| 106 | + &self, |
| 107 | + _builder: &mut AB, |
| 108 | + _local: &[AB::Var], |
| 109 | + _ctx: AdapterAirContext<AB::Expr, Self::Interface>, |
| 110 | + ) { |
| 111 | + // Empty eval function as requested |
| 112 | + } |
| 113 | + |
| 114 | + fn get_from_pc(&self, local: &[AB::Var]) -> AB::Var { |
| 115 | + let cols: &ConstsAdapterColsWom<_> = local.borrow(); |
| 116 | + cols.from_state.pc |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl<F: PrimeField32> VmAdapterChipWom<F> for ConstsAdapterChipWom<F> { |
| 121 | + type ReadRecord = (); |
| 122 | + type WriteRecord = ConstsWriteRecord; |
| 123 | + type Air = ConstsAdapterAirWom; |
| 124 | + type Interface = BasicAdapterInterface< |
| 125 | + F, |
| 126 | + MinimalInstruction<F>, |
| 127 | + 0, |
| 128 | + 1, |
| 129 | + RV32_REGISTER_NUM_LIMBS, |
| 130 | + RV32_REGISTER_NUM_LIMBS, |
| 131 | + >; |
| 132 | + |
| 133 | + fn preprocess( |
| 134 | + &mut self, |
| 135 | + _memory: &mut MemoryController<F>, |
| 136 | + _fp: u32, |
| 137 | + _instruction: &Instruction<F>, |
| 138 | + ) -> Result<( |
| 139 | + <Self::Interface as VmAdapterInterface<F>>::Reads, |
| 140 | + Self::ReadRecord, |
| 141 | + )> { |
| 142 | + Ok(([], ())) |
| 143 | + } |
| 144 | + |
| 145 | + fn postprocess( |
| 146 | + &mut self, |
| 147 | + memory: &mut MemoryController<F>, |
| 148 | + instruction: &Instruction<F>, |
| 149 | + from_state: ExecutionState<u32>, |
| 150 | + from_frame: FrameState<u32>, |
| 151 | + _output: AdapterRuntimeContextWom<F, Self::Interface>, |
| 152 | + _read_record: &Self::ReadRecord, |
| 153 | + ) -> Result<(ExecutionState<u32>, u32, Self::WriteRecord)> { |
| 154 | + let Instruction { |
| 155 | + a, |
| 156 | + b, |
| 157 | + c, |
| 158 | + f: enabled, |
| 159 | + .. |
| 160 | + } = *instruction; |
| 161 | + |
| 162 | + let mut destination_id = None; |
| 163 | + |
| 164 | + if enabled != F::ZERO { |
| 165 | + let imm_lo = b.as_canonical_u32(); |
| 166 | + let imm_hi = c.as_canonical_u32(); |
| 167 | + assert!( |
| 168 | + imm_lo < (1 << 16) && imm_hi < (1 << 16), |
| 169 | + "Immediate values out of range", |
| 170 | + ); |
| 171 | + let imm = imm_hi << 16 | imm_lo; |
| 172 | + let fp_f = F::from_canonical_u32(from_frame.fp); |
| 173 | + let write_result = memory.write(F::ONE, a + fp_f, decompose(imm)); |
| 174 | + destination_id = Some(write_result.0); |
| 175 | + } |
| 176 | + |
| 177 | + Ok(( |
| 178 | + ExecutionState { |
| 179 | + pc: from_state.pc + DEFAULT_PC_STEP, |
| 180 | + timestamp: memory.timestamp(), |
| 181 | + }, |
| 182 | + from_frame.fp, |
| 183 | + Self::WriteRecord { |
| 184 | + from_state, |
| 185 | + from_frame, |
| 186 | + rd: a.as_canonical_u32(), |
| 187 | + rd_id: destination_id, |
| 188 | + }, |
| 189 | + )) |
| 190 | + } |
| 191 | + |
| 192 | + fn generate_trace_row( |
| 193 | + &self, |
| 194 | + row_slice: &mut [F], |
| 195 | + _read_record: Self::ReadRecord, |
| 196 | + write_record: Self::WriteRecord, |
| 197 | + memory: &OfflineMemory<F>, |
| 198 | + ) { |
| 199 | + let aux_cols_factory = memory.aux_cols_factory(); |
| 200 | + let adapter_cols: &mut ConstsAdapterColsWom<_> = row_slice.borrow_mut(); |
| 201 | + |
| 202 | + adapter_cols.from_state = write_record.from_state.map(F::from_canonical_u32); |
| 203 | + adapter_cols.from_frame = write_record.from_frame.map(F::from_canonical_u32); |
| 204 | + adapter_cols.offset_within_frame = F::from_canonical_u32(write_record.rd); |
| 205 | + |
| 206 | + // Handle destination write |
| 207 | + if let Some(dest_id) = write_record.rd_id { |
| 208 | + let dest_record = memory.record_by_id(dest_id); |
| 209 | + adapter_cols.destination_ptr = dest_record.pointer; |
| 210 | + adapter_cols.needs_write = F::ONE; |
| 211 | + aux_cols_factory |
| 212 | + .generate_write_aux(dest_record, &mut adapter_cols.destination_aux_cols); |
| 213 | + } else { |
| 214 | + adapter_cols.needs_write = F::ZERO; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + fn air(&self) -> &Self::Air { |
| 219 | + &self.air |
| 220 | + } |
| 221 | +} |
0 commit comments