openvm_sdk/prover/
app.rs

1use std::sync::Arc;
2
3use getset::Getters;
4use openvm_circuit::arch::{ContinuationVmProof, VmConfig};
5use openvm_stark_backend::{proof::Proof, Chip};
6use openvm_stark_sdk::config::baby_bear_poseidon2::BabyBearPoseidon2Engine;
7use tracing::info_span;
8
9use super::vm::SingleSegmentVmProver;
10use crate::{
11    prover::vm::{local::VmLocalProver, types::VmProvingKey, ContinuationVmProver},
12    NonRootCommittedExe, StdIn, F, SC,
13};
14
15#[derive(Getters)]
16pub struct AppProver<VC> {
17    pub program_name: Option<String>,
18    #[getset(get = "pub")]
19    app_prover: VmLocalProver<SC, VC, BabyBearPoseidon2Engine>,
20}
21
22impl<VC> AppProver<VC> {
23    pub fn new(
24        app_vm_pk: Arc<VmProvingKey<SC, VC>>,
25        app_committed_exe: Arc<NonRootCommittedExe>,
26    ) -> Self
27    where
28        VC: VmConfig<F>,
29    {
30        Self {
31            program_name: None,
32            app_prover: VmLocalProver::<SC, VC, BabyBearPoseidon2Engine>::new(
33                app_vm_pk,
34                app_committed_exe,
35            ),
36        }
37    }
38    pub fn set_program_name(&mut self, program_name: impl AsRef<str>) -> &mut Self {
39        self.program_name = Some(program_name.as_ref().to_string());
40        self
41    }
42    pub fn with_program_name(mut self, program_name: impl AsRef<str>) -> Self {
43        self.set_program_name(program_name);
44        self
45    }
46
47    /// Generates proof for every continuation segment
48    pub fn generate_app_proof(&self, input: StdIn) -> ContinuationVmProof<SC>
49    where
50        VC: VmConfig<F>,
51        VC::Executor: Chip<SC>,
52        VC::Periphery: Chip<SC>,
53    {
54        assert!(
55            self.vm_config().system().continuation_enabled,
56            "Use generate_app_proof_without_continuations instead."
57        );
58        info_span!(
59            "app proof",
60            group = self
61                .program_name
62                .as_ref()
63                .unwrap_or(&"app_proof".to_string())
64        )
65        .in_scope(|| {
66            #[cfg(feature = "bench-metrics")]
67            metrics::counter!("fri.log_blowup")
68                .absolute(self.app_prover.pk.fri_params.log_blowup as u64);
69            ContinuationVmProver::prove(&self.app_prover, input)
70        })
71    }
72
73    pub fn generate_app_proof_without_continuations(&self, input: StdIn) -> Proof<SC>
74    where
75        VC: VmConfig<F>,
76        VC::Executor: Chip<SC>,
77        VC::Periphery: Chip<SC>,
78    {
79        assert!(
80            !self.vm_config().system().continuation_enabled,
81            "Use generate_app_proof instead."
82        );
83        info_span!(
84            "app proof",
85            group = self
86                .program_name
87                .as_ref()
88                .unwrap_or(&"app_proof".to_string())
89        )
90        .in_scope(|| {
91            #[cfg(feature = "bench-metrics")]
92            metrics::counter!("fri.log_blowup")
93                .absolute(self.app_prover.pk.fri_params.log_blowup as u64);
94            SingleSegmentVmProver::prove(&self.app_prover, input)
95        })
96    }
97
98    /// App VM config
99    pub fn vm_config(&self) -> &VC {
100        self.app_prover.vm_config()
101    }
102}