diff --git a/Cargo.lock b/Cargo.lock index 9f9296e..e30aca3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -873,7 +873,9 @@ dependencies = [ name = "mugraph-circuits" version = "0.0.1" dependencies = [ + "mugraph-core", "risc0-build", + "risc0-zkvm", "serde", ] @@ -911,7 +913,6 @@ version = "0.0.1" dependencies = [ "mugraph-circuits", "mugraph-core", - "risc0-zkvm", "serde_json", ] diff --git a/circuits/Cargo.toml b/circuits/Cargo.toml index 8f19928..e3044a3 100644 --- a/circuits/Cargo.toml +++ b/circuits/Cargo.toml @@ -3,9 +3,13 @@ name = "mugraph-circuits" version = "0.0.1" edition = "2021" +[dependencies] +mugraph-core = { workspace = true } +risc0-zkvm = { version = "1.0.3", features = ["std"] } +serde = { version = "1.0", features = ["derive"] } + [build-dependencies] risc0-build = { version = "1.0.3" } -serde = { version = "1.0", features = ["derive"] } [package.metadata.risc0] methods = ["guest"] diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs index 1bdb308..7062b30 100644 --- a/circuits/src/lib.rs +++ b/circuits/src/lib.rs @@ -1 +1,39 @@ +use mugraph_core::{Error, Result}; +use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts, Receipt}; +use serde::{de::DeserializeOwned, Serialize}; + include!(concat!(env!("OUT_DIR"), "/methods.rs")); + +pub struct Prover { + stdout: Vec, + opts: ProverOpts, +} + +impl Prover { + pub fn new() -> Self { + Self { + opts: ProverOpts::fast(), + stdout: Vec::new(), + } + } + + pub fn prove(&mut self, input: T) -> Result { + let env = ExecutorEnv::builder() + .write(&input) + .map_err(|_| Error::ExecutorWriteValue)? + .stdout(&mut self.stdout) + .build() + .map_err(|_| Error::ExecutorInitialize)?; + + let prover = default_prover(); + + Ok(prover + .prove_with_opts(env, FISSION_ELF, &self.opts) + .map_err(|_| Error::ProofGenerate)? + .receipt) + } + + pub fn read(&self) -> Result { + risc0_zkvm::serde::from_slice(&self.stdout).map_err(|_| Error::StdoutDecode) + } +} diff --git a/examples/Cargo.toml b/examples/Cargo.toml index b32f426..36a7e49 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -6,6 +6,4 @@ edition = "2021" [dependencies] mugraph-circuits = { path = "../circuits" } mugraph-core = { path = "../core" } - -risc0-zkvm = { version = "1.0.3", features = ["std"] } serde_json = "1.0.120" diff --git a/examples/src/bin/mugraph-example-generate-proof.rs b/examples/src/bin/mugraph-example-generate-proof.rs index 472ab3b..e2a4524 100644 --- a/examples/src/bin/mugraph-example-generate-proof.rs +++ b/examples/src/bin/mugraph-example-generate-proof.rs @@ -1,6 +1,5 @@ use mugraph_circuits::*; use mugraph_core::{Error, Fission, Hash, Note, Result, Split}; -use risc0_zkvm::{default_prover, serde::from_slice, ExecutorEnv, ProverOpts}; fn main() -> Result<()> { let request = Split { @@ -12,27 +11,11 @@ fn main() -> Result<()> { amount: 50, }; - let mut stdout = Vec::new(); + let mut prover = Prover::new(); + let receipt = prover.prove(&request)?; - let env = ExecutorEnv::builder() - .write(&request) - .map_err(|_| Error::ExecutorWriteValue)? - .stdout(&mut stdout) - .build() - .map_err(|_| Error::ExecutorInitialize)?; - - let prover = default_prover(); - let opts = ProverOpts::fast(); - - let receipt = prover - .prove_with_opts(env, FISSION_ELF, &opts) - .map_err(|e| { - println!("Error: {}", e); - Error::ProofGenerate - })? - .receipt; let fission: Fission = receipt.journal.decode().map_err(|_| Error::JournalDecode)?; - let (output, change): (Note, Note) = from_slice(&stdout).map_err(|_| Error::StdoutDecode)?; + let (output, change): (Note, Note) = prover.read()?; println!( "Spend:\n\n{}", diff --git a/nix/default.nix b/nix/default.nix index 4d1c1c0..58ce068 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -58,6 +58,8 @@ let ]; RISC0_RUST_SRC = "${rust}/lib/rustlib/src/rust"; + RISC0_EXECUTOR = "ipc"; + RISC0_SERVER_PATH = "${packages.r0vm}/bin/r0vm"; }; in {