openvm_sdk/config/
global.rs

1use bon::Builder;
2use derive_more::derive::From;
3use openvm_algebra_circuit::{
4    Fp2Extension, Fp2ExtensionExecutor, Fp2ExtensionPeriphery, ModularExtension,
5    ModularExtensionExecutor, ModularExtensionPeriphery,
6};
7use openvm_algebra_transpiler::{Fp2TranspilerExtension, ModularTranspilerExtension};
8use openvm_bigint_circuit::{Int256, Int256Executor, Int256Periphery};
9use openvm_bigint_transpiler::Int256TranspilerExtension;
10use openvm_circuit::{
11    arch::{
12        SystemConfig, SystemExecutor, SystemPeriphery, VmChipComplex, VmConfig, VmInventoryError,
13    },
14    circuit_derive::{Chip, ChipUsageGetter},
15    derive::{AnyEnum, InstructionExecutor},
16};
17use openvm_ecc_circuit::{
18    WeierstrassExtension, WeierstrassExtensionExecutor, WeierstrassExtensionPeriphery,
19};
20use openvm_ecc_transpiler::EccTranspilerExtension;
21use openvm_keccak256_circuit::{Keccak256, Keccak256Executor, Keccak256Periphery};
22use openvm_keccak256_transpiler::Keccak256TranspilerExtension;
23use openvm_native_circuit::{
24    CastFExtension, CastFExtensionExecutor, CastFExtensionPeriphery, Native, NativeExecutor,
25    NativePeriphery,
26};
27use openvm_pairing_circuit::{
28    PairingExtension, PairingExtensionExecutor, PairingExtensionPeriphery,
29};
30use openvm_pairing_transpiler::PairingTranspilerExtension;
31use openvm_rv32im_circuit::{
32    Rv32I, Rv32IExecutor, Rv32IPeriphery, Rv32Io, Rv32IoExecutor, Rv32IoPeriphery, Rv32M,
33    Rv32MExecutor, Rv32MPeriphery,
34};
35use openvm_rv32im_transpiler::{
36    Rv32ITranspilerExtension, Rv32IoTranspilerExtension, Rv32MTranspilerExtension,
37};
38use openvm_sha256_circuit::{Sha256, Sha256Executor, Sha256Periphery};
39use openvm_sha256_transpiler::Sha256TranspilerExtension;
40use openvm_stark_backend::p3_field::PrimeField32;
41use openvm_transpiler::transpiler::Transpiler;
42use serde::{Deserialize, Serialize};
43
44use crate::F;
45
46#[derive(Builder, Clone, Debug, Serialize, Deserialize)]
47pub struct SdkVmConfig {
48    #[serde(default)]
49    pub system: SdkSystemConfig,
50
51    pub rv32i: Option<UnitStruct>,
52    pub io: Option<UnitStruct>,
53    pub keccak: Option<UnitStruct>,
54    pub sha256: Option<UnitStruct>,
55    pub native: Option<UnitStruct>,
56    pub castf: Option<UnitStruct>,
57
58    pub rv32m: Option<Rv32M>,
59    pub bigint: Option<Int256>,
60    pub modular: Option<ModularExtension>,
61    pub fp2: Option<Fp2Extension>,
62    pub pairing: Option<PairingExtension>,
63    pub ecc: Option<WeierstrassExtension>,
64}
65
66#[derive(ChipUsageGetter, Chip, InstructionExecutor, From, AnyEnum)]
67pub enum SdkVmConfigExecutor<F: PrimeField32> {
68    #[any_enum]
69    System(SystemExecutor<F>),
70    #[any_enum]
71    Rv32i(Rv32IExecutor<F>),
72    #[any_enum]
73    Io(Rv32IoExecutor<F>),
74    #[any_enum]
75    Keccak(Keccak256Executor<F>),
76    #[any_enum]
77    Sha256(Sha256Executor<F>),
78    #[any_enum]
79    Native(NativeExecutor<F>),
80    #[any_enum]
81    Rv32m(Rv32MExecutor<F>),
82    #[any_enum]
83    BigInt(Int256Executor<F>),
84    #[any_enum]
85    Modular(ModularExtensionExecutor<F>),
86    #[any_enum]
87    Fp2(Fp2ExtensionExecutor<F>),
88    #[any_enum]
89    Pairing(PairingExtensionExecutor<F>),
90    #[any_enum]
91    Ecc(WeierstrassExtensionExecutor<F>),
92    #[any_enum]
93    CastF(CastFExtensionExecutor<F>),
94}
95
96#[derive(From, ChipUsageGetter, Chip, AnyEnum)]
97pub enum SdkVmConfigPeriphery<F: PrimeField32> {
98    #[any_enum]
99    System(SystemPeriphery<F>),
100    #[any_enum]
101    Rv32i(Rv32IPeriphery<F>),
102    #[any_enum]
103    Io(Rv32IoPeriphery<F>),
104    #[any_enum]
105    Keccak(Keccak256Periphery<F>),
106    #[any_enum]
107    Sha256(Sha256Periphery<F>),
108    #[any_enum]
109    Native(NativePeriphery<F>),
110    #[any_enum]
111    Rv32m(Rv32MPeriphery<F>),
112    #[any_enum]
113    BigInt(Int256Periphery<F>),
114    #[any_enum]
115    Modular(ModularExtensionPeriphery<F>),
116    #[any_enum]
117    Fp2(Fp2ExtensionPeriphery<F>),
118    #[any_enum]
119    Pairing(PairingExtensionPeriphery<F>),
120    #[any_enum]
121    Ecc(WeierstrassExtensionPeriphery<F>),
122    #[any_enum]
123    CastF(CastFExtensionPeriphery<F>),
124}
125
126impl SdkVmConfig {
127    pub fn transpiler(&self) -> Transpiler<F> {
128        let mut transpiler = Transpiler::default();
129        if self.rv32i.is_some() {
130            transpiler = transpiler.with_extension(Rv32ITranspilerExtension);
131        }
132        if self.io.is_some() {
133            transpiler = transpiler.with_extension(Rv32IoTranspilerExtension);
134        }
135        if self.keccak.is_some() {
136            transpiler = transpiler.with_extension(Keccak256TranspilerExtension);
137        }
138        if self.sha256.is_some() {
139            transpiler = transpiler.with_extension(Sha256TranspilerExtension);
140        }
141        if self.rv32m.is_some() {
142            transpiler = transpiler.with_extension(Rv32MTranspilerExtension);
143        }
144        if self.bigint.is_some() {
145            transpiler = transpiler.with_extension(Int256TranspilerExtension);
146        }
147        if self.modular.is_some() {
148            transpiler = transpiler.with_extension(ModularTranspilerExtension);
149        }
150        if self.fp2.is_some() {
151            transpiler = transpiler.with_extension(Fp2TranspilerExtension);
152        }
153        if self.pairing.is_some() {
154            transpiler = transpiler.with_extension(PairingTranspilerExtension);
155        }
156        if self.ecc.is_some() {
157            transpiler = transpiler.with_extension(EccTranspilerExtension);
158        }
159        transpiler
160    }
161}
162
163impl<F: PrimeField32> VmConfig<F> for SdkVmConfig {
164    type Executor = SdkVmConfigExecutor<F>;
165    type Periphery = SdkVmConfigPeriphery<F>;
166
167    fn system(&self) -> &SystemConfig {
168        &self.system.config
169    }
170
171    fn system_mut(&mut self) -> &mut SystemConfig {
172        &mut self.system.config
173    }
174
175    fn create_chip_complex(
176        &self,
177    ) -> Result<VmChipComplex<F, Self::Executor, Self::Periphery>, VmInventoryError> {
178        let mut complex = self.system.config.create_chip_complex()?.transmute();
179
180        if self.rv32i.is_some() {
181            complex = complex.extend(&Rv32I)?;
182        }
183        if self.io.is_some() {
184            complex = complex.extend(&Rv32Io)?;
185        }
186        if self.keccak.is_some() {
187            complex = complex.extend(&Keccak256)?;
188        }
189        if self.sha256.is_some() {
190            complex = complex.extend(&Sha256)?;
191        }
192        if self.native.is_some() {
193            complex = complex.extend(&Native)?;
194        }
195        if self.castf.is_some() {
196            complex = complex.extend(&CastFExtension)?;
197        }
198
199        if let Some(rv32m) = self.rv32m {
200            let mut rv32m = rv32m;
201            if let Some(ref bigint) = self.bigint {
202                rv32m.range_tuple_checker_sizes[0] =
203                    rv32m.range_tuple_checker_sizes[0].max(bigint.range_tuple_checker_sizes[0]);
204                rv32m.range_tuple_checker_sizes[1] =
205                    rv32m.range_tuple_checker_sizes[1].max(bigint.range_tuple_checker_sizes[1]);
206            }
207            complex = complex.extend(&rv32m)?;
208        }
209        if let Some(bigint) = self.bigint {
210            let mut bigint = bigint;
211            if let Some(ref rv32m) = self.rv32m {
212                bigint.range_tuple_checker_sizes[0] =
213                    rv32m.range_tuple_checker_sizes[0].max(bigint.range_tuple_checker_sizes[0]);
214                bigint.range_tuple_checker_sizes[1] =
215                    rv32m.range_tuple_checker_sizes[1].max(bigint.range_tuple_checker_sizes[1]);
216            }
217            complex = complex.extend(&bigint)?;
218        }
219        if let Some(ref modular) = self.modular {
220            complex = complex.extend(modular)?;
221        }
222        if let Some(ref fp2) = self.fp2 {
223            complex = complex.extend(fp2)?;
224        }
225        if let Some(ref pairing) = self.pairing {
226            complex = complex.extend(pairing)?;
227        }
228        if let Some(ref ecc) = self.ecc {
229            complex = complex.extend(ecc)?;
230        }
231
232        Ok(complex)
233    }
234}
235
236#[derive(Clone, Debug, Serialize, Deserialize)]
237pub struct SdkSystemConfig {
238    pub config: SystemConfig,
239}
240
241impl Default for SdkSystemConfig {
242    fn default() -> Self {
243        Self {
244            config: SystemConfig::default().with_continuations(),
245        }
246    }
247}
248
249impl From<SystemConfig> for SdkSystemConfig {
250    fn from(config: SystemConfig) -> Self {
251        Self { config }
252    }
253}
254
255/// A struct that is used to represent a unit struct in the config, used for
256/// serialization and deserialization.
257#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
258pub struct UnitStruct {}
259
260impl From<Rv32I> for UnitStruct {
261    fn from(_: Rv32I) -> Self {
262        UnitStruct {}
263    }
264}
265
266impl From<Rv32Io> for UnitStruct {
267    fn from(_: Rv32Io) -> Self {
268        UnitStruct {}
269    }
270}
271
272impl From<Keccak256> for UnitStruct {
273    fn from(_: Keccak256) -> Self {
274        UnitStruct {}
275    }
276}
277
278impl From<Sha256> for UnitStruct {
279    fn from(_: Sha256) -> Self {
280        UnitStruct {}
281    }
282}
283
284impl From<Native> for UnitStruct {
285    fn from(_: Native) -> Self {
286        UnitStruct {}
287    }
288}
289
290impl From<CastFExtension> for UnitStruct {
291    fn from(_: CastFExtension) -> Self {
292        UnitStruct {}
293    }
294}