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` function.
16    fn symbolic_interactions(&self) -> Vec<SymbolicInteraction<<Self as AirBuilder>::F>>;
17    /// The maximum constraint degree allowed in a RAP.
18    fn max_constraint_degree(&self) -> usize;
19    fn rap_phase_seq_kind(&self) -> RapPhaseSeqKind;
20}
21
22impl<AB, A> Rap<AB> for A
23where
24    A: Air<AB>,
25    AB: InteractionBuilder + PermutationAirBuilderWithExposedValues + InteractionPhaseAirBuilder,
26{
27    fn eval(&self, builder: &mut AB) {
28        // Constraints for the main trace:
29        Air::eval(self, builder);
30        builder.finalize_interactions();
31        if builder.num_interactions() != 0 {
32            match builder.rap_phase_seq_kind() {
33                RapPhaseSeqKind::FriLogUp => {
34                    let symbolic_interactions = builder.symbolic_interactions();
35                    eval_fri_log_up_phase(
36                        builder,
37                        &symbolic_interactions,
38                        builder.max_constraint_degree(),
39                    );
40                }
41            }
42        }
43    }
44}