Skip to main content

sp1_sdk/blocking/prover/
mod.rs

1//! # SP1 Prover Trait
2//!
3//! A trait that each prover variant must implement.
4
5use std::fmt;
6
7use crate::{prover::verify_proof, ProvingKey, SP1VerificationError, StatusCode};
8use anyhow::Result;
9use sp1_core_machine::io::SP1Stdin;
10use sp1_primitives::types::Elf;
11use sp1_prover::{worker::SP1NodeCore, SP1VerifyingKey, SP1_CIRCUIT_VERSION};
12
13/// The module that exposes the [`ExecuteRequest`] type.
14mod execute;
15
16/// The module that exposes the [`ProveRequest`] trait.
17mod prove;
18
19pub use execute::ExecuteRequest;
20pub(crate) use prove::BaseProveRequest;
21pub use prove::ProveRequest;
22
23use crate::SP1ProofWithPublicValues;
24
25/// The entire user-facing functionality of a prover.
26pub trait Prover: Clone + Send + Sync {
27    /// The proving key used for this prover type.
28    type ProvingKey: ProvingKey;
29
30    /// The possible errors that can occur when proving.
31    type Error: fmt::Debug + fmt::Display;
32
33    /// The prove request builder.
34    type ProveRequest<'a>: ProveRequest<'a, Self>
35    where
36        Self: 'a;
37
38    /// The inner [`SP1NodeCore`] struct used by the prover.
39    fn inner(&self) -> &SP1NodeCore;
40
41    /// The version of the current SP1 circuit.
42    fn version(&self) -> &str {
43        SP1_CIRCUIT_VERSION
44    }
45
46    /// Setup the prover with the given ELF.
47    fn setup(&self, elf: Elf) -> Result<Self::ProvingKey, Self::Error>;
48
49    /// Prove the given program on the given input in the given proof mode.
50    fn prove<'a>(&'a self, pk: &'a Self::ProvingKey, stdin: SP1Stdin) -> Self::ProveRequest<'a>;
51
52    /// Execute the program on the given input.
53    fn execute(&self, elf: Elf, stdin: SP1Stdin) -> ExecuteRequest<'_, Self> {
54        ExecuteRequest::new(self, elf, stdin)
55    }
56
57    /// Verify the given proof.
58    ///
59    /// If the status code is not set, the verification process will check for success.
60    fn verify(
61        &self,
62        proof: &SP1ProofWithPublicValues,
63        vkey: &SP1VerifyingKey,
64        status_code: Option<StatusCode>,
65    ) -> Result<(), SP1VerificationError> {
66        verify_proof(self.inner(), self.version(), proof, vkey, status_code)
67    }
68}