Skip to main content

airbender_host/prover/
dev_prover.rs

1use super::{resolve_app_bin_path, ProveResult, Prover};
2use crate::error::Result;
3use crate::proof::{hash_app_bin, hash_input_words, DevProof, Proof};
4use crate::runner::{Runner, TranspilerRunner, TranspilerRunnerBuilder};
5use crate::security::SecurityLevel;
6use std::path::{Path, PathBuf};
7
8/// Builder for creating a configured development prover.
9pub struct DevProverBuilder {
10    app_bin_path: PathBuf,
11    cycles: Option<usize>,
12    text_path: Option<PathBuf>,
13    security: SecurityLevel,
14}
15
16impl DevProverBuilder {
17    pub fn new(app_bin_path: impl AsRef<Path>) -> Self {
18        Self {
19            app_bin_path: app_bin_path.as_ref().to_path_buf(),
20            cycles: None,
21            text_path: None,
22            security: SecurityLevel::default(),
23        }
24    }
25
26    pub fn with_cycles(mut self, cycles: usize) -> Self {
27        self.cycles = Some(cycles);
28        self
29    }
30
31    pub fn maybe_cycles(self, cycles: Option<usize>) -> Self {
32        match cycles {
33            Some(v) => self.with_cycles(v),
34            None => self,
35        }
36    }
37
38    pub fn with_text_path(mut self, text_path: impl AsRef<Path>) -> Self {
39        self.text_path = Some(text_path.as_ref().to_path_buf());
40        self
41    }
42
43    pub fn with_security(mut self, security: SecurityLevel) -> Self {
44        self.security = security;
45        self
46    }
47
48    pub fn maybe_text_path(self, text_path: Option<impl AsRef<Path>>) -> Self {
49        match text_path {
50            Some(v) => self.with_text_path(v),
51            None => self,
52        }
53    }
54
55    pub fn build(self) -> Result<DevProver> {
56        DevProver::new(
57            &self.app_bin_path,
58            self.cycles,
59            self.text_path.as_deref(),
60            self.security,
61        )
62    }
63}
64
65/// Development prover that records transpiler execution metadata instead of generating a zk-proof.
66pub struct DevProver {
67    security: SecurityLevel,
68    app_bin_hash: [u8; 32],
69    runner: TranspilerRunner,
70}
71
72impl DevProver {
73    fn new(
74        app_bin_path: &Path,
75        cycles: Option<usize>,
76        text_path: Option<&Path>,
77        security: SecurityLevel,
78    ) -> Result<Self> {
79        let app_bin_path = resolve_app_bin_path(app_bin_path)?;
80        let app_bin_hash = hash_app_bin(&app_bin_path)?;
81
82        let runner = TranspilerRunnerBuilder::new(&app_bin_path)
83            .maybe_cycles(cycles)
84            .maybe_text_path(text_path)
85            .build()?;
86
87        Ok(Self {
88            security,
89            app_bin_hash,
90            runner,
91        })
92    }
93}
94
95impl Prover for DevProver {
96    fn prove(&self, input_words: &[u32]) -> Result<ProveResult> {
97        let execution = self.runner.run(input_words)?;
98        let cycles = execution.cycles_executed as u64;
99        let receipt = execution.receipt;
100
101        let proof = Proof::Dev(DevProof {
102            security: self.security,
103            app_bin_hash: self.app_bin_hash,
104            input_words_hash: hash_input_words(input_words),
105            receipt: receipt.clone(),
106            cycles,
107        });
108
109        Ok(ProveResult {
110            proof,
111            cycles,
112            receipt,
113        })
114    }
115}