openvm_sdk/config/
mod.rs

1use clap::Args;
2use openvm_circuit::arch::DEFAULT_MAX_NUM_PUBLIC_VALUES;
3use openvm_continuations::verifier::{
4    common::types::VmVerifierPvs, internal::types::InternalVmVerifierPvs,
5};
6use openvm_native_circuit::NativeConfig;
7use openvm_native_compiler::{conversion::CompilerOptions, ir::DIGEST_SIZE};
8use openvm_stark_sdk::config::FriParameters;
9use openvm_transpiler::transpiler::Transpiler;
10use serde::{Deserialize, Serialize};
11
12mod global;
13pub use global::*;
14
15pub const DEFAULT_APP_LOG_BLOWUP: usize = 1;
16pub const DEFAULT_LEAF_LOG_BLOWUP: usize = 1;
17pub const DEFAULT_INTERNAL_LOG_BLOWUP: usize = 2;
18pub const DEFAULT_ROOT_LOG_BLOWUP: usize = 3;
19
20#[cfg(not(feature = "legacy-v1-3-evm-verifier"))]
21pub const DEFAULT_HALO2_VERIFIER_K: usize = 23;
22#[cfg(feature = "legacy-v1-3-evm-verifier")]
23pub const DEFAULT_HALO2_VERIFIER_K: usize = 24;
24
25// Aggregation Tree Defaults
26pub const DEFAULT_NUM_CHILDREN_LEAF: usize = 1;
27pub const DEFAULT_NUM_CHILDREN_INTERNAL: usize = 3;
28pub const DEFAULT_MAX_INTERNAL_WRAPPER_LAYERS: usize = 4;
29
30pub trait TranspilerConfig<F> {
31    fn transpiler(&self) -> Transpiler<F>;
32}
33
34#[derive(Clone, Debug, Serialize, Deserialize)]
35pub struct AppConfig<VC> {
36    #[serde(default)]
37    pub app_fri_params: AppFriParams,
38    pub app_vm_config: VC,
39    #[serde(default)]
40    pub leaf_fri_params: LeafFriParams,
41    /// Only for AggVM debugging. App VM users should not need this in regular flow.
42    #[serde(default)]
43    pub compiler_options: CompilerOptions,
44}
45
46#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
47pub struct AggregationConfig {
48    pub max_num_user_public_values: usize,
49    pub leaf_fri_params: FriParameters,
50    pub internal_fri_params: FriParameters,
51    pub root_fri_params: FriParameters,
52    /// Sets the profiling mode of all aggregation VMs
53    pub profiling: bool,
54    /// Only for AggVM debugging.
55    pub compiler_options: CompilerOptions,
56    /// Max constraint degree for FRI logup chunking
57    pub root_max_constraint_degree: usize,
58}
59
60#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
61pub struct Halo2Config {
62    /// Log degree for the outer recursion verifier circuit.
63    pub verifier_k: usize,
64    /// If not specified, keygen will tune wrapper_k automatically.
65    pub wrapper_k: Option<usize>,
66    /// Sets the profiling mode of halo2 VM
67    pub profiling: bool,
68}
69
70#[derive(Clone, Copy, Debug, Serialize, Deserialize, Args)]
71pub struct AggregationTreeConfig {
72    /// Each leaf verifier circuit will aggregate this many App VM proofs.
73    #[arg(
74        long,
75        default_value_t = DEFAULT_NUM_CHILDREN_LEAF,
76        help = "Number of children per leaf verifier circuit",
77        help_heading = "Aggregation Tree Options"
78    )]
79    pub num_children_leaf: usize,
80    /// Each internal verifier circuit will aggregate this many proofs,
81    /// where each proof may be of either leaf or internal verifier (self) circuit.
82    #[arg(
83        long,
84        default_value_t = DEFAULT_NUM_CHILDREN_INTERNAL,
85        help = "Number of children per internal verifier circuit",
86        help_heading = "Aggregation Tree Options"
87    )]
88    pub num_children_internal: usize,
89    /// Safety threshold: how many times to do 1-to-1 aggregation of the "last" internal
90    /// verifier proof before it is small enough for the root verifier circuit.
91    /// Note: almost always no wrapping is needed.
92    #[arg(
93        long,
94        default_value_t = DEFAULT_MAX_INTERNAL_WRAPPER_LAYERS,
95        help = "Maximum number of internal wrapper layers",
96        help_heading = "Aggregation Tree Options"
97    )]
98    pub max_internal_wrapper_layers: usize,
99    // root currently always has 1 child for now
100}
101
102impl<VC> AppConfig<VC> {
103    pub fn new(app_fri_params: FriParameters, app_vm_config: VC) -> Self {
104        Self {
105            app_fri_params: AppFriParams::from(app_fri_params),
106            app_vm_config,
107            leaf_fri_params: Default::default(),
108            compiler_options: Default::default(),
109        }
110    }
111
112    pub fn new_with_leaf_fri_params(
113        app_fri_params: FriParameters,
114        app_vm_config: VC,
115        leaf_fri_params: FriParameters,
116    ) -> Self {
117        Self {
118            app_fri_params: AppFriParams::from(app_fri_params),
119            app_vm_config,
120            leaf_fri_params: LeafFriParams::from(leaf_fri_params),
121            compiler_options: Default::default(),
122        }
123    }
124}
125
126impl Default for AggregationConfig {
127    fn default() -> Self {
128        Self {
129            max_num_user_public_values: DEFAULT_MAX_NUM_PUBLIC_VALUES,
130            leaf_fri_params: FriParameters::standard_with_100_bits_conjectured_security(
131                DEFAULT_LEAF_LOG_BLOWUP,
132            ),
133            internal_fri_params: FriParameters::standard_with_100_bits_conjectured_security(
134                DEFAULT_INTERNAL_LOG_BLOWUP,
135            ),
136            root_fri_params: FriParameters::standard_with_100_bits_conjectured_security(
137                DEFAULT_ROOT_LOG_BLOWUP,
138            ),
139            profiling: false,
140            compiler_options: Default::default(),
141            root_max_constraint_degree: (1 << DEFAULT_ROOT_LOG_BLOWUP) + 1,
142        }
143    }
144}
145
146impl Default for Halo2Config {
147    fn default() -> Self {
148        Self {
149            verifier_k: DEFAULT_HALO2_VERIFIER_K,
150            wrapper_k: None,
151            profiling: false,
152        }
153    }
154}
155
156#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
157pub struct AppFriParams {
158    pub fri_params: FriParameters,
159}
160
161impl Default for AppFriParams {
162    fn default() -> Self {
163        Self {
164            fri_params: FriParameters::standard_with_100_bits_conjectured_security(
165                DEFAULT_APP_LOG_BLOWUP,
166            ),
167        }
168    }
169}
170
171impl From<FriParameters> for AppFriParams {
172    fn from(fri_params: FriParameters) -> Self {
173        Self { fri_params }
174    }
175}
176
177#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
178pub struct LeafFriParams {
179    pub fri_params: FriParameters,
180}
181
182impl Default for LeafFriParams {
183    fn default() -> Self {
184        Self {
185            fri_params: FriParameters::standard_with_100_bits_conjectured_security(
186                DEFAULT_LEAF_LOG_BLOWUP,
187            ),
188        }
189    }
190}
191
192impl From<FriParameters> for LeafFriParams {
193    fn from(fri_params: FriParameters) -> Self {
194        Self { fri_params }
195    }
196}
197
198pub const SBOX_SIZE: usize = 7;
199
200impl AggregationConfig {
201    pub fn leaf_vm_config(&self) -> NativeConfig {
202        let mut config = NativeConfig::aggregation(
203            VmVerifierPvs::<u8>::width(),
204            SBOX_SIZE.min(self.leaf_fri_params.max_constraint_degree()),
205        );
206        config.system.profiling = self.profiling;
207        config
208    }
209    pub fn internal_vm_config(&self) -> NativeConfig {
210        let mut config = NativeConfig::aggregation(
211            InternalVmVerifierPvs::<u8>::width(),
212            SBOX_SIZE.min(self.internal_fri_params.max_constraint_degree()),
213        );
214        config.system.profiling = self.profiling;
215        config
216    }
217    pub fn root_verifier_vm_config(&self) -> NativeConfig {
218        let mut config = NativeConfig::aggregation(
219            // app_commit + leaf_verifier_commit + public_values
220            DIGEST_SIZE * 2 + self.max_num_user_public_values,
221            SBOX_SIZE.min(self.root_fri_params.max_constraint_degree()),
222        );
223        config.system.profiling = self.profiling;
224        config
225    }
226}
227
228impl Default for AggregationTreeConfig {
229    fn default() -> Self {
230        Self {
231            num_children_leaf: DEFAULT_NUM_CHILDREN_LEAF,
232            num_children_internal: DEFAULT_NUM_CHILDREN_INTERNAL,
233            max_internal_wrapper_layers: DEFAULT_MAX_INTERNAL_WRAPPER_LAYERS,
234        }
235    }
236}