Skip to main content

moonproof_backend/
lib.rs

1pub mod local;
2pub mod remote;
3
4use async_trait::async_trait;
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8#[derive(Debug, thiserror::Error)]
9pub enum BackendError {
10    #[error("Proving failed: {0}")]
11    ProvingFailed(String),
12
13    #[error("Verification failed: {0}")]
14    VerificationFailed(String),
15
16    #[error("Network error: {0}")]
17    Network(String),
18
19    #[error("Invalid input: {0}")]
20    InvalidInput(String),
21
22    #[error("Backend unavailable: {0}")]
23    Unavailable(String),
24}
25
26pub type Result<T> = std::result::Result<T, BackendError>;
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct ProofOutput {
30    pub proof_hex: String,
31    pub duration_ms: u64,
32    pub k: u32,
33    pub proof_size: usize,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct CircuitInfo {
38    pub k: u32,
39    pub num_constraints: usize,
40    pub num_public_inputs: usize,
41    pub num_private_inputs: usize,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct BackendHealth {
46    pub healthy: bool,
47    pub name: String,
48    pub message: String,
49}
50
51#[async_trait]
52pub trait ProverBackend: Send + Sync {
53    async fn prove(&self, zkir_json: &str, witness_json: &str) -> Result<ProofOutput>;
54    async fn verify(&self, zkir_json: &str, witness_json: &str, proof_hex: &str) -> Result<bool>;
55    async fn circuit_info(&self, zkir_json: &str) -> Result<CircuitInfo>;
56    async fn health(&self) -> Result<BackendHealth>;
57    fn name(&self) -> &'static str;
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
61pub enum BackendMode {
62    Local,
63    Remote,
64}
65
66impl fmt::Display for BackendMode {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        match self {
69            BackendMode::Local => write!(f, "Local"),
70            BackendMode::Remote => write!(f, "Remote"),
71        }
72    }
73}