use deepsize2::DeepSizeOf;
use enum_map::Enum;
use serde::{Deserialize, Serialize};
use strum::EnumIter;
use crate::{events::FieldOperation, RiscvAirId};
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Hash,
EnumIter,
Ord,
PartialOrd,
Serialize,
Deserialize,
Enum,
Default,
DeepSizeOf,
)]
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
#[repr(u32)]
pub enum SyscallCode {
#[default]
HALT = 0x00_00_00_00,
WRITE = 0x00_00_00_02,
ENTER_UNCONSTRAINED = 0x00_00_00_03,
EXIT_UNCONSTRAINED = 0x00_00_00_04,
SHA_EXTEND = 0x00_30_01_05,
SHA_COMPRESS = 0x00_01_01_06,
ED_ADD = 0x00_01_01_07,
ED_DECOMPRESS = 0x00_00_01_08,
KECCAK_PERMUTE = 0x00_01_01_09,
SECP256K1_ADD = 0x00_01_01_0A,
SECP256K1_DOUBLE = 0x00_00_01_0B,
SECP256K1_DECOMPRESS = 0x00_00_01_0C,
BN254_ADD = 0x00_01_01_0E,
BN254_DOUBLE = 0x00_00_01_0F,
COMMIT = 0x00_00_00_10,
COMMIT_DEFERRED_PROOFS = 0x00_00_00_1A,
VERIFY_SP1_PROOF = 0x00_00_00_1B,
BLS12381_DECOMPRESS = 0x00_00_01_1C,
HINT_LEN = 0x00_00_00_F0,
HINT_READ = 0x00_00_00_F1,
UINT256_MUL = 0x00_01_01_1D,
U256XU2048_MUL = 0x00_01_01_2F,
BLS12381_ADD = 0x00_01_01_1E,
BLS12381_DOUBLE = 0x00_00_01_1F,
BLS12381_FP_ADD = 0x00_01_01_20,
BLS12381_FP_SUB = 0x00_01_01_21,
BLS12381_FP_MUL = 0x00_01_01_22,
BLS12381_FP2_ADD = 0x00_01_01_23,
BLS12381_FP2_SUB = 0x00_01_01_24,
BLS12381_FP2_MUL = 0x00_01_01_25,
BN254_FP_ADD = 0x00_01_01_26,
BN254_FP_SUB = 0x00_01_01_27,
BN254_FP_MUL = 0x00_01_01_28,
BN254_FP2_ADD = 0x00_01_01_29,
BN254_FP2_SUB = 0x00_01_01_2A,
BN254_FP2_MUL = 0x00_01_01_2B,
SECP256R1_ADD = 0x00_01_01_2C,
SECP256R1_DOUBLE = 0x00_00_01_2D,
SECP256R1_DECOMPRESS = 0x00_00_01_2E,
UINT256_ADD_CARRY = 0x00_01_01_30,
UINT256_MUL_CARRY = 0x00_01_01_31,
#[allow(clippy::mistyped_literal_suffixes)]
MPROTECT = 0x00_00_01_32,
POSEIDON2 = 0x00_00_01_33,
}
impl SyscallCode {
#[must_use]
pub fn from_u32(value: u32) -> Self {
match value {
0x00_00_00_00 => SyscallCode::HALT,
0x00_00_00_02 => SyscallCode::WRITE,
0x00_00_00_03 => SyscallCode::ENTER_UNCONSTRAINED,
0x00_00_00_04 => SyscallCode::EXIT_UNCONSTRAINED,
0x00_30_01_05 => SyscallCode::SHA_EXTEND,
0x00_01_01_06 => SyscallCode::SHA_COMPRESS,
0x00_01_01_07 => SyscallCode::ED_ADD,
0x00_00_01_08 => SyscallCode::ED_DECOMPRESS,
0x00_01_01_09 => SyscallCode::KECCAK_PERMUTE,
0x00_01_01_0A => SyscallCode::SECP256K1_ADD,
0x00_00_01_0B => SyscallCode::SECP256K1_DOUBLE,
0x00_00_01_0C => SyscallCode::SECP256K1_DECOMPRESS,
0x00_01_01_0E => SyscallCode::BN254_ADD,
0x00_00_01_0F => SyscallCode::BN254_DOUBLE,
0x00_01_01_1E => SyscallCode::BLS12381_ADD,
0x00_00_01_1F => SyscallCode::BLS12381_DOUBLE,
0x00_00_00_10 => SyscallCode::COMMIT,
0x00_00_00_1A => SyscallCode::COMMIT_DEFERRED_PROOFS,
0x00_00_00_1B => SyscallCode::VERIFY_SP1_PROOF,
0x00_00_00_F0 => SyscallCode::HINT_LEN,
0x00_00_00_F1 => SyscallCode::HINT_READ,
0x00_01_01_1D => SyscallCode::UINT256_MUL,
0x00_01_01_2F => SyscallCode::U256XU2048_MUL,
0x00_01_01_20 => SyscallCode::BLS12381_FP_ADD,
0x00_01_01_21 => SyscallCode::BLS12381_FP_SUB,
0x00_01_01_22 => SyscallCode::BLS12381_FP_MUL,
0x00_01_01_23 => SyscallCode::BLS12381_FP2_ADD,
0x00_01_01_24 => SyscallCode::BLS12381_FP2_SUB,
0x00_01_01_25 => SyscallCode::BLS12381_FP2_MUL,
0x00_01_01_26 => SyscallCode::BN254_FP_ADD,
0x00_01_01_27 => SyscallCode::BN254_FP_SUB,
0x00_01_01_28 => SyscallCode::BN254_FP_MUL,
0x00_01_01_29 => SyscallCode::BN254_FP2_ADD,
0x00_01_01_2A => SyscallCode::BN254_FP2_SUB,
0x00_01_01_2B => SyscallCode::BN254_FP2_MUL,
0x00_00_01_1C => SyscallCode::BLS12381_DECOMPRESS,
0x00_01_01_2C => SyscallCode::SECP256R1_ADD,
0x00_00_01_2D => SyscallCode::SECP256R1_DOUBLE,
0x00_00_01_2E => SyscallCode::SECP256R1_DECOMPRESS,
0x00_01_01_30 => SyscallCode::UINT256_ADD_CARRY,
0x00_01_01_31 => SyscallCode::UINT256_MUL_CARRY,
#[allow(clippy::mistyped_literal_suffixes)]
0x00_00_01_32 => SyscallCode::MPROTECT,
0x00_00_01_33 => SyscallCode::POSEIDON2,
_ => panic!("invalid syscall number: {value}"),
}
}
#[must_use]
pub fn syscall_id(self) -> u32 {
(self as u32).to_le_bytes()[0].into()
}
#[must_use]
pub fn should_send(self) -> u32 {
(self as u32).to_le_bytes()[1].into()
}
#[must_use]
#[allow(clippy::match_same_arms)]
pub fn count_map(&self) -> Self {
match self {
SyscallCode::BN254_FP_SUB => SyscallCode::BN254_FP_ADD,
SyscallCode::BN254_FP_MUL => SyscallCode::BN254_FP_ADD,
SyscallCode::BN254_FP2_SUB => SyscallCode::BN254_FP2_ADD,
SyscallCode::BLS12381_FP_SUB => SyscallCode::BLS12381_FP_ADD,
SyscallCode::BLS12381_FP_MUL => SyscallCode::BLS12381_FP_ADD,
SyscallCode::BLS12381_FP2_SUB => SyscallCode::BLS12381_FP2_ADD,
_ => *self,
}
}
#[must_use]
pub fn fp_op_map(&self) -> FieldOperation {
match self {
SyscallCode::BLS12381_FP_ADD
| SyscallCode::BLS12381_FP2_ADD
| SyscallCode::BN254_FP_ADD
| SyscallCode::BN254_FP2_ADD => FieldOperation::Add,
SyscallCode::BLS12381_FP_SUB
| SyscallCode::BLS12381_FP2_SUB
| SyscallCode::BN254_FP_SUB
| SyscallCode::BN254_FP2_SUB => FieldOperation::Sub,
SyscallCode::BLS12381_FP_MUL
| SyscallCode::BLS12381_FP2_MUL
| SyscallCode::BN254_FP_MUL
| SyscallCode::BN254_FP2_MUL => FieldOperation::Mul,
_ => unreachable!(),
}
}
#[must_use]
pub fn uint256_op_map(&self) -> crate::events::Uint256Operation {
match self {
SyscallCode::UINT256_ADD_CARRY => crate::events::Uint256Operation::Add,
SyscallCode::UINT256_MUL_CARRY => crate::events::Uint256Operation::Mul,
_ => unreachable!(),
}
}
#[must_use]
pub fn as_air_id(self) -> Option<RiscvAirId> {
Some(match self {
SyscallCode::SHA_EXTEND => RiscvAirId::ShaExtend,
SyscallCode::SHA_COMPRESS => RiscvAirId::ShaCompress,
SyscallCode::ED_ADD => RiscvAirId::EdAddAssign,
SyscallCode::ED_DECOMPRESS => RiscvAirId::EdDecompress,
SyscallCode::KECCAK_PERMUTE => RiscvAirId::KeccakPermute,
SyscallCode::SECP256K1_ADD => RiscvAirId::Secp256k1AddAssign,
SyscallCode::SECP256K1_DOUBLE => RiscvAirId::Secp256k1DoubleAssign,
SyscallCode::BN254_ADD => RiscvAirId::Bn254AddAssign,
SyscallCode::BN254_DOUBLE => RiscvAirId::Bn254DoubleAssign,
SyscallCode::UINT256_MUL => RiscvAirId::Uint256MulMod,
SyscallCode::BLS12381_ADD => RiscvAirId::Bls12381AddAssign,
SyscallCode::BLS12381_DOUBLE => RiscvAirId::Bls12381DoubleAssign,
SyscallCode::BLS12381_FP_ADD
| SyscallCode::BLS12381_FP_SUB
| SyscallCode::BLS12381_FP_MUL => RiscvAirId::Bls12381FpOpAssign,
SyscallCode::BLS12381_FP2_ADD | SyscallCode::BLS12381_FP2_SUB => {
RiscvAirId::Bls12381Fp2AddSubAssign
}
SyscallCode::BLS12381_FP2_MUL => RiscvAirId::Bls12381Fp2MulAssign,
SyscallCode::BN254_FP_ADD | SyscallCode::BN254_FP_SUB | SyscallCode::BN254_FP_MUL => {
RiscvAirId::Bn254FpOpAssign
}
SyscallCode::BN254_FP2_ADD | SyscallCode::BN254_FP2_SUB => {
RiscvAirId::Bn254Fp2AddSubAssign
}
SyscallCode::BN254_FP2_MUL => RiscvAirId::Bn254Fp2MulAssign,
SyscallCode::SECP256R1_ADD => RiscvAirId::Secp256r1AddAssign,
SyscallCode::SECP256R1_DOUBLE => RiscvAirId::Secp256r1DoubleAssign,
SyscallCode::UINT256_ADD_CARRY | SyscallCode::UINT256_MUL_CARRY => {
RiscvAirId::Uint256Ops
}
SyscallCode::POSEIDON2 => RiscvAirId::Poseidon2,
SyscallCode::MPROTECT
| SyscallCode::U256XU2048_MUL
| SyscallCode::SECP256K1_DECOMPRESS
| SyscallCode::BLS12381_DECOMPRESS
| SyscallCode::SECP256R1_DECOMPRESS
| SyscallCode::HALT
| SyscallCode::WRITE
| SyscallCode::ENTER_UNCONSTRAINED
| SyscallCode::EXIT_UNCONSTRAINED
| SyscallCode::COMMIT
| SyscallCode::COMMIT_DEFERRED_PROOFS
| SyscallCode::VERIFY_SP1_PROOF
| SyscallCode::HINT_LEN
| SyscallCode::HINT_READ => return None,
})
}
#[must_use]
#[allow(clippy::match_same_arms)]
pub fn touched_addresses(&self) -> usize {
match self {
SyscallCode::SHA_EXTEND => 64,
SyscallCode::SHA_COMPRESS => 72,
SyscallCode::KECCAK_PERMUTE => 25,
SyscallCode::BLS12381_ADD
| SyscallCode::BLS12381_FP2_ADD
| SyscallCode::BLS12381_FP2_SUB
| SyscallCode::BLS12381_FP2_MUL => 24,
SyscallCode::BLS12381_DECOMPRESS
| SyscallCode::BLS12381_DOUBLE
| SyscallCode::BLS12381_FP_ADD
| SyscallCode::BLS12381_FP_SUB
| SyscallCode::BLS12381_FP_MUL => 12,
SyscallCode::ED_ADD
| SyscallCode::SECP256K1_ADD
| SyscallCode::SECP256R1_ADD
| SyscallCode::BN254_ADD
| SyscallCode::BN254_FP2_ADD
| SyscallCode::BN254_FP2_SUB
| SyscallCode::BN254_FP2_MUL => 16,
SyscallCode::ED_DECOMPRESS
| SyscallCode::SECP256K1_DECOMPRESS
| SyscallCode::SECP256R1_DECOMPRESS
| SyscallCode::SECP256K1_DOUBLE
| SyscallCode::SECP256R1_DOUBLE
| SyscallCode::BN254_DOUBLE
| SyscallCode::BN254_FP_ADD
| SyscallCode::BN254_FP_SUB
| SyscallCode::BN254_FP_MUL => 8,
SyscallCode::UINT256_MUL => 12,
SyscallCode::UINT256_ADD_CARRY | SyscallCode::UINT256_MUL_CARRY => 20,
SyscallCode::U256XU2048_MUL => 72,
SyscallCode::MPROTECT => 0,
SyscallCode::POSEIDON2 => 8,
_ => 0,
}
}
#[must_use]
#[allow(clippy::match_same_arms)]
pub fn touched_pages(&self) -> usize {
match self {
SyscallCode::SHA_EXTEND => 2 * 2,
SyscallCode::SHA_COMPRESS => 3 * 2,
SyscallCode::KECCAK_PERMUTE => 2 * 2,
SyscallCode::BLS12381_ADD
| SyscallCode::SECP256K1_ADD
| SyscallCode::SECP256R1_ADD
| SyscallCode::BN254_ADD
| SyscallCode::ED_ADD
| SyscallCode::BLS12381_DECOMPRESS
| SyscallCode::SECP256K1_DECOMPRESS
| SyscallCode::SECP256R1_DECOMPRESS
| SyscallCode::ED_DECOMPRESS => 2 * 2,
SyscallCode::BLS12381_DOUBLE
| SyscallCode::SECP256K1_DOUBLE
| SyscallCode::SECP256R1_DOUBLE
| SyscallCode::BN254_DOUBLE => 2,
SyscallCode::BLS12381_FP2_ADD
| SyscallCode::BLS12381_FP2_SUB
| SyscallCode::BLS12381_FP2_MUL
| SyscallCode::BLS12381_FP_ADD
| SyscallCode::BLS12381_FP_SUB
| SyscallCode::BLS12381_FP_MUL
| SyscallCode::BN254_FP2_ADD
| SyscallCode::BN254_FP2_SUB
| SyscallCode::BN254_FP2_MUL
| SyscallCode::BN254_FP_ADD
| SyscallCode::BN254_FP_SUB
| SyscallCode::BN254_FP_MUL => 2 * 2,
SyscallCode::UINT256_MUL => 2 * 2,
SyscallCode::UINT256_ADD_CARRY | SyscallCode::UINT256_MUL_CARRY => 5 * 2,
SyscallCode::U256XU2048_MUL => 4 * 2,
SyscallCode::MPROTECT => 1,
SyscallCode::POSEIDON2 => 2,
_ => 0,
}
}
}
impl std::fmt::Display for SyscallCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}