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