risc0_zkvm/host/client/prove/
external.rs1use std::path::{Path, PathBuf};
16
17use anyhow::{ensure, Result};
18
19use super::{Executor, Prover, ProverOpts};
20use crate::{
21 host::api::AssetRequest, is_dev_mode, ApiClient, Asset, ExecutorEnv, InnerReceipt, ProveInfo,
22 Receipt, ReceiptKind, SessionInfo, VerifierContext,
23};
24
25pub struct ExternalProver {
28 name: String,
29 r0vm_path: PathBuf,
30}
31
32impl ExternalProver {
33 pub fn new<P: AsRef<Path>>(name: &str, r0vm_path: P) -> Self {
35 Self {
36 name: name.to_string(),
37 r0vm_path: r0vm_path.as_ref().to_path_buf(),
38 }
39 }
40}
41
42impl Prover for ExternalProver {
43 fn prove_with_ctx(
44 &self,
45 env: ExecutorEnv<'_>,
46 ctx: &VerifierContext,
47 elf: &[u8],
48 opts: &ProverOpts,
49 ) -> Result<ProveInfo> {
50 tracing::debug!("Launching {}", &self.r0vm_path.to_string_lossy());
51
52 let client = ApiClient::new_sub_process(&self.r0vm_path)?;
53 let binary = Asset::Inline(elf.to_vec().into());
54 let prove_info = client.prove(&env, opts, binary)?;
55 prove_info.receipt.verify_integrity_with_context(ctx)?;
56
57 Ok(prove_info)
58 }
59
60 fn get_name(&self) -> String {
61 self.name.clone()
62 }
63
64 fn compress(&self, opts: &ProverOpts, receipt: &Receipt) -> Result<Receipt> {
65 match (&receipt.inner, opts.receipt_kind) {
66 (InnerReceipt::Composite(_), ReceiptKind::Composite)
68 | (InnerReceipt::Succinct(_), ReceiptKind::Composite | ReceiptKind::Succinct)
69 | (
70 InnerReceipt::Groth16(_),
71 ReceiptKind::Composite | ReceiptKind::Succinct | ReceiptKind::Groth16,
72 ) => Ok(receipt.clone()),
73 (InnerReceipt::Fake { .. }, _) => {
75 ensure!(
76 is_dev_mode(),
77 "dev mode must be enabled to compress fake receipts"
78 );
79 Ok(receipt.clone())
80 }
81 (_, _) => {
82 let client = ApiClient::new_sub_process(&self.r0vm_path)?;
83 client.compress(opts, receipt.clone().try_into()?, AssetRequest::Inline)
84 }
85 }
86 }
87}
88
89impl Executor for ExternalProver {
90 fn execute(&self, env: ExecutorEnv<'_>, elf: &[u8]) -> Result<SessionInfo> {
91 let binary = Asset::Inline(elf.to_vec().into());
92 let client = ApiClient::new_sub_process(&self.r0vm_path)?;
93 let segments_out = AssetRequest::Inline;
94 client.execute(&env, binary, segments_out, |_, _| Ok(()))
95 }
96}