Skip to main content

sp1_verifier/
proof.rs

1use alloc::{boxed::Box, string::String, vec::Vec};
2use core::fmt::{self, Debug, Display, Formatter};
3
4use serde::{Deserialize, Serialize};
5use sp1_hypercube::{SP1PcsProofInner, SP1RecursionProof, ShardProof};
6use sp1_primitives::{io::SP1PublicValues, SP1GlobalContext};
7use strum::{EnumDiscriminants, EnumTryAs, IntoDiscriminant};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub enum ProofBn254 {
11    Plonk(PlonkBn254Proof),
12    Groth16(Groth16Bn254Proof),
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize, Default)]
16pub struct PlonkBn254Proof {
17    pub public_inputs: [String; 5],
18    pub encoded_proof: String,
19    pub raw_proof: String,
20    pub plonk_vkey_hash: [u8; 32],
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize, Default)]
24pub struct Groth16Bn254Proof {
25    pub public_inputs: [String; 5],
26    pub encoded_proof: String,
27    pub raw_proof: String,
28    pub groth16_vkey_hash: [u8; 32],
29}
30
31/// A proof generated by the SP1 RISC-V zkVM.
32#[derive(Clone, Serialize, Deserialize, EnumDiscriminants, EnumTryAs)]
33#[strum_discriminants(derive(Default, Hash, PartialOrd, Ord))]
34#[strum_discriminants(name(SP1ProofMode))]
35pub enum SP1Proof {
36    /// A proof generated by the core proof mode.
37    ///
38    /// The proof size scales linearly with the number of cycles.
39    #[strum_discriminants(default)]
40    Core(Vec<ShardProof<SP1GlobalContext, SP1PcsProofInner>>),
41    /// A proof generated by the compress proof mode.
42    ///
43    /// The proof size is constant, regardless of the number of cycles.
44    Compressed(Box<SP1RecursionProof<SP1GlobalContext, SP1PcsProofInner>>),
45    /// A proof generated by the Plonk proof mode.
46    Plonk(PlonkBn254Proof),
47    /// A proof generated by the Groth16 proof mode.
48    Groth16(Groth16Bn254Proof),
49}
50
51impl Debug for SP1Proof {
52    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
53        match self {
54            SP1Proof::Core(_) => write!(f, "Core"),
55            SP1Proof::Compressed(_) => write!(f, "Compressed"),
56            SP1Proof::Plonk(_) => write!(f, "Plonk"),
57            SP1Proof::Groth16(_) => write!(f, "Groth16"),
58        }
59    }
60}
61
62impl Display for SP1Proof {
63    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
64        match self {
65            SP1Proof::Core(_) => write!(f, "Core"),
66            SP1Proof::Compressed(_) => write!(f, "Compressed"),
67            SP1Proof::Plonk(_) => write!(f, "Plonk"),
68            SP1Proof::Groth16(_) => write!(f, "Groth16"),
69        }
70    }
71}
72
73/// The proof generated by the prover network.
74///
75/// Since [`bincode`] is not self describing, it cannot handle "nullable" optional values.
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct ProofFromNetwork {
78    pub proof: SP1Proof,
79    pub public_values: SP1PublicValues,
80    pub sp1_version: String,
81}
82
83impl ProofFromNetwork {
84    #[inline]
85    pub fn mode(&self) -> SP1ProofMode {
86        self.proof.discriminant()
87    }
88}