From 1cd4903ca03b7e055c0f973bfb17c937b6eed5b9 Mon Sep 17 00:00:00 2001 From: Moudy Date: Mon, 11 Aug 2025 13:57:53 +0200 Subject: [PATCH] creating shake demo with R0 --- shake256-demo/.DS_Store | Bin 0 -> 6148 bytes shake256-demo/Cargo.toml | 29 ++++++++ shake256-demo/README.md | 111 ++++++++++++++++++++++++++++++ shake256-demo/build.rs | 3 + shake256-demo/rust-toolchain.toml | 4 ++ shake256-demo/src/lib.rs | 37 ++++++++++ shake256-demo/src/main.rs | 44 ++++++++++++ 7 files changed, 228 insertions(+) create mode 100644 shake256-demo/.DS_Store create mode 100644 shake256-demo/Cargo.toml create mode 100644 shake256-demo/README.md create mode 100644 shake256-demo/build.rs create mode 100644 shake256-demo/rust-toolchain.toml create mode 100644 shake256-demo/src/lib.rs create mode 100644 shake256-demo/src/main.rs diff --git a/shake256-demo/.DS_Store b/shake256-demo/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0e33cd1e1b7712ffd1920d24eb4e099dbcd6b5a3 GIT binary patch literal 6148 zcmeHKOK;Oa5S~p^Vz)wy1W0f|vcxqFNhJgk7n7z34qR#k2SCA&qt?RpR(_B}6v=n^ zDM+08Bm5Um@XhX4UbVwog z`3Xtv_Y&n9rM%}?e7U6p0yWFu}9GvqW~k0m}`ZWYaobF+qT(RrE_WxxMjG@7lQ z8*QiU>^bj(Q#lXHu&jzvn7`rHODWUnq8>)ClG%LHxqT?JGEA~rt{jqh29vk1lPs3= zk*u<~P`-g4a9qcobnYz{j~_khdi#UHVA=H+!-Ia;8x9YaOV_#c;NjEb$%phTlbkw4 z;CNPY)8GYsM#U`pV~}U5%$O3k-T{p>MW{N?SAMJ?7+w7w(6n+L889D(vv(bnw>et| zECYX(0X`o*D5Gz%)~JpS6zT~8Y@u5T>in01eLRD{!CE6mAVRwWwW}~s458i8?-@Ja zV69QRlQ54DVIm9jLJ=xD=J%92316cvEd!Q;bq1Qc+2Q;D=<4_Xx|3~L1}p>r6$7F* z4#p!a$=t15i{rc2gT9BduwAWDxu7uDv8wP@d>5(&V;)z4zQI}}dLZ_XfTF<`mVv*@ Fz%Pc`rU?K5 literal 0 HcmV?d00001 diff --git a/shake256-demo/Cargo.toml b/shake256-demo/Cargo.toml new file mode 100644 index 0000000..126afe9 --- /dev/null +++ b/shake256-demo/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "shake256-demo" +version = "0.1.0" +edition = "2021" +build = "build.rs" + +[dependencies] +risc0-zkvm = { version = "2.3.1", features = ["std", "prove"] } +anyhow = "1.0" +hex = "0.4" +serde = { version = "1.0", features = ["derive"] } +sha2 = "0.10" +hkdf = "0.12" +sha3 = "0.10" + +[dev-dependencies] +rand = "0.8" +cipher = { version = "0.4", features = ["std"] } + +[build-dependencies] +risc0-build = "2.3.1" + +[package.metadata.risc0] +methods = ["methods/guest"] + +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(rust_analyzer)'] } + + diff --git a/shake256-demo/README.md b/shake256-demo/README.md new file mode 100644 index 0000000..df70292 --- /dev/null +++ b/shake256-demo/README.md @@ -0,0 +1,111 @@ +# RISC Zero Rust Starter Template + +Welcome to the RISC Zero Rust Starter Template! This template is intended to +give you a starting point for building a project using the RISC Zero zkVM. +Throughout the template (including in this README), you'll find comments +labelled `TODO` in places where you'll need to make changes. To better +understand the concepts behind this template, check out the [zkVM +Overview][zkvm-overview]. + +## Quick Start + +First, make sure [rustup] is installed. The +[`rust-toolchain.toml`][rust-toolchain] file will be used by `cargo` to +automatically install the correct version. + +To build all methods and execute the method within the zkVM, run the following +command: + +```bash +cargo run +``` + +This is an empty template, and so there is no expected output (until you modify +the code). + +### Executing the Project Locally in Development Mode + +During development, faster iteration upon code changes can be achieved by leveraging [dev-mode], we strongly suggest activating it during your early development phase. Furthermore, you might want to get insights into the execution statistics of your project, and this can be achieved by specifying the environment variable `RUST_LOG="[executor]=info"` before running your project. + +Put together, the command to run your project in development mode while getting execution statistics is: + +```bash +RUST_LOG="[executor]=info" RISC0_DEV_MODE=1 cargo run +``` + +### Running Proofs Remotely on Bonsai + +_Note: The Bonsai proving service is still in early Alpha; an API key is +required for access. [Click here to request access][bonsai access]._ + +If you have access to the URL and API key to Bonsai you can run your proofs +remotely. To prove in Bonsai mode, invoke `cargo run` with two additional +environment variables: + +```bash +BONSAI_API_KEY="YOUR_API_KEY" BONSAI_API_URL="BONSAI_URL" cargo run +``` + +## How to Create a Project Based on This Template + +Search this template for the string `TODO`, and make the necessary changes to +implement the required feature described by the `TODO` comment. Some of these +changes will be complex, and so we have a number of instructional resources to +assist you in learning how to write your own code for the RISC Zero zkVM: + +- The [RISC Zero Developer Docs][dev-docs] is a great place to get started. +- Example projects are available in the [examples folder][examples] of + [`risc0`][risc0-repo] repository. +- Reference documentation is available at [https://docs.rs][docs.rs], including + [`risc0-zkvm`][risc0-zkvm], [`cargo-risczero`][cargo-risczero], + [`risc0-build`][risc0-build], and [others][crates]. + +## Directory Structure + +It is possible to organize the files for these components in various ways. +However, in this starter template we use a standard directory structure for zkVM +applications, which we think is a good starting point for your applications. + +```text +project_name +├── Cargo.toml +├── host +│ ├── Cargo.toml +│ └── src +│ └── main.rs <-- [Host code goes here] +└── methods + ├── Cargo.toml + ├── build.rs + ├── guest + │ ├── Cargo.toml + │ └── src + │ └── method_name.rs <-- [Guest code goes here] + └── src + └── lib.rs +``` + +## Video Tutorial + +For a walk-through of how to build with this template, check out this [excerpt +from our workshop at ZK HACK III][zkhack-iii]. + +## Questions, Feedback, and Collaborations + +We'd love to hear from you on [Discord][discord] or [Twitter][twitter]. + +[bonsai access]: https://bonsai.xyz/apply +[cargo-risczero]: https://docs.rs/cargo-risczero +[crates]: https://github.com/risc0/risc0/blob/main/README.md#rust-binaries +[dev-docs]: https://dev.risczero.com +[dev-mode]: https://dev.risczero.com/api/generating-proofs/dev-mode +[discord]: https://discord.gg/risczero +[docs.rs]: https://docs.rs/releases/search?query=risc0 +[examples]: https://github.com/risc0/risc0/tree/main/examples +[risc0-build]: https://docs.rs/risc0-build +[risc0-repo]: https://www.github.com/risc0/risc0 +[risc0-zkvm]: https://docs.rs/risc0-zkvm +[rust-toolchain]: rust-toolchain.toml +[rustup]: https://rustup.rs +[twitter]: https://twitter.com/risczero +[zkhack-iii]: https://www.youtube.com/watch?v=Yg_BGqj_6lg&list=PLcPzhUaCxlCgig7ofeARMPwQ8vbuD6hC5&index=5 +[zkvm-overview]: https://dev.risczero.com/zkvm diff --git a/shake256-demo/build.rs b/shake256-demo/build.rs new file mode 100644 index 0000000..08a8a4e --- /dev/null +++ b/shake256-demo/build.rs @@ -0,0 +1,3 @@ +fn main() { + risc0_build::embed_methods(); +} diff --git a/shake256-demo/rust-toolchain.toml b/shake256-demo/rust-toolchain.toml new file mode 100644 index 0000000..1026997 --- /dev/null +++ b/shake256-demo/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "stable" +components = ["rustfmt", "rust-src", "clippy"] +profile = "minimal" diff --git a/shake256-demo/src/lib.rs b/shake256-demo/src/lib.rs new file mode 100644 index 0000000..d2461e1 --- /dev/null +++ b/shake256-demo/src/lib.rs @@ -0,0 +1,37 @@ +#[cfg(not(rust_analyzer))] +mod generated { + include!(concat!(env!("OUT_DIR"), "/methods.rs")); +} +#[cfg(not(rust_analyzer))] +pub use generated::{GUEST_ELF, GUEST_ID}; + + +#[cfg(rust_analyzer)] +pub const GUEST_ELF: &[u8] = &[]; +#[cfg(rust_analyzer)] +pub const GUEST_ID: [u32; 8] = [0; 8]; + +use anyhow::Result; +use risc0_zkvm::{default_prover, ExecutorEnv, Receipt}; + + +pub fn prove_encrypt( + key: [u8; 32], + nonce: [u8; 12], + plaintext: &[u8], +) -> Result<(Receipt, Vec)> { + let env = ExecutorEnv::builder() + .write(&key)? + .write(&nonce)? + .write(&plaintext.to_vec())? + .build()?; + + let prover = default_prover(); + let prove_info = prover.prove(env, GUEST_ELF)?; + let receipt = prove_info.receipt; + receipt.verify(GUEST_ID)?; + + let ciphertext: Vec = receipt.journal.bytes.clone(); // if this errors, use .to_vec() + + Ok((receipt, ciphertext)) +} diff --git a/shake256-demo/src/main.rs b/shake256-demo/src/main.rs new file mode 100644 index 0000000..61db16b --- /dev/null +++ b/shake256-demo/src/main.rs @@ -0,0 +1,44 @@ +#[cfg(not(rust_analyzer))] +include!(concat!(env!("OUT_DIR"), "/methods.rs")); + +#[cfg(rust_analyzer)] +mod methods { + pub const GUEST_ELF: &[u8] = &[]; + pub const GUEST_ID: [u32; 8] = [0; 8]; +} +#[cfg(rust_analyzer)] +use methods::*; + + +use anyhow::Result; +use hex::encode; +use risc0_zkvm::{default_prover, ExecutorEnv}; + +fn main() -> Result<()> { + // Example inputs + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; + + let env = ExecutorEnv::builder() + .write(&key)? + .write(&nonce)? + .write(&plaintext.to_vec())? + .build()?; + + + let prover = default_prover(); + let prove_info = prover.prove(env, GUEST_ELF)?; + let receipt = prove_info.receipt; + + // (Optionally) verify the proof + receipt.verify(GUEST_ID)?; + + // Extract and print the ciphertext + let ct: &[u8] = &receipt.journal.bytes; + println!("Ciphertext: {}", encode(ct)); + + Ok(()) +} + +