nexus_sdk/legacy/jolt/
mod.rs1use 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#[derive(Debug, Error)]
18pub enum Error {
19 #[error(transparent)]
21 ProofError(#[from] ProofError),
22
23 #[error(transparent)]
25 BuildError(#[from] BuildError),
26
27 #[error(transparent)]
29 HostIOError(#[from] std::io::Error),
30
31 #[error(transparent)]
33 GuestIOError(#[from] IOError),
34
35 #[error(transparent)]
37 ConfigurationError(#[from] ConfigurationError),
38}
39
40pub struct Jolt<C: Compute = Local> {
44 vm: JoltVM<MerkleTrie>,
45 pre: Option<JoltPreprocessing>,
46 _compute: PhantomData<C>,
47}
48
49pub struct Proof {
52 proof: (JoltProof, JoltCommitments),
54 pre: JoltPreprocessing,
57}
58
59impl Jolt<Local> {
60 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 pub fn prove(mut self) -> Result<Proof, Error> {
75 let pre = preprocess(&self.vm);
76 self.pre = Some(pre.clone()); 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 pub fn verify(self) -> Result<(), Error> {
124 Ok(verify(self.pre, self.proof.0, self.proof.1)?)
125 }
126}