moonproof-backend 0.2.2

ProverBackend trait with local and remote implementations for Moonproof
Documentation
use async_trait::async_trait;

use crate::{BackendError, BackendHealth, CircuitInfo, ProofOutput, ProverBackend, Result};

/// Local proving backend for desktop use.
///
/// When the `local` feature is enabled, delegates to `moonproof-prover`
/// for ZKIR parsing and circuit analysis. Full proving requires the standard
/// Midnight binary protocol (`ProofPreimage` + `ProvingKeyMaterial`).
#[derive(Debug)]
pub struct LocalBackend {
    #[cfg(feature = "local")]
    engine: moonproof_prover::ProverEngine,
}

impl LocalBackend {
    #[cfg(feature = "local")]
    pub fn new() -> std::result::Result<Self, String> {
        let engine = moonproof_prover::ProverEngine::from_env()
            .map_err(|e| format!("Failed to init prover: {e}"))?;
        Ok(Self { engine })
    }

    #[cfg(not(feature = "local"))]
    pub fn new() -> std::result::Result<Self, String> {
        Ok(Self {})
    }
}

#[cfg(feature = "local")]
#[async_trait]
impl ProverBackend for LocalBackend {
    async fn prove(&self, zkir_json: &str, _witness_json: &str) -> Result<ProofOutput> {
        // Parse ZKIR to validate and get circuit info
        let ir = moonproof_prover::ProverEngine::parse_zkir(zkir_json)
            .map_err(|e| BackendError::ProvingFailed(e.to_string()))?;
        let k = moonproof_prover::ProverEngine::circuit_k(&ir);

        // Full proving requires the binary ProofPreimage protocol.
        // This JSON API exists for circuit analysis only.
        Err(BackendError::ProvingFailed(format!(
            "Local proving requires the binary Midnight protocol (ProofPreimage + ProvingKeyMaterial). \
             Circuit k={k}. Use the Midnight JS SDK's httpClientProofProvider for full proof generation."
        )))
    }

    async fn verify(
        &self,
        _zkir_json: &str,
        _witness_json: &str,
        _proof_hex: &str,
    ) -> Result<bool> {
        Err(BackendError::VerificationFailed(
            "Verification requires the binary Midnight protocol".into(),
        ))
    }

    async fn circuit_info(&self, zkir_json: &str) -> Result<CircuitInfo> {
        let ir = moonproof_prover::ProverEngine::parse_zkir(zkir_json)
            .map_err(|e| BackendError::InvalidInput(e.to_string()))?;

        Ok(CircuitInfo {
            k: u32::from(ir.k()),
            num_constraints: ir.rows(),
            num_public_inputs: 0,
            num_private_inputs: 0,
        })
    }

    async fn health(&self) -> Result<BackendHealth> {
        Ok(BackendHealth {
            healthy: true,
            name: self.name().to_string(),
            message: "Local backend ready (midnight-ledger ZKIR v2 + v3 dispatch)".to_string(),
        })
    }

    fn name(&self) -> &'static str {
        "local"
    }
}

#[cfg(not(feature = "local"))]
#[async_trait]
impl ProverBackend for LocalBackend {
    async fn prove(&self, _zkir_json: &str, _witness_json: &str) -> Result<ProofOutput> {
        Err(BackendError::Unavailable(
            "Local proving requires the 'local' feature. Rebuild with --features local".into(),
        ))
    }

    async fn verify(
        &self,
        _zkir_json: &str,
        _witness_json: &str,
        _proof_hex: &str,
    ) -> Result<bool> {
        Err(BackendError::Unavailable(
            "Local verification requires the 'local' feature".into(),
        ))
    }

    async fn circuit_info(&self, _zkir_json: &str) -> Result<CircuitInfo> {
        Err(BackendError::Unavailable(
            "Local circuit info requires the 'local' feature".into(),
        ))
    }

    async fn health(&self) -> Result<BackendHealth> {
        Ok(BackendHealth {
            healthy: false,
            name: self.name().to_string(),
            message: "Local backend stub. Built without 'local' feature.".to_string(),
        })
    }

    fn name(&self) -> &'static str {
        "local"
    }
}