1use deepsize2::DeepSizeOf;
4use enum_map::Enum;
5use serde::{Deserialize, Serialize};
6use strum::EnumIter;
7
8use crate::{events::FieldOperation, RiscvAirId};
9
10#[derive(
27 Debug,
28 Copy,
29 Clone,
30 PartialEq,
31 Eq,
32 Hash,
33 EnumIter,
34 Ord,
35 PartialOrd,
36 Serialize,
37 Deserialize,
38 Enum,
39 Default,
40 DeepSizeOf,
41)]
42#[allow(non_camel_case_types)]
43#[allow(clippy::upper_case_acronyms)]
44#[repr(u32)]
45pub enum SyscallCode {
46 #[default]
48 HALT = 0x00_00_00_00,
49
50 WRITE = 0x00_00_00_02,
52
53 ENTER_UNCONSTRAINED = 0x00_00_00_03,
55
56 EXIT_UNCONSTRAINED = 0x00_00_00_04,
58
59 SHA_EXTEND = 0x00_30_01_05,
61
62 SHA_COMPRESS = 0x00_01_01_06,
64
65 ED_ADD = 0x00_01_01_07,
67
68 ED_DECOMPRESS = 0x00_00_01_08,
70
71 KECCAK_PERMUTE = 0x00_01_01_09,
73
74 SECP256K1_ADD = 0x00_01_01_0A,
76
77 SECP256K1_DOUBLE = 0x00_00_01_0B,
79
80 SECP256K1_DECOMPRESS = 0x00_00_01_0C,
82
83 BN254_ADD = 0x00_01_01_0E,
85
86 BN254_DOUBLE = 0x00_00_01_0F,
88
89 COMMIT = 0x00_00_00_10,
91
92 COMMIT_DEFERRED_PROOFS = 0x00_00_00_1A,
94
95 VERIFY_SP1_PROOF = 0x00_00_00_1B,
97
98 BLS12381_DECOMPRESS = 0x00_00_01_1C,
100
101 HINT_LEN = 0x00_00_00_F0,
103
104 HINT_READ = 0x00_00_00_F1,
106
107 UINT256_MUL = 0x00_01_01_1D,
109
110 U256XU2048_MUL = 0x00_01_01_2F,
112
113 BLS12381_ADD = 0x00_01_01_1E,
115
116 BLS12381_DOUBLE = 0x00_00_01_1F,
118
119 BLS12381_FP_ADD = 0x00_01_01_20,
121
122 BLS12381_FP_SUB = 0x00_01_01_21,
124
125 BLS12381_FP_MUL = 0x00_01_01_22,
127
128 BLS12381_FP2_ADD = 0x00_01_01_23,
130
131 BLS12381_FP2_SUB = 0x00_01_01_24,
133
134 BLS12381_FP2_MUL = 0x00_01_01_25,
136
137 BN254_FP_ADD = 0x00_01_01_26,
139
140 BN254_FP_SUB = 0x00_01_01_27,
142
143 BN254_FP_MUL = 0x00_01_01_28,
145
146 BN254_FP2_ADD = 0x00_01_01_29,
148
149 BN254_FP2_SUB = 0x00_01_01_2A,
151
152 BN254_FP2_MUL = 0x00_01_01_2B,
154
155 SECP256R1_ADD = 0x00_01_01_2C,
157
158 SECP256R1_DOUBLE = 0x00_00_01_2D,
160
161 SECP256R1_DECOMPRESS = 0x00_00_01_2E,
163
164 UINT256_ADD_CARRY = 0x00_01_01_30,
166
167 UINT256_MUL_CARRY = 0x00_01_01_31,
169
170 #[allow(clippy::mistyped_literal_suffixes)]
172 MPROTECT = 0x00_00_01_32,
173
174 POSEIDON2 = 0x00_00_01_33,
176}
177
178impl SyscallCode {
179 #[must_use]
181 pub fn from_u32(value: u32) -> Self {
182 match value {
183 0x00_00_00_00 => SyscallCode::HALT,
184 0x00_00_00_02 => SyscallCode::WRITE,
185 0x00_00_00_03 => SyscallCode::ENTER_UNCONSTRAINED,
186 0x00_00_00_04 => SyscallCode::EXIT_UNCONSTRAINED,
187 0x00_30_01_05 => SyscallCode::SHA_EXTEND,
188 0x00_01_01_06 => SyscallCode::SHA_COMPRESS,
189 0x00_01_01_07 => SyscallCode::ED_ADD,
190 0x00_00_01_08 => SyscallCode::ED_DECOMPRESS,
191 0x00_01_01_09 => SyscallCode::KECCAK_PERMUTE,
192 0x00_01_01_0A => SyscallCode::SECP256K1_ADD,
193 0x00_00_01_0B => SyscallCode::SECP256K1_DOUBLE,
194 0x00_00_01_0C => SyscallCode::SECP256K1_DECOMPRESS,
195 0x00_01_01_0E => SyscallCode::BN254_ADD,
196 0x00_00_01_0F => SyscallCode::BN254_DOUBLE,
197 0x00_01_01_1E => SyscallCode::BLS12381_ADD,
198 0x00_00_01_1F => SyscallCode::BLS12381_DOUBLE,
199 0x00_00_00_10 => SyscallCode::COMMIT,
200 0x00_00_00_1A => SyscallCode::COMMIT_DEFERRED_PROOFS,
201 0x00_00_00_1B => SyscallCode::VERIFY_SP1_PROOF,
202 0x00_00_00_F0 => SyscallCode::HINT_LEN,
203 0x00_00_00_F1 => SyscallCode::HINT_READ,
204 0x00_01_01_1D => SyscallCode::UINT256_MUL,
205 0x00_01_01_2F => SyscallCode::U256XU2048_MUL,
206 0x00_01_01_20 => SyscallCode::BLS12381_FP_ADD,
207 0x00_01_01_21 => SyscallCode::BLS12381_FP_SUB,
208 0x00_01_01_22 => SyscallCode::BLS12381_FP_MUL,
209 0x00_01_01_23 => SyscallCode::BLS12381_FP2_ADD,
210 0x00_01_01_24 => SyscallCode::BLS12381_FP2_SUB,
211 0x00_01_01_25 => SyscallCode::BLS12381_FP2_MUL,
212 0x00_01_01_26 => SyscallCode::BN254_FP_ADD,
213 0x00_01_01_27 => SyscallCode::BN254_FP_SUB,
214 0x00_01_01_28 => SyscallCode::BN254_FP_MUL,
215 0x00_01_01_29 => SyscallCode::BN254_FP2_ADD,
216 0x00_01_01_2A => SyscallCode::BN254_FP2_SUB,
217 0x00_01_01_2B => SyscallCode::BN254_FP2_MUL,
218 0x00_00_01_1C => SyscallCode::BLS12381_DECOMPRESS,
219 0x00_01_01_2C => SyscallCode::SECP256R1_ADD,
220 0x00_00_01_2D => SyscallCode::SECP256R1_DOUBLE,
221 0x00_00_01_2E => SyscallCode::SECP256R1_DECOMPRESS,
222 0x00_01_01_30 => SyscallCode::UINT256_ADD_CARRY,
223 0x00_01_01_31 => SyscallCode::UINT256_MUL_CARRY,
224 #[allow(clippy::mistyped_literal_suffixes)]
225 0x00_00_01_32 => SyscallCode::MPROTECT,
226 0x00_00_01_33 => SyscallCode::POSEIDON2,
227 _ => panic!("invalid syscall number: {value}"),
228 }
229 }
230
231 #[must_use]
233 pub fn syscall_id(self) -> u32 {
234 (self as u32).to_le_bytes()[0].into()
235 }
236
237 #[must_use]
239 pub fn should_send(self) -> u32 {
240 (self as u32).to_le_bytes()[1].into()
241 }
242
243 #[must_use]
245 #[allow(clippy::match_same_arms)]
246 pub fn count_map(&self) -> Self {
247 match self {
248 SyscallCode::BN254_FP_SUB => SyscallCode::BN254_FP_ADD,
249 SyscallCode::BN254_FP_MUL => SyscallCode::BN254_FP_ADD,
250 SyscallCode::BN254_FP2_SUB => SyscallCode::BN254_FP2_ADD,
251 SyscallCode::BLS12381_FP_SUB => SyscallCode::BLS12381_FP_ADD,
252 SyscallCode::BLS12381_FP_MUL => SyscallCode::BLS12381_FP_ADD,
253 SyscallCode::BLS12381_FP2_SUB => SyscallCode::BLS12381_FP2_ADD,
254 _ => *self,
255 }
256 }
257
258 #[must_use]
260 pub fn fp_op_map(&self) -> FieldOperation {
261 match self {
262 SyscallCode::BLS12381_FP_ADD
263 | SyscallCode::BLS12381_FP2_ADD
264 | SyscallCode::BN254_FP_ADD
265 | SyscallCode::BN254_FP2_ADD => FieldOperation::Add,
266 SyscallCode::BLS12381_FP_SUB
267 | SyscallCode::BLS12381_FP2_SUB
268 | SyscallCode::BN254_FP_SUB
269 | SyscallCode::BN254_FP2_SUB => FieldOperation::Sub,
270 SyscallCode::BLS12381_FP_MUL
271 | SyscallCode::BLS12381_FP2_MUL
272 | SyscallCode::BN254_FP_MUL
273 | SyscallCode::BN254_FP2_MUL => FieldOperation::Mul,
274 _ => unreachable!(),
275 }
276 }
277
278 #[must_use]
280 pub fn uint256_op_map(&self) -> crate::events::Uint256Operation {
281 match self {
282 SyscallCode::UINT256_ADD_CARRY => crate::events::Uint256Operation::Add,
283 SyscallCode::UINT256_MUL_CARRY => crate::events::Uint256Operation::Mul,
284 _ => unreachable!(),
285 }
286 }
287
288 #[must_use]
290 pub fn as_air_id(self) -> Option<RiscvAirId> {
291 Some(match self {
292 SyscallCode::SHA_EXTEND => RiscvAirId::ShaExtend,
293 SyscallCode::SHA_COMPRESS => RiscvAirId::ShaCompress,
294 SyscallCode::ED_ADD => RiscvAirId::EdAddAssign,
295 SyscallCode::ED_DECOMPRESS => RiscvAirId::EdDecompress,
296 SyscallCode::KECCAK_PERMUTE => RiscvAirId::KeccakPermute,
297 SyscallCode::SECP256K1_ADD => RiscvAirId::Secp256k1AddAssign,
298 SyscallCode::SECP256K1_DOUBLE => RiscvAirId::Secp256k1DoubleAssign,
299 SyscallCode::SECP256K1_DECOMPRESS => RiscvAirId::Secp256k1Decompress,
300 SyscallCode::BN254_ADD => RiscvAirId::Bn254AddAssign,
301 SyscallCode::BN254_DOUBLE => RiscvAirId::Bn254DoubleAssign,
302 SyscallCode::BLS12381_DECOMPRESS => RiscvAirId::Bls12381Decompress,
303 SyscallCode::UINT256_MUL => RiscvAirId::Uint256MulMod,
304 SyscallCode::U256XU2048_MUL => RiscvAirId::U256XU2048Mul,
305 SyscallCode::BLS12381_ADD => RiscvAirId::Bls12381AddAssign,
306 SyscallCode::BLS12381_DOUBLE => RiscvAirId::Bls12381DoubleAssign,
307 SyscallCode::BLS12381_FP_ADD
308 | SyscallCode::BLS12381_FP_SUB
309 | SyscallCode::BLS12381_FP_MUL => RiscvAirId::Bls12381FpOpAssign,
310 SyscallCode::BLS12381_FP2_ADD | SyscallCode::BLS12381_FP2_SUB => {
311 RiscvAirId::Bls12381Fp2AddSubAssign
312 }
313 SyscallCode::BLS12381_FP2_MUL => RiscvAirId::Bls12381Fp2MulAssign,
314 SyscallCode::BN254_FP_ADD | SyscallCode::BN254_FP_SUB | SyscallCode::BN254_FP_MUL => {
315 RiscvAirId::Bn254FpOpAssign
316 }
317 SyscallCode::BN254_FP2_ADD | SyscallCode::BN254_FP2_SUB => {
318 RiscvAirId::Bn254Fp2AddSubAssign
319 }
320 SyscallCode::BN254_FP2_MUL => RiscvAirId::Bn254Fp2MulAssign,
321 SyscallCode::SECP256R1_ADD => RiscvAirId::Secp256r1AddAssign,
322 SyscallCode::SECP256R1_DOUBLE => RiscvAirId::Secp256r1DoubleAssign,
323 SyscallCode::SECP256R1_DECOMPRESS => RiscvAirId::Secp256r1Decompress,
324 SyscallCode::UINT256_ADD_CARRY | SyscallCode::UINT256_MUL_CARRY => {
325 RiscvAirId::Uint256Ops
326 }
327 SyscallCode::MPROTECT => RiscvAirId::Mprotect,
328 SyscallCode::POSEIDON2 => RiscvAirId::Poseidon2,
329 SyscallCode::HALT
330 | SyscallCode::WRITE
331 | SyscallCode::ENTER_UNCONSTRAINED
332 | SyscallCode::EXIT_UNCONSTRAINED
333 | SyscallCode::COMMIT
334 | SyscallCode::COMMIT_DEFERRED_PROOFS
335 | SyscallCode::VERIFY_SP1_PROOF
336 | SyscallCode::HINT_LEN
337 | SyscallCode::HINT_READ => return None,
338 })
339 }
340
341 #[must_use]
343 #[allow(clippy::match_same_arms)]
344 pub fn touched_addresses(&self) -> usize {
345 match self {
349 SyscallCode::SHA_EXTEND => 64,
350 SyscallCode::SHA_COMPRESS => 72,
351 SyscallCode::KECCAK_PERMUTE => 25,
352 SyscallCode::BLS12381_ADD
353 | SyscallCode::BLS12381_FP2_ADD
354 | SyscallCode::BLS12381_FP2_SUB
355 | SyscallCode::BLS12381_FP2_MUL => 24,
356 SyscallCode::BLS12381_DECOMPRESS
357 | SyscallCode::BLS12381_DOUBLE
358 | SyscallCode::BLS12381_FP_ADD
359 | SyscallCode::BLS12381_FP_SUB
360 | SyscallCode::BLS12381_FP_MUL => 12,
361 SyscallCode::ED_ADD
362 | SyscallCode::SECP256K1_ADD
363 | SyscallCode::SECP256R1_ADD
364 | SyscallCode::BN254_ADD
365 | SyscallCode::BN254_FP2_ADD
366 | SyscallCode::BN254_FP2_SUB
367 | SyscallCode::BN254_FP2_MUL => 16,
368 SyscallCode::ED_DECOMPRESS
369 | SyscallCode::SECP256K1_DECOMPRESS
370 | SyscallCode::SECP256R1_DECOMPRESS
371 | SyscallCode::SECP256K1_DOUBLE
372 | SyscallCode::SECP256R1_DOUBLE
373 | SyscallCode::BN254_DOUBLE
374 | SyscallCode::BN254_FP_ADD
375 | SyscallCode::BN254_FP_SUB
376 | SyscallCode::BN254_FP_MUL => 8,
377 SyscallCode::UINT256_MUL => 12,
378 SyscallCode::UINT256_ADD_CARRY | SyscallCode::UINT256_MUL_CARRY => 20,
379 SyscallCode::U256XU2048_MUL => 72,
380 SyscallCode::MPROTECT => 0,
381 SyscallCode::POSEIDON2 => 8,
382 _ => 0,
383 }
384 }
385
386 #[must_use]
388 #[allow(clippy::match_same_arms)]
389 pub fn touched_pages(&self) -> usize {
390 match self {
394 SyscallCode::SHA_EXTEND => 2 * 2,
395 SyscallCode::SHA_COMPRESS => 3 * 2,
396 SyscallCode::KECCAK_PERMUTE => 2 * 2,
397 SyscallCode::BLS12381_ADD
398 | SyscallCode::SECP256K1_ADD
399 | SyscallCode::SECP256R1_ADD
400 | SyscallCode::BN254_ADD
401 | SyscallCode::ED_ADD
402 | SyscallCode::BLS12381_DECOMPRESS
403 | SyscallCode::SECP256K1_DECOMPRESS
404 | SyscallCode::SECP256R1_DECOMPRESS
405 | SyscallCode::ED_DECOMPRESS => 2 * 2,
406
407 SyscallCode::BLS12381_DOUBLE
408 | SyscallCode::SECP256K1_DOUBLE
409 | SyscallCode::SECP256R1_DOUBLE
410 | SyscallCode::BN254_DOUBLE => 2,
411
412 SyscallCode::BLS12381_FP2_ADD
413 | SyscallCode::BLS12381_FP2_SUB
414 | SyscallCode::BLS12381_FP2_MUL
415 | SyscallCode::BLS12381_FP_ADD
416 | SyscallCode::BLS12381_FP_SUB
417 | SyscallCode::BLS12381_FP_MUL
418 | SyscallCode::BN254_FP2_ADD
419 | SyscallCode::BN254_FP2_SUB
420 | SyscallCode::BN254_FP2_MUL
421 | SyscallCode::BN254_FP_ADD
422 | SyscallCode::BN254_FP_SUB
423 | SyscallCode::BN254_FP_MUL => 2 * 2,
424
425 SyscallCode::UINT256_MUL => 2 * 2,
426 SyscallCode::UINT256_ADD_CARRY | SyscallCode::UINT256_MUL_CARRY => 5 * 2,
427 SyscallCode::U256XU2048_MUL => 4 * 2,
428 SyscallCode::MPROTECT => 1,
429 SyscallCode::POSEIDON2 => 2,
430 _ => 0,
431 }
432 }
433}
434
435impl std::fmt::Display for SyscallCode {
436 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
437 write!(f, "{self:?}")
438 }
439}