risc0_zkvm/host/client/prove/
external.rsuse std::path::{Path, PathBuf};
use anyhow::{ensure, Result};
use super::{Executor, Prover, ProverOpts};
use crate::{
compute_image_id, host::api::AssetRequest, is_dev_mode, ApiClient, Asset, ExecutorEnv,
InnerReceipt, ProveInfo, Receipt, ReceiptKind, SessionInfo, VerifierContext,
};
pub struct ExternalProver {
name: String,
r0vm_path: PathBuf,
}
impl ExternalProver {
pub fn new<P: AsRef<Path>>(name: &str, r0vm_path: P) -> Self {
Self {
name: name.to_string(),
r0vm_path: r0vm_path.as_ref().to_path_buf(),
}
}
}
impl Prover for ExternalProver {
fn prove_with_ctx(
&self,
env: ExecutorEnv<'_>,
ctx: &VerifierContext,
elf: &[u8],
opts: &ProverOpts,
) -> Result<ProveInfo> {
tracing::debug!("Launching {}", &self.r0vm_path.to_string_lossy());
let image_id = compute_image_id(elf)?;
let client = ApiClient::new_sub_process(&self.r0vm_path)?;
let binary = Asset::Inline(elf.to_vec().into());
let prove_info = client.prove(&env, opts, binary)?;
if opts.prove_guest_errors {
prove_info.receipt.verify_integrity_with_context(ctx)?;
} else {
prove_info.receipt.verify_with_context(ctx, image_id)?;
}
Ok(prove_info)
}
fn get_name(&self) -> String {
self.name.clone()
}
fn compress(&self, opts: &ProverOpts, receipt: &Receipt) -> Result<Receipt> {
match (&receipt.inner, opts.receipt_kind) {
(InnerReceipt::Composite(_), ReceiptKind::Composite)
| (InnerReceipt::Succinct(_), ReceiptKind::Composite | ReceiptKind::Succinct)
| (
InnerReceipt::Groth16(_),
ReceiptKind::Composite | ReceiptKind::Succinct | ReceiptKind::Groth16,
) => Ok(receipt.clone()),
(InnerReceipt::Fake { .. }, _) => {
ensure!(
is_dev_mode(),
"dev mode must be enabled to compress fake receipts"
);
Ok(receipt.clone())
}
(_, _) => {
let client = ApiClient::new_sub_process(&self.r0vm_path)?;
client.compress(opts, receipt.clone().try_into()?, AssetRequest::Inline)
}
}
}
}
impl Executor for ExternalProver {
fn execute(&self, env: ExecutorEnv<'_>, elf: &[u8]) -> Result<SessionInfo> {
let binary = Asset::Inline(elf.to_vec().into());
let client = ApiClient::new_sub_process(&self.r0vm_path)?;
let segments_out = AssetRequest::Inline;
client.execute(&env, binary, segments_out, |_, _| Ok(()))
}
}