openvm_circuit/system/program/
air.rs

1use openvm_circuit_primitives::{ColumnsAir, StructReflection, StructReflectionHelper};
2use openvm_circuit_primitives_derive::AlignedBorrow;
3use openvm_stark_backend::{
4    air_builders::PartitionedAirBuilder,
5    interaction::InteractionBuilder,
6    p3_air::{Air, BaseAir},
7    p3_field::Field,
8    p3_matrix::Matrix,
9    BaseAirWithPublicValues, PartitionedBaseAir,
10};
11
12use super::ProgramBus;
13
14#[derive(Copy, Clone, Debug, AlignedBorrow, StructReflection, PartialEq, Eq)]
15#[repr(C)]
16pub struct ProgramCols<T> {
17    pub exec: ProgramExecutionCols<T>,
18    pub exec_freq: T,
19}
20
21#[derive(Copy, Clone, Debug, AlignedBorrow, StructReflection, PartialEq, Eq)]
22#[repr(C)]
23pub struct ProgramExecutionCols<T> {
24    pub pc: T,
25
26    pub opcode: T,
27    pub a: T,
28    pub b: T,
29    pub c: T,
30    pub d: T,
31    pub e: T,
32    pub f: T,
33    pub g: T,
34}
35
36#[derive(Clone, Copy, Debug, derive_new::new, ColumnsAir)]
37#[columns_via(ProgramCols<u8>)]
38pub struct ProgramAir {
39    pub bus: ProgramBus,
40}
41
42impl<F: Field> BaseAirWithPublicValues<F> for ProgramAir {}
43impl<F: Field> PartitionedBaseAir<F> for ProgramAir {
44    fn cached_main_widths(&self) -> Vec<usize> {
45        vec![ProgramExecutionCols::<F>::width()]
46    }
47    fn common_main_width(&self) -> usize {
48        1
49    }
50}
51impl<F: Field> BaseAir<F> for ProgramAir {
52    fn width(&self) -> usize {
53        ProgramCols::<F>::width()
54    }
55}
56
57impl<AB: PartitionedAirBuilder + InteractionBuilder> Air<AB> for ProgramAir {
58    fn eval(&self, builder: &mut AB) {
59        let common_trace = builder.common_main();
60        let cached_trace = &builder.cached_mains()[0];
61
62        let exec_freq = common_trace.row_slice(0).expect("row 0 present")[0];
63        let exec_cols = cached_trace.row_slice(0).expect("row 0 present").to_vec();
64
65        self.bus
66            .inner
67            .add_key_with_lookups(builder, exec_cols, exec_freq);
68    }
69}