Skip to content

Commit

Permalink
feat: use r0vm to run risc0 programs
Browse files Browse the repository at this point in the history
  • Loading branch information
cfcosta committed Jul 28, 2024
1 parent 41260bc commit 251e982
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
38 changes: 38 additions & 0 deletions circuits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
opts: ProverOpts,
}

impl Prover {
pub fn new() -> Self {
Self {
opts: ProverOpts::fast(),
stdout: Vec::new(),
}
}

pub fn prove<T: Serialize>(&mut self, input: T) -> Result<Receipt> {
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<T: DeserializeOwned>(&self) -> Result<T> {
risc0_zkvm::serde::from_slice(&self.stdout).map_err(|_| Error::StdoutDecode)
}
}
2 changes: 0 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 3 additions & 20 deletions examples/src/bin/mugraph-example-generate-proof.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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{}",
Expand Down
2 changes: 2 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ let
];

RISC0_RUST_SRC = "${rust}/lib/rustlib/src/rust";
RISC0_EXECUTOR = "ipc";
RISC0_SERVER_PATH = "${packages.r0vm}/bin/r0vm";
};
in
{
Expand Down

0 comments on commit 251e982

Please sign in to comment.