openvm_stark_sdk/
engine.rs

1pub use openvm_stark_backend::engine::StarkEngine;
2use openvm_stark_backend::{
3    config::{StarkGenericConfig, Val},
4    engine::VerificationData,
5    p3_matrix::dense::RowMajorMatrix,
6    prover::types::AirProofInput,
7    verifier::VerificationError,
8    AirRef,
9};
10use tracing::Level;
11
12use crate::config::{instrument::StarkHashStatistics, setup_tracing_with_log_level, FriParameters};
13
14pub trait StarkEngineWithHashInstrumentation<SC: StarkGenericConfig>: StarkEngine<SC> {
15    fn clear_instruments(&mut self);
16    fn stark_hash_statistics<T>(&self, custom: T) -> StarkHashStatistics<T>;
17}
18
19/// All necessary data to verify a Stark proof.
20pub struct VerificationDataWithFriParams<SC: StarkGenericConfig> {
21    pub data: VerificationData<SC>,
22    pub fri_params: FriParameters,
23}
24
25/// Stark engine using Fri.
26pub trait StarkFriEngine<SC: StarkGenericConfig>: StarkEngine<SC> + Sized {
27    fn new(fri_params: FriParameters) -> Self;
28    fn fri_params(&self) -> FriParameters;
29    fn run_test(
30        &self,
31        airs: Vec<AirRef<SC>>,
32        air_proof_inputs: Vec<AirProofInput<SC>>,
33    ) -> Result<VerificationDataWithFriParams<SC>, VerificationError>
34    where
35        AirProofInput<SC>: Send + Sync,
36    {
37        setup_tracing_with_log_level(Level::WARN);
38        let data = <Self as StarkEngine<_>>::run_test_impl(self, airs, air_proof_inputs)?;
39        Ok(VerificationDataWithFriParams {
40            data,
41            fri_params: self.fri_params(),
42        })
43    }
44    fn run_test_fast(
45        airs: Vec<AirRef<SC>>,
46        air_proof_inputs: Vec<AirProofInput<SC>>,
47    ) -> Result<VerificationDataWithFriParams<SC>, VerificationError>
48    where
49        AirProofInput<SC>: Send + Sync,
50    {
51        let engine = Self::new(FriParameters::standard_fast());
52        engine.run_test(airs, air_proof_inputs)
53    }
54    /// Runs a single end-to-end test for a given set of AIRs and traces.
55    /// This includes proving/verifying key generation, creating a proof, and verifying the proof.
56    /// This function should only be used on AIRs where the main trace is **not** partitioned.
57    fn run_simple_test_impl(
58        &self,
59        chips: Vec<AirRef<SC>>,
60        traces: Vec<RowMajorMatrix<Val<SC>>>,
61        public_values: Vec<Vec<Val<SC>>>,
62    ) -> Result<VerificationDataWithFriParams<SC>, VerificationError>
63    where
64        AirProofInput<SC>: Send + Sync,
65    {
66        self.run_test(chips, AirProofInput::multiple_simple(traces, public_values))
67    }
68    fn run_simple_test_fast(
69        airs: Vec<AirRef<SC>>,
70        traces: Vec<RowMajorMatrix<Val<SC>>>,
71        public_values: Vec<Vec<Val<SC>>>,
72    ) -> Result<VerificationDataWithFriParams<SC>, VerificationError> {
73        let engine = Self::new(FriParameters::standard_fast());
74        StarkFriEngine::<_>::run_simple_test_impl(&engine, airs, traces, public_values)
75    }
76    fn run_simple_test_no_pis_fast(
77        airs: Vec<AirRef<SC>>,
78        traces: Vec<RowMajorMatrix<Val<SC>>>,
79    ) -> Result<VerificationDataWithFriParams<SC>, VerificationError> {
80        let pis = vec![vec![]; airs.len()];
81        <Self as StarkFriEngine<SC>>::run_simple_test_fast(airs, traces, pis)
82    }
83}
84
85#[macro_export]
86macro_rules! collect_airs_and_inputs {
87    ($($chip:expr),+ $(,)?) => {
88        (
89            vec![$($chip.air()),+],
90            vec![$($chip.generate_air_proof_input()),+]
91        )
92    }
93}