From e8b7b1c4e644679f84e1e20e170d4efa7a624622 Mon Sep 17 00:00:00 2001 From: Kamen Mladenov Date: Tue, 14 Jan 2025 16:39:48 +0200 Subject: feat(zkvms): Export io logic to it's own crate --- zkvms/risc0/Cargo.lock | 135 ++++++++++++++++++++++++++++++++++++++++++- zkvms/risc0/default.nix | 1 + zkvms/risc0/host/Cargo.toml | 2 + zkvms/risc0/host/src/main.rs | 72 +++++++++++++---------- 4 files changed, 177 insertions(+), 33 deletions(-) (limited to 'zkvms/risc0') diff --git a/zkvms/risc0/Cargo.lock b/zkvms/risc0/Cargo.lock index 145227c..176a442 100644 --- a/zkvms/risc0/Cargo.lock +++ b/zkvms/risc0/Cargo.lock @@ -38,6 +38,56 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys 0.59.0", +] + [[package]] name = "anyhow" version = "1.0.95" @@ -418,6 +468,52 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "clap" +version = "4.5.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8eb5e908ef3a6efbe1ed62520fb7287959888c88485abe072543190ecc66783" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b01801b5fc6a0a232407abc821660c9c6d25a1cafc0d4f85f29fb8d9afc121" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54b755194d6389280185988721fffba69495eed5ee9feeee9a599b53db80318c" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.96", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "const-oid" version = "0.9.6" @@ -543,7 +639,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de78e66ac9061e030587b2a2e75cc88f22304913c907b11307bca737141230cb" dependencies = [ - "heck", + "heck 0.4.1", "proc-macro-error", ] @@ -739,6 +835,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" @@ -760,6 +862,7 @@ dependencies = [ "risc0-zkvm", "serde", "tracing-subscriber 0.3.19", + "zkvms_host_io", ] [[package]] @@ -1013,6 +1116,12 @@ version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.10.5" @@ -1112,9 +1221,9 @@ checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" [[package]] name = "log" -version = "0.4.22" +version = "0.4.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" [[package]] name = "malloc_buf" @@ -1959,6 +2068,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -2296,6 +2411,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "valuable" version = "0.1.0" @@ -2771,3 +2892,11 @@ version = "0.1.0" dependencies = [ "guests_macro", ] + +[[package]] +name = "zkvms_host_io" +version = "0.1.0" +dependencies = [ + "clap", + "num-traits", +] diff --git a/zkvms/risc0/default.nix b/zkvms/risc0/default.nix index f9b5dac..4f43a54 100644 --- a/zkvms/risc0/default.nix +++ b/zkvms/risc0/default.nix @@ -17,6 +17,7 @@ let ./. ../../guests ../../guests_macro + ../../zkvms_host_io ../../Vertices-010.in ]); }; diff --git a/zkvms/risc0/host/Cargo.toml b/zkvms/risc0/host/Cargo.toml index 5074af1..ff28294 100644 --- a/zkvms/risc0/host/Cargo.toml +++ b/zkvms/risc0/host/Cargo.toml @@ -10,3 +10,5 @@ risc0-zkp = { path = "/nix/store/l1rxg23rrrdwi9cf6rc82mqavklvqc18-risc0-unstable hex = { version = "0.4.3", default-features = false, features = ["alloc"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] } serde = "1.0" + +zkvms_host_io = { path = "../../../zkvms_host_io" } diff --git a/zkvms/risc0/host/src/main.rs b/zkvms/risc0/host/src/main.rs index ec1eca3..4077a44 100644 --- a/zkvms/risc0/host/src/main.rs +++ b/zkvms/risc0/host/src/main.rs @@ -1,4 +1,5 @@ -use risc0_zkvm::{default_prover, default_executor, ExecutorEnv}; +use zkvms_host_io::{read_args, RunType::{ Execute, Prove, Verify }}; +use risc0_zkvm::{default_prover, default_executor, ExecutorEnv, Receipt}; use risc0_zkp::core::digest::Digest; use hex::FromHex; @@ -7,12 +8,10 @@ static HELLO_GUEST_ELF: &[u8] = include_bytes!("./guest"); // https://github.com/risc0/risc0/blob/881e512732eca72849b2d0e263a1242aba3158af/risc0/build/src/lib.rs#L255 static HELLO_GUEST_ID: &str = env!("GUEST_ID"); -fn main() { - let args: Vec = std::env::args().collect(); - - let (graph, colors, coloring): (Vec>, u32, Vec>) = include!(env!("INPUTS")); +type Input = (Vec>, u32, Vec>); - let env = ExecutorEnv::builder() +fn build_env((graph, colors, coloring): &Input) -> ExecutorEnv { + ExecutorEnv::builder() .write(&graph) .unwrap() .write(&colors) @@ -20,41 +19,54 @@ fn main() { .write(&coloring) .unwrap() .build() - .unwrap(); + .unwrap() +} + +fn prove(env: ExecutorEnv) -> Receipt { + default_prover() + .prove(env, HELLO_GUEST_ELF) + .expect("Error occured") + .receipt +} + +fn journal(receipt: Receipt) -> bool { + receipt + .journal + .decode() + .unwrap() +} + +fn main() { + let run_info = read_args(); + let env = build_env(&run_info.input); - match args[1].as_str() { - "execute" => { + match run_info.run_type { + Execute => { let exec = default_executor(); - let output = exec.execute(env, HELLO_GUEST_ELF).unwrap().receipt_claim.unwrap().output.value().unwrap(); + let output = default_executor() + .execute(env, HELLO_GUEST_ELF) + .unwrap() + .receipt_claim + .unwrap() + .output + .value() + .unwrap(); println!("{:#?}", output); }, - "prove" => { - // Obtain the default prover. - let prover = default_prover(); - - // Produce a receipt by proving the specified ELF binary. - let receipt = prover.prove(env, HELLO_GUEST_ELF).expect("Error occured").receipt; - - // Extract journal of receipt - let journal: bool = receipt.journal.decode().unwrap(); - // Print, notice, after committing to a journal, the private input became public - println!("Output from journal: {:?}", journal); + Prove => { + let receipt = prove(env); + println!("Output from journal: {:?}", journal(receipt)); }, - "verify" => { + Verify => { // https://github.com/risc0/risc0/blob/881e512732eca72849b2d0e263a1242aba3158af/risc0/build/src/lib.rs#L197-L199 - let guest_id: Digest = Digest::from_hex(HELLO_GUEST_ID).expect(""); - // https://github.com/risc0/risc0/blob/881e512732eca72849b2d0e263a1242aba3158af/risc0/build/src/lib.rs#L278 - - let prover = default_prover(); - let receipt = prover.prove(env, HELLO_GUEST_ELF).unwrap().receipt; + let guest_id: Digest = Digest::from_hex(HELLO_GUEST_ID).unwrap(); + let receipt = prove(env); receipt.verify(guest_id).unwrap(); - let journal: bool = receipt.journal.decode().unwrap(); - println!("Output from verify: {:?}", journal); + println!("Output from verify: {:?}", journal(receipt)); }, - _ => println!("No arguments provided! Expected execute, prove or verify!"), } } -- cgit v1.2.3