Skip to main content

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