openvm_sdk/config/
global.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use bon::Builder;
use derive_more::derive::From;
use openvm_algebra_circuit::{
    Fp2Extension, Fp2ExtensionExecutor, Fp2ExtensionPeriphery, ModularExtension,
    ModularExtensionExecutor, ModularExtensionPeriphery,
};
use openvm_algebra_transpiler::{Fp2TranspilerExtension, ModularTranspilerExtension};
use openvm_bigint_circuit::{Int256, Int256Executor, Int256Periphery};
use openvm_bigint_transpiler::Int256TranspilerExtension;
use openvm_circuit::{
    arch::{
        SystemConfig, SystemExecutor, SystemPeriphery, VmChipComplex, VmConfig, VmInventoryError,
    },
    circuit_derive::{Chip, ChipUsageGetter},
    derive::{AnyEnum, InstructionExecutor},
};
use openvm_ecc_circuit::{
    WeierstrassExtension, WeierstrassExtensionExecutor, WeierstrassExtensionPeriphery,
};
use openvm_ecc_transpiler::EccTranspilerExtension;
use openvm_keccak256_circuit::{Keccak256, Keccak256Executor, Keccak256Periphery};
use openvm_keccak256_transpiler::Keccak256TranspilerExtension;
use openvm_native_circuit::{Native, NativeExecutor, NativePeriphery};
use openvm_pairing_circuit::{
    PairingExtension, PairingExtensionExecutor, PairingExtensionPeriphery,
};
use openvm_pairing_transpiler::PairingTranspilerExtension;
use openvm_rv32im_circuit::{
    Rv32I, Rv32IExecutor, Rv32IPeriphery, Rv32Io, Rv32IoExecutor, Rv32IoPeriphery, Rv32M,
    Rv32MExecutor, Rv32MPeriphery,
};
use openvm_rv32im_transpiler::{
    Rv32ITranspilerExtension, Rv32IoTranspilerExtension, Rv32MTranspilerExtension,
};
use openvm_stark_backend::p3_field::PrimeField32;
use openvm_transpiler::transpiler::Transpiler;
use serde::{Deserialize, Serialize};

use crate::F;

#[derive(Builder, Clone, Debug, Serialize, Deserialize)]
pub struct SdkVmConfig {
    #[serde(default)]
    pub system: SdkSystemConfig,

    pub rv32i: Option<UnitStruct>,
    pub io: Option<UnitStruct>,
    pub keccak: Option<UnitStruct>,
    pub native: Option<UnitStruct>,

    pub rv32m: Option<Rv32M>,
    pub bigint: Option<Int256>,
    pub modular: Option<ModularExtension>,
    pub fp2: Option<Fp2Extension>,
    pub pairing: Option<PairingExtension>,
    pub ecc: Option<WeierstrassExtension>,
}

#[derive(ChipUsageGetter, Chip, InstructionExecutor, From, AnyEnum)]
pub enum SdkVmConfigExecutor<F: PrimeField32> {
    #[any_enum]
    System(SystemExecutor<F>),
    #[any_enum]
    Rv32i(Rv32IExecutor<F>),
    #[any_enum]
    Io(Rv32IoExecutor<F>),
    #[any_enum]
    Keccak(Keccak256Executor<F>),
    #[any_enum]
    Native(NativeExecutor<F>),
    #[any_enum]
    Rv32m(Rv32MExecutor<F>),
    #[any_enum]
    BigInt(Int256Executor<F>),
    #[any_enum]
    Modular(ModularExtensionExecutor<F>),
    #[any_enum]
    Fp2(Fp2ExtensionExecutor<F>),
    #[any_enum]
    Pairing(PairingExtensionExecutor<F>),
    #[any_enum]
    Ecc(WeierstrassExtensionExecutor<F>),
}

#[derive(From, ChipUsageGetter, Chip, AnyEnum)]
pub enum SdkVmConfigPeriphery<F: PrimeField32> {
    #[any_enum]
    System(SystemPeriphery<F>),
    #[any_enum]
    Rv32i(Rv32IPeriphery<F>),
    #[any_enum]
    Io(Rv32IoPeriphery<F>),
    #[any_enum]
    Keccak(Keccak256Periphery<F>),
    #[any_enum]
    Native(NativePeriphery<F>),
    #[any_enum]
    Rv32m(Rv32MPeriphery<F>),
    #[any_enum]
    BigInt(Int256Periphery<F>),
    #[any_enum]
    Modular(ModularExtensionPeriphery<F>),
    #[any_enum]
    Fp2(Fp2ExtensionPeriphery<F>),
    #[any_enum]
    Pairing(PairingExtensionPeriphery<F>),
    #[any_enum]
    Ecc(WeierstrassExtensionPeriphery<F>),
}

impl SdkVmConfig {
    pub fn transpiler(&self) -> Transpiler<F> {
        let mut transpiler = Transpiler::default();
        if self.rv32i.is_some() {
            transpiler = transpiler.with_extension(Rv32ITranspilerExtension);
        }
        if self.io.is_some() {
            transpiler = transpiler.with_extension(Rv32IoTranspilerExtension);
        }
        if self.keccak.is_some() {
            transpiler = transpiler.with_extension(Keccak256TranspilerExtension);
        }
        if self.rv32m.is_some() {
            transpiler = transpiler.with_extension(Rv32MTranspilerExtension);
        }
        if self.bigint.is_some() {
            transpiler = transpiler.with_extension(Int256TranspilerExtension);
        }
        if self.modular.is_some() {
            transpiler = transpiler.with_extension(ModularTranspilerExtension);
        }
        if self.fp2.is_some() {
            transpiler = transpiler.with_extension(Fp2TranspilerExtension);
        }
        if self.pairing.is_some() {
            transpiler = transpiler.with_extension(PairingTranspilerExtension);
        }
        if self.ecc.is_some() {
            transpiler = transpiler.with_extension(EccTranspilerExtension);
        }
        transpiler
    }
}

impl<F: PrimeField32> VmConfig<F> for SdkVmConfig {
    type Executor = SdkVmConfigExecutor<F>;
    type Periphery = SdkVmConfigPeriphery<F>;

    fn system(&self) -> &SystemConfig {
        &self.system.config
    }

    fn system_mut(&mut self) -> &mut SystemConfig {
        &mut self.system.config
    }

    fn create_chip_complex(
        &self,
    ) -> Result<VmChipComplex<F, Self::Executor, Self::Periphery>, VmInventoryError> {
        let mut complex = self.system.config.create_chip_complex()?.transmute();

        if self.rv32i.is_some() {
            complex = complex.extend(&Rv32I)?;
        }
        if self.io.is_some() {
            complex = complex.extend(&Rv32Io)?;
        }
        if self.keccak.is_some() {
            complex = complex.extend(&Keccak256)?;
        }
        if self.native.is_some() {
            complex = complex.extend(&Native)?;
        }

        if let Some(rv32m) = self.rv32m {
            let mut rv32m = rv32m;
            if let Some(ref bigint) = self.bigint {
                rv32m.range_tuple_checker_sizes[0] =
                    rv32m.range_tuple_checker_sizes[0].max(bigint.range_tuple_checker_sizes[0]);
                rv32m.range_tuple_checker_sizes[1] =
                    rv32m.range_tuple_checker_sizes[1].max(bigint.range_tuple_checker_sizes[1]);
            }
            complex = complex.extend(&rv32m)?;
        }
        if let Some(bigint) = self.bigint {
            let mut bigint = bigint;
            if let Some(ref rv32m) = self.rv32m {
                bigint.range_tuple_checker_sizes[0] =
                    rv32m.range_tuple_checker_sizes[0].max(bigint.range_tuple_checker_sizes[0]);
                bigint.range_tuple_checker_sizes[1] =
                    rv32m.range_tuple_checker_sizes[1].max(bigint.range_tuple_checker_sizes[1]);
            }
            complex = complex.extend(&bigint)?;
        }
        if let Some(ref modular) = self.modular {
            complex = complex.extend(modular)?;
        }
        if let Some(ref fp2) = self.fp2 {
            complex = complex.extend(fp2)?;
        }
        if let Some(ref pairing) = self.pairing {
            complex = complex.extend(pairing)?;
        }
        if let Some(ref ecc) = self.ecc {
            complex = complex.extend(ecc)?;
        }

        Ok(complex)
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SdkSystemConfig {
    pub config: SystemConfig,
}

impl Default for SdkSystemConfig {
    fn default() -> Self {
        Self {
            config: SystemConfig::default().with_continuations(),
        }
    }
}

impl From<SystemConfig> for SdkSystemConfig {
    fn from(config: SystemConfig) -> Self {
        Self { config }
    }
}

/// A struct that is used to represent a unit struct in the config, used for
/// serialization and deserialization.
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
pub struct UnitStruct {}

impl From<Rv32I> for UnitStruct {
    fn from(_: Rv32I) -> Self {
        UnitStruct {}
    }
}

impl From<Rv32Io> for UnitStruct {
    fn from(_: Rv32Io) -> Self {
        UnitStruct {}
    }
}

impl From<Keccak256> for UnitStruct {
    fn from(_: Keccak256) -> Self {
        UnitStruct {}
    }
}

impl From<Native> for UnitStruct {
    fn from(_: Native) -> Self {
        UnitStruct {}
    }
}