sp1_lib/
lib.rs

1//! Syscalls for the SP1 zkVM.
2//!
3//! Documentation for these syscalls can be found in the zkVM entrypoint
4//! `sp1_zkvm::syscalls` module.
5
6pub mod bls12381;
7pub mod bn254;
8
9#[cfg(feature = "ecdsa")]
10pub mod ecdsa;
11
12pub mod ed25519;
13pub mod io;
14pub mod secp256k1;
15pub mod secp256r1;
16pub mod unconstrained;
17pub mod utils;
18
19#[cfg(feature = "verify")]
20pub mod verify;
21
22extern "C" {
23    /// Halts the program with the given exit code.
24    pub fn syscall_halt(exit_code: u8) -> !;
25
26    /// Writes the bytes in the given buffer to the given file descriptor.
27    pub fn syscall_write(fd: u32, write_buf: *const u8, nbytes: usize);
28
29    /// Reads the bytes from the given file descriptor into the given buffer.
30    pub fn syscall_read(fd: u32, read_buf: *mut u8, nbytes: usize);
31
32    /// Executes the SHA-256 extend operation on the given word array.
33    pub fn syscall_sha256_extend(w: *mut [u32; 64]);
34
35    /// Executes the SHA-256 compress operation on the given word array and a given state.
36    pub fn syscall_sha256_compress(w: *mut [u32; 64], state: *mut [u32; 8]);
37
38    /// Executes an Ed25519 curve addition on the given points.
39    pub fn syscall_ed_add(p: *mut [u32; 16], q: *const [u32; 16]);
40
41    /// Executes an Ed25519 curve decompression on the given point.
42    pub fn syscall_ed_decompress(point: &mut [u8; 64]);
43
44    /// Executes an Sepc256k1 curve addition on the given points.
45    pub fn syscall_secp256k1_add(p: *mut [u32; 16], q: *const [u32; 16]);
46
47    /// Executes an Secp256k1 curve doubling on the given point.
48    pub fn syscall_secp256k1_double(p: *mut [u32; 16]);
49
50    /// Executes an Secp256k1 curve decompression on the given point.
51    pub fn syscall_secp256k1_decompress(point: &mut [u8; 64], is_odd: bool);
52
53    /// Executes an Secp256r1 curve addition on the given points.
54    pub fn syscall_secp256r1_add(p: *mut [u32; 16], q: *const [u32; 16]);
55
56    /// Executes an Secp256r1 curve doubling on the given point.
57    pub fn syscall_secp256r1_double(p: *mut [u32; 16]);
58
59    /// Executes an Secp256r1 curve decompression on the given point.
60    pub fn syscall_secp256r1_decompress(point: &mut [u8; 64], is_odd: bool);
61
62    /// Executes a Bn254 curve addition on the given points.
63    pub fn syscall_bn254_add(p: *mut [u32; 16], q: *const [u32; 16]);
64
65    /// Executes a Bn254 curve doubling on the given point.
66    pub fn syscall_bn254_double(p: *mut [u32; 16]);
67
68    /// Executes a BLS12-381 curve addition on the given points.
69    pub fn syscall_bls12381_add(p: *mut [u32; 24], q: *const [u32; 24]);
70
71    /// Executes a BLS12-381 curve doubling on the given point.
72    pub fn syscall_bls12381_double(p: *mut [u32; 24]);
73
74    /// Executes the Keccak-256 permutation on the given state.
75    pub fn syscall_keccak_permute(state: *mut [u64; 25]);
76
77    /// Executes an uint256 multiplication on the given inputs.
78    pub fn syscall_uint256_mulmod(x: *mut [u32; 8], y: *const [u32; 8]);
79
80    /// Executes a 256-bit by 2048-bit multiplication on the given inputs.
81    pub fn syscall_u256x2048_mul(
82        x: *const [u32; 8],
83        y: *const [u32; 64],
84        lo: *mut [u32; 64],
85        hi: *mut [u32; 8],
86    );
87    /// Enters unconstrained mode.
88    pub fn syscall_enter_unconstrained() -> bool;
89
90    /// Exits unconstrained mode.
91    pub fn syscall_exit_unconstrained();
92
93    /// Defers the verification of a valid SP1 zkVM proof.
94    pub fn syscall_verify_sp1_proof(vk_digest: &[u32; 8], pv_digest: &[u8; 32]);
95
96    /// Returns the length of the next element in the hint stream.
97    pub fn syscall_hint_len() -> usize;
98
99    /// Reads the next element in the hint stream into the given buffer.
100    pub fn syscall_hint_read(ptr: *mut u8, len: usize);
101
102    /// Allocates a buffer aligned to the given alignment.
103    pub fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u8;
104
105    /// Decompresses a BLS12-381 point.
106    pub fn syscall_bls12381_decompress(point: &mut [u8; 96], is_odd: bool);
107
108    /// Computes a big integer operation with a modulus.
109    pub fn sys_bigint(
110        result: *mut [u32; 8],
111        op: u32,
112        x: *const [u32; 8],
113        y: *const [u32; 8],
114        modulus: *const [u32; 8],
115    );
116
117    /// Executes a BLS12-381 field addition on the given inputs.
118    pub fn syscall_bls12381_fp_addmod(p: *mut u32, q: *const u32);
119
120    /// Executes a BLS12-381 field subtraction on the given inputs.
121    pub fn syscall_bls12381_fp_submod(p: *mut u32, q: *const u32);
122
123    /// Executes a BLS12-381 field multiplication on the given inputs.
124    pub fn syscall_bls12381_fp_mulmod(p: *mut u32, q: *const u32);
125
126    /// Executes a BLS12-381 Fp2 addition on the given inputs.
127    pub fn syscall_bls12381_fp2_addmod(p: *mut u32, q: *const u32);
128
129    /// Executes a BLS12-381 Fp2 subtraction on the given inputs.
130    pub fn syscall_bls12381_fp2_submod(p: *mut u32, q: *const u32);
131
132    /// Executes a BLS12-381 Fp2 multiplication on the given inputs.
133    pub fn syscall_bls12381_fp2_mulmod(p: *mut u32, q: *const u32);
134
135    /// Executes a BN254 field addition on the given inputs.
136    pub fn syscall_bn254_fp_addmod(p: *mut u32, q: *const u32);
137
138    /// Executes a BN254 field subtraction on the given inputs.
139    pub fn syscall_bn254_fp_submod(p: *mut u32, q: *const u32);
140
141    /// Executes a BN254 field multiplication on the given inputs.
142    pub fn syscall_bn254_fp_mulmod(p: *mut u32, q: *const u32);
143
144    /// Executes a BN254 Fp2 addition on the given inputs.
145    pub fn syscall_bn254_fp2_addmod(p: *mut u32, q: *const u32);
146
147    /// Executes a BN254 Fp2 subtraction on the given inputs.
148    pub fn syscall_bn254_fp2_submod(p: *mut u32, q: *const u32);
149
150    /// Executes a BN254 Fp2 multiplication on the given inputs.
151    pub fn syscall_bn254_fp2_mulmod(p: *mut u32, q: *const u32);
152
153    /// Reads a buffer from the input stream.
154    pub fn read_vec_raw() -> ReadVecResult;
155}
156
157#[repr(C)]
158pub struct ReadVecResult {
159    pub ptr: *mut u8,
160    pub len: usize,
161    pub capacity: usize,
162}