|
| 1 | +use std::{ |
| 2 | + borrow::{Borrow, BorrowMut}, |
| 3 | + marker::PhantomData, |
| 4 | +}; |
| 5 | + |
| 6 | +use openvm_circuit::{ |
| 7 | + arch::{ |
| 8 | + AdapterAirContext, AdapterRuntimeContext, BasicAdapterInterface, ExecutionBridge, |
| 9 | + ExecutionBus, ExecutionState, MinimalInstruction, Result, VmAdapterAir, VmAdapterChip, |
| 10 | + VmAdapterInterface, |
| 11 | + }, |
| 12 | + system::{ |
| 13 | + memory::{ |
| 14 | + offline_checker::{MemoryBridge, MemoryReadOrImmediateAuxCols, MemoryWriteAuxCols}, |
| 15 | + MemoryAddress, MemoryController, OfflineMemory, |
| 16 | + }, |
| 17 | + native_adapter::{NativeReadRecord, NativeWriteRecord}, |
| 18 | + program::ProgramBus, |
| 19 | + }, |
| 20 | +}; |
| 21 | +use openvm_circuit_primitives_derive::AlignedBorrow; |
| 22 | +use openvm_instructions::{instruction::Instruction, program::DEFAULT_PC_STEP}; |
| 23 | +use openvm_native_compiler::conversion::AS; |
| 24 | +use openvm_stark_backend::{ |
| 25 | + interaction::InteractionBuilder, |
| 26 | + p3_air::BaseAir, |
| 27 | + p3_field::{Field, FieldAlgebra, PrimeField32}, |
| 28 | +}; |
| 29 | + |
| 30 | +#[derive(Debug)] |
| 31 | +pub struct AluNativeAdapterChip<F: Field> { |
| 32 | + pub air: AluNativeAdapterAir, |
| 33 | + _marker: PhantomData<F>, |
| 34 | +} |
| 35 | + |
| 36 | +impl<F: PrimeField32> AluNativeAdapterChip<F> { |
| 37 | + pub fn new( |
| 38 | + execution_bus: ExecutionBus, |
| 39 | + program_bus: ProgramBus, |
| 40 | + memory_bridge: MemoryBridge, |
| 41 | + ) -> Self { |
| 42 | + Self { |
| 43 | + air: AluNativeAdapterAir { |
| 44 | + execution_bridge: ExecutionBridge::new(execution_bus, program_bus), |
| 45 | + memory_bridge, |
| 46 | + }, |
| 47 | + _marker: PhantomData, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[repr(C)] |
| 53 | +#[derive(AlignedBorrow)] |
| 54 | +pub struct AluNativeAdapterCols<T> { |
| 55 | + pub from_state: ExecutionState<T>, |
| 56 | + pub a_pointer: T, |
| 57 | + pub b_pointer: T, |
| 58 | + pub c_pointer: T, |
| 59 | + pub e_as: T, |
| 60 | + pub f_as: T, |
| 61 | + pub reads_aux: [MemoryReadOrImmediateAuxCols<T>; 2], |
| 62 | + pub write_aux: MemoryWriteAuxCols<T, 1>, |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Clone, Copy, Debug, derive_new::new)] |
| 66 | +pub struct AluNativeAdapterAir { |
| 67 | + pub(super) execution_bridge: ExecutionBridge, |
| 68 | + pub(super) memory_bridge: MemoryBridge, |
| 69 | +} |
| 70 | + |
| 71 | +impl<F: Field> BaseAir<F> for AluNativeAdapterAir { |
| 72 | + fn width(&self) -> usize { |
| 73 | + AluNativeAdapterCols::<F>::width() |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<AB: InteractionBuilder> VmAdapterAir<AB> for AluNativeAdapterAir { |
| 78 | + type Interface = BasicAdapterInterface<AB::Expr, MinimalInstruction<AB::Expr>, 2, 1, 1, 1>; |
| 79 | + |
| 80 | + fn eval( |
| 81 | + &self, |
| 82 | + builder: &mut AB, |
| 83 | + local: &[AB::Var], |
| 84 | + ctx: AdapterAirContext<AB::Expr, Self::Interface>, |
| 85 | + ) { |
| 86 | + let cols: &AluNativeAdapterCols<_> = local.borrow(); |
| 87 | + let timestamp = cols.from_state.timestamp; |
| 88 | + let mut timestamp_delta = 0usize; |
| 89 | + let mut timestamp_pp = || { |
| 90 | + timestamp_delta += 1; |
| 91 | + timestamp + AB::F::from_canonical_usize(timestamp_delta - 1) |
| 92 | + }; |
| 93 | + |
| 94 | + let native_as = AB::Expr::from_canonical_u32(AS::Native as u32); |
| 95 | + |
| 96 | + self.memory_bridge |
| 97 | + .read_or_immediate( |
| 98 | + MemoryAddress::new(cols.e_as, cols.b_pointer), |
| 99 | + ctx.reads[0][0].clone(), |
| 100 | + timestamp_pp(), |
| 101 | + &cols.reads_aux[0], |
| 102 | + ) |
| 103 | + .eval(builder, ctx.instruction.is_valid.clone()); |
| 104 | + |
| 105 | + self.memory_bridge |
| 106 | + .read_or_immediate( |
| 107 | + MemoryAddress::new(cols.f_as, cols.c_pointer), |
| 108 | + ctx.reads[1][0].clone(), |
| 109 | + timestamp_pp(), |
| 110 | + &cols.reads_aux[1], |
| 111 | + ) |
| 112 | + .eval(builder, ctx.instruction.is_valid.clone()); |
| 113 | + |
| 114 | + self.memory_bridge |
| 115 | + .write( |
| 116 | + MemoryAddress::new(native_as.clone(), cols.a_pointer), |
| 117 | + ctx.writes[0].clone(), |
| 118 | + timestamp_pp(), |
| 119 | + &cols.write_aux, |
| 120 | + ) |
| 121 | + .eval(builder, ctx.instruction.is_valid.clone()); |
| 122 | + |
| 123 | + self.execution_bridge |
| 124 | + .execute_and_increment_or_set_pc( |
| 125 | + ctx.instruction.opcode, |
| 126 | + [ |
| 127 | + cols.a_pointer.into(), |
| 128 | + cols.b_pointer.into(), |
| 129 | + cols.c_pointer.into(), |
| 130 | + native_as.clone(), |
| 131 | + cols.e_as.into(), |
| 132 | + cols.f_as.into(), |
| 133 | + ], |
| 134 | + cols.from_state, |
| 135 | + AB::F::from_canonical_usize(timestamp_delta), |
| 136 | + (DEFAULT_PC_STEP, ctx.to_pc), |
| 137 | + ) |
| 138 | + .eval(builder, ctx.instruction.is_valid); |
| 139 | + } |
| 140 | + |
| 141 | + fn get_from_pc(&self, local: &[AB::Var]) -> AB::Var { |
| 142 | + let cols: &AluNativeAdapterCols<_> = local.borrow(); |
| 143 | + cols.from_state.pc |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl<F: PrimeField32> VmAdapterChip<F> for AluNativeAdapterChip<F> { |
| 148 | + type ReadRecord = NativeReadRecord<F, 2>; |
| 149 | + type WriteRecord = NativeWriteRecord<F, 1>; |
| 150 | + type Air = AluNativeAdapterAir; |
| 151 | + type Interface = BasicAdapterInterface<F, MinimalInstruction<F>, 2, 1, 1, 1>; |
| 152 | + |
| 153 | + fn preprocess( |
| 154 | + &mut self, |
| 155 | + memory: &mut MemoryController<F>, |
| 156 | + instruction: &Instruction<F>, |
| 157 | + ) -> Result<( |
| 158 | + <Self::Interface as VmAdapterInterface<F>>::Reads, |
| 159 | + Self::ReadRecord, |
| 160 | + )> { |
| 161 | + let Instruction { b, c, e, f, .. } = *instruction; |
| 162 | + |
| 163 | + let reads = vec![memory.read::<1>(e, b), memory.read::<1>(f, c)]; |
| 164 | + let i_reads: [_; 2] = std::array::from_fn(|i| reads[i].1); |
| 165 | + |
| 166 | + Ok(( |
| 167 | + i_reads, |
| 168 | + Self::ReadRecord { |
| 169 | + reads: reads.try_into().unwrap(), |
| 170 | + }, |
| 171 | + )) |
| 172 | + } |
| 173 | + |
| 174 | + fn postprocess( |
| 175 | + &mut self, |
| 176 | + memory: &mut MemoryController<F>, |
| 177 | + _instruction: &Instruction<F>, |
| 178 | + from_state: ExecutionState<u32>, |
| 179 | + output: AdapterRuntimeContext<F, Self::Interface>, |
| 180 | + _read_record: &Self::ReadRecord, |
| 181 | + ) -> Result<(ExecutionState<u32>, Self::WriteRecord)> { |
| 182 | + let Instruction { a, .. } = *_instruction; |
| 183 | + let writes = vec![memory.write( |
| 184 | + F::from_canonical_u32(AS::Native as u32), |
| 185 | + a, |
| 186 | + output.writes[0], |
| 187 | + )]; |
| 188 | + |
| 189 | + Ok(( |
| 190 | + ExecutionState { |
| 191 | + pc: output.to_pc.unwrap_or(from_state.pc + DEFAULT_PC_STEP), |
| 192 | + timestamp: memory.timestamp(), |
| 193 | + }, |
| 194 | + Self::WriteRecord { |
| 195 | + from_state, |
| 196 | + writes: writes.try_into().unwrap(), |
| 197 | + }, |
| 198 | + )) |
| 199 | + } |
| 200 | + |
| 201 | + fn generate_trace_row( |
| 202 | + &self, |
| 203 | + row_slice: &mut [F], |
| 204 | + read_record: Self::ReadRecord, |
| 205 | + write_record: Self::WriteRecord, |
| 206 | + memory: &OfflineMemory<F>, |
| 207 | + ) { |
| 208 | + let row_slice: &mut AluNativeAdapterCols<_> = row_slice.borrow_mut(); |
| 209 | + let aux_cols_factory = memory.aux_cols_factory(); |
| 210 | + |
| 211 | + row_slice.from_state = write_record.from_state.map(F::from_canonical_u32); |
| 212 | + |
| 213 | + row_slice.a_pointer = memory.record_by_id(write_record.writes[0].0).pointer; |
| 214 | + row_slice.b_pointer = memory.record_by_id(read_record.reads[0].0).pointer; |
| 215 | + row_slice.c_pointer = memory.record_by_id(read_record.reads[1].0).pointer; |
| 216 | + row_slice.e_as = memory.record_by_id(read_record.reads[0].0).address_space; |
| 217 | + row_slice.f_as = memory.record_by_id(read_record.reads[1].0).address_space; |
| 218 | + |
| 219 | + for (i, x) in read_record.reads.iter().enumerate() { |
| 220 | + let read = memory.record_by_id(x.0); |
| 221 | + aux_cols_factory.generate_read_or_immediate_aux(read, &mut row_slice.reads_aux[i]); |
| 222 | + } |
| 223 | + |
| 224 | + let write = memory.record_by_id(write_record.writes[0].0); |
| 225 | + aux_cols_factory.generate_write_aux(write, &mut row_slice.write_aux); |
| 226 | + } |
| 227 | + |
| 228 | + fn air(&self) -> &Self::Air { |
| 229 | + &self.air |
| 230 | + } |
| 231 | +} |
0 commit comments