Skip to main content

sp1_cuda/
api.rs

1use serde::{Deserialize, Serialize};
2use sp1_prover::{worker::ProofFromNetwork, SP1VerifyingKey};
3use sp1_prover_types::{network_base_types::ProofMode, SerializableRiscvMachine};
4
5use crate::CudaClientError;
6use sp1_core_machine::io::SP1Stdin;
7
8#[derive(Serialize, Deserialize)]
9pub enum Request {
10    /// Tell the server to create a new proving key.
11    Setup { elf: Vec<u8>, machine: SerializableRiscvMachine },
12
13    /// Tell the server to create a proof with the given mode.
14    ProveWithMode { mode: ProofMode, key: [u8; 32], stdin: SP1Stdin, proof_nonce: [u32; 4] },
15
16    /// Tell the server to destroy a proving key.
17    Destroy { key: [u8; 32] },
18}
19
20#[derive(Serialize, Deserialize)]
21pub enum Response {
22    /// The server has initialized.
23    Ok,
24    /// The setup response, containing the vkey and key id.
25    Setup { id: [u8; 32], vk: SP1VerifyingKey },
26    /// A generic proof that can be any of the proof types.
27    Proof { proof: ProofFromNetwork },
28    /// The server returned a prover error.
29    ProverError(String),
30    /// The error response, containing the error message.
31    InternalError(String),
32    /// The server has disconnected the client.
33    ///
34    /// This is really only useful for debugging purposes,
35    /// if for some reason we dont send enoug bytes.
36    ConnectionClosed,
37}
38
39impl Response {
40    /// Get the type of the response.
41    pub(crate) const fn type_of(&self) -> &'static str {
42        match self {
43            Response::Ok => "Ok",
44            Response::Setup { .. } => "Setup",
45            Response::Proof { .. } => "Proof",
46            Response::InternalError(_) => "InternalError",
47            Response::ProverError(_) => "ProverError",
48            Response::ConnectionClosed => "ConnectionClosed",
49        }
50    }
51
52    /// Capture any expected errors and convert them to a [`CudaClientError`].
53    pub(crate) fn into_result(self) -> Result<Self, CudaClientError> {
54        match self {
55            Self::InternalError(e) => Err(CudaClientError::ServerError(e)),
56            Self::ProverError(e) => {
57                // todo!(n): can we make the [`SP1ProverError`] serde compatible?
58                Err(CudaClientError::ServerError(e))
59            }
60            _ => Ok(self),
61        }
62    }
63}