use async_trait::async_trait;
use crate::{BackendError, BackendHealth, CircuitInfo, ProofOutput, ProverBackend, Result};
#[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> {
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);
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"
}
}