nexus_sdk/legacy/
traits.rs

1use serde::{de::DeserializeOwned, Serialize};
2use std::fs;
3use std::path::Path;
4
5use nexus_core::nvm::View;
6
7use crate::error::IOError;
8use crate::legacy::compile::*;
9use crate::traits::*;
10
11/// An unchecked view, the correctness of which is _not_ guaranteed by the proving or checked by the verification.
12pub trait UncheckedView {}
13impl UncheckedView for View {}
14
15/// A prover for the zkVM.
16pub trait LegacyProver<'a>: Setup<'a> {
17    type Proof: LegacyVerifiable<'a>;
18    type View: LegacyViewable;
19
20    /// Construct a new proving instance from raw ELF bytes.
21    fn new(elf_bytes: &[u8]) -> Result<Self, Self::Error>
22    where
23        Self: Sized;
24
25    /// Construct a new proving instance by reading an ELF file.
26    fn new_from_file<P: AsRef<Path>>(path: &P) -> Result<Self, Self::Error>
27    where
28        Self: Sized,
29        Self::Error: From<std::io::Error>,
30    {
31        Self::new(&fs::read(path)?)
32    }
33
34    /// Construct a new proving instance through dynamic compilation (see [`compile`](crate::compile)).
35    fn compile(opts: &CompileOpts) -> Result<Self, Self::Error>
36    where
37        Self: Sized,
38        Self::Error: From<std::io::Error>;
39
40    /// Run the zkVM and return a view of the execution output.
41    fn run(self) -> Result<Self::View, Self::Error>
42    where
43        Self: Sized,
44    {
45        Self::run_with_input::<()>(self, &())
46    }
47
48    /// Run the zkVM on private input of type `S` and return a view of the execution output.
49    fn run_with_input<S: Serialize + Sized>(
50        self,
51        private_input: &S,
52    ) -> Result<Self::View, Self::Error>;
53
54    /// Run the zkVM and return a verifiable proof, along with a view of the execution output.
55    fn prove(self) -> Result<Self::Proof, Self::Error>
56    where
57        Self: Sized,
58    {
59        Self::prove_with_input::<()>(self, &())
60    }
61
62    /// Run the zkVM on private input of type `S` and return a verifiable proof, along with a view of the execution output.
63    fn prove_with_input<S: Serialize + Sized>(
64        self,
65        private_input: &S,
66    ) -> Result<Self::Proof, Self::Error>;
67}
68
69/// A view capturing the output of the machine.
70pub trait LegacyViewable {
71    /// Get the contents of the output tape written by the zkVM execution by deserializing the output tape as of type `U`.
72    fn output<U: DeserializeOwned>(&self) -> Result<U, IOError>;
73
74    /// Get the logging output of the zkVM.
75    fn logs(&self) -> &Vec<String>;
76}
77
78/// A verifiable proof of a zkVM execution. Also contains a view capturing the output of the machine.
79pub trait LegacyVerifiable<'a>: Setup<'a> + Serialize + DeserializeOwned {
80    type View: LegacyViewable;
81
82    /// Get the contents of the output tape written by the zkVM execution.
83    fn output<U: DeserializeOwned>(&self) -> Result<U, Self::Error>;
84
85    /// Get the logging output of the zkVM.
86    fn logs(&self) -> &Vec<String>;
87
88    /// Detach proof from setup to make it easier to pass around without needing to manage lifetimes.
89    fn detach(&mut self);
90
91    /// Verify the proof of an execution.
92    fn verify(&self) -> Result<(), Self::Error>;
93}