Skip to main content

moonproof_backend/
local.rs

1use async_trait::async_trait;
2
3use crate::{BackendError, BackendHealth, CircuitInfo, ProofOutput, ProverBackend, Result};
4
5/// Local proving backend for desktop use.
6///
7/// When the `local` feature is enabled, delegates to `moonproof-prover`
8/// for ZKIR parsing and circuit analysis. Full proving requires the standard
9/// Midnight binary protocol (`ProofPreimage` + `ProvingKeyMaterial`).
10#[derive(Debug)]
11pub struct LocalBackend {
12    #[cfg(feature = "local")]
13    engine: moonproof_prover::ProverEngine,
14}
15
16impl LocalBackend {
17    #[cfg(feature = "local")]
18    pub fn new() -> std::result::Result<Self, String> {
19        let engine = moonproof_prover::ProverEngine::from_env()
20            .map_err(|e| format!("Failed to init prover: {e}"))?;
21        Ok(Self { engine })
22    }
23
24    #[cfg(not(feature = "local"))]
25    pub fn new() -> std::result::Result<Self, String> {
26        Ok(Self {})
27    }
28}
29
30#[cfg(feature = "local")]
31#[async_trait]
32impl ProverBackend for LocalBackend {
33    async fn prove(&self, zkir_json: &str, _witness_json: &str) -> Result<ProofOutput> {
34        // Parse ZKIR to validate and get circuit info
35        let ir = moonproof_prover::ProverEngine::parse_zkir(zkir_json)
36            .map_err(|e| BackendError::ProvingFailed(e.to_string()))?;
37        let k = moonproof_prover::ProverEngine::circuit_k(&ir);
38
39        // Full proving requires the binary ProofPreimage protocol.
40        // This JSON API exists for circuit analysis only.
41        Err(BackendError::ProvingFailed(format!(
42            "Local proving requires the binary Midnight protocol (ProofPreimage + ProvingKeyMaterial). \
43             Circuit k={k}. Use the Midnight JS SDK's httpClientProofProvider for full proof generation."
44        )))
45    }
46
47    async fn verify(
48        &self,
49        _zkir_json: &str,
50        _witness_json: &str,
51        _proof_hex: &str,
52    ) -> Result<bool> {
53        Err(BackendError::VerificationFailed(
54            "Verification requires the binary Midnight protocol".into(),
55        ))
56    }
57
58    async fn circuit_info(&self, zkir_json: &str) -> Result<CircuitInfo> {
59        let ir = moonproof_prover::ProverEngine::parse_zkir(zkir_json)
60            .map_err(|e| BackendError::InvalidInput(e.to_string()))?;
61
62        Ok(CircuitInfo {
63            k: u32::from(ir.k()),
64            num_constraints: ir.rows(),
65            num_public_inputs: 0,
66            num_private_inputs: 0,
67        })
68    }
69
70    async fn health(&self) -> Result<BackendHealth> {
71        Ok(BackendHealth {
72            healthy: true,
73            name: self.name().to_string(),
74            message: "Local backend ready (midnight-ledger ZKIR v2 + v3 dispatch)".to_string(),
75        })
76    }
77
78    fn name(&self) -> &'static str {
79        "local"
80    }
81}
82
83#[cfg(not(feature = "local"))]
84#[async_trait]
85impl ProverBackend for LocalBackend {
86    async fn prove(&self, _zkir_json: &str, _witness_json: &str) -> Result<ProofOutput> {
87        Err(BackendError::Unavailable(
88            "Local proving requires the 'local' feature. Rebuild with --features local".into(),
89        ))
90    }
91
92    async fn verify(
93        &self,
94        _zkir_json: &str,
95        _witness_json: &str,
96        _proof_hex: &str,
97    ) -> Result<bool> {
98        Err(BackendError::Unavailable(
99            "Local verification requires the 'local' feature".into(),
100        ))
101    }
102
103    async fn circuit_info(&self, _zkir_json: &str) -> Result<CircuitInfo> {
104        Err(BackendError::Unavailable(
105            "Local circuit info requires the 'local' feature".into(),
106        ))
107    }
108
109    async fn health(&self) -> Result<BackendHealth> {
110        Ok(BackendHealth {
111            healthy: false,
112            name: self.name().to_string(),
113            message: "Local backend stub. Built without 'local' feature.".to_string(),
114        })
115    }
116
117    fn name(&self) -> &'static str {
118        "local"
119    }
120}