openvm_stark_backend/verifier/
constraints.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::marker::PhantomData;

use itertools::Itertools;
use p3_commit::PolynomialSpace;
use p3_field::{AbstractExtensionField, AbstractField, Field};
use p3_matrix::{dense::RowMajorMatrixView, stack::VerticalPair};
use tracing::instrument;

use super::error::VerificationError;
use crate::{
    air_builders::{
        symbolic::symbolic_expression::SymbolicExpression,
        verifier::{GenericVerifierConstraintFolder, VerifierConstraintFolder},
    },
    config::{Domain, StarkGenericConfig, Val},
    prover::opener::AdjacentOpenedValues,
};

#[allow(clippy::too_many_arguments)]
#[instrument(skip_all, level = "trace")]
pub fn verify_single_rap_constraints<SC>(
    constraints: &[SymbolicExpression<Val<SC>>],
    preprocessed_values: Option<&AdjacentOpenedValues<SC::Challenge>>,
    partitioned_main_values: Vec<&AdjacentOpenedValues<SC::Challenge>>,
    after_challenge_values: Vec<&AdjacentOpenedValues<SC::Challenge>>,
    quotient_chunks: &[Vec<SC::Challenge>],
    domain: Domain<SC>, // trace domain
    qc_domains: &[Domain<SC>],
    zeta: SC::Challenge,
    alpha: SC::Challenge,
    challenges: &[Vec<SC::Challenge>],
    public_values: &[Val<SC>],
    exposed_values_after_challenge: &[Vec<SC::Challenge>],
) -> Result<(), VerificationError>
where
    SC: StarkGenericConfig,
{
    let zps = qc_domains
        .iter()
        .enumerate()
        .map(|(i, domain)| {
            qc_domains
                .iter()
                .enumerate()
                .filter(|(j, _)| *j != i)
                .map(|(_, other_domain)| {
                    other_domain.zp_at_point(zeta)
                        * other_domain.zp_at_point(domain.first_point()).inverse()
                })
                .product::<SC::Challenge>()
        })
        .collect_vec();

    let quotient = quotient_chunks
        .iter()
        .enumerate()
        .map(|(ch_i, ch)| {
            ch.iter()
                .enumerate()
                .map(|(e_i, &c)| zps[ch_i] * SC::Challenge::monomial(e_i) * c)
                .sum::<SC::Challenge>()
        })
        .sum::<SC::Challenge>();

    let unflatten = |v: &[SC::Challenge]| {
        v.chunks_exact(SC::Challenge::D)
            .map(|chunk| {
                chunk
                    .iter()
                    .enumerate()
                    .map(|(e_i, &c)| SC::Challenge::monomial(e_i) * c)
                    .sum()
            })
            .collect::<Vec<SC::Challenge>>()
    };

    let sels = domain.selectors_at_point(zeta);

    let (preprocessed_local, preprocessed_next) = preprocessed_values
        .as_ref()
        .map(|values| (values.local.as_slice(), values.next.as_slice()))
        .unwrap_or((&[], &[]));
    let preprocessed = VerticalPair::new(
        RowMajorMatrixView::new_row(preprocessed_local),
        RowMajorMatrixView::new_row(preprocessed_next),
    );

    let partitioned_main: Vec<_> = partitioned_main_values
        .into_iter()
        .map(|values| {
            VerticalPair::new(
                RowMajorMatrixView::new_row(&values.local),
                RowMajorMatrixView::new_row(&values.next),
            )
        })
        .collect();

    let after_challenge_ext_values: Vec<_> = after_challenge_values
        .into_iter()
        .map(|values| {
            let [local, next] = [&values.local, &values.next]
                .map(|flattened_ext_values| unflatten(flattened_ext_values));
            (local, next)
        })
        .collect();
    let after_challenge = after_challenge_ext_values
        .iter()
        .map(|(local, next)| {
            VerticalPair::new(
                RowMajorMatrixView::new_row(local),
                RowMajorMatrixView::new_row(next),
            )
        })
        .collect();

    let mut folder: VerifierConstraintFolder<'_, SC> = GenericVerifierConstraintFolder {
        preprocessed,
        partitioned_main,
        after_challenge,
        is_first_row: sels.is_first_row,
        is_last_row: sels.is_last_row,
        is_transition: sels.is_transition,
        alpha,
        accumulator: SC::Challenge::ZERO,
        challenges,
        public_values,
        exposed_values_after_challenge,
        _marker: PhantomData,
    };
    folder.eval_constraints(constraints);

    let folded_constraints = folder.accumulator;
    // Finally, check that
    //     folded_constraints(zeta) / Z_H(zeta) = quotient(zeta)
    if folded_constraints * sels.inv_zeroifier != quotient {
        return Err(VerificationError::OodEvaluationMismatch);
    }

    Ok(())
}