openvm_stark_backend/interaction/
rap.rs

1//! An AIR with specified interactions can be augmented into a RAP.
2//! This module auto-converts any [Air] implemented on an [InteractionBuilder] into a [Rap].
3
4use p3_air::{Air, AirBuilder};
5
6use super::{InteractionBuilder, RapPhaseSeqKind, SymbolicInteraction};
7use crate::{
8    interaction::fri_log_up::eval_fri_log_up_phase,
9    rap::{PermutationAirBuilderWithExposedValues, Rap},
10};
11
12/// Used internally to select RAP phase evaluation function.
13pub(crate) trait InteractionPhaseAirBuilder: InteractionBuilder {
14    fn finalize_interactions(&mut self);
15    /// The symbolic interactions **must** correspond to the `InteractionBuilder::all_interactions`
16    /// function.
17    fn symbolic_interactions(&self) -> Vec<SymbolicInteraction<<Self as AirBuilder>::F>>;
18    /// The maximum constraint degree allowed in a RAP.
19    fn max_constraint_degree(&self) -> usize;
20    fn rap_phase_seq_kind(&self) -> RapPhaseSeqKind;
21}
22
23impl<AB, A> Rap<AB> for A
24where
25    A: Air<AB>,
26    AB: InteractionBuilder + PermutationAirBuilderWithExposedValues + InteractionPhaseAirBuilder,
27{
28    fn eval(&self, builder: &mut AB) {
29        // Constraints for the main trace:
30        Air::eval(self, builder);
31        builder.finalize_interactions();
32        if builder.num_interactions() != 0 {
33            match builder.rap_phase_seq_kind() {
34                RapPhaseSeqKind::FriLogUp => {
35                    let symbolic_interactions = builder.symbolic_interactions();
36                    eval_fri_log_up_phase(
37                        builder,
38                        &symbolic_interactions,
39                        builder.max_constraint_degree(),
40                    );
41                }
42            }
43        }
44    }
45}