nexus_sdk/legacy/jolt/
mod.rs

1use crate::legacy::compile;
2use crate::traits::*;
3
4use std::fs;
5use thiserror::Error;
6
7use nexus_core_legacy::nvm::memory::MerkleTrie;
8use nexus_core_legacy::prover::jolt::types::{JoltCommitments, JoltPreprocessing, JoltProof};
9use nexus_core_legacy::prover::jolt::{
10    parse_elf, preprocess, prove, trace, verify, Error as ProofError, VM as JoltVM,
11};
12
13use crate::error::{BuildError, ConfigurationError, IOError};
14use std::marker::PhantomData;
15
16/// Errors that occur while proving using Jolt.
17#[derive(Debug, Error)]
18pub enum Error {
19    /// An error occurred during parameter generation, execution, proving, or proof verification for the zkVM.
20    #[error(transparent)]
21    ProofError(#[from] ProofError),
22
23    /// An error occurred building the guest program dynamically.
24    #[error(transparent)]
25    BuildError(#[from] BuildError),
26
27    /// An error occurred reading or writing to the filesystem.
28    #[error(transparent)]
29    HostIOError(#[from] std::io::Error),
30
31    /// An error occurred reading or writing to the zkVM input/output tapes.
32    #[error(transparent)]
33    GuestIOError(#[from] IOError),
34
35    /// An error occured configuring the prover.
36    #[error(transparent)]
37    ConfigurationError(#[from] ConfigurationError),
38}
39
40/// Prover for the Nexus zkVM using Jolt.
41///
42/// An experimental implementation, which does not implement the [`Prover`] trait due to missing functionality.
43pub struct Jolt<C: Compute = Local> {
44    vm: JoltVM<MerkleTrie>,
45    pre: Option<JoltPreprocessing>,
46    _compute: PhantomData<C>,
47}
48
49/// A Jolt proof (and auxiliary preprocessing information needed for verification).
50//#[derive(Serialize, Deserialize)]
51pub struct Proof {
52    //#[serde(serialize_with = "ark_se", deserialize_with = "ark_de")]
53    proof: (JoltProof, JoltCommitments),
54    // todo: since the preprocessing is per-program, it makes sense to communicate
55    //       it along with the proof, but that requires implementing serialization
56    pre: JoltPreprocessing,
57}
58
59impl Jolt<Local> {
60    /// Construct a new proving instance through dynamic compilation (see [`compile`]).
61    pub fn compile(opts: &compile::CompileOpts) -> Result<Self, Error> {
62        let mut iopts = opts.to_owned();
63
64        let elf_path = iopts.build(&compile::ForProver::Jolt)?;
65
66        Ok(Jolt::<Local> {
67            vm: parse_elf::<MerkleTrie>(fs::read(elf_path)?.as_slice())?,
68            pre: None,
69            _compute: PhantomData,
70        })
71    }
72
73    /// Prove the zkVM and return a verifiable proof.
74    pub fn prove(mut self) -> Result<Proof, Error> {
75        let pre = preprocess(&self.vm);
76        self.pre = Some(pre.clone()); // keep a copy in the prover object
77
78        let tr = trace(self.vm)?;
79
80        let proof = prove(tr, &pre)?;
81
82        Ok(Proof { proof, pre })
83    }
84}
85
86impl<'a> Setup<'a> for Proof {
87    type Reference = ();
88    type Parameters = ();
89    type Preprocessing = JoltPreprocessing;
90    type Error = Error;
91
92    fn setup_reference<'b: 'a>(
93        &mut self,
94        _reference: &'b Self::Reference,
95    ) -> Result<(), Self::Error> {
96        Err(Error::from(ConfigurationError::NotApplicableOperation))
97    }
98
99    fn setup_parameters<'b: 'a>(
100        &mut self,
101        _parameters: &'b Self::Parameters,
102    ) -> Result<(), Self::Error> {
103        Err(Error::from(ConfigurationError::NotApplicableOperation))
104    }
105
106    fn detach(&mut self) {}
107
108    fn reference(&self) -> Result<&'a Self::Reference, Self::Error> {
109        Ok(&())
110    }
111
112    fn parameters(&self) -> Result<&'a Self::Parameters, Self::Error> {
113        Ok(&())
114    }
115
116    fn preprocessing(&self) -> Result<Self::Preprocessing, Self::Error> {
117        Ok(self.pre.clone())
118    }
119}
120
121impl Proof {
122    /// Verify the proof of an execution.
123    pub fn verify(self) -> Result<(), Error> {
124        Ok(verify(self.pre, self.proof.0, self.proof.1)?)
125    }
126}