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
25pub 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 #[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 pub profiling: bool,
54 pub compiler_options: CompilerOptions,
56 pub root_max_constraint_degree: usize,
58}
59
60#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
61pub struct Halo2Config {
62 pub verifier_k: usize,
64 pub wrapper_k: Option<usize>,
66 pub profiling: bool,
68}
69
70#[derive(Clone, Copy, Debug, Serialize, Deserialize, Args)]
71pub struct AggregationTreeConfig {
72 #[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 #[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 #[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 }
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_security(
131 DEFAULT_LEAF_LOG_BLOWUP,
132 ),
133 internal_fri_params: FriParameters::standard_with_100_bits_security(
134 DEFAULT_INTERNAL_LOG_BLOWUP,
135 ),
136 root_fri_params: FriParameters::standard_with_100_bits_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_security(DEFAULT_APP_LOG_BLOWUP),
165 }
166 }
167}
168
169impl From<FriParameters> for AppFriParams {
170 fn from(fri_params: FriParameters) -> Self {
171 Self { fri_params }
172 }
173}
174
175#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
176pub struct LeafFriParams {
177 pub fri_params: FriParameters,
178}
179
180impl Default for LeafFriParams {
181 fn default() -> Self {
182 Self {
183 fri_params: FriParameters::standard_with_100_bits_security(DEFAULT_LEAF_LOG_BLOWUP),
184 }
185 }
186}
187
188impl From<FriParameters> for LeafFriParams {
189 fn from(fri_params: FriParameters) -> Self {
190 Self { fri_params }
191 }
192}
193
194pub const SBOX_SIZE: usize = 7;
195
196impl AggregationConfig {
197 pub fn leaf_vm_config(&self) -> NativeConfig {
198 let mut config = NativeConfig::aggregation(
199 VmVerifierPvs::<u8>::width(),
200 SBOX_SIZE.min(self.leaf_fri_params.max_constraint_degree()),
201 );
202 config.system.profiling = self.profiling;
203 config
204 }
205 pub fn internal_vm_config(&self) -> NativeConfig {
206 let mut config = NativeConfig::aggregation(
207 InternalVmVerifierPvs::<u8>::width(),
208 SBOX_SIZE.min(self.internal_fri_params.max_constraint_degree()),
209 );
210 config.system.profiling = self.profiling;
211 config
212 }
213 pub fn root_verifier_vm_config(&self) -> NativeConfig {
214 let mut config = NativeConfig::aggregation(
215 DIGEST_SIZE * 2 + self.max_num_user_public_values,
217 SBOX_SIZE.min(self.root_fri_params.max_constraint_degree()),
218 );
219 config.system.profiling = self.profiling;
220 config
221 }
222}
223
224impl Default for AggregationTreeConfig {
225 fn default() -> Self {
226 Self {
227 num_children_leaf: DEFAULT_NUM_CHILDREN_LEAF,
228 num_children_internal: DEFAULT_NUM_CHILDREN_INTERNAL,
229 max_internal_wrapper_layers: DEFAULT_MAX_INTERNAL_WRAPPER_LAYERS,
230 }
231 }
232}