sp1_core_executor/syscalls/code.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
use enum_map::Enum;
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
/// System Calls.
///
/// A system call is invoked by the the `ecall` instruction with a specific value in register t0.
/// The syscall number is a 32-bit integer with the following little-endian layout:
///
/// | Byte 0 | Byte 1 | Byte 2 | Byte 3 |
/// | ------ | ------ | ------ | ------ |
/// | ID | Table | Cycles | Unused |
///
/// where:
/// - Byte 0: The system call identifier.
/// - Byte 1: Whether the handler of the system call has its own table. This is used in the CPU
/// table to determine whether to lookup the syscall using the syscall interaction.
/// - Byte 2: The number of additional cycles the syscall uses. This is used to make sure the # of
/// memory accesses is bounded.
/// - Byte 3: Currently unused.
#[derive(
Debug, Copy, Clone, PartialEq, Eq, Hash, EnumIter, Ord, PartialOrd, Serialize, Deserialize, Enum,
)]
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
pub enum SyscallCode {
/// Halts the program.
HALT = 0x00_00_00_00,
/// Write to the output buffer.
WRITE = 0x00_00_00_02,
/// Enter unconstrained block.
ENTER_UNCONSTRAINED = 0x00_00_00_03,
/// Exit unconstrained block.
EXIT_UNCONSTRAINED = 0x00_00_00_04,
/// Executes the `SHA_EXTEND` precompile.
SHA_EXTEND = 0x00_30_01_05,
/// Executes the `SHA_COMPRESS` precompile.
SHA_COMPRESS = 0x00_01_01_06,
/// Executes the `ED_ADD` precompile.
ED_ADD = 0x00_01_01_07,
/// Executes the `ED_DECOMPRESS` precompile.
ED_DECOMPRESS = 0x00_00_01_08,
/// Executes the `KECCAK_PERMUTE` precompile.
KECCAK_PERMUTE = 0x00_01_01_09,
/// Executes the `SECP256K1_ADD` precompile.
SECP256K1_ADD = 0x00_01_01_0A,
/// Executes the `SECP256K1_DOUBLE` precompile.
SECP256K1_DOUBLE = 0x00_00_01_0B,
/// Executes the `SECP256K1_DECOMPRESS` precompile.
SECP256K1_DECOMPRESS = 0x00_00_01_0C,
/// Executes the `BN254_ADD` precompile.
BN254_ADD = 0x00_01_01_0E,
/// Executes the `BN254_DOUBLE` precompile.
BN254_DOUBLE = 0x00_00_01_0F,
/// Executes the `COMMIT` precompile.
COMMIT = 0x00_00_00_10,
/// Executes the `COMMIT_DEFERRED_PROOFS` precompile.
COMMIT_DEFERRED_PROOFS = 0x00_00_00_1A,
/// Executes the `VERIFY_SP1_PROOF` precompile.
VERIFY_SP1_PROOF = 0x00_00_00_1B,
/// Executes the `BLS12381_DECOMPRESS` precompile.
BLS12381_DECOMPRESS = 0x00_00_01_1C,
/// Executes the `HINT_LEN` precompile.
HINT_LEN = 0x00_00_00_F0,
/// Executes the `HINT_READ` precompile.
HINT_READ = 0x00_00_00_F1,
/// Executes the `UINT256_MUL` precompile.
UINT256_MUL = 0x00_01_01_1D,
/// Executes the `BLS12381_ADD` precompile.
BLS12381_ADD = 0x00_01_01_1E,
/// Executes the `BLS12381_DOUBLE` precompile.
BLS12381_DOUBLE = 0x00_00_01_1F,
/// Executes the `BLS12381_FP_ADD` precompile.
BLS12381_FP_ADD = 0x00_01_01_20,
/// Executes the `BLS12381_FP_SUB` precompile.
BLS12381_FP_SUB = 0x00_01_01_21,
/// Executes the `BLS12381_FP_MUL` precompile.
BLS12381_FP_MUL = 0x00_01_01_22,
/// Executes the `BLS12381_FP2_ADD` precompile.
BLS12381_FP2_ADD = 0x00_01_01_23,
/// Executes the `BLS12381_FP2_SUB` precompile.
BLS12381_FP2_SUB = 0x00_01_01_24,
/// Executes the `BLS12381_FP2_MUL` precompile.
BLS12381_FP2_MUL = 0x00_01_01_25,
/// Executes the `BN254_FP_ADD` precompile.
BN254_FP_ADD = 0x00_01_01_26,
/// Executes the `BN254_FP_SUB` precompile.
BN254_FP_SUB = 0x00_01_01_27,
/// Executes the `BN254_FP_MUL` precompile.
BN254_FP_MUL = 0x00_01_01_28,
/// Executes the `BN254_FP2_ADD` precompile.
BN254_FP2_ADD = 0x00_01_01_29,
/// Executes the `BN254_FP2_SUB` precompile.
BN254_FP2_SUB = 0x00_01_01_2A,
/// Executes the `BN254_FP2_MUL` precompile.
BN254_FP2_MUL = 0x00_01_01_2B,
}
impl SyscallCode {
/// Create a [`SyscallCode`] from a u32.
#[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_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,
_ => panic!("invalid syscall number: {value}"),
}
}
/// Get the system call identifier.
#[must_use]
pub fn syscall_id(self) -> u32 {
(self as u32).to_le_bytes()[0].into()
}
/// Get whether the handler of the system call has its own table.
#[must_use]
pub fn should_send(self) -> u32 {
(self as u32).to_le_bytes()[1].into()
}
/// Get the number of additional cycles the syscall uses.
#[must_use]
pub fn num_cycles(self) -> u32 {
(self as u32).to_le_bytes()[2].into()
}
/// Map a syscall to another one in order to coalesce their counts.
#[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,
}
}
}
impl std::fmt::Display for SyscallCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}