openvm_stark_sdk/dummy_airs/interaction/
mod.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
use std::sync::Arc;

use itertools::{izip, Itertools};
use openvm_stark_backend::{
    keygen::MultiStarkKeygenBuilder,
    p3_matrix::dense::RowMajorMatrix,
    prover::{
        types::{AirProofInput, AirProofRawInput, ProofInput},
        MultiTraceStarkProver,
    },
    rap::AnyRap,
    verifier::{MultiTraceStarkVerifier, VerificationError},
};
use p3_baby_bear::BabyBear;

use crate::config::{self, baby_bear_poseidon2::BabyBearPoseidon2Config};

pub mod dummy_interaction_air;

type Val = BabyBear;

pub fn verify_interactions(
    traces: Vec<RowMajorMatrix<Val>>,
    airs: Vec<Arc<dyn AnyRap<BabyBearPoseidon2Config>>>,
    pis: Vec<Vec<Val>>,
) -> Result<(), VerificationError> {
    let perm = config::baby_bear_poseidon2::random_perm();
    let config = config::baby_bear_poseidon2::default_config(&perm);

    let mut keygen_builder = MultiStarkKeygenBuilder::new(&config);
    let air_ids = airs
        .iter()
        .map(|air| keygen_builder.add_air(air.clone()))
        .collect_vec();
    let pk = keygen_builder.generate_pk();
    let vk = pk.get_vk();

    let per_air: Vec<_> = izip!(air_ids, airs, traces, pis)
        .map(|(air_id, air, trace, pvs)| {
            (
                air_id,
                AirProofInput {
                    air,
                    cached_mains_pdata: vec![],
                    raw: AirProofRawInput {
                        cached_mains: vec![],
                        common_main: Some(trace),
                        public_values: pvs,
                    },
                },
            )
        })
        .collect();

    let prover = MultiTraceStarkProver::new(&config);
    let mut challenger = config::baby_bear_poseidon2::Challenger::new(perm.clone());
    let proof = prover.prove(&mut challenger, &pk, ProofInput { per_air });

    // Verify the proof:
    // Start from clean challenger
    let mut challenger = config::baby_bear_poseidon2::Challenger::new(perm.clone());
    let verifier = MultiTraceStarkVerifier::new(prover.config);
    verifier.verify(&mut challenger, &vk, &proof)
}