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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use super::Prover;
use crate::{SP1ProofMode, SP1ProofWithPublicValues, StatusCode};
use sp1_core_executor::SP1ContextBuilder;
use sp1_core_machine::io::SP1Stdin;
/// A unified collection of methods for all prover types.
pub trait ProveRequest<'a, P>
where
Self: Sized,
P: Prover + 'a,
{
/// Get the base request for the prover.
fn base(&mut self) -> &mut BaseProveRequest<'a, P>;
/// Run the prove request.
fn run(self) -> Result<SP1ProofWithPublicValues, P::Error>;
/// Set the proof mode to the given [`SP1ProofKind`].
///
/// # Details
/// This method is useful for setting the proof mode to a custom mode.
///
/// # Example
/// ```rust,no_run
/// use sp1_sdk::blocking::{Elf, ProveRequest, Prover, ProverClient, SP1ProofMode, SP1Stdin};
///
/// let elf = Elf::Static(&[1, 2, 3]);
/// let stdin = SP1Stdin::new();
///
/// let client = ProverClient::builder().cpu().build();
/// let pk = client.setup(elf).unwrap();
/// let proof = client.prove(&pk, stdin).mode(SP1ProofMode::Groth16).run().unwrap();
/// ```
#[must_use]
fn mode(mut self, mode: SP1ProofMode) -> Self {
self.base().mode(mode);
self
}
/// Set the proof kind to [`SP1ProofKind::Compressed`] mode.
///
/// # Details
/// This mode produces a proof that is of constant size, regardless of the number of cycles. It
/// takes longer to prove than [`SP1ProofKind::Core`] due to the need to recursively aggregate
/// proofs into a single proof.
///
/// # Example
/// ```rust,no_run
/// use sp1_sdk::blocking::{Elf, ProveRequest, Prover, ProverClient, SP1ProofMode, SP1Stdin};
///
/// let elf = Elf::Static(&[1, 2, 3]);
/// let stdin = SP1Stdin::new();
///
/// let client = ProverClient::builder().cpu().build();
/// let pk = client.setup(elf).unwrap();
/// let proof = client.prove(&pk, stdin).compressed().run().unwrap();
/// ```
#[must_use]
fn compressed(mut self) -> Self {
self.base().compressed();
self
}
/// Set the proof mode to [`SP1ProofKind::Plonk`] mode.
///
/// # Details
/// This mode produces a const size PLONK proof that can be verified on chain for roughly ~300k
/// gas. This mode is useful for producing a maximally small proof that can be verified on
/// chain. For more efficient SNARK wrapping, you can use the [`SP1ProofKind::Groth16`] mode but
/// this mode is more .
///
/// # Example
/// ```rust,no_run
/// use sp1_sdk::blocking::{Elf, ProveRequest, Prover, ProverClient, SP1ProofMode, SP1Stdin};
///
/// let elf = Elf::Static(&[1, 2, 3]);
/// let stdin = SP1Stdin::new();
///
/// let client = ProverClient::builder().cpu().build();
/// let pk = client.setup(elf).unwrap();
/// let proof = client.prove(&pk, stdin).plonk().run().unwrap();
/// ```
#[must_use]
fn plonk(mut self) -> Self {
self.base().plonk();
self
}
/// Set the proof mode to [`SP1ProofKind::Groth16`] mode.
///
/// # Details
/// This mode produces a Groth16 proof that can be verified on chain for roughly ~100k gas. This
/// mode is useful for producing a proof that can be verified on chain with minimal gas.
///
/// # Example
/// ```rust,no_run
/// use sp1_sdk::blocking::{Elf, ProveRequest, Prover, ProverClient, SP1ProofMode, SP1Stdin};
///
/// let elf = Elf::Static(&[1, 2, 3]);
/// let stdin = SP1Stdin::new();
///
/// let client = ProverClient::builder().cpu().build();
/// let pk = client.setup(elf).unwrap();
/// let builder = client.prove(&pk, stdin).groth16().run();
/// ```
#[must_use]
fn groth16(mut self) -> Self {
self.base().groth16();
self
}
/// Set the proof kind to [`SP1ProofKind::Core`] mode.
///
/// # Details
/// This is the default mode for the prover. The proofs grow linearly in size with the number
/// of cycles.
///
/// # Example
/// ```rust,no_run
/// use sp1_sdk::blocking::{Elf, ProveRequest, Prover, ProverClient, SP1ProofMode, SP1Stdin};
///
/// let elf = Elf::Static(&[1, 2, 3]);
/// let stdin = SP1Stdin::new();
///
/// let client = ProverClient::builder().cpu().build();
/// let pk = client.setup(elf).unwrap();
/// let builder = client.prove(&pk, stdin).core().run();
/// ```
#[must_use]
fn core(mut self) -> Self {
self.base().core();
self
}
/// Set the maximum number of cpu cycles to use for execution.
///
/// # Details
/// If the cycle limit is exceeded, execution will return
/// [`sp1_core_executor::ExecutionError::ExceededCycleLimit`].
///
/// # Example
/// ```rust,no_run
/// use sp1_sdk::blocking::{Elf, ProveRequest, Prover, ProverClient, SP1ProofMode, SP1Stdin};
///
/// let elf = Elf::Static(&[1, 2, 3]);
/// let stdin = SP1Stdin::new();
///
/// let client = ProverClient::builder().cpu().build();
/// let pk = client.setup(elf).unwrap();
/// let builder = client.prove(&pk, stdin).cycle_limit(1000000).run();
/// ```
#[must_use]
fn cycle_limit(mut self, cycle_limit: u64) -> Self {
self.base().cycle_limit(cycle_limit);
self
}
/// Whether to enable deferred proof verification in the executor.
///
/// # Arguments
/// * `value` - Whether to enable deferred proof verification in the executor.
///
/// # Details
/// Default: `true`. If set to `false`, the executor will skip deferred proof verification.
/// This is useful for reducing the execution time of the program and optimistically assuming
/// that the deferred proofs are correct. Can also be used for mock proof setups that require
/// verifying mock compressed proofs.
///
/// # Example
/// ```rust,no_run
/// use sp1_sdk::blocking::{Elf, ProveRequest, Prover, ProverClient, SP1ProofMode, SP1Stdin};
///
/// let elf = Elf::Static(&[1, 2, 3]);
/// let stdin = SP1Stdin::new();
///
/// let client = ProverClient::builder().cpu().build();
/// let pk = client.setup(elf).unwrap();
/// let proof = client.prove(&pk, stdin).deferred_proof_verification(false).run().unwrap();
/// ```
#[must_use]
fn deferred_proof_verification(mut self, value: bool) -> Self {
self.base().deferred_proof_verification(value);
self
}
/// Set the expected exit code of the program.
///
/// # Arguments
/// * `code` - The expected exit code of the program.
#[must_use]
fn expected_exit_code(mut self, code: StatusCode) -> Self {
self.base().expected_exit_code(code);
self
}
/// Set the proof nonce for this execution.
///
/// The nonce ensures each proof is unique even for identical programs and inputs.
/// If not provided, will default to 0.
///
/// # Arguments
/// * `nonce` - A 4-element array representing 128 bits of nonce data.
#[must_use]
fn with_proof_nonce(mut self, nonce: [u32; 4]) -> Self {
self.base().context_builder.proof_nonce(nonce);
self
}
}
/// The base prove request for a prover.
///
/// This exposes all the options that are shared across different prover types.
pub struct BaseProveRequest<'a, P: Prover> {
pub(crate) prover: &'a P,
pub(crate) pk: &'a P::ProvingKey,
pub(crate) stdin: SP1Stdin,
pub(crate) mode: SP1ProofMode,
pub(crate) context_builder: SP1ContextBuilder<'static>,
}
impl<'a, P: Prover> BaseProveRequest<'a, P> {
/// Create a new [`BaseProveRequest`] with the given prover, proving key, and stdin.
///
/// # Arguments
/// * `prover` - The prover to use.
/// * `pk` - The proving key to use.
/// * `stdin` - The stdin to use.
pub const fn new(prover: &'a P, pk: &'a P::ProvingKey, stdin: SP1Stdin) -> Self {
Self {
prover,
pk,
stdin,
mode: SP1ProofMode::Core,
context_builder: SP1ContextBuilder::new(),
}
}
/// See [`ProveRequest::compressed`].
pub fn compressed(&mut self) {
self.mode = SP1ProofMode::Compressed;
}
/// See [`ProveRequest::plonk`].
pub fn plonk(&mut self) {
self.mode = SP1ProofMode::Plonk;
}
/// See [`ProveRequest::groth16`].
pub fn groth16(&mut self) {
self.mode = SP1ProofMode::Groth16;
}
/// See [`ProveRequest::core`].
pub fn core(&mut self) {
self.mode = SP1ProofMode::Core;
}
/// See [`ProveRequest::mode`].
pub fn mode(&mut self, mode: SP1ProofMode) {
self.mode = mode;
}
/// See [`ProveRequest::cycle_limit`].
pub fn cycle_limit(&mut self, cycle_limit: u64) {
self.context_builder.max_cycles(cycle_limit);
}
/// See [`ProveRequest::deferred_proof_verification`].
pub fn deferred_proof_verification(&mut self, value: bool) {
self.context_builder.set_deferred_proof_verification(value);
}
/// See [`ProveRequest::expected_exit_code`].
pub fn expected_exit_code(&mut self, code: StatusCode) {
self.context_builder.expected_exit_code(code);
}
pub fn with_nonce(&mut self, nonce: [u32; 4]) {
self.context_builder.proof_nonce(nonce);
}
}