openvm_circuit/system/program/
air.rs1use std::borrow::Borrow;
2
3use openvm_circuit_primitives::{ColumnsAir, StructReflection, StructReflectionHelper};
4use openvm_circuit_primitives_derive::AlignedBorrow;
5use openvm_stark_backend::{
6 air_builders::PartitionedAirBuilder,
7 interaction::InteractionBuilder,
8 p3_air::{Air, BaseAir},
9 p3_field::{Field, PrimeCharacteristicRing},
10 p3_matrix::Matrix,
11 BaseAirWithPublicValues, PartitionedBaseAir,
12};
13
14use super::ProgramBus;
15
16#[derive(Copy, Clone, Debug, AlignedBorrow, StructReflection, PartialEq, Eq)]
17#[repr(C)]
18pub struct ProgramCols<T> {
19 pub cached: ProgramCachedCols<T>,
20 pub exec_freq: T,
21}
22
23#[derive(Copy, Clone, Debug, AlignedBorrow, StructReflection, PartialEq, Eq)]
24#[repr(C)]
25pub struct ProgramCachedCols<T> {
26 pub exec_end: T,
27 pub exec: ProgramExecutionCols<T>,
28 pub exec_start: T,
29}
30
31#[derive(Copy, Clone, Debug, AlignedBorrow, StructReflection, PartialEq, Eq)]
32#[repr(C)]
33pub struct ProgramExecutionCols<T> {
34 pub pc: T,
35
36 pub opcode: T,
37 pub a: T,
38 pub b: T,
39 pub c: T,
40 pub d: T,
41 pub e: T,
42 pub f: T,
43 pub g: T,
44}
45
46#[derive(Clone, Copy, Debug, derive_new::new, ColumnsAir)]
47#[columns_via(ProgramCols<u8>)]
48pub struct ProgramAir {
49 pub bus: ProgramBus,
50}
51
52impl<F: Field> BaseAirWithPublicValues<F> for ProgramAir {}
53impl<F: Field> PartitionedBaseAir<F> for ProgramAir {
54 fn cached_main_widths(&self) -> Vec<usize> {
55 vec![ProgramCachedCols::<F>::width()]
56 }
57 fn common_main_width(&self) -> usize {
58 1
59 }
60}
61impl<F: Field> BaseAir<F> for ProgramAir {
62 fn width(&self) -> usize {
63 ProgramCols::<F>::width()
64 }
65}
66
67impl<AB: PartitionedAirBuilder + InteractionBuilder> Air<AB> for ProgramAir {
68 fn eval(&self, builder: &mut AB) {
69 let common_trace = builder.common_main();
70 let cached_trace = &builder.cached_mains()[0];
71
72 let exec_freq = common_trace.row_slice(0).expect("row 0 present")[0];
73 let cached_row = cached_trace.row_slice(0).expect("row 0 present").to_vec();
74 let cached_cols: &ProgramCachedCols<AB::Var> = cached_row.as_slice().borrow();
75
76 builder.assert_eq(cached_cols.exec_end, AB::Expr::ONE + builder.is_last_row());
79 builder.assert_eq(cached_cols.exec_start, builder.is_first_row());
80
81 self.bus.inner.add_key_with_lookups(
82 builder,
83 cached_row[1..cached_row.len() - 1].iter().copied(),
84 exec_freq,
85 );
86 }
87}