openvm_stark_backend/rap.rs
1//! # RAP (Randomized Air with Preprocessing)
2//! See <https://hackmd.io/@aztec-network/plonk-arithmetiization-air> for formal definition.
3
4use std::{
5 any::{type_name, Any},
6 sync::Arc,
7};
8
9use p3_air::{BaseAir, PermutationAirBuilder};
10
11use crate::{
12 air_builders::{debug::DebugConstraintBuilder, symbolic::SymbolicRapBuilder},
13 config::{StarkGenericConfig, Val},
14};
15
16/// An AIR with 0 or more public values.
17/// This trait will be merged into Plonky3 in PR: <https://github.com/Plonky3/Plonky3/pull/470>
18pub trait BaseAirWithPublicValues<F>: BaseAir<F> {
19 fn num_public_values(&self) -> usize {
20 0
21 }
22}
23
24/// An AIR with 1 or more main trace partitions.
25pub trait PartitionedBaseAir<F>: BaseAir<F> {
26 /// By default, an AIR has no cached main trace.
27 fn cached_main_widths(&self) -> Vec<usize> {
28 vec![]
29 }
30 /// By default, an AIR has only one private main trace.
31 fn common_main_width(&self) -> usize {
32 self.width()
33 }
34}
35
36/// An AIR that works with a particular `AirBuilder` which allows preprocessing
37/// and injected randomness.
38///
39/// Currently this is not a fully general RAP. Only the following phases are allowed:
40/// - Preprocessing
41/// - Main trace generation and commitment
42/// - Permutation trace generation and commitment
43///
44/// Randomness is drawn after the main trace commitment phase, and used in the permutation trace.
45///
46/// Does not inherit [Air](p3_air::Air) trait to allow overrides for technical reasons
47/// around dynamic dispatch.
48pub trait Rap<AB>: Sync
49where
50 AB: PermutationAirBuilder,
51{
52 fn eval(&self, builder: &mut AB);
53}
54
55/// Permutation AIR builder that exposes certain values to both prover and verifier
56/// _after_ the permutation challenges are drawn. These can be thought of as
57/// "public values" known after the challenges are drawn.
58///
59/// Exposed values are used internally by the prover and verifier
60/// in cross-table permutation arguments.
61pub trait PermutationAirBuilderWithExposedValues: PermutationAirBuilder {
62 fn permutation_exposed_values(&self) -> &[Self::VarEF];
63}
64
65/// Shared reference to any Interactive Air.
66/// This type is the main interface for keygen.
67pub type AirRef<SC> = Arc<dyn AnyRap<SC>>;
68
69/// RAP trait for all-purpose dynamic dispatch use.
70/// This trait is auto-implemented if you implement `Air` and `BaseAirWithPublicValues` and `PartitionedBaseAir` traits.
71pub trait AnyRap<SC: StarkGenericConfig>:
72Rap<SymbolicRapBuilder<Val<SC>>> // for keygen to extract fixed data about the RAP
73 + for<'a> Rap<DebugConstraintBuilder<'a, SC>> // for debugging
74 + BaseAirWithPublicValues<Val<SC>>
75 + PartitionedBaseAir<Val<SC>>
76 + Send + Sync
77{
78 fn as_any(&self) -> &dyn Any;
79 /// Name for display purposes
80 fn name(&self) -> String;
81}
82
83impl<SC, T> AnyRap<SC> for T
84where
85 SC: StarkGenericConfig,
86 T: Rap<SymbolicRapBuilder<Val<SC>>>
87 + for<'a> Rap<DebugConstraintBuilder<'a, SC>>
88 + BaseAirWithPublicValues<Val<SC>>
89 + PartitionedBaseAir<Val<SC>>
90 + Send
91 + Sync
92 + 'static,
93{
94 fn as_any(&self) -> &dyn Any {
95 self
96 }
97
98 fn name(&self) -> String {
99 get_air_name(self)
100 }
101}
102
103/// Automatically derives the AIR name from the type name for pretty display purposes.
104pub fn get_air_name<T>(_rap: &T) -> String {
105 let full_name = type_name::<T>().to_string();
106 // Split the input by the first '<' to separate the main type from its generics
107 if let Some((main_part, generics_part)) = full_name.split_once('<') {
108 // Extract the last segment of the main type
109 let main_type = main_part.split("::").last().unwrap_or("");
110
111 // Remove the trailing '>' from the generics part and split by ", " to handle multiple generics
112 let generics: Vec<String> = generics_part
113 .trim_end_matches('>')
114 .split(", ")
115 .map(|generic| {
116 // For each generic type, extract the last segment after "::"
117 generic.split("::").last().unwrap_or("").to_string()
118 })
119 .collect();
120
121 // Join the simplified generics back together with ", " and format the result
122 format!("{}<{}>", main_type, generics.join(", "))
123 } else {
124 // If there's no generic part, just return the last segment after "::"
125 full_name.split("::").last().unwrap_or("").to_string()
126 }
127}