use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProverType {
Exec,
RISC0,
SP1,
TDX,
}
impl From<ProverType> for u32 {
fn from(value: ProverType) -> u32 {
match value {
ProverType::Exec => 0,
ProverType::RISC0 => 1,
ProverType::SP1 => 2,
ProverType::TDX => 3,
}
}
}
impl ProverType {
pub fn all() -> impl Iterator<Item = ProverType> {
[
ProverType::Exec,
ProverType::RISC0,
ProverType::SP1,
ProverType::TDX,
]
.into_iter()
}
}
impl Display for ProverType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Exec => write!(f, "Exec"),
Self::RISC0 => write!(f, "RISC0"),
Self::SP1 => write!(f, "SP1"),
Self::TDX => write!(f, "TDX"),
}
}
}
#[derive(PartialEq, Serialize, Deserialize, Clone, Debug)]
pub struct ProofBytes {
pub prover_type: ProverType,
pub proof: Vec<u8>,
}
#[derive(PartialEq, Serialize, Deserialize, Clone, Debug)]
pub enum ProverOutput {
Proof(ProofBytes),
ProofWithPublicValues {
proof_bytes: ProofBytes,
public_values: Vec<u8>,
},
}
impl ProverOutput {
pub fn prover_type(&self) -> ProverType {
match self {
Self::Proof(p) => p.prover_type,
Self::ProofWithPublicValues { proof_bytes, .. } => proof_bytes.prover_type,
}
}
pub fn proof_bytes(&self) -> &ProofBytes {
match self {
Self::Proof(p) => p,
Self::ProofWithPublicValues { proof_bytes, .. } => proof_bytes,
}
}
pub fn into_proof_bytes(self) -> ProofBytes {
match self {
Self::Proof(p) => p,
Self::ProofWithPublicValues { proof_bytes, .. } => proof_bytes,
}
}
pub fn public_values(&self) -> Option<&[u8]> {
match self {
Self::Proof(_) => None,
Self::ProofWithPublicValues { public_values, .. } => Some(public_values),
}
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default)]
pub enum ProofFormat {
#[default]
Groth16,
Compressed,
}
#[allow(clippy::large_enum_variant)]
#[derive(Serialize, Deserialize)]
pub enum ProofData<I> {
ProverSetup {
prover_type: ProverType,
payload: Bytes,
},
ProverSetupACK,
InputRequest {
commit_hash: String,
prover_type: ProverType,
},
VersionMismatch,
ProverTypeNotNeeded { prover_type: ProverType },
InputResponse {
id: Option<u64>,
input: Option<I>,
format: Option<ProofFormat>,
},
ProofSubmit { id: u64, proof: ProverOutput },
ProofSubmitACK { id: u64 },
}
impl<I> ProofData<I> {
pub fn prover_setup(prover_type: ProverType, payload: Bytes) -> Self {
ProofData::ProverSetup {
prover_type,
payload,
}
}
pub fn prover_setup_ack() -> Self {
ProofData::ProverSetupACK
}
pub fn input_request(commit_hash: String, prover_type: ProverType) -> Self {
ProofData::InputRequest {
commit_hash,
prover_type,
}
}
pub fn version_mismatch() -> Self {
ProofData::VersionMismatch
}
pub fn input_response(id: u64, input: I, format: ProofFormat) -> Self {
ProofData::InputResponse {
id: Some(id),
input: Some(input),
format: Some(format),
}
}
pub fn empty_input_response() -> Self {
ProofData::InputResponse {
id: None,
input: None,
format: None,
}
}
pub fn proof_submit(id: u64, proof: ProverOutput) -> Self {
ProofData::ProofSubmit { id, proof }
}
pub fn proof_submit_ack(id: u64) -> Self {
ProofData::ProofSubmitACK { id }
}
}