openvm_sdk_config/
deferral.rs

1use std::sync::Arc;
2
3use openvm_continuations::CommitBytes;
4use openvm_deferral_circuit::{DeferralExtension, DeferralFn};
5use openvm_verify_stark_circuit::extension::verify_stark_deferral_fn;
6use serde::{Deserialize, Serialize};
7
8#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
9pub enum SupportedDeferral {
10    /// The built-in deferral circuit for recursively verifying OpenVM STARK proofs.
11    VerifyStark,
12    /// A custom deferral circuit.
13    ///
14    /// This variant can preserve the config shape through serialization, but the corresponding
15    /// [`DeferralFn`] must be supplied manually after deserialization.
16    Other(String),
17}
18
19/// A serializable deferral circuit entry in an [`SdkVmConfig`](crate::SdkVmConfig).
20///
21/// Entries are indexed by their position in [`DeferralConfig::circuits`]. Guest programs pass that
22/// index to deferral guest APIs.
23#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
24pub struct DeferralCircuitConfig {
25    /// The supported deferral function this circuit computes.
26    pub def_type: SupportedDeferral,
27    /// The commitment to the deferral circuit proving key expected by the guest VM.
28    pub commit: CommitBytes,
29}
30
31/// Serializable SDK configuration for the deferral extension.
32///
33/// The SDK converts this into the lower-level [`DeferralExtension`] when building execution,
34/// transpiler, and prover components.
35#[derive(Clone, Debug, Serialize, Deserialize)]
36pub struct DeferralConfig {
37    /// Ordered deferral circuits available to the VM.
38    pub circuits: Vec<DeferralCircuitConfig>,
39}
40
41impl DeferralConfig {
42    pub fn new(circuits: Vec<DeferralCircuitConfig>) -> Self {
43        Self { circuits }
44    }
45
46    pub fn to_extension(&self) -> DeferralExtension {
47        DeferralExtension {
48            fns: self
49                .circuits
50                .iter()
51                .map(|circuit| circuit.def_type.deferral_fn())
52                .collect(),
53            def_circuit_commits: self
54                .circuits
55                .iter()
56                .map(|circuit| circuit.commit.to_field_le_bytes())
57                .collect(),
58        }
59    }
60}
61
62impl From<DeferralConfig> for DeferralExtension {
63    fn from(config: DeferralConfig) -> Self {
64        config.to_extension()
65    }
66}
67
68impl SupportedDeferral {
69    fn deferral_fn(&self) -> Arc<DeferralFn> {
70        match self {
71            SupportedDeferral::VerifyStark => Arc::new(DeferralFn::new(verify_stark_deferral_fn)),
72            SupportedDeferral::Other(name) => {
73                let name = name.clone();
74                Arc::new(DeferralFn::new(move |_| {
75                    panic!(
76                        "deferral `{name}`: placeholder DeferralFn from `SupportedDeferral::Other` \
77                         was never replaced — set the real DeferralFn in the DeferralExtension \
78                         after deserialize"
79                    )
80                }))
81            }
82        }
83    }
84}