From 579bf0a624cd99db3c8927f68a904aa2105a9563 Mon Sep 17 00:00:00 2001 From: Moudy Date: Tue, 5 Aug 2025 13:31:57 +0200 Subject: [PATCH 01/22] encryption example with risc0 --- .DS_Store | Bin 0 -> 8196 bytes Cargo.toml | 6 + encryption-demo-methods/Cargo.toml | 15 +++ encryption-demo-methods/build.rs | 4 + .../guest/chacha20/Cargo.toml | 19 +++ .../guest/chacha20/src/main.rs | 27 +++++ .../guest/xchacha20/Cargo.toml | 19 +++ .../guest/xchacha20/src/main.rs | 21 ++++ encryption-demo-methods/src/lib.rs | 2 + encryption-demo/Cargo.toml | 16 +++ encryption-demo/encryption-demo/src/main.rs | 14 +++ encryption-demo/src/lib.rs | 114 ++++++++++++++++++ encryption-demo/src/main.rs | 1 + risc0-selective-privacy-poc/.DS_Store | Bin 0 -> 6148 bytes 14 files changed, 258 insertions(+) create mode 100644 .DS_Store create mode 100644 Cargo.toml create mode 100644 encryption-demo-methods/Cargo.toml create mode 100644 encryption-demo-methods/build.rs create mode 100644 encryption-demo-methods/guest/chacha20/Cargo.toml create mode 100644 encryption-demo-methods/guest/chacha20/src/main.rs create mode 100644 encryption-demo-methods/guest/xchacha20/Cargo.toml create mode 100644 encryption-demo-methods/guest/xchacha20/src/main.rs create mode 100644 encryption-demo-methods/src/lib.rs create mode 100644 encryption-demo/Cargo.toml create mode 100644 encryption-demo/encryption-demo/src/main.rs create mode 100644 encryption-demo/src/lib.rs create mode 100644 encryption-demo/src/main.rs create mode 100644 risc0-selective-privacy-poc/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1472fea03afefdd29f856e433147c816ce7bcb5d GIT binary patch literal 8196 zcmeHMyKWOf6g@+1V<{k|O9agdAu5960*ObnL<`hWJ|GV}N)TRlWhdcLtthBb_yKx^ zM1w?07YZbxpoRib17E#ZG+vW@OnmH&_>S2&KB0v!=mg^T02y^BZjiW*&n&M z*xABbI~>X#K9oaN?u4QYI`c;+94fXpv_XNOKve;Kc5mPUHgOBv-ub=x{E76XS(2>x zvvu~hXV3rmbZK_~n(yx&=+C#ryMf~i9Ev9HU<3ECgQ16v=^MURVh{FKK3=rgVtks7 z&?iTD_;_sb2b}c~T`c(c=bjn-(kt=zUI}~=F~Xl5;p0Em_@_~h$2@s#pwHUHHd*cR zY96_X*u@%q@0-sVx{~XahsBx0XOi<`mUow}u7~6$?*X%Y{=IBcud6rTGa*{kXp345 zs7+6*0G#+f?w51>vR~daFNb7Y7bAJNJ#C6Sa&$Zfjg=2%;5-uh<*C(Q>$UWb)=Jgu z;WfY6dFvI>=c?#o%|k`|tC_d(OYEhr#BOm#l{w_R>tZ|}oUXxnfZIsPBOm|9F1;Q1 zOCHH1!9(PbJnCXR9(FfxLFc&3`3#ZzdYpVw@^F61BYHE&ql_8dk-8X<2VN5-F#dkMtY^`Jg~h;9vz*BUw%s8RO2@m1up|j#yV&b anyhow::Result<()> { + let msg = b"zk-encrypted hello world!"; + let mut rng = rand::thread_rng(); + let key: [u8; 32] = rng.gen(); + let nonce: [u8; 12] = rng.gen(); + + let (ciphertext, _proof) = encrypt_chacha20(&key, &nonce, msg)?; + println!("ciphertext = {}", hex::encode(ciphertext)); + println!("proof OK"); + Ok(()) +} diff --git a/encryption-demo/src/lib.rs b/encryption-demo/src/lib.rs new file mode 100644 index 0000000..feaab80 --- /dev/null +++ b/encryption-demo/src/lib.rs @@ -0,0 +1,114 @@ +//! Host-side helpers: prove encryption inside RISC Zero and verify receipts. + +use risc0_zkvm::{ + default_prover, ExecutorEnv, Receipt, +}; +use encryption_demo_methods::{ + CHACHA20_ELF, CHACHA20_ID, XCHACHA20_ELF, XCHACHA20_ID, +}; + +/// Encrypt `plaintext` with ChaCha20 inside the zkVM. +/// Returns `(ciphertext, receipt)`. +pub fn encrypt_chacha20( + key: &[u8; 32], + nonce: &[u8; 12], + plaintext: &[u8], +) -> anyhow::Result<(Vec, Receipt)> { + let env = ExecutorEnv::builder() + .write(key)? + .write(nonce)? + .write(&(plaintext.len() as u32))? + .write_slice(plaintext)? + .build()?; + + let prover = default_prover(); + let receipt = prover.prove(env, CHACHA20_ELF)?; + receipt.verify(CHACHA20_ID)?; + + let ciphertext: Vec = receipt.journal.decode()?; + Ok((ciphertext, receipt)) +} + +/// Same API for XChaCha20 (24-byte nonce) +pub fn encrypt_xchacha20( + key: &[u8; 32], + nonce: &[u8; 24], + plaintext: &[u8], +) -> anyhow::Result<(Vec, Receipt)> { + let env = ExecutorEnv::builder() + .write(key)? + .write(nonce)? + .write(&(plaintext.len() as u32))? + .write_slice(plaintext)? + .build()?; + + let prover = default_prover(); + let receipt = prover.prove(env, XCHACHA20_ELF)?; + receipt.verify(XCHACHA20_ID)?; + + let ciphertext: Vec = receipt.journal.decode()?; + Ok((ciphertext, receipt)) +} + + +// Test for chacha20 +#[cfg(test)] +mod tests { + use super::*; + use chacha20::cipher::{KeyIvInit, StreamCipher}; + use chacha20::ChaCha20; + + // RFC 8439 test vector + const KEY: [u8; 32] = [ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 + ]; + const NONCE: [u8; 12] = [0; 12]; + const PLAINTEXT: [u8; 16] = *b"example message!"; + + #[test] + fn chacha20_vector_matches() { + let (ciphertext, _) = encrypt_chacha20(&KEY, &NONCE, &PLAINTEXT).unwrap(); + + // host decryption + let mut buf = ciphertext.clone(); + let mut cipher = ChaCha20::new(&KEY.into(), &NONCE.into()); + cipher.apply_keystream(&mut buf); + + assert_eq!(&buf, &PLAINTEXT); + } +} + + +// Tests for Xchacha20 +#[cfg(test)] +mod tests { + use super::*; + use xchacha20::cipher::{KeyIvInit, StreamCipher}; + use xchacha20::xChaCha20; + + // RFC 8439 test vector + const KEY: [u8; 32] = [ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 + ]; + const NONCE: [u8; 12] = [0; 12]; + const PLAINTEXT: [u8; 16] = *b"example message!"; + + #[test] + fn xchacha20_vector_matches() { + let (ciphertext, _) = encrypt_xchacha20(&KEY, &NONCE, &PLAINTEXT).unwrap(); + + // host decryption + let mut buf = ciphertext.clone(); + let mut cipher = xChaCha20::new(&KEY.into(), &NONCE.into()); + cipher.apply_keystream(&mut buf); + + assert_eq!(&buf, &PLAINTEXT); + } +} + diff --git a/encryption-demo/src/main.rs b/encryption-demo/src/main.rs new file mode 100644 index 0000000..1a4baf5 --- /dev/null +++ b/encryption-demo/src/main.rs @@ -0,0 +1 @@ + diff --git a/risc0-selective-privacy-poc/.DS_Store b/risc0-selective-privacy-poc/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a84a518e783f0aeeada57137811323ced2d215d1 GIT binary patch literal 6148 zcmeHKy-ou$47Q;{Cw1utV>afFv`aW$7bc{>0FukSbe{#DyFvt>Jjy?Xon`nWpZULVHHcQNo?jtVjBr(d4N^)13& zoB?OR8E^)ifuk^hJ6ojcD|+t?I0MeWjsZCz0-9hnOp5vFK$lAZpgg0qK$luVa)Mzr zOp5S8SW|(T%2r~qro$dAE*d69O((YEgKg!X;)V0-m_Ouj;;87oGvEw#893G9K<@tu zewkvC-%at6GvEyTGX}WVOq&`XWq0er=gD0g(C*MgBrb^pf!=upU?AtnMRuw`h>o~u Wm=t9evFCK4KLj!%-Z=wDVBi}T2rWne literal 0 HcmV?d00001 From 09046d39344d4b549f39d6ba2b221c9e02d9a9b8 Mon Sep 17 00:00:00 2001 From: Moudy Date: Tue, 5 Aug 2025 15:07:04 +0200 Subject: [PATCH 02/22] fixed some errors --- Cargo.toml | 4 +++- encryption-demo-methods/Cargo.toml | 7 ++++--- encryption-demo-methods/guest/chacha20/Cargo.toml | 4 ++-- encryption-demo-methods/guest/xchacha20/Cargo.toml | 4 ++-- encryption-demo/Cargo.toml | 3 ++- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d8f3b24..325fc79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,8 @@ [workspace] +resolver = "2" members = [ "encryption-demo", "encryption-demo-methods", + "encryption-demo-methods/guest/chacha20", + "encryption-demo-methods/guest/xchacha20", ] -workspace.resolver = "2" diff --git a/encryption-demo-methods/Cargo.toml b/encryption-demo-methods/Cargo.toml index ff9c336..bccc044 100644 --- a/encryption-demo-methods/Cargo.toml +++ b/encryption-demo-methods/Cargo.toml @@ -6,9 +6,10 @@ publish = false build = "build.rs" [dependencies] -build = "0.0.2" -risc0-zkvm = { version = "2.2", default-features = false } -risc0-build = { version = "2.2", default-features = false } +risc0-zkvm = { version = "2.3.1", default-features = false } + +[build-dependencies] +risc0-build = { version = "2.3.1", default-features = false } [package.metadata.risc0] methods = ["guest/chacha20", "guest/xchacha20"] diff --git a/encryption-demo-methods/guest/chacha20/Cargo.toml b/encryption-demo-methods/guest/chacha20/Cargo.toml index bce8cdd..fdf0bfb 100644 --- a/encryption-demo-methods/guest/chacha20/Cargo.toml +++ b/encryption-demo-methods/guest/chacha20/Cargo.toml @@ -9,9 +9,9 @@ crate-type = ["staticlib"] [dependencies] # no_std stream cipher chacha20 = { version = "0.9", default-features = false } -cipher = { version = "0.5", default-features = false } +cipher = { version = "0.4", default-features = false } -risc0-zkvm-guest = "2.2" +risc0-zkvm-guest = "2.3.1" [features] default = ["alloc"] # pull in alloc inside guest diff --git a/encryption-demo-methods/guest/xchacha20/Cargo.toml b/encryption-demo-methods/guest/xchacha20/Cargo.toml index f94e10f..efd6e1e 100644 --- a/encryption-demo-methods/guest/xchacha20/Cargo.toml +++ b/encryption-demo-methods/guest/xchacha20/Cargo.toml @@ -9,9 +9,9 @@ crate-type = ["staticlib"] [dependencies] # no_std stream cipher xchacha20 = { version = "0.9", default-features = false } -cipher = { version = "0.5", default-features = false } +cipher = { version = "0.4", default-features = false } -risc0-zkvm-guest = "2.2" +risc0-zkvm-guest = "2.3.1" [features] default = ["alloc"] # pull in alloc inside guest diff --git a/encryption-demo/Cargo.toml b/encryption-demo/Cargo.toml index 6d6a565..bd556c7 100644 --- a/encryption-demo/Cargo.toml +++ b/encryption-demo/Cargo.toml @@ -5,11 +5,12 @@ edition = "2021" publish = false [dependencies] -risc0-zkvm = "2.2" +risc0-zkvm = "2.3.1" rand = { version = "0.8", features = ["std"]} hex = "0.4" encryption-demo-methods = { path = "../encryption-demo-methods" } +risc0-build = "2.3.1" [[bin]] name = "encrypt-demo" From 145d3d389b31a3f371cf053a94afeec5efbe018d Mon Sep 17 00:00:00 2001 From: Moudy Date: Thu, 7 Aug 2025 10:47:00 +0200 Subject: [PATCH 03/22] some changes --- Cargo.toml | 1 - encryption-demo-methods/Cargo.toml | 4 ++-- .../guest/chacha20/Cargo.toml | 1 - .../guest/xchacha20/Cargo.toml | 19 ----------------- .../guest/xchacha20/src/main.rs | 21 ------------------- encryption-demo/Cargo.toml | 4 ++-- 6 files changed, 4 insertions(+), 46 deletions(-) delete mode 100644 encryption-demo-methods/guest/xchacha20/Cargo.toml delete mode 100644 encryption-demo-methods/guest/xchacha20/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 325fc79..25b0599 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,4 @@ members = [ "encryption-demo", "encryption-demo-methods", "encryption-demo-methods/guest/chacha20", - "encryption-demo-methods/guest/xchacha20", ] diff --git a/encryption-demo-methods/Cargo.toml b/encryption-demo-methods/Cargo.toml index bccc044..508c77f 100644 --- a/encryption-demo-methods/Cargo.toml +++ b/encryption-demo-methods/Cargo.toml @@ -6,10 +6,10 @@ publish = false build = "build.rs" [dependencies] -risc0-zkvm = { version = "2.3.1", default-features = false } +risc0-zkvm = { version = "2.2", default-features = false } [build-dependencies] -risc0-build = { version = "2.3.1", default-features = false } +risc0-build = { version = "2.2", default-features = false } [package.metadata.risc0] methods = ["guest/chacha20", "guest/xchacha20"] diff --git a/encryption-demo-methods/guest/chacha20/Cargo.toml b/encryption-demo-methods/guest/chacha20/Cargo.toml index fdf0bfb..0d21f6a 100644 --- a/encryption-demo-methods/guest/chacha20/Cargo.toml +++ b/encryption-demo-methods/guest/chacha20/Cargo.toml @@ -11,7 +11,6 @@ crate-type = ["staticlib"] chacha20 = { version = "0.9", default-features = false } cipher = { version = "0.4", default-features = false } -risc0-zkvm-guest = "2.3.1" [features] default = ["alloc"] # pull in alloc inside guest diff --git a/encryption-demo-methods/guest/xchacha20/Cargo.toml b/encryption-demo-methods/guest/xchacha20/Cargo.toml deleted file mode 100644 index efd6e1e..0000000 --- a/encryption-demo-methods/guest/xchacha20/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "xchacha20" -version = "0.1.0" -edition = "2021" -publish = false -# Ensure staticlib for RISC-V -crate-type = ["staticlib"] - -[dependencies] -# no_std stream cipher -xchacha20 = { version = "0.9", default-features = false } -cipher = { version = "0.4", default-features = false } - -risc0-zkvm-guest = "2.3.1" - -[features] -default = ["alloc"] # pull in alloc inside guest -alloc = [] - diff --git a/encryption-demo-methods/guest/xchacha20/src/main.rs b/encryption-demo-methods/guest/xchacha20/src/main.rs deleted file mode 100644 index e76ec7a..0000000 --- a/encryption-demo-methods/guest/xchacha20/src/main.rs +++ /dev/null @@ -1,21 +0,0 @@ -#![no_std] -#![no_main] - -use chacha20::cipher::{KeyIvInit, StreamCipher}; -use chacha20::XChaCha20; -use risc0_zkvm::guest::env; -risc0_zkvm_guest::entry!(main); - -pub fn main() { - let key: [u8; 32] = env::read(); - let nonce: [u8; 24] = env::read(); - let len: u32 = env::read(); - let mut plaintext = vec![0u8; len as usize]; - env::read_slice(&mut plaintext).unwrap(); - - let mut cipher = XChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut plaintext); - - env::commit_slice(&plaintext); -} - diff --git a/encryption-demo/Cargo.toml b/encryption-demo/Cargo.toml index bd556c7..3461c8f 100644 --- a/encryption-demo/Cargo.toml +++ b/encryption-demo/Cargo.toml @@ -5,12 +5,12 @@ edition = "2021" publish = false [dependencies] -risc0-zkvm = "2.3.1" +risc0-zkvm = "2.2" rand = { version = "0.8", features = ["std"]} hex = "0.4" encryption-demo-methods = { path = "../encryption-demo-methods" } -risc0-build = "2.3.1" +risc0-build = "2.2" [[bin]] name = "encrypt-demo" From f0ff8340bac4f666d487b8ed4807f2942fbfd185 Mon Sep 17 00:00:00 2001 From: Moudy Date: Thu, 7 Aug 2025 11:00:20 +0200 Subject: [PATCH 04/22] removed --- .DS_Store | Bin 8196 -> 10244 bytes encryption-demo-methods/Cargo.toml | 16 --- encryption-demo-methods/build.rs | 4 - .../guest/chacha20/Cargo.toml | 18 --- .../guest/chacha20/src/main.rs | 27 ----- encryption-demo-methods/src/lib.rs | 2 - encryption-demo/.DS_Store | Bin 0 -> 6148 bytes encryption-demo/Cargo.toml | 17 --- encryption-demo/encryption-demo/.DS_Store | Bin 0 -> 6148 bytes encryption-demo/encryption-demo/src/main.rs | 14 --- encryption-demo/src/lib.rs | 114 ------------------ encryption-demo/src/main.rs | 1 - 12 files changed, 213 deletions(-) delete mode 100644 encryption-demo-methods/Cargo.toml delete mode 100644 encryption-demo-methods/build.rs delete mode 100644 encryption-demo-methods/guest/chacha20/Cargo.toml delete mode 100644 encryption-demo-methods/guest/chacha20/src/main.rs delete mode 100644 encryption-demo-methods/src/lib.rs create mode 100644 encryption-demo/.DS_Store delete mode 100644 encryption-demo/Cargo.toml create mode 100644 encryption-demo/encryption-demo/.DS_Store delete mode 100644 encryption-demo/encryption-demo/src/main.rs delete mode 100644 encryption-demo/src/lib.rs delete mode 100644 encryption-demo/src/main.rs diff --git a/.DS_Store b/.DS_Store index 1472fea03afefdd29f856e433147c816ce7bcb5d..15851dd5933914217659d919a767db875ab3046b 100644 GIT binary patch literal 10244 zcmeHM&u<$=6n>l7&W5xi2dYR#NGp&KLTT!h{tCUgsROD6NUae)(8{&FR$^uCv1}&> zqDWQ`NWcY^e}Fq6xF9%kD+eUt09QC5&XgOc^1Ydf{brLbB1klc8EN+I&b;^Ld*8en zXC_1xtZv22L=h2%s4VA?;c-CWey;nGfGvFEU40r}S z14o4c{AP0r=Zy4E&wyvZGcaa=_lF3TWy#8sk$UUELmvSkb7S1gJ z1PPuE+Ps>mh8>5OGLNzJKo>Si*$iOQrX*$adCKN-YElQjd-OJSX@E!-zE4q%_BJWk zI(X{eS=WdCuEqORy_>Z9gJSXLU}`#-FU*89;aqsDvZ02RL2a zwfY;a=CC^d#1+*U)LNZpPaCx2CRA?TXmw&WTvc10xUX$1rX!pUXRGrIJ3D93zpxlx zIJ>(T?W~+BE=DUWXLfgI!xPV(zI461nY26VJ?0HNqTM;^xcMGG-^bfOP_$j?brRKC zXE&$|4<{7VwxKbg4GE!-Y zBzGPu+M+&EwV2Z6l+Ms}V|(@!9oyKCjP35%A~vBhiS1wwH@1&406jTDirMaCMiSJ9 zUR>LFJB5YDBWz=VH?ZvZ8_U~`N-s4T)giq~241`~diwDABwO(E{ffziL*iwohmXW`PzKfoas!2E{ZLYwK_3V4-CZXI387}2r&X{~82y{|}Y z1)bFivOA`ltII1ocwVe%N7spl!r%3{yh5xm8Phd2j>+*MBg@ip;se=woOfswJ5URs zsr{I+x1=x&T^IAU@6x^`=JLA&v+yA?XX!YY%^&RrVjn~B)2Ft}Gw&P+V79t0=Hp+C zG24ELIr=t*d8{*8yIDF8X8H!h_~IGx40r}S1D=7S&%m^S81eo8TK50{kA5H?p=ZD| zaA*dCT)9$Sh56nMQQ8yVwMVG8QMs_*$VhF12di+qzK+KSU&lXZ+lnl?puc40$VhFD vc5M#%fBiEc<9s zzWKZe8{=d~v8Kr@#j+>oh)`0h_@s1e)-s!)#4F$x_@5Lo z=YxbB3>hnj_UJ%ip8&u*x~-vZeisZ!GKP$mLquT0QGt#s_=zDL9sQE?LdMFWqm$sr zhv1n7zfgpo9pg)zP9k*ZORs=eU|E5hYqre&e{%i%f0^W4UIDMbjZ#3=kE7!rmc)1K y%HqsjE8*|q#!O!2&{9zFb!;o;T2e`0>1!|!L;uH literal 0 HcmV?d00001 diff --git a/encryption-demo/Cargo.toml b/encryption-demo/Cargo.toml deleted file mode 100644 index 3461c8f..0000000 --- a/encryption-demo/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "encryption-demo" -version = "0.1.0" -edition = "2021" -publish = false - -[dependencies] -risc0-zkvm = "2.2" -rand = { version = "0.8", features = ["std"]} -hex = "0.4" - -encryption-demo-methods = { path = "../encryption-demo-methods" } -risc0-build = "2.2" - -[[bin]] -name = "encrypt-demo" -path = "src/main.rs" diff --git a/encryption-demo/encryption-demo/.DS_Store b/encryption-demo/encryption-demo/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..20772dc2d294cfe04ce68d4c2e3f7800ea1ebebd GIT binary patch literal 6148 zcmeHKK}!Nb6rNGb6$v~PcwFczq*z&WSKQKUhmlcT^itcH&uzwBAoPB7~h1jTIk__xFzXgLROh(n}yUb*BYwLL@?-ZR| z|3OXsQD-zxnw{Y_JzXdj1#`O-T!#HgyR><%;!!7z`$L@&_WB69xeDW+nl#lo?j<_c zHxo|5DYQ%F>9pS1ueb+$vx+;dRd;K!>xZ*h!P(m0IXZ7YMo+PNF~m!JlUg<`&fyh? zt>)Pu#*vEe;h9IzqZpY1W`G%3T?Xul=M-1BM6Qe(Ue z;X--_d1MBdfkg(W_k)z^{y+J-|JOl0FaylMYBC^mEx*-7ZT4(n7kURJp}5rG kR|*V$6(g3e;xbeV{4N<{9 literal 0 HcmV?d00001 diff --git a/encryption-demo/encryption-demo/src/main.rs b/encryption-demo/encryption-demo/src/main.rs deleted file mode 100644 index 85f20af..0000000 --- a/encryption-demo/encryption-demo/src/main.rs +++ /dev/null @@ -1,14 +0,0 @@ -use encryption_demo::*; -use rand::Rng; - -fn main() -> anyhow::Result<()> { - let msg = b"zk-encrypted hello world!"; - let mut rng = rand::thread_rng(); - let key: [u8; 32] = rng.gen(); - let nonce: [u8; 12] = rng.gen(); - - let (ciphertext, _proof) = encrypt_chacha20(&key, &nonce, msg)?; - println!("ciphertext = {}", hex::encode(ciphertext)); - println!("proof OK"); - Ok(()) -} diff --git a/encryption-demo/src/lib.rs b/encryption-demo/src/lib.rs deleted file mode 100644 index feaab80..0000000 --- a/encryption-demo/src/lib.rs +++ /dev/null @@ -1,114 +0,0 @@ -//! Host-side helpers: prove encryption inside RISC Zero and verify receipts. - -use risc0_zkvm::{ - default_prover, ExecutorEnv, Receipt, -}; -use encryption_demo_methods::{ - CHACHA20_ELF, CHACHA20_ID, XCHACHA20_ELF, XCHACHA20_ID, -}; - -/// Encrypt `plaintext` with ChaCha20 inside the zkVM. -/// Returns `(ciphertext, receipt)`. -pub fn encrypt_chacha20( - key: &[u8; 32], - nonce: &[u8; 12], - plaintext: &[u8], -) -> anyhow::Result<(Vec, Receipt)> { - let env = ExecutorEnv::builder() - .write(key)? - .write(nonce)? - .write(&(plaintext.len() as u32))? - .write_slice(plaintext)? - .build()?; - - let prover = default_prover(); - let receipt = prover.prove(env, CHACHA20_ELF)?; - receipt.verify(CHACHA20_ID)?; - - let ciphertext: Vec = receipt.journal.decode()?; - Ok((ciphertext, receipt)) -} - -/// Same API for XChaCha20 (24-byte nonce) -pub fn encrypt_xchacha20( - key: &[u8; 32], - nonce: &[u8; 24], - plaintext: &[u8], -) -> anyhow::Result<(Vec, Receipt)> { - let env = ExecutorEnv::builder() - .write(key)? - .write(nonce)? - .write(&(plaintext.len() as u32))? - .write_slice(plaintext)? - .build()?; - - let prover = default_prover(); - let receipt = prover.prove(env, XCHACHA20_ELF)?; - receipt.verify(XCHACHA20_ID)?; - - let ciphertext: Vec = receipt.journal.decode()?; - Ok((ciphertext, receipt)) -} - - -// Test for chacha20 -#[cfg(test)] -mod tests { - use super::*; - use chacha20::cipher::{KeyIvInit, StreamCipher}; - use chacha20::ChaCha20; - - // RFC 8439 test vector - const KEY: [u8; 32] = [ - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 - ]; - const NONCE: [u8; 12] = [0; 12]; - const PLAINTEXT: [u8; 16] = *b"example message!"; - - #[test] - fn chacha20_vector_matches() { - let (ciphertext, _) = encrypt_chacha20(&KEY, &NONCE, &PLAINTEXT).unwrap(); - - // host decryption - let mut buf = ciphertext.clone(); - let mut cipher = ChaCha20::new(&KEY.into(), &NONCE.into()); - cipher.apply_keystream(&mut buf); - - assert_eq!(&buf, &PLAINTEXT); - } -} - - -// Tests for Xchacha20 -#[cfg(test)] -mod tests { - use super::*; - use xchacha20::cipher::{KeyIvInit, StreamCipher}; - use xchacha20::xChaCha20; - - // RFC 8439 test vector - const KEY: [u8; 32] = [ - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 - ]; - const NONCE: [u8; 12] = [0; 12]; - const PLAINTEXT: [u8; 16] = *b"example message!"; - - #[test] - fn xchacha20_vector_matches() { - let (ciphertext, _) = encrypt_xchacha20(&KEY, &NONCE, &PLAINTEXT).unwrap(); - - // host decryption - let mut buf = ciphertext.clone(); - let mut cipher = xChaCha20::new(&KEY.into(), &NONCE.into()); - cipher.apply_keystream(&mut buf); - - assert_eq!(&buf, &PLAINTEXT); - } -} - diff --git a/encryption-demo/src/main.rs b/encryption-demo/src/main.rs deleted file mode 100644 index 1a4baf5..0000000 --- a/encryption-demo/src/main.rs +++ /dev/null @@ -1 +0,0 @@ - From 117f9ec8fa0d644b6f2609b04555c580671ef331 Mon Sep 17 00:00:00 2001 From: Moudy Date: Thu, 7 Aug 2025 11:01:10 +0200 Subject: [PATCH 05/22] removed all --- encryption-demo/.DS_Store | Bin 6148 -> 0 bytes encryption-demo/encryption-demo/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 encryption-demo/.DS_Store delete mode 100644 encryption-demo/encryption-demo/.DS_Store diff --git a/encryption-demo/.DS_Store b/encryption-demo/.DS_Store deleted file mode 100644 index 196cefdf0533c86192a13ad47228b9455a137f45..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK!EO^V5FIyxY+505D1u8ROI$+`0h_@s1e)-s!)#4F$x_@5Lo z=YxbB3>hnj_UJ%ip8&u*x~-vZeisZ!GKP$mLquT0QGt#s_=zDL9sQE?LdMFWqm$sr zhv1n7zfgpo9pg)zP9k*ZORs=eU|E5hYqre&e{%i%f0^W4UIDMbjZ#3=kE7!rmc)1K y%HqsjE8*|q#!O!2&{9zFb!;o;T2e`0>1!|!L;uH diff --git a/encryption-demo/encryption-demo/.DS_Store b/encryption-demo/encryption-demo/.DS_Store deleted file mode 100644 index 20772dc2d294cfe04ce68d4c2e3f7800ea1ebebd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKK}!Nb6rNGb6$v~PcwFczq*z&WSKQKUhmlcT^itcH&uzwBAoPB7~h1jTIk__xFzXgLROh(n}yUb*BYwLL@?-ZR| z|3OXsQD-zxnw{Y_JzXdj1#`O-T!#HgyR><%;!!7z`$L@&_WB69xeDW+nl#lo?j<_c zHxo|5DYQ%F>9pS1ueb+$vx+;dRd;K!>xZ*h!P(m0IXZ7YMo+PNF~m!JlUg<`&fyh? zt>)Pu#*vEe;h9IzqZpY1W`G%3T?Xul=M-1BM6Qe(Ue z;X--_d1MBdfkg(W_k)z^{y+J-|JOl0FaylMYBC^mEx*-7ZT4(n7kURJp}5rG kR|*V$6(g3e;xbeV{4N<{9 From 1d54ad2bc6e14c9d2267b01c3583b61248b3bafe Mon Sep 17 00:00:00 2001 From: Moudy Date: Thu, 7 Aug 2025 17:49:26 +0200 Subject: [PATCH 06/22] WIP RISC Zero ChaCha20 demo --- .DS_Store | Bin 10244 -> 10244 bytes Cargo.toml | 5 +- encryption-demo/.gitignore | 4 + encryption-demo/Cargo.toml | 3 + encryption-demo/LICENSE | 201 ++++++++++++++++++++++ encryption-demo/README.md | 111 ++++++++++++ encryption-demo/build.log | 256 ++++++++++++++++++++++++++++ encryption-demo/guest/Cargo.toml | 13 ++ encryption-demo/guest/src/lib.rs | 22 +++ encryption-demo/guest/src/main.rs | 1 + encryption-demo/host/Cargo.toml | 15 ++ encryption-demo/host/build.rs | 3 + encryption-demo/host/src/main.rs | 32 ++++ encryption-demo/rust-toolchain.toml | 4 + 14 files changed, 667 insertions(+), 3 deletions(-) create mode 100644 encryption-demo/.gitignore create mode 100644 encryption-demo/Cargo.toml create mode 100644 encryption-demo/LICENSE create mode 100644 encryption-demo/README.md create mode 100644 encryption-demo/build.log create mode 100644 encryption-demo/guest/Cargo.toml create mode 100644 encryption-demo/guest/src/lib.rs create mode 100644 encryption-demo/guest/src/main.rs create mode 100644 encryption-demo/host/Cargo.toml create mode 100644 encryption-demo/host/build.rs create mode 100644 encryption-demo/host/src/main.rs create mode 100644 encryption-demo/rust-toolchain.toml diff --git a/.DS_Store b/.DS_Store index 15851dd5933914217659d919a767db875ab3046b..1bee366945eef3546dd29903c2034cc1db573693 100644 GIT binary patch delta 74 zcmZn(XbIR5F37lZa)e-^hGccMxtW2Eg07*dVXclrwWSe|V`*trTg%BIs;qAv6rY`w do0s1``Msb#W6x%Op>MpK*%f}XY!YQ+1^_~m7GMAX delta 77 zcmZn(XbIR5F37lja)e-^ns{}!iJ7sEg1JepjzYDik%5karKM4AEhmSlvc7dte0EN5 hUVi7~kAm`yU7H1kzVU8mRrt#?IY?A;v#jV#CIFWE7#IKm diff --git a/Cargo.toml b/Cargo.toml index 25b0599..127f1e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,6 @@ [workspace] resolver = "2" members = [ - "encryption-demo", - "encryption-demo-methods", - "encryption-demo-methods/guest/chacha20", + "guest", + "host", ] diff --git a/encryption-demo/.gitignore b/encryption-demo/.gitignore new file mode 100644 index 0000000..f4247e1 --- /dev/null +++ b/encryption-demo/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +Cargo.lock +methods/guest/Cargo.lock +target/ diff --git a/encryption-demo/Cargo.toml b/encryption-demo/Cargo.toml new file mode 100644 index 0000000..26a793e --- /dev/null +++ b/encryption-demo/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +resolver = "2" +members = ["guest", "host"] diff --git a/encryption-demo/LICENSE b/encryption-demo/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/encryption-demo/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/encryption-demo/README.md b/encryption-demo/README.md new file mode 100644 index 0000000..df70292 --- /dev/null +++ b/encryption-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/encryption-demo/build.log b/encryption-demo/build.log new file mode 100644 index 0000000..54c533e --- /dev/null +++ b/encryption-demo/build.log @@ -0,0 +1,256 @@ + Compiling proc-macro2 v1.0.95 + Compiling unicode-ident v1.0.18 + Compiling libc v0.2.174 + Compiling version_check v0.9.5 + Compiling serde v1.0.219 + Compiling typenum v1.18.0 + Compiling zerocopy v0.8.26 + Compiling cfg-if v1.0.1 + Compiling once_cell v1.21.3 + Compiling paste v1.0.15 + Compiling subtle v2.6.1 + Compiling pin-project-lite v0.2.16 + Compiling autocfg v1.5.0 + Compiling rand_core v0.6.4 + Compiling thiserror v2.0.12 + Compiling generic-array v0.14.7 + Compiling ahash v0.8.12 + Compiling hashbrown v0.15.4 + Compiling equivalent v1.0.2 + Compiling const-oid v0.9.6 + Compiling winnow v0.7.12 + Compiling toml_write v0.1.2 + Compiling anyhow v1.0.98 + Compiling indexmap v2.10.0 + Compiling tracing-core v0.1.34 + Compiling log v0.4.27 + Compiling cfg_aliases v0.2.1 + Compiling semver v1.0.26 + Compiling borsh v1.5.7 + Compiling num-traits v0.2.19 + Compiling stable_deref_trait v1.2.0 + Compiling memchr v2.7.5 + Compiling quote v1.0.40 + Compiling bitflags v2.9.1 + Compiling syn v2.0.104 + Compiling itoa v1.0.15 + Compiling cpufeatures v0.2.17 + Compiling getrandom v0.3.3 + Compiling allocator-api2 v0.2.21 + Compiling futures-core v0.3.31 + Compiling unicode-xid v0.2.6 + Compiling rustix v1.0.8 + Compiling serde_json v1.0.142 + Compiling shlex v1.3.0 + Compiling ident_case v1.0.1 + Compiling rustversion v1.0.21 + Compiling fnv v1.0.7 + Compiling strsim v0.11.1 + Compiling cc v1.2.31 + Compiling mio v1.0.4 + Compiling socket2 v0.6.0 + Compiling either v1.15.0 + Compiling core-foundation-sys v0.8.7 + Compiling num-integer v0.1.46 + Compiling camino v1.1.10 + Compiling futures-sink v0.3.31 + Compiling crypto-common v0.1.6 + Compiling block-buffer v0.10.4 + Compiling arrayvec v0.7.6 + Compiling core-foundation v0.9.4 + Compiling digest v0.10.7 + Compiling itertools v0.13.0 + Compiling ppv-lite86 v0.2.21 + Compiling num-bigint v0.4.6 + Compiling sha2 v0.10.9 + Compiling blake2 v0.10.6 + Compiling malloc_buf v0.0.6 + Compiling rand_chacha v0.3.1 + Compiling heck v0.5.0 + Compiling bitflags v1.3.2 + Compiling smallvec v1.15.1 + Compiling rand v0.8.5 + Compiling ring v0.17.14 + Compiling syn v1.0.109 + Compiling litemap v0.8.0 + Compiling hex v0.4.3 + Compiling writeable v0.6.1 + Compiling foreign-types-shared v0.3.1 + Compiling core-graphics-types v0.1.3 + Compiling ark-std v0.5.0 + Compiling objc v0.2.7 + Compiling risc0-zkp v2.0.2 + Compiling block v0.1.6 + Compiling icu_normalizer_data v2.0.0 + Compiling futures-io v0.3.31 + Compiling icu_properties_data v2.0.1 + Compiling futures-task v0.3.31 + Compiling pin-utils v0.1.0 + Compiling synstructure v0.13.2 + Compiling darling_core v0.20.11 + Compiling slab v0.4.10 + Compiling futures-util v0.3.31 + Compiling getrandom v0.2.16 + Compiling percent-encoding v2.3.1 + Compiling hex-literal v0.4.1 + Compiling untrusted v0.9.0 + Compiling httparse v1.10.1 + Compiling rustls v0.23.31 + Compiling try-lock v0.2.5 + Compiling spin v0.9.8 + Compiling ryu v1.0.20 + Compiling tower-service v0.3.3 + Compiling lazy_static v1.5.0 + Compiling want v0.3.1 + Compiling serde_derive v1.0.219 + Compiling tracing-attributes v0.1.30 + Compiling zeroize_derive v1.4.2 + Compiling thiserror-impl v2.0.12 + Compiling bytemuck_derive v1.8.1 + Compiling foreign-types-macros v0.2.3 + Compiling zerofrom-derive v0.1.6 + Compiling zeroize v1.8.1 + Compiling yoke-derive v0.8.0 + Compiling stability v0.2.1 + Compiling zerovec-derive v0.11.1 + Compiling displaydoc v0.2.5 + Compiling enum-ordinalize-derive v4.3.1 + Compiling tracing v0.1.41 + Compiling derive_more-impl v2.0.1 + Compiling ark-serialize-derive v0.5.0 + Compiling darling_macro v0.20.11 + Compiling ark-ff-macros v0.5.0 + Compiling enum-ordinalize v4.3.0 + Compiling ark-ff-asm v0.5.0 + Compiling darling v0.20.11 + Compiling zerofrom v0.1.6 + Compiling ark-serialize v0.5.0 + Compiling bytemuck v1.23.1 + Compiling foreign-types v0.5.0 + Compiling strum_macros v0.26.4 + Compiling educe v0.6.0 + Compiling risc0-zkvm-platform v2.0.3 + Compiling rustls-pki-types v1.12.0 + Compiling derive_builder_core v0.20.2 + Compiling cobs v0.3.0 + Compiling yoke v0.8.0 + Compiling derive_builder_macro v0.20.2 + Compiling derive_more v2.0.1 + Compiling risc0-core v2.0.0 + Compiling metal v0.29.0 + Compiling futures-channel v0.3.31 + Compiling tracing-subscriber v0.2.25 + Compiling proc-macro-error-attr v1.0.4 + Compiling ark-ff v0.5.0 + Compiling zerovec v0.11.4 + Compiling zerotrie v0.2.2 + Compiling rustls-webpki v0.103.4 + Compiling elf v0.7.4 + Compiling hashbrown v0.14.5 + Compiling form_urlencoded v1.2.1 + Compiling aho-corasick v1.1.3 + Compiling toml_datetime v0.6.11 + Compiling serde_spanned v0.6.9 + Compiling toml_edit v0.22.27 + Compiling bytes v1.10.1 + Compiling tinystr v0.8.1 + Compiling potential_utf v0.1.2 + Compiling postcard v1.1.3 + Compiling icu_locale_core v2.0.0 + Compiling icu_collections v2.0.0 + Compiling tokio v1.47.1 + Compiling http v1.3.1 + Compiling icu_provider v2.0.0 + Compiling sync_wrapper v1.0.2 + Compiling keccak v0.1.5 + Compiling errno v0.3.13 + Compiling icu_properties v2.0.1 + Compiling icu_normalizer v2.0.0 + Compiling proc-macro-error v1.0.4 + Compiling http-body v1.0.1 + Compiling proc-macro-crate v3.3.0 + Compiling regex-syntax v0.8.5 + Compiling base64 v0.22.1 + Compiling idna_adapter v1.2.1 + Compiling tower-layer v0.3.3 + Compiling ipnet v2.11.0 + Compiling borsh-derive v1.5.7 + Compiling utf8_iter v1.0.4 + Compiling byteorder v1.5.0 + Compiling merlin v3.0.0 + Compiling idna v1.0.3 + Compiling regex-automata v0.4.9 + Compiling ark-poly v0.5.0 + Compiling ark-relations v0.5.1 + Compiling hyper v1.6.0 + Compiling tower v0.5.2 + Compiling ark-snark v0.5.1 + Compiling tokio-rustls v0.26.2 + Compiling hyper-util v0.1.16 + Compiling hashlink v0.9.1 + Compiling ark-ec v0.5.0 + Compiling risc0-binfmt v2.0.2 + Compiling webpki-roots v1.0.2 + Compiling derivative v2.2.0 + Compiling ark-crypto-primitives-macros v0.5.0 + Compiling risc0-circuit-recursion v3.0.0 + Compiling encoding_rs v0.8.35 + Compiling option-ext v0.2.0 + Compiling arraydeque v0.5.1 + Compiling iri-string v0.7.8 + Compiling thiserror v1.0.69 + Compiling fastrand v2.3.0 + Compiling itertools v0.14.0 + Compiling tempfile v3.20.0 + Compiling dirs-sys v0.4.1 + Compiling yaml-rust2 v0.9.0 + Compiling ark-crypto-primitives v0.5.0 + Compiling tower-http v0.6.6 + Compiling hyper-rustls v0.27.7 + Compiling toml v0.8.23 + Compiling regex v1.11.1 + Compiling tokio-util v0.7.16 + Compiling url v2.5.4 + Compiling http-body-util v0.1.3 + Compiling serde_urlencoded v0.7.1 + Compiling cargo-platform v0.1.9 + Compiling strum v0.26.3 + Compiling thiserror-impl v1.0.69 + Compiling include_bytes_aligned v0.1.4 + Compiling heck v0.4.1 + Compiling no_std_strings v0.1.3 + Compiling duplicate v1.0.0 + Compiling risc0-zkos-v1compat v2.0.1 + Compiling rzup v0.4.1 + Compiling cargo_metadata v0.19.2 + Compiling reqwest v0.12.22 + Compiling lazy-regex-proc_macros v3.4.1 + Compiling ark-groth16 v0.5.0 + Compiling dirs v5.0.1 + Compiling prost-derive v0.13.5 + Compiling ark-bn254 v0.5.0 + Compiling derive_builder v0.20.2 + Compiling maybe-async v0.2.10 + Compiling docker-generate v0.1.3 + Compiling bit-vec v0.8.0 + Compiling downcast-rs v1.2.1 + Compiling rrs-lib v0.1.0 + Compiling risc0-circuit-rv32im v3.0.0 + Compiling risc0-build v2.3.1 + Compiling bonsai-sdk v1.4.0 + Compiling risc0-groth16 v2.0.2 + Compiling prost v0.13.5 + Compiling risc0-circuit-keccak v3.0.0 + Compiling lazy-regex v3.4.1 + Compiling bincode v1.3.3 + Compiling inout v0.1.4 + Compiling host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host) + Compiling cipher v0.4.4 + Compiling chacha20 v0.9.1 +error: failed to run custom build command for `host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host)` + +Caused by: + process didn't exit successfully: `/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/target/release/build/host-26fa01eaea54642e/build-script-build` (exit status: 255) + --- stderr + ERROR: No package found in "/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host/../guest" +warning: build failed, waiting for other jobs to finish... diff --git a/encryption-demo/guest/Cargo.toml b/encryption-demo/guest/Cargo.toml new file mode 100644 index 0000000..f842f1a --- /dev/null +++ b/encryption-demo/guest/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "guest" +version = "0.1.0" +edition = "2021" + +[dependencies] +risc0-zkvm = { version = "2.3.1", default-features = false } +chacha20 = "0.9" +cipher = { version = "0.4", default-features = false } + +[features] +default = ["alloc"] # pull in alloc inside guest +alloc = [] diff --git a/encryption-demo/guest/src/lib.rs b/encryption-demo/guest/src/lib.rs new file mode 100644 index 0000000..cf0f528 --- /dev/null +++ b/encryption-demo/guest/src/lib.rs @@ -0,0 +1,22 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} diff --git a/encryption-demo/guest/src/main.rs b/encryption-demo/guest/src/main.rs new file mode 100644 index 0000000..f328e4d --- /dev/null +++ b/encryption-demo/guest/src/main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/encryption-demo/host/Cargo.toml b/encryption-demo/host/Cargo.toml new file mode 100644 index 0000000..fea8e02 --- /dev/null +++ b/encryption-demo/host/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "host" +version = "0.1.0" +edition = "2021" +build = "build.rs" + +[dependencies] +risc0-zkvm = { version = "2.3.1", features = ["std"] } +hex = "0.4" + +[build-dependencies] +risc0-build = "2.3.1" + +[package.metadata.risc0] +methods = ["../guest"] diff --git a/encryption-demo/host/build.rs b/encryption-demo/host/build.rs new file mode 100644 index 0000000..08a8a4e --- /dev/null +++ b/encryption-demo/host/build.rs @@ -0,0 +1,3 @@ +fn main() { + risc0_build::embed_methods(); +} diff --git a/encryption-demo/host/src/main.rs b/encryption-demo/host/src/main.rs new file mode 100644 index 0000000..3da01f8 --- /dev/null +++ b/encryption-demo/host/src/main.rs @@ -0,0 +1,32 @@ +include!(concat!(env!("OUT_DIR"), "/methods.rs")); + +use risc0_zkvm::{Prover, Receipt}; +use hex::encode; + +fn main() -> anyhow::Result<()> { + // Example inputs + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; + + // 1) Create the prover with the embedded guest code + let mut prover = Prover::new(&GUEST_ELF, &GUEST_ID)?; + + // 2) Supply inputs + prover.add_input_u8_slice(&key); + prover.add_input_u8_slice(&nonce); + prover.add_input_u8_slice(plaintext); + + // 3) Run, getting a Receipt (proof + journal) + let receipt: Receipt = prover.run()?; + + // 4) (Optionally) verify the proof + receipt.verify(&GUEST_ID)?; + + // 5) Extract and print the ciphertext + let ct: &[u8] = receipt.get_journal_bytes(); + println!("Ciphertext: {}", encode(ct)); + + Ok(()) +} + diff --git a/encryption-demo/rust-toolchain.toml b/encryption-demo/rust-toolchain.toml new file mode 100644 index 0000000..36614c3 --- /dev/null +++ b/encryption-demo/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "stable" +components = ["rustfmt", "rust-src"] +profile = "minimal" From 379e098ddb021383db33a1e0bec322055523a41b Mon Sep 17 00:00:00 2001 From: Moudy Date: Thu, 7 Aug 2025 18:12:34 +0200 Subject: [PATCH 07/22] revomed lib --- encryption-demo/guest/Cargo.toml | 6 +++--- encryption-demo/guest/src/lib.rs | 22 ---------------------- encryption-demo/host/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 26 deletions(-) delete mode 100644 encryption-demo/guest/src/lib.rs diff --git a/encryption-demo/guest/Cargo.toml b/encryption-demo/guest/Cargo.toml index f842f1a..59cefb5 100644 --- a/encryption-demo/guest/Cargo.toml +++ b/encryption-demo/guest/Cargo.toml @@ -8,6 +8,6 @@ risc0-zkvm = { version = "2.3.1", default-features = false } chacha20 = "0.9" cipher = { version = "0.4", default-features = false } -[features] -default = ["alloc"] # pull in alloc inside guest -alloc = [] +[[bin]] +name = "guest" +path = "src/main.rs" \ No newline at end of file diff --git a/encryption-demo/guest/src/lib.rs b/encryption-demo/guest/src/lib.rs deleted file mode 100644 index cf0f528..0000000 --- a/encryption-demo/guest/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/encryption-demo/host/Cargo.toml b/encryption-demo/host/Cargo.toml index fea8e02..35aef02 100644 --- a/encryption-demo/host/Cargo.toml +++ b/encryption-demo/host/Cargo.toml @@ -12,4 +12,4 @@ hex = "0.4" risc0-build = "2.3.1" [package.metadata.risc0] -methods = ["../guest"] +methods = ["/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/guest"] From 0e1a633a432857102b889bf05da6dd565d78b5d7 Mon Sep 17 00:00:00 2001 From: Moudy Date: Fri, 8 Aug 2025 07:43:12 +0200 Subject: [PATCH 08/22] fixed structure + content --- Cargo.toml | 3 +- encryption-demo/Cargo.toml | 20 +++++-- encryption-demo/{host => }/build.rs | 1 + encryption-demo/guest/src/main.rs | 1 - encryption-demo/host/Cargo.toml | 15 ------ encryption-demo/host/src/main.rs | 32 ----------- encryption-demo/methods/Cargo.toml | 10 ++++ .../{ => methods}/guest/Cargo.toml | 11 ++-- encryption-demo/methods/guest/src/lib.rs | 22 ++++++++ encryption-demo/methods/guest/src/main.rs | 22 ++++++++ encryption-demo/methods/lib.rs | 1 + encryption-demo/src/main.rs | 54 +++++++++++++++++++ 12 files changed, 133 insertions(+), 59 deletions(-) rename encryption-demo/{host => }/build.rs (97%) delete mode 100644 encryption-demo/guest/src/main.rs delete mode 100644 encryption-demo/host/Cargo.toml delete mode 100644 encryption-demo/host/src/main.rs create mode 100644 encryption-demo/methods/Cargo.toml rename encryption-demo/{ => methods}/guest/Cargo.toml (50%) create mode 100644 encryption-demo/methods/guest/src/lib.rs create mode 100644 encryption-demo/methods/guest/src/main.rs create mode 100644 encryption-demo/methods/lib.rs create mode 100644 encryption-demo/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 127f1e9..80d3323 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,5 @@ [workspace] resolver = "2" members = [ - "guest", - "host", + "encryption-demo", ] diff --git a/encryption-demo/Cargo.toml b/encryption-demo/Cargo.toml index 26a793e..cefdc78 100644 --- a/encryption-demo/Cargo.toml +++ b/encryption-demo/Cargo.toml @@ -1,3 +1,17 @@ -[workspace] -resolver = "2" -members = ["guest", "host"] +[package] +name = "encryption-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" + +[build-dependencies] +risc0-build = "2.3.1" + +[package.metadata.risc0] +methods = ["methods/guest"] + diff --git a/encryption-demo/host/build.rs b/encryption-demo/build.rs similarity index 97% rename from encryption-demo/host/build.rs rename to encryption-demo/build.rs index 08a8a4e..aa600df 100644 --- a/encryption-demo/host/build.rs +++ b/encryption-demo/build.rs @@ -1,3 +1,4 @@ fn main() { risc0_build::embed_methods(); } + diff --git a/encryption-demo/guest/src/main.rs b/encryption-demo/guest/src/main.rs deleted file mode 100644 index f328e4d..0000000 --- a/encryption-demo/guest/src/main.rs +++ /dev/null @@ -1 +0,0 @@ -fn main() {} diff --git a/encryption-demo/host/Cargo.toml b/encryption-demo/host/Cargo.toml deleted file mode 100644 index 35aef02..0000000 --- a/encryption-demo/host/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "host" -version = "0.1.0" -edition = "2021" -build = "build.rs" - -[dependencies] -risc0-zkvm = { version = "2.3.1", features = ["std"] } -hex = "0.4" - -[build-dependencies] -risc0-build = "2.3.1" - -[package.metadata.risc0] -methods = ["/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/guest"] diff --git a/encryption-demo/host/src/main.rs b/encryption-demo/host/src/main.rs deleted file mode 100644 index 3da01f8..0000000 --- a/encryption-demo/host/src/main.rs +++ /dev/null @@ -1,32 +0,0 @@ -include!(concat!(env!("OUT_DIR"), "/methods.rs")); - -use risc0_zkvm::{Prover, Receipt}; -use hex::encode; - -fn main() -> anyhow::Result<()> { - // Example inputs - let key = [0x42u8; 32]; - let nonce = [0x24u8; 12]; - let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; - - // 1) Create the prover with the embedded guest code - let mut prover = Prover::new(&GUEST_ELF, &GUEST_ID)?; - - // 2) Supply inputs - prover.add_input_u8_slice(&key); - prover.add_input_u8_slice(&nonce); - prover.add_input_u8_slice(plaintext); - - // 3) Run, getting a Receipt (proof + journal) - let receipt: Receipt = prover.run()?; - - // 4) (Optionally) verify the proof - receipt.verify(&GUEST_ID)?; - - // 5) Extract and print the ciphertext - let ct: &[u8] = receipt.get_journal_bytes(); - println!("Ciphertext: {}", encode(ct)); - - Ok(()) -} - diff --git a/encryption-demo/methods/Cargo.toml b/encryption-demo/methods/Cargo.toml new file mode 100644 index 0000000..9ab59da --- /dev/null +++ b/encryption-demo/methods/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "methods" +version = "0.1.0" +edition = "2021" + +[build-dependencies] +risc0-build = { version = "2.3.1" } + +[package.metadata.risc0] +methods = ["methods/guest"] diff --git a/encryption-demo/guest/Cargo.toml b/encryption-demo/methods/guest/Cargo.toml similarity index 50% rename from encryption-demo/guest/Cargo.toml rename to encryption-demo/methods/guest/Cargo.toml index 59cefb5..ed8fc71 100644 --- a/encryption-demo/guest/Cargo.toml +++ b/encryption-demo/methods/guest/Cargo.toml @@ -1,13 +1,12 @@ [package] -name = "guest" +name = "guest" version = "0.1.0" edition = "2021" +[workspace] + [dependencies] -risc0-zkvm = { version = "2.3.1", default-features = false } -chacha20 = "0.9" +risc0-zkvm = { version = "2.3.1", default-features = false} +chacha20 = { version = "0.9", default-features = false } cipher = { version = "0.4", default-features = false } -[[bin]] -name = "guest" -path = "src/main.rs" \ No newline at end of file diff --git a/encryption-demo/methods/guest/src/lib.rs b/encryption-demo/methods/guest/src/lib.rs new file mode 100644 index 0000000..cf0f528 --- /dev/null +++ b/encryption-demo/methods/guest/src/lib.rs @@ -0,0 +1,22 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} diff --git a/encryption-demo/methods/guest/src/main.rs b/encryption-demo/methods/guest/src/main.rs new file mode 100644 index 0000000..71a7f39 --- /dev/null +++ b/encryption-demo/methods/guest/src/main.rs @@ -0,0 +1,22 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::guest::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} diff --git a/encryption-demo/methods/lib.rs b/encryption-demo/methods/lib.rs new file mode 100644 index 0000000..1bdb308 --- /dev/null +++ b/encryption-demo/methods/lib.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/methods.rs")); diff --git a/encryption-demo/src/main.rs b/encryption-demo/src/main.rs new file mode 100644 index 0000000..a02f3de --- /dev/null +++ b/encryption-demo/src/main.rs @@ -0,0 +1,54 @@ +#[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::ExecutorEnv; +use risc0_zkvm::prove::{Prover, default_prover}; + +fn main() -> Result<()> { + // Example inputs + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; + + /* + 1) Create the prover with the embedded guest code + let mut prover: ! = Prover::new(&GUEST_ELF, &GUEST_ID)?; + 2) Supply inputs + prover.add_input_u8_slice(&key); + prover.add_input_u8_slice(&nonce); + prover.add_input_u8_slice(plaintext); + */ + let env = ExecutorEnv::builder() + .write(&key)? + .write(&nonce)? + .write_slice(plaintext) + .build()?; + + + let prover = default_prover(); + let receipt: ! = prover.prove_elf(env, GUEST_ELF)?; + // 3) Run, getting a Receipt (proof + journal) + //let receipt: Receipt = prover.run()?; + + // 4) (Optionally) verify the proof + receipt.verify(&GUEST_ID)?; + + // 5) Extract and print the ciphertext + let ct: &[u8] = receipt.get_journal_bytes(); + println!("Ciphertext: {}", encode(ct)); + + Ok(()) +} + + From 9410af40ecb6a20d7520935a72825e5afcede7d0 Mon Sep 17 00:00:00 2001 From: Moudy Date: Fri, 8 Aug 2025 08:41:55 +0200 Subject: [PATCH 09/22] fix: prover method --- encryption-demo/src/main.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/encryption-demo/src/main.rs b/encryption-demo/src/main.rs index a02f3de..61db16b 100644 --- a/encryption-demo/src/main.rs +++ b/encryption-demo/src/main.rs @@ -12,8 +12,7 @@ use methods::*; use anyhow::Result; use hex::encode; -use risc0_zkvm::ExecutorEnv; -use risc0_zkvm::prove::{Prover, default_prover}; +use risc0_zkvm::{default_prover, ExecutorEnv}; fn main() -> Result<()> { // Example inputs @@ -21,31 +20,22 @@ fn main() -> Result<()> { let nonce = [0x24u8; 12]; let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; - /* - 1) Create the prover with the embedded guest code - let mut prover: ! = Prover::new(&GUEST_ELF, &GUEST_ID)?; - 2) Supply inputs - prover.add_input_u8_slice(&key); - prover.add_input_u8_slice(&nonce); - prover.add_input_u8_slice(plaintext); - */ let env = ExecutorEnv::builder() .write(&key)? .write(&nonce)? - .write_slice(plaintext) + .write(&plaintext.to_vec())? .build()?; let prover = default_prover(); - let receipt: ! = prover.prove_elf(env, GUEST_ELF)?; - // 3) Run, getting a Receipt (proof + journal) - //let receipt: Receipt = prover.run()?; + let prove_info = prover.prove(env, GUEST_ELF)?; + let receipt = prove_info.receipt; + + // (Optionally) verify the proof + receipt.verify(GUEST_ID)?; - // 4) (Optionally) verify the proof - receipt.verify(&GUEST_ID)?; - - // 5) Extract and print the ciphertext - let ct: &[u8] = receipt.get_journal_bytes(); + // Extract and print the ciphertext + let ct: &[u8] = &receipt.journal.bytes; println!("Ciphertext: {}", encode(ct)); Ok(()) From 51e0893887f37f4d772c492877dac76d89a3901c Mon Sep 17 00:00:00 2001 From: Moudy Date: Fri, 8 Aug 2025 10:12:30 +0200 Subject: [PATCH 10/22] added some tests --- encryption-demo/tests/bad_guest.rs | 30 +++++++++++++++ encryption-demo/tests/proof_works.rs | 49 +++++++++++++++++++++++++ encryption-demo/tests/wrong_image_id.rs | 22 +++++++++++ 3 files changed, 101 insertions(+) create mode 100644 encryption-demo/tests/bad_guest.rs create mode 100644 encryption-demo/tests/proof_works.rs create mode 100644 encryption-demo/tests/wrong_image_id.rs diff --git a/encryption-demo/tests/bad_guest.rs b/encryption-demo/tests/bad_guest.rs new file mode 100644 index 0000000..264c226 --- /dev/null +++ b/encryption-demo/tests/bad_guest.rs @@ -0,0 +1,30 @@ +#[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 risc0_zkvm::{default_prover, ExecutorEnv}; + +#[test] +fn guest_panics_on_bad_key() { + // This key triggers the panic in the guest + let key = [0xFFu8; 32]; + let nonce = [0u8; 12]; + let plaintext = b"panic please".to_vec(); + + let env = ExecutorEnv::builder() + .write(&key).unwrap() + .write(&nonce).unwrap() + .write(&plaintext).unwrap() + .build().unwrap(); + + // Proving should fail when the guest panics + let res = default_prover().prove(env, GUEST_ELF); + assert!(res.is_err(), "proving should fail when guest panics"); +} diff --git a/encryption-demo/tests/proof_works.rs b/encryption-demo/tests/proof_works.rs new file mode 100644 index 0000000..74722c6 --- /dev/null +++ b/encryption-demo/tests/proof_works.rs @@ -0,0 +1,49 @@ +#[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 risc0_zkvm::{default_prover, ExecutorEnv}; + +// Host-side ChaCha20 +use chacha20::{ChaCha20, Key, Nonce}; +use cipher::{KeyIvInit, StreamCipher}; + +#[test] +fn proof_works_and_matches_host_chacha() { + // Inputs (must match what your guest expects) + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; + + // Prove with the R0 guest + let env = ExecutorEnv::builder() + .write(&key).unwrap() + .write(&nonce).unwrap() + .write(&plaintext.to_vec()).unwrap() + .build().unwrap(); + + let prove_info = default_prover().prove(env, GUEST_ELF).expect("prove failed"); + prove_info.receipt.verify(GUEST_ID).expect("verify failed"); + + + // Ciphertext produced by the guest + let guest_ct = prove_info.receipt.journal.bytes.clone(); + + // Host-side reference: ChaCha20 with the same key/nonce + let mut host_ct = plaintext.to_vec(); + let key = Key::from_slice(&key); + let nonce = Nonce::from_slice(&nonce); + let mut cipher = ChaCha20::new(key, nonce); + cipher.apply_keystream(&mut host_ct); + + // Compare + assert_eq!(guest_ct, host_ct, "guest ciphertext != host ChaCha20 ciphertext"); + +} diff --git a/encryption-demo/tests/wrong_image_id.rs b/encryption-demo/tests/wrong_image_id.rs new file mode 100644 index 0000000..8097c62 --- /dev/null +++ b/encryption-demo/tests/wrong_image_id.rs @@ -0,0 +1,22 @@ +use anyhow::Result; +use risc0_zkvm::{default_prover, ExecutorEnv, Digest}; + +#[cfg(not(rust_analyzer))] +include!(concat!(env!("OUT_DIR"), "/methods.rs")); + +#[test] +fn verify_rejects_wrong_image() -> Result<()> { + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"bad id test".to_vec(); + + let env = ExecutorEnv::builder() + .write(&key)?.write(&nonce)?.write(&plaintext)?.build()?; + + let info = default_prover().prove(env, GUEST_ELF)?; + + // Intentionally bogus image id + let bogus = Digest::from([0u32; 8]); + assert!(info.receipt.verify(bogus).is_err(), "verification should fail"); + Ok(()) +} From 9284565d0d95180f81b1e6c7b837f5e3653c984d Mon Sep 17 00:00:00 2001 From: Moudy Date: Fri, 8 Aug 2025 10:13:26 +0200 Subject: [PATCH 11/22] added src inside methods --- encryption-demo/methods/src/lib.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 encryption-demo/methods/src/lib.rs diff --git a/encryption-demo/methods/src/lib.rs b/encryption-demo/methods/src/lib.rs new file mode 100644 index 0000000..1bdb308 --- /dev/null +++ b/encryption-demo/methods/src/lib.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/methods.rs")); From a2b1a9d14c58dcccbebf7746b25e4fe5fefdeca3 Mon Sep 17 00:00:00 2001 From: Moudy Date: Fri, 8 Aug 2025 10:14:27 +0200 Subject: [PATCH 12/22] added lib.rs in methods src --- encryption-demo/src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 encryption-demo/src/lib.rs diff --git a/encryption-demo/src/lib.rs b/encryption-demo/src/lib.rs new file mode 100644 index 0000000..d2461e1 --- /dev/null +++ b/encryption-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)) +} From f34a988e16513f46205f8886a2d652efd1958f83 Mon Sep 17 00:00:00 2001 From: Moudy Date: Fri, 8 Aug 2025 10:14:57 +0200 Subject: [PATCH 13/22] some modifications for tests --- encryption-demo/Cargo.toml | 7 +++++++ encryption-demo/methods/guest/src/main.rs | 4 ++++ encryption-demo/methods/lib.rs | 1 - 3 files changed, 11 insertions(+), 1 deletion(-) delete mode 100644 encryption-demo/methods/lib.rs diff --git a/encryption-demo/Cargo.toml b/encryption-demo/Cargo.toml index cefdc78..ccb9bb1 100644 --- a/encryption-demo/Cargo.toml +++ b/encryption-demo/Cargo.toml @@ -9,9 +9,16 @@ risc0-zkvm = { version = "2.3.1", features = ["std", "prove"] } anyhow = "1.0" hex = "0.4" +[dev-dependencies] +chacha20 = "0.9" +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/encryption-demo/methods/guest/src/main.rs b/encryption-demo/methods/guest/src/main.rs index 71a7f39..ea76354 100644 --- a/encryption-demo/methods/guest/src/main.rs +++ b/encryption-demo/methods/guest/src/main.rs @@ -12,6 +12,10 @@ use chacha20::cipher::{KeyIvInit, StreamCipher}; fn main() { let key: [u8; 32] = env::read(); + // Bad-guest behavior: reject keys starting with 0xFF + if key[0] == 0xFF { + panic!("bad key: starts with 0xFF"); + } let nonce: [u8; 12] = env::read(); let mut buf: Vec = env::read(); diff --git a/encryption-demo/methods/lib.rs b/encryption-demo/methods/lib.rs deleted file mode 100644 index 1bdb308..0000000 --- a/encryption-demo/methods/lib.rs +++ /dev/null @@ -1 +0,0 @@ -include!(concat!(env!("OUT_DIR"), "/methods.rs")); From 4f75773307558ab92c24660c9d696f0540f4e90f Mon Sep 17 00:00:00 2001 From: Moudy Date: Mon, 11 Aug 2025 12:08:47 +0200 Subject: [PATCH 14/22] general fix --- encryption-demo/methods/build.rs | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 encryption-demo/methods/build.rs diff --git a/encryption-demo/methods/build.rs b/encryption-demo/methods/build.rs new file mode 100644 index 0000000..08a8a4e --- /dev/null +++ b/encryption-demo/methods/build.rs @@ -0,0 +1,3 @@ +fn main() { + risc0_build::embed_methods(); +} From 58b624ff3f82f37d6897456bdfce8d64ef7f81a6 Mon Sep 17 00:00:00 2001 From: Moudy Date: Mon, 11 Aug 2025 12:33:52 +0200 Subject: [PATCH 15/22] change: mv encryption-demo chacha20-demo --- chacha20-demo/.gitignore | 4 + chacha20-demo/Cargo.toml | 24 ++ chacha20-demo/LICENSE | 201 ++++++++++++++ chacha20-demo/README.md | 111 ++++++++ chacha20-demo/build.log | 256 ++++++++++++++++++ chacha20-demo/build.rs | 4 + chacha20-demo/methods/Cargo.toml | 10 + chacha20-demo/methods/build.rs | 3 + .../methods/guest/Cargo.toml | 0 .../methods/guest/src/lib.rs | 0 .../methods/guest/src/main.rs | 0 chacha20-demo/methods/src/lib.rs | 1 + chacha20-demo/rust-toolchain.toml | 4 + chacha20-demo/src/lib.rs | 37 +++ chacha20-demo/src/main.rs | 44 +++ chacha20-demo/tests/bad_guest.rs | 30 ++ chacha20-demo/tests/proof_works.rs | 49 ++++ chacha20-demo/tests/wrong_image_id.rs | 22 ++ encryption-demo/methods/chacha20/Cargo.toml | 12 + encryption-demo/methods/chacha20/src/lib.rs | 22 ++ encryption-demo/methods/chacha20/src/main.rs | 26 ++ 21 files changed, 860 insertions(+) create mode 100644 chacha20-demo/.gitignore create mode 100644 chacha20-demo/Cargo.toml create mode 100644 chacha20-demo/LICENSE create mode 100644 chacha20-demo/README.md create mode 100644 chacha20-demo/build.log create mode 100644 chacha20-demo/build.rs create mode 100644 chacha20-demo/methods/Cargo.toml create mode 100644 chacha20-demo/methods/build.rs rename {encryption-demo => chacha20-demo}/methods/guest/Cargo.toml (100%) rename {encryption-demo => chacha20-demo}/methods/guest/src/lib.rs (100%) rename {encryption-demo => chacha20-demo}/methods/guest/src/main.rs (100%) create mode 100644 chacha20-demo/methods/src/lib.rs create mode 100644 chacha20-demo/rust-toolchain.toml create mode 100644 chacha20-demo/src/lib.rs create mode 100644 chacha20-demo/src/main.rs create mode 100644 chacha20-demo/tests/bad_guest.rs create mode 100644 chacha20-demo/tests/proof_works.rs create mode 100644 chacha20-demo/tests/wrong_image_id.rs create mode 100644 encryption-demo/methods/chacha20/Cargo.toml create mode 100644 encryption-demo/methods/chacha20/src/lib.rs create mode 100644 encryption-demo/methods/chacha20/src/main.rs diff --git a/chacha20-demo/.gitignore b/chacha20-demo/.gitignore new file mode 100644 index 0000000..f4247e1 --- /dev/null +++ b/chacha20-demo/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +Cargo.lock +methods/guest/Cargo.lock +target/ diff --git a/chacha20-demo/Cargo.toml b/chacha20-demo/Cargo.toml new file mode 100644 index 0000000..ccb9bb1 --- /dev/null +++ b/chacha20-demo/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "encryption-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" + +[dev-dependencies] +chacha20 = "0.9" +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/chacha20-demo/LICENSE b/chacha20-demo/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/chacha20-demo/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/chacha20-demo/README.md b/chacha20-demo/README.md new file mode 100644 index 0000000..df70292 --- /dev/null +++ b/chacha20-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/chacha20-demo/build.log b/chacha20-demo/build.log new file mode 100644 index 0000000..54c533e --- /dev/null +++ b/chacha20-demo/build.log @@ -0,0 +1,256 @@ + Compiling proc-macro2 v1.0.95 + Compiling unicode-ident v1.0.18 + Compiling libc v0.2.174 + Compiling version_check v0.9.5 + Compiling serde v1.0.219 + Compiling typenum v1.18.0 + Compiling zerocopy v0.8.26 + Compiling cfg-if v1.0.1 + Compiling once_cell v1.21.3 + Compiling paste v1.0.15 + Compiling subtle v2.6.1 + Compiling pin-project-lite v0.2.16 + Compiling autocfg v1.5.0 + Compiling rand_core v0.6.4 + Compiling thiserror v2.0.12 + Compiling generic-array v0.14.7 + Compiling ahash v0.8.12 + Compiling hashbrown v0.15.4 + Compiling equivalent v1.0.2 + Compiling const-oid v0.9.6 + Compiling winnow v0.7.12 + Compiling toml_write v0.1.2 + Compiling anyhow v1.0.98 + Compiling indexmap v2.10.0 + Compiling tracing-core v0.1.34 + Compiling log v0.4.27 + Compiling cfg_aliases v0.2.1 + Compiling semver v1.0.26 + Compiling borsh v1.5.7 + Compiling num-traits v0.2.19 + Compiling stable_deref_trait v1.2.0 + Compiling memchr v2.7.5 + Compiling quote v1.0.40 + Compiling bitflags v2.9.1 + Compiling syn v2.0.104 + Compiling itoa v1.0.15 + Compiling cpufeatures v0.2.17 + Compiling getrandom v0.3.3 + Compiling allocator-api2 v0.2.21 + Compiling futures-core v0.3.31 + Compiling unicode-xid v0.2.6 + Compiling rustix v1.0.8 + Compiling serde_json v1.0.142 + Compiling shlex v1.3.0 + Compiling ident_case v1.0.1 + Compiling rustversion v1.0.21 + Compiling fnv v1.0.7 + Compiling strsim v0.11.1 + Compiling cc v1.2.31 + Compiling mio v1.0.4 + Compiling socket2 v0.6.0 + Compiling either v1.15.0 + Compiling core-foundation-sys v0.8.7 + Compiling num-integer v0.1.46 + Compiling camino v1.1.10 + Compiling futures-sink v0.3.31 + Compiling crypto-common v0.1.6 + Compiling block-buffer v0.10.4 + Compiling arrayvec v0.7.6 + Compiling core-foundation v0.9.4 + Compiling digest v0.10.7 + Compiling itertools v0.13.0 + Compiling ppv-lite86 v0.2.21 + Compiling num-bigint v0.4.6 + Compiling sha2 v0.10.9 + Compiling blake2 v0.10.6 + Compiling malloc_buf v0.0.6 + Compiling rand_chacha v0.3.1 + Compiling heck v0.5.0 + Compiling bitflags v1.3.2 + Compiling smallvec v1.15.1 + Compiling rand v0.8.5 + Compiling ring v0.17.14 + Compiling syn v1.0.109 + Compiling litemap v0.8.0 + Compiling hex v0.4.3 + Compiling writeable v0.6.1 + Compiling foreign-types-shared v0.3.1 + Compiling core-graphics-types v0.1.3 + Compiling ark-std v0.5.0 + Compiling objc v0.2.7 + Compiling risc0-zkp v2.0.2 + Compiling block v0.1.6 + Compiling icu_normalizer_data v2.0.0 + Compiling futures-io v0.3.31 + Compiling icu_properties_data v2.0.1 + Compiling futures-task v0.3.31 + Compiling pin-utils v0.1.0 + Compiling synstructure v0.13.2 + Compiling darling_core v0.20.11 + Compiling slab v0.4.10 + Compiling futures-util v0.3.31 + Compiling getrandom v0.2.16 + Compiling percent-encoding v2.3.1 + Compiling hex-literal v0.4.1 + Compiling untrusted v0.9.0 + Compiling httparse v1.10.1 + Compiling rustls v0.23.31 + Compiling try-lock v0.2.5 + Compiling spin v0.9.8 + Compiling ryu v1.0.20 + Compiling tower-service v0.3.3 + Compiling lazy_static v1.5.0 + Compiling want v0.3.1 + Compiling serde_derive v1.0.219 + Compiling tracing-attributes v0.1.30 + Compiling zeroize_derive v1.4.2 + Compiling thiserror-impl v2.0.12 + Compiling bytemuck_derive v1.8.1 + Compiling foreign-types-macros v0.2.3 + Compiling zerofrom-derive v0.1.6 + Compiling zeroize v1.8.1 + Compiling yoke-derive v0.8.0 + Compiling stability v0.2.1 + Compiling zerovec-derive v0.11.1 + Compiling displaydoc v0.2.5 + Compiling enum-ordinalize-derive v4.3.1 + Compiling tracing v0.1.41 + Compiling derive_more-impl v2.0.1 + Compiling ark-serialize-derive v0.5.0 + Compiling darling_macro v0.20.11 + Compiling ark-ff-macros v0.5.0 + Compiling enum-ordinalize v4.3.0 + Compiling ark-ff-asm v0.5.0 + Compiling darling v0.20.11 + Compiling zerofrom v0.1.6 + Compiling ark-serialize v0.5.0 + Compiling bytemuck v1.23.1 + Compiling foreign-types v0.5.0 + Compiling strum_macros v0.26.4 + Compiling educe v0.6.0 + Compiling risc0-zkvm-platform v2.0.3 + Compiling rustls-pki-types v1.12.0 + Compiling derive_builder_core v0.20.2 + Compiling cobs v0.3.0 + Compiling yoke v0.8.0 + Compiling derive_builder_macro v0.20.2 + Compiling derive_more v2.0.1 + Compiling risc0-core v2.0.0 + Compiling metal v0.29.0 + Compiling futures-channel v0.3.31 + Compiling tracing-subscriber v0.2.25 + Compiling proc-macro-error-attr v1.0.4 + Compiling ark-ff v0.5.0 + Compiling zerovec v0.11.4 + Compiling zerotrie v0.2.2 + Compiling rustls-webpki v0.103.4 + Compiling elf v0.7.4 + Compiling hashbrown v0.14.5 + Compiling form_urlencoded v1.2.1 + Compiling aho-corasick v1.1.3 + Compiling toml_datetime v0.6.11 + Compiling serde_spanned v0.6.9 + Compiling toml_edit v0.22.27 + Compiling bytes v1.10.1 + Compiling tinystr v0.8.1 + Compiling potential_utf v0.1.2 + Compiling postcard v1.1.3 + Compiling icu_locale_core v2.0.0 + Compiling icu_collections v2.0.0 + Compiling tokio v1.47.1 + Compiling http v1.3.1 + Compiling icu_provider v2.0.0 + Compiling sync_wrapper v1.0.2 + Compiling keccak v0.1.5 + Compiling errno v0.3.13 + Compiling icu_properties v2.0.1 + Compiling icu_normalizer v2.0.0 + Compiling proc-macro-error v1.0.4 + Compiling http-body v1.0.1 + Compiling proc-macro-crate v3.3.0 + Compiling regex-syntax v0.8.5 + Compiling base64 v0.22.1 + Compiling idna_adapter v1.2.1 + Compiling tower-layer v0.3.3 + Compiling ipnet v2.11.0 + Compiling borsh-derive v1.5.7 + Compiling utf8_iter v1.0.4 + Compiling byteorder v1.5.0 + Compiling merlin v3.0.0 + Compiling idna v1.0.3 + Compiling regex-automata v0.4.9 + Compiling ark-poly v0.5.0 + Compiling ark-relations v0.5.1 + Compiling hyper v1.6.0 + Compiling tower v0.5.2 + Compiling ark-snark v0.5.1 + Compiling tokio-rustls v0.26.2 + Compiling hyper-util v0.1.16 + Compiling hashlink v0.9.1 + Compiling ark-ec v0.5.0 + Compiling risc0-binfmt v2.0.2 + Compiling webpki-roots v1.0.2 + Compiling derivative v2.2.0 + Compiling ark-crypto-primitives-macros v0.5.0 + Compiling risc0-circuit-recursion v3.0.0 + Compiling encoding_rs v0.8.35 + Compiling option-ext v0.2.0 + Compiling arraydeque v0.5.1 + Compiling iri-string v0.7.8 + Compiling thiserror v1.0.69 + Compiling fastrand v2.3.0 + Compiling itertools v0.14.0 + Compiling tempfile v3.20.0 + Compiling dirs-sys v0.4.1 + Compiling yaml-rust2 v0.9.0 + Compiling ark-crypto-primitives v0.5.0 + Compiling tower-http v0.6.6 + Compiling hyper-rustls v0.27.7 + Compiling toml v0.8.23 + Compiling regex v1.11.1 + Compiling tokio-util v0.7.16 + Compiling url v2.5.4 + Compiling http-body-util v0.1.3 + Compiling serde_urlencoded v0.7.1 + Compiling cargo-platform v0.1.9 + Compiling strum v0.26.3 + Compiling thiserror-impl v1.0.69 + Compiling include_bytes_aligned v0.1.4 + Compiling heck v0.4.1 + Compiling no_std_strings v0.1.3 + Compiling duplicate v1.0.0 + Compiling risc0-zkos-v1compat v2.0.1 + Compiling rzup v0.4.1 + Compiling cargo_metadata v0.19.2 + Compiling reqwest v0.12.22 + Compiling lazy-regex-proc_macros v3.4.1 + Compiling ark-groth16 v0.5.0 + Compiling dirs v5.0.1 + Compiling prost-derive v0.13.5 + Compiling ark-bn254 v0.5.0 + Compiling derive_builder v0.20.2 + Compiling maybe-async v0.2.10 + Compiling docker-generate v0.1.3 + Compiling bit-vec v0.8.0 + Compiling downcast-rs v1.2.1 + Compiling rrs-lib v0.1.0 + Compiling risc0-circuit-rv32im v3.0.0 + Compiling risc0-build v2.3.1 + Compiling bonsai-sdk v1.4.0 + Compiling risc0-groth16 v2.0.2 + Compiling prost v0.13.5 + Compiling risc0-circuit-keccak v3.0.0 + Compiling lazy-regex v3.4.1 + Compiling bincode v1.3.3 + Compiling inout v0.1.4 + Compiling host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host) + Compiling cipher v0.4.4 + Compiling chacha20 v0.9.1 +error: failed to run custom build command for `host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host)` + +Caused by: + process didn't exit successfully: `/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/target/release/build/host-26fa01eaea54642e/build-script-build` (exit status: 255) + --- stderr + ERROR: No package found in "/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host/../guest" +warning: build failed, waiting for other jobs to finish... diff --git a/chacha20-demo/build.rs b/chacha20-demo/build.rs new file mode 100644 index 0000000..aa600df --- /dev/null +++ b/chacha20-demo/build.rs @@ -0,0 +1,4 @@ +fn main() { + risc0_build::embed_methods(); +} + diff --git a/chacha20-demo/methods/Cargo.toml b/chacha20-demo/methods/Cargo.toml new file mode 100644 index 0000000..9ab59da --- /dev/null +++ b/chacha20-demo/methods/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "methods" +version = "0.1.0" +edition = "2021" + +[build-dependencies] +risc0-build = { version = "2.3.1" } + +[package.metadata.risc0] +methods = ["methods/guest"] diff --git a/chacha20-demo/methods/build.rs b/chacha20-demo/methods/build.rs new file mode 100644 index 0000000..08a8a4e --- /dev/null +++ b/chacha20-demo/methods/build.rs @@ -0,0 +1,3 @@ +fn main() { + risc0_build::embed_methods(); +} diff --git a/encryption-demo/methods/guest/Cargo.toml b/chacha20-demo/methods/guest/Cargo.toml similarity index 100% rename from encryption-demo/methods/guest/Cargo.toml rename to chacha20-demo/methods/guest/Cargo.toml diff --git a/encryption-demo/methods/guest/src/lib.rs b/chacha20-demo/methods/guest/src/lib.rs similarity index 100% rename from encryption-demo/methods/guest/src/lib.rs rename to chacha20-demo/methods/guest/src/lib.rs diff --git a/encryption-demo/methods/guest/src/main.rs b/chacha20-demo/methods/guest/src/main.rs similarity index 100% rename from encryption-demo/methods/guest/src/main.rs rename to chacha20-demo/methods/guest/src/main.rs diff --git a/chacha20-demo/methods/src/lib.rs b/chacha20-demo/methods/src/lib.rs new file mode 100644 index 0000000..1bdb308 --- /dev/null +++ b/chacha20-demo/methods/src/lib.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/methods.rs")); diff --git a/chacha20-demo/rust-toolchain.toml b/chacha20-demo/rust-toolchain.toml new file mode 100644 index 0000000..36614c3 --- /dev/null +++ b/chacha20-demo/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "stable" +components = ["rustfmt", "rust-src"] +profile = "minimal" diff --git a/chacha20-demo/src/lib.rs b/chacha20-demo/src/lib.rs new file mode 100644 index 0000000..d2461e1 --- /dev/null +++ b/chacha20-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/chacha20-demo/src/main.rs b/chacha20-demo/src/main.rs new file mode 100644 index 0000000..61db16b --- /dev/null +++ b/chacha20-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(()) +} + + diff --git a/chacha20-demo/tests/bad_guest.rs b/chacha20-demo/tests/bad_guest.rs new file mode 100644 index 0000000..264c226 --- /dev/null +++ b/chacha20-demo/tests/bad_guest.rs @@ -0,0 +1,30 @@ +#[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 risc0_zkvm::{default_prover, ExecutorEnv}; + +#[test] +fn guest_panics_on_bad_key() { + // This key triggers the panic in the guest + let key = [0xFFu8; 32]; + let nonce = [0u8; 12]; + let plaintext = b"panic please".to_vec(); + + let env = ExecutorEnv::builder() + .write(&key).unwrap() + .write(&nonce).unwrap() + .write(&plaintext).unwrap() + .build().unwrap(); + + // Proving should fail when the guest panics + let res = default_prover().prove(env, GUEST_ELF); + assert!(res.is_err(), "proving should fail when guest panics"); +} diff --git a/chacha20-demo/tests/proof_works.rs b/chacha20-demo/tests/proof_works.rs new file mode 100644 index 0000000..74722c6 --- /dev/null +++ b/chacha20-demo/tests/proof_works.rs @@ -0,0 +1,49 @@ +#[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 risc0_zkvm::{default_prover, ExecutorEnv}; + +// Host-side ChaCha20 +use chacha20::{ChaCha20, Key, Nonce}; +use cipher::{KeyIvInit, StreamCipher}; + +#[test] +fn proof_works_and_matches_host_chacha() { + // Inputs (must match what your guest expects) + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; + + // Prove with the R0 guest + let env = ExecutorEnv::builder() + .write(&key).unwrap() + .write(&nonce).unwrap() + .write(&plaintext.to_vec()).unwrap() + .build().unwrap(); + + let prove_info = default_prover().prove(env, GUEST_ELF).expect("prove failed"); + prove_info.receipt.verify(GUEST_ID).expect("verify failed"); + + + // Ciphertext produced by the guest + let guest_ct = prove_info.receipt.journal.bytes.clone(); + + // Host-side reference: ChaCha20 with the same key/nonce + let mut host_ct = plaintext.to_vec(); + let key = Key::from_slice(&key); + let nonce = Nonce::from_slice(&nonce); + let mut cipher = ChaCha20::new(key, nonce); + cipher.apply_keystream(&mut host_ct); + + // Compare + assert_eq!(guest_ct, host_ct, "guest ciphertext != host ChaCha20 ciphertext"); + +} diff --git a/chacha20-demo/tests/wrong_image_id.rs b/chacha20-demo/tests/wrong_image_id.rs new file mode 100644 index 0000000..8097c62 --- /dev/null +++ b/chacha20-demo/tests/wrong_image_id.rs @@ -0,0 +1,22 @@ +use anyhow::Result; +use risc0_zkvm::{default_prover, ExecutorEnv, Digest}; + +#[cfg(not(rust_analyzer))] +include!(concat!(env!("OUT_DIR"), "/methods.rs")); + +#[test] +fn verify_rejects_wrong_image() -> Result<()> { + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"bad id test".to_vec(); + + let env = ExecutorEnv::builder() + .write(&key)?.write(&nonce)?.write(&plaintext)?.build()?; + + let info = default_prover().prove(env, GUEST_ELF)?; + + // Intentionally bogus image id + let bogus = Digest::from([0u32; 8]); + assert!(info.receipt.verify(bogus).is_err(), "verification should fail"); + Ok(()) +} diff --git a/encryption-demo/methods/chacha20/Cargo.toml b/encryption-demo/methods/chacha20/Cargo.toml new file mode 100644 index 0000000..ed8fc71 --- /dev/null +++ b/encryption-demo/methods/chacha20/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "guest" +version = "0.1.0" +edition = "2021" + +[workspace] + +[dependencies] +risc0-zkvm = { version = "2.3.1", default-features = false} +chacha20 = { version = "0.9", default-features = false } +cipher = { version = "0.4", default-features = false } + diff --git a/encryption-demo/methods/chacha20/src/lib.rs b/encryption-demo/methods/chacha20/src/lib.rs new file mode 100644 index 0000000..cf0f528 --- /dev/null +++ b/encryption-demo/methods/chacha20/src/lib.rs @@ -0,0 +1,22 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} diff --git a/encryption-demo/methods/chacha20/src/main.rs b/encryption-demo/methods/chacha20/src/main.rs new file mode 100644 index 0000000..ea76354 --- /dev/null +++ b/encryption-demo/methods/chacha20/src/main.rs @@ -0,0 +1,26 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::guest::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + // Bad-guest behavior: reject keys starting with 0xFF + if key[0] == 0xFF { + panic!("bad key: starts with 0xFF"); + } + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} From 15af986ef3301b88a3cf2f1dd0941219cf15bf3a Mon Sep 17 00:00:00 2001 From: Moudy Date: Mon, 11 Aug 2025 12:36:26 +0200 Subject: [PATCH 16/22] removed modified files --- chacha20-demo/.gitignore | 4 - chacha20-demo/Cargo.toml | 24 --- chacha20-demo/LICENSE | 201 ------------------- chacha20-demo/README.md | 111 ---------- chacha20-demo/build.log | 256 ------------------------ chacha20-demo/build.rs | 4 - chacha20-demo/methods/Cargo.toml | 10 - chacha20-demo/methods/build.rs | 3 - chacha20-demo/methods/guest/Cargo.toml | 12 -- chacha20-demo/methods/guest/src/lib.rs | 22 -- chacha20-demo/methods/guest/src/main.rs | 26 --- chacha20-demo/methods/src/lib.rs | 1 - chacha20-demo/rust-toolchain.toml | 4 - chacha20-demo/src/lib.rs | 37 ---- chacha20-demo/src/main.rs | 44 ---- chacha20-demo/tests/bad_guest.rs | 30 --- chacha20-demo/tests/proof_works.rs | 49 ----- chacha20-demo/tests/wrong_image_id.rs | 22 -- 18 files changed, 860 deletions(-) delete mode 100644 chacha20-demo/.gitignore delete mode 100644 chacha20-demo/Cargo.toml delete mode 100644 chacha20-demo/LICENSE delete mode 100644 chacha20-demo/README.md delete mode 100644 chacha20-demo/build.log delete mode 100644 chacha20-demo/build.rs delete mode 100644 chacha20-demo/methods/Cargo.toml delete mode 100644 chacha20-demo/methods/build.rs delete mode 100644 chacha20-demo/methods/guest/Cargo.toml delete mode 100644 chacha20-demo/methods/guest/src/lib.rs delete mode 100644 chacha20-demo/methods/guest/src/main.rs delete mode 100644 chacha20-demo/methods/src/lib.rs delete mode 100644 chacha20-demo/rust-toolchain.toml delete mode 100644 chacha20-demo/src/lib.rs delete mode 100644 chacha20-demo/src/main.rs delete mode 100644 chacha20-demo/tests/bad_guest.rs delete mode 100644 chacha20-demo/tests/proof_works.rs delete mode 100644 chacha20-demo/tests/wrong_image_id.rs diff --git a/chacha20-demo/.gitignore b/chacha20-demo/.gitignore deleted file mode 100644 index f4247e1..0000000 --- a/chacha20-demo/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -.DS_Store -Cargo.lock -methods/guest/Cargo.lock -target/ diff --git a/chacha20-demo/Cargo.toml b/chacha20-demo/Cargo.toml deleted file mode 100644 index ccb9bb1..0000000 --- a/chacha20-demo/Cargo.toml +++ /dev/null @@ -1,24 +0,0 @@ -[package] -name = "encryption-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" - -[dev-dependencies] -chacha20 = "0.9" -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/chacha20-demo/LICENSE b/chacha20-demo/LICENSE deleted file mode 100644 index 261eeb9..0000000 --- a/chacha20-demo/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/chacha20-demo/README.md b/chacha20-demo/README.md deleted file mode 100644 index df70292..0000000 --- a/chacha20-demo/README.md +++ /dev/null @@ -1,111 +0,0 @@ -# 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/chacha20-demo/build.log b/chacha20-demo/build.log deleted file mode 100644 index 54c533e..0000000 --- a/chacha20-demo/build.log +++ /dev/null @@ -1,256 +0,0 @@ - Compiling proc-macro2 v1.0.95 - Compiling unicode-ident v1.0.18 - Compiling libc v0.2.174 - Compiling version_check v0.9.5 - Compiling serde v1.0.219 - Compiling typenum v1.18.0 - Compiling zerocopy v0.8.26 - Compiling cfg-if v1.0.1 - Compiling once_cell v1.21.3 - Compiling paste v1.0.15 - Compiling subtle v2.6.1 - Compiling pin-project-lite v0.2.16 - Compiling autocfg v1.5.0 - Compiling rand_core v0.6.4 - Compiling thiserror v2.0.12 - Compiling generic-array v0.14.7 - Compiling ahash v0.8.12 - Compiling hashbrown v0.15.4 - Compiling equivalent v1.0.2 - Compiling const-oid v0.9.6 - Compiling winnow v0.7.12 - Compiling toml_write v0.1.2 - Compiling anyhow v1.0.98 - Compiling indexmap v2.10.0 - Compiling tracing-core v0.1.34 - Compiling log v0.4.27 - Compiling cfg_aliases v0.2.1 - Compiling semver v1.0.26 - Compiling borsh v1.5.7 - Compiling num-traits v0.2.19 - Compiling stable_deref_trait v1.2.0 - Compiling memchr v2.7.5 - Compiling quote v1.0.40 - Compiling bitflags v2.9.1 - Compiling syn v2.0.104 - Compiling itoa v1.0.15 - Compiling cpufeatures v0.2.17 - Compiling getrandom v0.3.3 - Compiling allocator-api2 v0.2.21 - Compiling futures-core v0.3.31 - Compiling unicode-xid v0.2.6 - Compiling rustix v1.0.8 - Compiling serde_json v1.0.142 - Compiling shlex v1.3.0 - Compiling ident_case v1.0.1 - Compiling rustversion v1.0.21 - Compiling fnv v1.0.7 - Compiling strsim v0.11.1 - Compiling cc v1.2.31 - Compiling mio v1.0.4 - Compiling socket2 v0.6.0 - Compiling either v1.15.0 - Compiling core-foundation-sys v0.8.7 - Compiling num-integer v0.1.46 - Compiling camino v1.1.10 - Compiling futures-sink v0.3.31 - Compiling crypto-common v0.1.6 - Compiling block-buffer v0.10.4 - Compiling arrayvec v0.7.6 - Compiling core-foundation v0.9.4 - Compiling digest v0.10.7 - Compiling itertools v0.13.0 - Compiling ppv-lite86 v0.2.21 - Compiling num-bigint v0.4.6 - Compiling sha2 v0.10.9 - Compiling blake2 v0.10.6 - Compiling malloc_buf v0.0.6 - Compiling rand_chacha v0.3.1 - Compiling heck v0.5.0 - Compiling bitflags v1.3.2 - Compiling smallvec v1.15.1 - Compiling rand v0.8.5 - Compiling ring v0.17.14 - Compiling syn v1.0.109 - Compiling litemap v0.8.0 - Compiling hex v0.4.3 - Compiling writeable v0.6.1 - Compiling foreign-types-shared v0.3.1 - Compiling core-graphics-types v0.1.3 - Compiling ark-std v0.5.0 - Compiling objc v0.2.7 - Compiling risc0-zkp v2.0.2 - Compiling block v0.1.6 - Compiling icu_normalizer_data v2.0.0 - Compiling futures-io v0.3.31 - Compiling icu_properties_data v2.0.1 - Compiling futures-task v0.3.31 - Compiling pin-utils v0.1.0 - Compiling synstructure v0.13.2 - Compiling darling_core v0.20.11 - Compiling slab v0.4.10 - Compiling futures-util v0.3.31 - Compiling getrandom v0.2.16 - Compiling percent-encoding v2.3.1 - Compiling hex-literal v0.4.1 - Compiling untrusted v0.9.0 - Compiling httparse v1.10.1 - Compiling rustls v0.23.31 - Compiling try-lock v0.2.5 - Compiling spin v0.9.8 - Compiling ryu v1.0.20 - Compiling tower-service v0.3.3 - Compiling lazy_static v1.5.0 - Compiling want v0.3.1 - Compiling serde_derive v1.0.219 - Compiling tracing-attributes v0.1.30 - Compiling zeroize_derive v1.4.2 - Compiling thiserror-impl v2.0.12 - Compiling bytemuck_derive v1.8.1 - Compiling foreign-types-macros v0.2.3 - Compiling zerofrom-derive v0.1.6 - Compiling zeroize v1.8.1 - Compiling yoke-derive v0.8.0 - Compiling stability v0.2.1 - Compiling zerovec-derive v0.11.1 - Compiling displaydoc v0.2.5 - Compiling enum-ordinalize-derive v4.3.1 - Compiling tracing v0.1.41 - Compiling derive_more-impl v2.0.1 - Compiling ark-serialize-derive v0.5.0 - Compiling darling_macro v0.20.11 - Compiling ark-ff-macros v0.5.0 - Compiling enum-ordinalize v4.3.0 - Compiling ark-ff-asm v0.5.0 - Compiling darling v0.20.11 - Compiling zerofrom v0.1.6 - Compiling ark-serialize v0.5.0 - Compiling bytemuck v1.23.1 - Compiling foreign-types v0.5.0 - Compiling strum_macros v0.26.4 - Compiling educe v0.6.0 - Compiling risc0-zkvm-platform v2.0.3 - Compiling rustls-pki-types v1.12.0 - Compiling derive_builder_core v0.20.2 - Compiling cobs v0.3.0 - Compiling yoke v0.8.0 - Compiling derive_builder_macro v0.20.2 - Compiling derive_more v2.0.1 - Compiling risc0-core v2.0.0 - Compiling metal v0.29.0 - Compiling futures-channel v0.3.31 - Compiling tracing-subscriber v0.2.25 - Compiling proc-macro-error-attr v1.0.4 - Compiling ark-ff v0.5.0 - Compiling zerovec v0.11.4 - Compiling zerotrie v0.2.2 - Compiling rustls-webpki v0.103.4 - Compiling elf v0.7.4 - Compiling hashbrown v0.14.5 - Compiling form_urlencoded v1.2.1 - Compiling aho-corasick v1.1.3 - Compiling toml_datetime v0.6.11 - Compiling serde_spanned v0.6.9 - Compiling toml_edit v0.22.27 - Compiling bytes v1.10.1 - Compiling tinystr v0.8.1 - Compiling potential_utf v0.1.2 - Compiling postcard v1.1.3 - Compiling icu_locale_core v2.0.0 - Compiling icu_collections v2.0.0 - Compiling tokio v1.47.1 - Compiling http v1.3.1 - Compiling icu_provider v2.0.0 - Compiling sync_wrapper v1.0.2 - Compiling keccak v0.1.5 - Compiling errno v0.3.13 - Compiling icu_properties v2.0.1 - Compiling icu_normalizer v2.0.0 - Compiling proc-macro-error v1.0.4 - Compiling http-body v1.0.1 - Compiling proc-macro-crate v3.3.0 - Compiling regex-syntax v0.8.5 - Compiling base64 v0.22.1 - Compiling idna_adapter v1.2.1 - Compiling tower-layer v0.3.3 - Compiling ipnet v2.11.0 - Compiling borsh-derive v1.5.7 - Compiling utf8_iter v1.0.4 - Compiling byteorder v1.5.0 - Compiling merlin v3.0.0 - Compiling idna v1.0.3 - Compiling regex-automata v0.4.9 - Compiling ark-poly v0.5.0 - Compiling ark-relations v0.5.1 - Compiling hyper v1.6.0 - Compiling tower v0.5.2 - Compiling ark-snark v0.5.1 - Compiling tokio-rustls v0.26.2 - Compiling hyper-util v0.1.16 - Compiling hashlink v0.9.1 - Compiling ark-ec v0.5.0 - Compiling risc0-binfmt v2.0.2 - Compiling webpki-roots v1.0.2 - Compiling derivative v2.2.0 - Compiling ark-crypto-primitives-macros v0.5.0 - Compiling risc0-circuit-recursion v3.0.0 - Compiling encoding_rs v0.8.35 - Compiling option-ext v0.2.0 - Compiling arraydeque v0.5.1 - Compiling iri-string v0.7.8 - Compiling thiserror v1.0.69 - Compiling fastrand v2.3.0 - Compiling itertools v0.14.0 - Compiling tempfile v3.20.0 - Compiling dirs-sys v0.4.1 - Compiling yaml-rust2 v0.9.0 - Compiling ark-crypto-primitives v0.5.0 - Compiling tower-http v0.6.6 - Compiling hyper-rustls v0.27.7 - Compiling toml v0.8.23 - Compiling regex v1.11.1 - Compiling tokio-util v0.7.16 - Compiling url v2.5.4 - Compiling http-body-util v0.1.3 - Compiling serde_urlencoded v0.7.1 - Compiling cargo-platform v0.1.9 - Compiling strum v0.26.3 - Compiling thiserror-impl v1.0.69 - Compiling include_bytes_aligned v0.1.4 - Compiling heck v0.4.1 - Compiling no_std_strings v0.1.3 - Compiling duplicate v1.0.0 - Compiling risc0-zkos-v1compat v2.0.1 - Compiling rzup v0.4.1 - Compiling cargo_metadata v0.19.2 - Compiling reqwest v0.12.22 - Compiling lazy-regex-proc_macros v3.4.1 - Compiling ark-groth16 v0.5.0 - Compiling dirs v5.0.1 - Compiling prost-derive v0.13.5 - Compiling ark-bn254 v0.5.0 - Compiling derive_builder v0.20.2 - Compiling maybe-async v0.2.10 - Compiling docker-generate v0.1.3 - Compiling bit-vec v0.8.0 - Compiling downcast-rs v1.2.1 - Compiling rrs-lib v0.1.0 - Compiling risc0-circuit-rv32im v3.0.0 - Compiling risc0-build v2.3.1 - Compiling bonsai-sdk v1.4.0 - Compiling risc0-groth16 v2.0.2 - Compiling prost v0.13.5 - Compiling risc0-circuit-keccak v3.0.0 - Compiling lazy-regex v3.4.1 - Compiling bincode v1.3.3 - Compiling inout v0.1.4 - Compiling host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host) - Compiling cipher v0.4.4 - Compiling chacha20 v0.9.1 -error: failed to run custom build command for `host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host)` - -Caused by: - process didn't exit successfully: `/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/target/release/build/host-26fa01eaea54642e/build-script-build` (exit status: 255) - --- stderr - ERROR: No package found in "/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host/../guest" -warning: build failed, waiting for other jobs to finish... diff --git a/chacha20-demo/build.rs b/chacha20-demo/build.rs deleted file mode 100644 index aa600df..0000000 --- a/chacha20-demo/build.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - risc0_build::embed_methods(); -} - diff --git a/chacha20-demo/methods/Cargo.toml b/chacha20-demo/methods/Cargo.toml deleted file mode 100644 index 9ab59da..0000000 --- a/chacha20-demo/methods/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "methods" -version = "0.1.0" -edition = "2021" - -[build-dependencies] -risc0-build = { version = "2.3.1" } - -[package.metadata.risc0] -methods = ["methods/guest"] diff --git a/chacha20-demo/methods/build.rs b/chacha20-demo/methods/build.rs deleted file mode 100644 index 08a8a4e..0000000 --- a/chacha20-demo/methods/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - risc0_build::embed_methods(); -} diff --git a/chacha20-demo/methods/guest/Cargo.toml b/chacha20-demo/methods/guest/Cargo.toml deleted file mode 100644 index ed8fc71..0000000 --- a/chacha20-demo/methods/guest/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "guest" -version = "0.1.0" -edition = "2021" - -[workspace] - -[dependencies] -risc0-zkvm = { version = "2.3.1", default-features = false} -chacha20 = { version = "0.9", default-features = false } -cipher = { version = "0.4", default-features = false } - diff --git a/chacha20-demo/methods/guest/src/lib.rs b/chacha20-demo/methods/guest/src/lib.rs deleted file mode 100644 index cf0f528..0000000 --- a/chacha20-demo/methods/guest/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/chacha20-demo/methods/guest/src/main.rs b/chacha20-demo/methods/guest/src/main.rs deleted file mode 100644 index ea76354..0000000 --- a/chacha20-demo/methods/guest/src/main.rs +++ /dev/null @@ -1,26 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::guest::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - // Bad-guest behavior: reject keys starting with 0xFF - if key[0] == 0xFF { - panic!("bad key: starts with 0xFF"); - } - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/chacha20-demo/methods/src/lib.rs b/chacha20-demo/methods/src/lib.rs deleted file mode 100644 index 1bdb308..0000000 --- a/chacha20-demo/methods/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -include!(concat!(env!("OUT_DIR"), "/methods.rs")); diff --git a/chacha20-demo/rust-toolchain.toml b/chacha20-demo/rust-toolchain.toml deleted file mode 100644 index 36614c3..0000000 --- a/chacha20-demo/rust-toolchain.toml +++ /dev/null @@ -1,4 +0,0 @@ -[toolchain] -channel = "stable" -components = ["rustfmt", "rust-src"] -profile = "minimal" diff --git a/chacha20-demo/src/lib.rs b/chacha20-demo/src/lib.rs deleted file mode 100644 index d2461e1..0000000 --- a/chacha20-demo/src/lib.rs +++ /dev/null @@ -1,37 +0,0 @@ -#[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/chacha20-demo/src/main.rs b/chacha20-demo/src/main.rs deleted file mode 100644 index 61db16b..0000000 --- a/chacha20-demo/src/main.rs +++ /dev/null @@ -1,44 +0,0 @@ -#[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(()) -} - - diff --git a/chacha20-demo/tests/bad_guest.rs b/chacha20-demo/tests/bad_guest.rs deleted file mode 100644 index 264c226..0000000 --- a/chacha20-demo/tests/bad_guest.rs +++ /dev/null @@ -1,30 +0,0 @@ -#[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 risc0_zkvm::{default_prover, ExecutorEnv}; - -#[test] -fn guest_panics_on_bad_key() { - // This key triggers the panic in the guest - let key = [0xFFu8; 32]; - let nonce = [0u8; 12]; - let plaintext = b"panic please".to_vec(); - - let env = ExecutorEnv::builder() - .write(&key).unwrap() - .write(&nonce).unwrap() - .write(&plaintext).unwrap() - .build().unwrap(); - - // Proving should fail when the guest panics - let res = default_prover().prove(env, GUEST_ELF); - assert!(res.is_err(), "proving should fail when guest panics"); -} diff --git a/chacha20-demo/tests/proof_works.rs b/chacha20-demo/tests/proof_works.rs deleted file mode 100644 index 74722c6..0000000 --- a/chacha20-demo/tests/proof_works.rs +++ /dev/null @@ -1,49 +0,0 @@ -#[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 risc0_zkvm::{default_prover, ExecutorEnv}; - -// Host-side ChaCha20 -use chacha20::{ChaCha20, Key, Nonce}; -use cipher::{KeyIvInit, StreamCipher}; - -#[test] -fn proof_works_and_matches_host_chacha() { - // Inputs (must match what your guest expects) - let key = [0x42u8; 32]; - let nonce = [0x24u8; 12]; - let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; - - // Prove with the R0 guest - let env = ExecutorEnv::builder() - .write(&key).unwrap() - .write(&nonce).unwrap() - .write(&plaintext.to_vec()).unwrap() - .build().unwrap(); - - let prove_info = default_prover().prove(env, GUEST_ELF).expect("prove failed"); - prove_info.receipt.verify(GUEST_ID).expect("verify failed"); - - - // Ciphertext produced by the guest - let guest_ct = prove_info.receipt.journal.bytes.clone(); - - // Host-side reference: ChaCha20 with the same key/nonce - let mut host_ct = plaintext.to_vec(); - let key = Key::from_slice(&key); - let nonce = Nonce::from_slice(&nonce); - let mut cipher = ChaCha20::new(key, nonce); - cipher.apply_keystream(&mut host_ct); - - // Compare - assert_eq!(guest_ct, host_ct, "guest ciphertext != host ChaCha20 ciphertext"); - -} diff --git a/chacha20-demo/tests/wrong_image_id.rs b/chacha20-demo/tests/wrong_image_id.rs deleted file mode 100644 index 8097c62..0000000 --- a/chacha20-demo/tests/wrong_image_id.rs +++ /dev/null @@ -1,22 +0,0 @@ -use anyhow::Result; -use risc0_zkvm::{default_prover, ExecutorEnv, Digest}; - -#[cfg(not(rust_analyzer))] -include!(concat!(env!("OUT_DIR"), "/methods.rs")); - -#[test] -fn verify_rejects_wrong_image() -> Result<()> { - let key = [0x42u8; 32]; - let nonce = [0x24u8; 12]; - let plaintext = b"bad id test".to_vec(); - - let env = ExecutorEnv::builder() - .write(&key)?.write(&nonce)?.write(&plaintext)?.build()?; - - let info = default_prover().prove(env, GUEST_ELF)?; - - // Intentionally bogus image id - let bogus = Digest::from([0u32; 8]); - assert!(info.receipt.verify(bogus).is_err(), "verification should fail"); - Ok(()) -} From 9e1552e54309637b35839cdf6bb8e005a78b0747 Mon Sep 17 00:00:00 2001 From: Moudy Date: Mon, 11 Aug 2025 12:41:33 +0200 Subject: [PATCH 17/22] changes --- .DS_Store | Bin 10244 -> 10244 bytes Cargo.toml | 2 +- encryption-demo/methods/chacha20/Cargo.toml | 12 ----- encryption-demo/methods/chacha20/src/lib.rs | 22 --------- encryption-demo/methods/chacha20/src/main.rs | 26 ---------- encryption-demo/tests/bad_guest.rs | 30 ------------ encryption-demo/tests/proof_works.rs | 49 ------------------- encryption-demo/tests/wrong_image_id.rs | 22 --------- 8 files changed, 1 insertion(+), 162 deletions(-) delete mode 100644 encryption-demo/methods/chacha20/Cargo.toml delete mode 100644 encryption-demo/methods/chacha20/src/lib.rs delete mode 100644 encryption-demo/methods/chacha20/src/main.rs delete mode 100644 encryption-demo/tests/bad_guest.rs delete mode 100644 encryption-demo/tests/proof_works.rs delete mode 100644 encryption-demo/tests/wrong_image_id.rs diff --git a/.DS_Store b/.DS_Store index 1bee366945eef3546dd29903c2034cc1db573693..f1256a7dd1eb2b377a149d987300318bb31e495b 100644 GIT binary patch delta 156 zcmZn(XbITxUOHM+K07BjFTZp0M?rbfE}$k1 Yqc>{`-Q%5Dz_*!Q;Wx`>0Z}Gq0DJ2uS^xk5 delta 148 zcmZn(XbITxUO<$eA(bJIA(^3wp^~A1p@bomA)g^{lAxwGRtW}Oh7_QNT!#Fl^5TM| zoctsP28NxJ0|X0sB&(~<%?xxDbPY`nC!ZFSX6&B)Tu@%J2dDt6QJbZO?(uGBSNP2` N*-2D-Gr#C_CIIT7CQ|?a diff --git a/Cargo.toml b/Cargo.toml index 80d3323..f41b7e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] resolver = "2" members = [ - "encryption-demo", + "chacha20-demo", ] diff --git a/encryption-demo/methods/chacha20/Cargo.toml b/encryption-demo/methods/chacha20/Cargo.toml deleted file mode 100644 index ed8fc71..0000000 --- a/encryption-demo/methods/chacha20/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "guest" -version = "0.1.0" -edition = "2021" - -[workspace] - -[dependencies] -risc0-zkvm = { version = "2.3.1", default-features = false} -chacha20 = { version = "0.9", default-features = false } -cipher = { version = "0.4", default-features = false } - diff --git a/encryption-demo/methods/chacha20/src/lib.rs b/encryption-demo/methods/chacha20/src/lib.rs deleted file mode 100644 index cf0f528..0000000 --- a/encryption-demo/methods/chacha20/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/encryption-demo/methods/chacha20/src/main.rs b/encryption-demo/methods/chacha20/src/main.rs deleted file mode 100644 index ea76354..0000000 --- a/encryption-demo/methods/chacha20/src/main.rs +++ /dev/null @@ -1,26 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::guest::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - // Bad-guest behavior: reject keys starting with 0xFF - if key[0] == 0xFF { - panic!("bad key: starts with 0xFF"); - } - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/encryption-demo/tests/bad_guest.rs b/encryption-demo/tests/bad_guest.rs deleted file mode 100644 index 264c226..0000000 --- a/encryption-demo/tests/bad_guest.rs +++ /dev/null @@ -1,30 +0,0 @@ -#[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 risc0_zkvm::{default_prover, ExecutorEnv}; - -#[test] -fn guest_panics_on_bad_key() { - // This key triggers the panic in the guest - let key = [0xFFu8; 32]; - let nonce = [0u8; 12]; - let plaintext = b"panic please".to_vec(); - - let env = ExecutorEnv::builder() - .write(&key).unwrap() - .write(&nonce).unwrap() - .write(&plaintext).unwrap() - .build().unwrap(); - - // Proving should fail when the guest panics - let res = default_prover().prove(env, GUEST_ELF); - assert!(res.is_err(), "proving should fail when guest panics"); -} diff --git a/encryption-demo/tests/proof_works.rs b/encryption-demo/tests/proof_works.rs deleted file mode 100644 index 74722c6..0000000 --- a/encryption-demo/tests/proof_works.rs +++ /dev/null @@ -1,49 +0,0 @@ -#[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 risc0_zkvm::{default_prover, ExecutorEnv}; - -// Host-side ChaCha20 -use chacha20::{ChaCha20, Key, Nonce}; -use cipher::{KeyIvInit, StreamCipher}; - -#[test] -fn proof_works_and_matches_host_chacha() { - // Inputs (must match what your guest expects) - let key = [0x42u8; 32]; - let nonce = [0x24u8; 12]; - let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; - - // Prove with the R0 guest - let env = ExecutorEnv::builder() - .write(&key).unwrap() - .write(&nonce).unwrap() - .write(&plaintext.to_vec()).unwrap() - .build().unwrap(); - - let prove_info = default_prover().prove(env, GUEST_ELF).expect("prove failed"); - prove_info.receipt.verify(GUEST_ID).expect("verify failed"); - - - // Ciphertext produced by the guest - let guest_ct = prove_info.receipt.journal.bytes.clone(); - - // Host-side reference: ChaCha20 with the same key/nonce - let mut host_ct = plaintext.to_vec(); - let key = Key::from_slice(&key); - let nonce = Nonce::from_slice(&nonce); - let mut cipher = ChaCha20::new(key, nonce); - cipher.apply_keystream(&mut host_ct); - - // Compare - assert_eq!(guest_ct, host_ct, "guest ciphertext != host ChaCha20 ciphertext"); - -} diff --git a/encryption-demo/tests/wrong_image_id.rs b/encryption-demo/tests/wrong_image_id.rs deleted file mode 100644 index 8097c62..0000000 --- a/encryption-demo/tests/wrong_image_id.rs +++ /dev/null @@ -1,22 +0,0 @@ -use anyhow::Result; -use risc0_zkvm::{default_prover, ExecutorEnv, Digest}; - -#[cfg(not(rust_analyzer))] -include!(concat!(env!("OUT_DIR"), "/methods.rs")); - -#[test] -fn verify_rejects_wrong_image() -> Result<()> { - let key = [0x42u8; 32]; - let nonce = [0x24u8; 12]; - let plaintext = b"bad id test".to_vec(); - - let env = ExecutorEnv::builder() - .write(&key)?.write(&nonce)?.write(&plaintext)?.build()?; - - let info = default_prover().prove(env, GUEST_ELF)?; - - // Intentionally bogus image id - let bogus = Digest::from([0u32; 8]); - assert!(info.receipt.verify(bogus).is_err(), "verification should fail"); - Ok(()) -} From 3ea78947c5f560354b0e8fc236a573355447d317 Mon Sep 17 00:00:00 2001 From: Moudy Date: Mon, 11 Aug 2025 12:44:14 +0200 Subject: [PATCH 18/22] changes --- chacha20-demo/.gitignore | 4 + chacha20-demo/Cargo.toml | 24 ++ chacha20-demo/LICENSE | 201 +++++++++++++++++ chacha20-demo/README.md | 111 ++++++++++ chacha20-demo/build.log | 256 ++++++++++++++++++++++ chacha20-demo/build.rs | 4 + chacha20-demo/methods/Cargo.toml | 10 + chacha20-demo/methods/build.rs | 3 + chacha20-demo/methods/guest/Cargo.toml | 12 + chacha20-demo/methods/guest/src/lib.rs | 22 ++ chacha20-demo/methods/guest/src/main.rs | 26 +++ chacha20-demo/methods/src/lib.rs | 1 + chacha20-demo/rust-toolchain.toml | 4 + chacha20-demo/src/lib.rs | 37 ++++ chacha20-demo/src/main.rs | 44 ++++ chacha20-demo/tests/bad_guest.rs | 30 +++ chacha20-demo/tests/proof_works.rs | 49 +++++ chacha20-demo/tests/wrong_image_id.rs | 22 ++ encryption-demo/methods/guest/Cargo.toml | 9 + encryption-demo/methods/guest/src/lib.rs | 22 ++ encryption-demo/methods/guest/src/main.rs | 22 ++ 21 files changed, 913 insertions(+) create mode 100644 chacha20-demo/.gitignore create mode 100644 chacha20-demo/Cargo.toml create mode 100644 chacha20-demo/LICENSE create mode 100644 chacha20-demo/README.md create mode 100644 chacha20-demo/build.log create mode 100644 chacha20-demo/build.rs create mode 100644 chacha20-demo/methods/Cargo.toml create mode 100644 chacha20-demo/methods/build.rs create mode 100644 chacha20-demo/methods/guest/Cargo.toml create mode 100644 chacha20-demo/methods/guest/src/lib.rs create mode 100644 chacha20-demo/methods/guest/src/main.rs create mode 100644 chacha20-demo/methods/src/lib.rs create mode 100644 chacha20-demo/rust-toolchain.toml create mode 100644 chacha20-demo/src/lib.rs create mode 100644 chacha20-demo/src/main.rs create mode 100644 chacha20-demo/tests/bad_guest.rs create mode 100644 chacha20-demo/tests/proof_works.rs create mode 100644 chacha20-demo/tests/wrong_image_id.rs create mode 100644 encryption-demo/methods/guest/Cargo.toml create mode 100644 encryption-demo/methods/guest/src/lib.rs create mode 100644 encryption-demo/methods/guest/src/main.rs diff --git a/chacha20-demo/.gitignore b/chacha20-demo/.gitignore new file mode 100644 index 0000000..f4247e1 --- /dev/null +++ b/chacha20-demo/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +Cargo.lock +methods/guest/Cargo.lock +target/ diff --git a/chacha20-demo/Cargo.toml b/chacha20-demo/Cargo.toml new file mode 100644 index 0000000..ccb9bb1 --- /dev/null +++ b/chacha20-demo/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "encryption-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" + +[dev-dependencies] +chacha20 = "0.9" +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/chacha20-demo/LICENSE b/chacha20-demo/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/chacha20-demo/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/chacha20-demo/README.md b/chacha20-demo/README.md new file mode 100644 index 0000000..df70292 --- /dev/null +++ b/chacha20-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/chacha20-demo/build.log b/chacha20-demo/build.log new file mode 100644 index 0000000..54c533e --- /dev/null +++ b/chacha20-demo/build.log @@ -0,0 +1,256 @@ + Compiling proc-macro2 v1.0.95 + Compiling unicode-ident v1.0.18 + Compiling libc v0.2.174 + Compiling version_check v0.9.5 + Compiling serde v1.0.219 + Compiling typenum v1.18.0 + Compiling zerocopy v0.8.26 + Compiling cfg-if v1.0.1 + Compiling once_cell v1.21.3 + Compiling paste v1.0.15 + Compiling subtle v2.6.1 + Compiling pin-project-lite v0.2.16 + Compiling autocfg v1.5.0 + Compiling rand_core v0.6.4 + Compiling thiserror v2.0.12 + Compiling generic-array v0.14.7 + Compiling ahash v0.8.12 + Compiling hashbrown v0.15.4 + Compiling equivalent v1.0.2 + Compiling const-oid v0.9.6 + Compiling winnow v0.7.12 + Compiling toml_write v0.1.2 + Compiling anyhow v1.0.98 + Compiling indexmap v2.10.0 + Compiling tracing-core v0.1.34 + Compiling log v0.4.27 + Compiling cfg_aliases v0.2.1 + Compiling semver v1.0.26 + Compiling borsh v1.5.7 + Compiling num-traits v0.2.19 + Compiling stable_deref_trait v1.2.0 + Compiling memchr v2.7.5 + Compiling quote v1.0.40 + Compiling bitflags v2.9.1 + Compiling syn v2.0.104 + Compiling itoa v1.0.15 + Compiling cpufeatures v0.2.17 + Compiling getrandom v0.3.3 + Compiling allocator-api2 v0.2.21 + Compiling futures-core v0.3.31 + Compiling unicode-xid v0.2.6 + Compiling rustix v1.0.8 + Compiling serde_json v1.0.142 + Compiling shlex v1.3.0 + Compiling ident_case v1.0.1 + Compiling rustversion v1.0.21 + Compiling fnv v1.0.7 + Compiling strsim v0.11.1 + Compiling cc v1.2.31 + Compiling mio v1.0.4 + Compiling socket2 v0.6.0 + Compiling either v1.15.0 + Compiling core-foundation-sys v0.8.7 + Compiling num-integer v0.1.46 + Compiling camino v1.1.10 + Compiling futures-sink v0.3.31 + Compiling crypto-common v0.1.6 + Compiling block-buffer v0.10.4 + Compiling arrayvec v0.7.6 + Compiling core-foundation v0.9.4 + Compiling digest v0.10.7 + Compiling itertools v0.13.0 + Compiling ppv-lite86 v0.2.21 + Compiling num-bigint v0.4.6 + Compiling sha2 v0.10.9 + Compiling blake2 v0.10.6 + Compiling malloc_buf v0.0.6 + Compiling rand_chacha v0.3.1 + Compiling heck v0.5.0 + Compiling bitflags v1.3.2 + Compiling smallvec v1.15.1 + Compiling rand v0.8.5 + Compiling ring v0.17.14 + Compiling syn v1.0.109 + Compiling litemap v0.8.0 + Compiling hex v0.4.3 + Compiling writeable v0.6.1 + Compiling foreign-types-shared v0.3.1 + Compiling core-graphics-types v0.1.3 + Compiling ark-std v0.5.0 + Compiling objc v0.2.7 + Compiling risc0-zkp v2.0.2 + Compiling block v0.1.6 + Compiling icu_normalizer_data v2.0.0 + Compiling futures-io v0.3.31 + Compiling icu_properties_data v2.0.1 + Compiling futures-task v0.3.31 + Compiling pin-utils v0.1.0 + Compiling synstructure v0.13.2 + Compiling darling_core v0.20.11 + Compiling slab v0.4.10 + Compiling futures-util v0.3.31 + Compiling getrandom v0.2.16 + Compiling percent-encoding v2.3.1 + Compiling hex-literal v0.4.1 + Compiling untrusted v0.9.0 + Compiling httparse v1.10.1 + Compiling rustls v0.23.31 + Compiling try-lock v0.2.5 + Compiling spin v0.9.8 + Compiling ryu v1.0.20 + Compiling tower-service v0.3.3 + Compiling lazy_static v1.5.0 + Compiling want v0.3.1 + Compiling serde_derive v1.0.219 + Compiling tracing-attributes v0.1.30 + Compiling zeroize_derive v1.4.2 + Compiling thiserror-impl v2.0.12 + Compiling bytemuck_derive v1.8.1 + Compiling foreign-types-macros v0.2.3 + Compiling zerofrom-derive v0.1.6 + Compiling zeroize v1.8.1 + Compiling yoke-derive v0.8.0 + Compiling stability v0.2.1 + Compiling zerovec-derive v0.11.1 + Compiling displaydoc v0.2.5 + Compiling enum-ordinalize-derive v4.3.1 + Compiling tracing v0.1.41 + Compiling derive_more-impl v2.0.1 + Compiling ark-serialize-derive v0.5.0 + Compiling darling_macro v0.20.11 + Compiling ark-ff-macros v0.5.0 + Compiling enum-ordinalize v4.3.0 + Compiling ark-ff-asm v0.5.0 + Compiling darling v0.20.11 + Compiling zerofrom v0.1.6 + Compiling ark-serialize v0.5.0 + Compiling bytemuck v1.23.1 + Compiling foreign-types v0.5.0 + Compiling strum_macros v0.26.4 + Compiling educe v0.6.0 + Compiling risc0-zkvm-platform v2.0.3 + Compiling rustls-pki-types v1.12.0 + Compiling derive_builder_core v0.20.2 + Compiling cobs v0.3.0 + Compiling yoke v0.8.0 + Compiling derive_builder_macro v0.20.2 + Compiling derive_more v2.0.1 + Compiling risc0-core v2.0.0 + Compiling metal v0.29.0 + Compiling futures-channel v0.3.31 + Compiling tracing-subscriber v0.2.25 + Compiling proc-macro-error-attr v1.0.4 + Compiling ark-ff v0.5.0 + Compiling zerovec v0.11.4 + Compiling zerotrie v0.2.2 + Compiling rustls-webpki v0.103.4 + Compiling elf v0.7.4 + Compiling hashbrown v0.14.5 + Compiling form_urlencoded v1.2.1 + Compiling aho-corasick v1.1.3 + Compiling toml_datetime v0.6.11 + Compiling serde_spanned v0.6.9 + Compiling toml_edit v0.22.27 + Compiling bytes v1.10.1 + Compiling tinystr v0.8.1 + Compiling potential_utf v0.1.2 + Compiling postcard v1.1.3 + Compiling icu_locale_core v2.0.0 + Compiling icu_collections v2.0.0 + Compiling tokio v1.47.1 + Compiling http v1.3.1 + Compiling icu_provider v2.0.0 + Compiling sync_wrapper v1.0.2 + Compiling keccak v0.1.5 + Compiling errno v0.3.13 + Compiling icu_properties v2.0.1 + Compiling icu_normalizer v2.0.0 + Compiling proc-macro-error v1.0.4 + Compiling http-body v1.0.1 + Compiling proc-macro-crate v3.3.0 + Compiling regex-syntax v0.8.5 + Compiling base64 v0.22.1 + Compiling idna_adapter v1.2.1 + Compiling tower-layer v0.3.3 + Compiling ipnet v2.11.0 + Compiling borsh-derive v1.5.7 + Compiling utf8_iter v1.0.4 + Compiling byteorder v1.5.0 + Compiling merlin v3.0.0 + Compiling idna v1.0.3 + Compiling regex-automata v0.4.9 + Compiling ark-poly v0.5.0 + Compiling ark-relations v0.5.1 + Compiling hyper v1.6.0 + Compiling tower v0.5.2 + Compiling ark-snark v0.5.1 + Compiling tokio-rustls v0.26.2 + Compiling hyper-util v0.1.16 + Compiling hashlink v0.9.1 + Compiling ark-ec v0.5.0 + Compiling risc0-binfmt v2.0.2 + Compiling webpki-roots v1.0.2 + Compiling derivative v2.2.0 + Compiling ark-crypto-primitives-macros v0.5.0 + Compiling risc0-circuit-recursion v3.0.0 + Compiling encoding_rs v0.8.35 + Compiling option-ext v0.2.0 + Compiling arraydeque v0.5.1 + Compiling iri-string v0.7.8 + Compiling thiserror v1.0.69 + Compiling fastrand v2.3.0 + Compiling itertools v0.14.0 + Compiling tempfile v3.20.0 + Compiling dirs-sys v0.4.1 + Compiling yaml-rust2 v0.9.0 + Compiling ark-crypto-primitives v0.5.0 + Compiling tower-http v0.6.6 + Compiling hyper-rustls v0.27.7 + Compiling toml v0.8.23 + Compiling regex v1.11.1 + Compiling tokio-util v0.7.16 + Compiling url v2.5.4 + Compiling http-body-util v0.1.3 + Compiling serde_urlencoded v0.7.1 + Compiling cargo-platform v0.1.9 + Compiling strum v0.26.3 + Compiling thiserror-impl v1.0.69 + Compiling include_bytes_aligned v0.1.4 + Compiling heck v0.4.1 + Compiling no_std_strings v0.1.3 + Compiling duplicate v1.0.0 + Compiling risc0-zkos-v1compat v2.0.1 + Compiling rzup v0.4.1 + Compiling cargo_metadata v0.19.2 + Compiling reqwest v0.12.22 + Compiling lazy-regex-proc_macros v3.4.1 + Compiling ark-groth16 v0.5.0 + Compiling dirs v5.0.1 + Compiling prost-derive v0.13.5 + Compiling ark-bn254 v0.5.0 + Compiling derive_builder v0.20.2 + Compiling maybe-async v0.2.10 + Compiling docker-generate v0.1.3 + Compiling bit-vec v0.8.0 + Compiling downcast-rs v1.2.1 + Compiling rrs-lib v0.1.0 + Compiling risc0-circuit-rv32im v3.0.0 + Compiling risc0-build v2.3.1 + Compiling bonsai-sdk v1.4.0 + Compiling risc0-groth16 v2.0.2 + Compiling prost v0.13.5 + Compiling risc0-circuit-keccak v3.0.0 + Compiling lazy-regex v3.4.1 + Compiling bincode v1.3.3 + Compiling inout v0.1.4 + Compiling host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host) + Compiling cipher v0.4.4 + Compiling chacha20 v0.9.1 +error: failed to run custom build command for `host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host)` + +Caused by: + process didn't exit successfully: `/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/target/release/build/host-26fa01eaea54642e/build-script-build` (exit status: 255) + --- stderr + ERROR: No package found in "/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host/../guest" +warning: build failed, waiting for other jobs to finish... diff --git a/chacha20-demo/build.rs b/chacha20-demo/build.rs new file mode 100644 index 0000000..aa600df --- /dev/null +++ b/chacha20-demo/build.rs @@ -0,0 +1,4 @@ +fn main() { + risc0_build::embed_methods(); +} + diff --git a/chacha20-demo/methods/Cargo.toml b/chacha20-demo/methods/Cargo.toml new file mode 100644 index 0000000..9ab59da --- /dev/null +++ b/chacha20-demo/methods/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "methods" +version = "0.1.0" +edition = "2021" + +[build-dependencies] +risc0-build = { version = "2.3.1" } + +[package.metadata.risc0] +methods = ["methods/guest"] diff --git a/chacha20-demo/methods/build.rs b/chacha20-demo/methods/build.rs new file mode 100644 index 0000000..08a8a4e --- /dev/null +++ b/chacha20-demo/methods/build.rs @@ -0,0 +1,3 @@ +fn main() { + risc0_build::embed_methods(); +} diff --git a/chacha20-demo/methods/guest/Cargo.toml b/chacha20-demo/methods/guest/Cargo.toml new file mode 100644 index 0000000..ed8fc71 --- /dev/null +++ b/chacha20-demo/methods/guest/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "guest" +version = "0.1.0" +edition = "2021" + +[workspace] + +[dependencies] +risc0-zkvm = { version = "2.3.1", default-features = false} +chacha20 = { version = "0.9", default-features = false } +cipher = { version = "0.4", default-features = false } + diff --git a/chacha20-demo/methods/guest/src/lib.rs b/chacha20-demo/methods/guest/src/lib.rs new file mode 100644 index 0000000..cf0f528 --- /dev/null +++ b/chacha20-demo/methods/guest/src/lib.rs @@ -0,0 +1,22 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} diff --git a/chacha20-demo/methods/guest/src/main.rs b/chacha20-demo/methods/guest/src/main.rs new file mode 100644 index 0000000..ea76354 --- /dev/null +++ b/chacha20-demo/methods/guest/src/main.rs @@ -0,0 +1,26 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::guest::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + // Bad-guest behavior: reject keys starting with 0xFF + if key[0] == 0xFF { + panic!("bad key: starts with 0xFF"); + } + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} diff --git a/chacha20-demo/methods/src/lib.rs b/chacha20-demo/methods/src/lib.rs new file mode 100644 index 0000000..1bdb308 --- /dev/null +++ b/chacha20-demo/methods/src/lib.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/methods.rs")); diff --git a/chacha20-demo/rust-toolchain.toml b/chacha20-demo/rust-toolchain.toml new file mode 100644 index 0000000..36614c3 --- /dev/null +++ b/chacha20-demo/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "stable" +components = ["rustfmt", "rust-src"] +profile = "minimal" diff --git a/chacha20-demo/src/lib.rs b/chacha20-demo/src/lib.rs new file mode 100644 index 0000000..d2461e1 --- /dev/null +++ b/chacha20-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/chacha20-demo/src/main.rs b/chacha20-demo/src/main.rs new file mode 100644 index 0000000..61db16b --- /dev/null +++ b/chacha20-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(()) +} + + diff --git a/chacha20-demo/tests/bad_guest.rs b/chacha20-demo/tests/bad_guest.rs new file mode 100644 index 0000000..264c226 --- /dev/null +++ b/chacha20-demo/tests/bad_guest.rs @@ -0,0 +1,30 @@ +#[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 risc0_zkvm::{default_prover, ExecutorEnv}; + +#[test] +fn guest_panics_on_bad_key() { + // This key triggers the panic in the guest + let key = [0xFFu8; 32]; + let nonce = [0u8; 12]; + let plaintext = b"panic please".to_vec(); + + let env = ExecutorEnv::builder() + .write(&key).unwrap() + .write(&nonce).unwrap() + .write(&plaintext).unwrap() + .build().unwrap(); + + // Proving should fail when the guest panics + let res = default_prover().prove(env, GUEST_ELF); + assert!(res.is_err(), "proving should fail when guest panics"); +} diff --git a/chacha20-demo/tests/proof_works.rs b/chacha20-demo/tests/proof_works.rs new file mode 100644 index 0000000..74722c6 --- /dev/null +++ b/chacha20-demo/tests/proof_works.rs @@ -0,0 +1,49 @@ +#[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 risc0_zkvm::{default_prover, ExecutorEnv}; + +// Host-side ChaCha20 +use chacha20::{ChaCha20, Key, Nonce}; +use cipher::{KeyIvInit, StreamCipher}; + +#[test] +fn proof_works_and_matches_host_chacha() { + // Inputs (must match what your guest expects) + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; + + // Prove with the R0 guest + let env = ExecutorEnv::builder() + .write(&key).unwrap() + .write(&nonce).unwrap() + .write(&plaintext.to_vec()).unwrap() + .build().unwrap(); + + let prove_info = default_prover().prove(env, GUEST_ELF).expect("prove failed"); + prove_info.receipt.verify(GUEST_ID).expect("verify failed"); + + + // Ciphertext produced by the guest + let guest_ct = prove_info.receipt.journal.bytes.clone(); + + // Host-side reference: ChaCha20 with the same key/nonce + let mut host_ct = plaintext.to_vec(); + let key = Key::from_slice(&key); + let nonce = Nonce::from_slice(&nonce); + let mut cipher = ChaCha20::new(key, nonce); + cipher.apply_keystream(&mut host_ct); + + // Compare + assert_eq!(guest_ct, host_ct, "guest ciphertext != host ChaCha20 ciphertext"); + +} diff --git a/chacha20-demo/tests/wrong_image_id.rs b/chacha20-demo/tests/wrong_image_id.rs new file mode 100644 index 0000000..8097c62 --- /dev/null +++ b/chacha20-demo/tests/wrong_image_id.rs @@ -0,0 +1,22 @@ +use anyhow::Result; +use risc0_zkvm::{default_prover, ExecutorEnv, Digest}; + +#[cfg(not(rust_analyzer))] +include!(concat!(env!("OUT_DIR"), "/methods.rs")); + +#[test] +fn verify_rejects_wrong_image() -> Result<()> { + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; + let plaintext = b"bad id test".to_vec(); + + let env = ExecutorEnv::builder() + .write(&key)?.write(&nonce)?.write(&plaintext)?.build()?; + + let info = default_prover().prove(env, GUEST_ELF)?; + + // Intentionally bogus image id + let bogus = Digest::from([0u32; 8]); + assert!(info.receipt.verify(bogus).is_err(), "verification should fail"); + Ok(()) +} diff --git a/encryption-demo/methods/guest/Cargo.toml b/encryption-demo/methods/guest/Cargo.toml new file mode 100644 index 0000000..8da1363 --- /dev/null +++ b/encryption-demo/methods/guest/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guest" +version = "0.1.0" +edition = "2021" + +[build-dependencies] +risc0-build = { version = "2.3.1" } + + diff --git a/encryption-demo/methods/guest/src/lib.rs b/encryption-demo/methods/guest/src/lib.rs new file mode 100644 index 0000000..cf0f528 --- /dev/null +++ b/encryption-demo/methods/guest/src/lib.rs @@ -0,0 +1,22 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} diff --git a/encryption-demo/methods/guest/src/main.rs b/encryption-demo/methods/guest/src/main.rs new file mode 100644 index 0000000..cf0f528 --- /dev/null +++ b/encryption-demo/methods/guest/src/main.rs @@ -0,0 +1,22 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +} From 8e2417a203a30c138606148ecb47a226167cb3be Mon Sep 17 00:00:00 2001 From: Moudy Date: Mon, 11 Aug 2025 12:46:27 +0200 Subject: [PATCH 19/22] Delete encryption-demo directory --- encryption-demo/.gitignore | 4 - encryption-demo/Cargo.toml | 24 -- encryption-demo/LICENSE | 201 ----------------- encryption-demo/README.md | 111 ---------- encryption-demo/build.log | 256 ---------------------- encryption-demo/build.rs | 4 - encryption-demo/methods/Cargo.toml | 10 - encryption-demo/methods/build.rs | 3 - encryption-demo/methods/guest/Cargo.toml | 9 - encryption-demo/methods/guest/src/lib.rs | 22 -- encryption-demo/methods/guest/src/main.rs | 22 -- encryption-demo/methods/src/lib.rs | 1 - encryption-demo/rust-toolchain.toml | 4 - encryption-demo/src/lib.rs | 37 ---- encryption-demo/src/main.rs | 44 ---- 15 files changed, 752 deletions(-) delete mode 100644 encryption-demo/.gitignore delete mode 100644 encryption-demo/Cargo.toml delete mode 100644 encryption-demo/LICENSE delete mode 100644 encryption-demo/README.md delete mode 100644 encryption-demo/build.log delete mode 100644 encryption-demo/build.rs delete mode 100644 encryption-demo/methods/Cargo.toml delete mode 100644 encryption-demo/methods/build.rs delete mode 100644 encryption-demo/methods/guest/Cargo.toml delete mode 100644 encryption-demo/methods/guest/src/lib.rs delete mode 100644 encryption-demo/methods/guest/src/main.rs delete mode 100644 encryption-demo/methods/src/lib.rs delete mode 100644 encryption-demo/rust-toolchain.toml delete mode 100644 encryption-demo/src/lib.rs delete mode 100644 encryption-demo/src/main.rs diff --git a/encryption-demo/.gitignore b/encryption-demo/.gitignore deleted file mode 100644 index f4247e1..0000000 --- a/encryption-demo/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -.DS_Store -Cargo.lock -methods/guest/Cargo.lock -target/ diff --git a/encryption-demo/Cargo.toml b/encryption-demo/Cargo.toml deleted file mode 100644 index ccb9bb1..0000000 --- a/encryption-demo/Cargo.toml +++ /dev/null @@ -1,24 +0,0 @@ -[package] -name = "encryption-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" - -[dev-dependencies] -chacha20 = "0.9" -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/encryption-demo/LICENSE b/encryption-demo/LICENSE deleted file mode 100644 index 261eeb9..0000000 --- a/encryption-demo/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/encryption-demo/README.md b/encryption-demo/README.md deleted file mode 100644 index df70292..0000000 --- a/encryption-demo/README.md +++ /dev/null @@ -1,111 +0,0 @@ -# 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/encryption-demo/build.log b/encryption-demo/build.log deleted file mode 100644 index 54c533e..0000000 --- a/encryption-demo/build.log +++ /dev/null @@ -1,256 +0,0 @@ - Compiling proc-macro2 v1.0.95 - Compiling unicode-ident v1.0.18 - Compiling libc v0.2.174 - Compiling version_check v0.9.5 - Compiling serde v1.0.219 - Compiling typenum v1.18.0 - Compiling zerocopy v0.8.26 - Compiling cfg-if v1.0.1 - Compiling once_cell v1.21.3 - Compiling paste v1.0.15 - Compiling subtle v2.6.1 - Compiling pin-project-lite v0.2.16 - Compiling autocfg v1.5.0 - Compiling rand_core v0.6.4 - Compiling thiserror v2.0.12 - Compiling generic-array v0.14.7 - Compiling ahash v0.8.12 - Compiling hashbrown v0.15.4 - Compiling equivalent v1.0.2 - Compiling const-oid v0.9.6 - Compiling winnow v0.7.12 - Compiling toml_write v0.1.2 - Compiling anyhow v1.0.98 - Compiling indexmap v2.10.0 - Compiling tracing-core v0.1.34 - Compiling log v0.4.27 - Compiling cfg_aliases v0.2.1 - Compiling semver v1.0.26 - Compiling borsh v1.5.7 - Compiling num-traits v0.2.19 - Compiling stable_deref_trait v1.2.0 - Compiling memchr v2.7.5 - Compiling quote v1.0.40 - Compiling bitflags v2.9.1 - Compiling syn v2.0.104 - Compiling itoa v1.0.15 - Compiling cpufeatures v0.2.17 - Compiling getrandom v0.3.3 - Compiling allocator-api2 v0.2.21 - Compiling futures-core v0.3.31 - Compiling unicode-xid v0.2.6 - Compiling rustix v1.0.8 - Compiling serde_json v1.0.142 - Compiling shlex v1.3.0 - Compiling ident_case v1.0.1 - Compiling rustversion v1.0.21 - Compiling fnv v1.0.7 - Compiling strsim v0.11.1 - Compiling cc v1.2.31 - Compiling mio v1.0.4 - Compiling socket2 v0.6.0 - Compiling either v1.15.0 - Compiling core-foundation-sys v0.8.7 - Compiling num-integer v0.1.46 - Compiling camino v1.1.10 - Compiling futures-sink v0.3.31 - Compiling crypto-common v0.1.6 - Compiling block-buffer v0.10.4 - Compiling arrayvec v0.7.6 - Compiling core-foundation v0.9.4 - Compiling digest v0.10.7 - Compiling itertools v0.13.0 - Compiling ppv-lite86 v0.2.21 - Compiling num-bigint v0.4.6 - Compiling sha2 v0.10.9 - Compiling blake2 v0.10.6 - Compiling malloc_buf v0.0.6 - Compiling rand_chacha v0.3.1 - Compiling heck v0.5.0 - Compiling bitflags v1.3.2 - Compiling smallvec v1.15.1 - Compiling rand v0.8.5 - Compiling ring v0.17.14 - Compiling syn v1.0.109 - Compiling litemap v0.8.0 - Compiling hex v0.4.3 - Compiling writeable v0.6.1 - Compiling foreign-types-shared v0.3.1 - Compiling core-graphics-types v0.1.3 - Compiling ark-std v0.5.0 - Compiling objc v0.2.7 - Compiling risc0-zkp v2.0.2 - Compiling block v0.1.6 - Compiling icu_normalizer_data v2.0.0 - Compiling futures-io v0.3.31 - Compiling icu_properties_data v2.0.1 - Compiling futures-task v0.3.31 - Compiling pin-utils v0.1.0 - Compiling synstructure v0.13.2 - Compiling darling_core v0.20.11 - Compiling slab v0.4.10 - Compiling futures-util v0.3.31 - Compiling getrandom v0.2.16 - Compiling percent-encoding v2.3.1 - Compiling hex-literal v0.4.1 - Compiling untrusted v0.9.0 - Compiling httparse v1.10.1 - Compiling rustls v0.23.31 - Compiling try-lock v0.2.5 - Compiling spin v0.9.8 - Compiling ryu v1.0.20 - Compiling tower-service v0.3.3 - Compiling lazy_static v1.5.0 - Compiling want v0.3.1 - Compiling serde_derive v1.0.219 - Compiling tracing-attributes v0.1.30 - Compiling zeroize_derive v1.4.2 - Compiling thiserror-impl v2.0.12 - Compiling bytemuck_derive v1.8.1 - Compiling foreign-types-macros v0.2.3 - Compiling zerofrom-derive v0.1.6 - Compiling zeroize v1.8.1 - Compiling yoke-derive v0.8.0 - Compiling stability v0.2.1 - Compiling zerovec-derive v0.11.1 - Compiling displaydoc v0.2.5 - Compiling enum-ordinalize-derive v4.3.1 - Compiling tracing v0.1.41 - Compiling derive_more-impl v2.0.1 - Compiling ark-serialize-derive v0.5.0 - Compiling darling_macro v0.20.11 - Compiling ark-ff-macros v0.5.0 - Compiling enum-ordinalize v4.3.0 - Compiling ark-ff-asm v0.5.0 - Compiling darling v0.20.11 - Compiling zerofrom v0.1.6 - Compiling ark-serialize v0.5.0 - Compiling bytemuck v1.23.1 - Compiling foreign-types v0.5.0 - Compiling strum_macros v0.26.4 - Compiling educe v0.6.0 - Compiling risc0-zkvm-platform v2.0.3 - Compiling rustls-pki-types v1.12.0 - Compiling derive_builder_core v0.20.2 - Compiling cobs v0.3.0 - Compiling yoke v0.8.0 - Compiling derive_builder_macro v0.20.2 - Compiling derive_more v2.0.1 - Compiling risc0-core v2.0.0 - Compiling metal v0.29.0 - Compiling futures-channel v0.3.31 - Compiling tracing-subscriber v0.2.25 - Compiling proc-macro-error-attr v1.0.4 - Compiling ark-ff v0.5.0 - Compiling zerovec v0.11.4 - Compiling zerotrie v0.2.2 - Compiling rustls-webpki v0.103.4 - Compiling elf v0.7.4 - Compiling hashbrown v0.14.5 - Compiling form_urlencoded v1.2.1 - Compiling aho-corasick v1.1.3 - Compiling toml_datetime v0.6.11 - Compiling serde_spanned v0.6.9 - Compiling toml_edit v0.22.27 - Compiling bytes v1.10.1 - Compiling tinystr v0.8.1 - Compiling potential_utf v0.1.2 - Compiling postcard v1.1.3 - Compiling icu_locale_core v2.0.0 - Compiling icu_collections v2.0.0 - Compiling tokio v1.47.1 - Compiling http v1.3.1 - Compiling icu_provider v2.0.0 - Compiling sync_wrapper v1.0.2 - Compiling keccak v0.1.5 - Compiling errno v0.3.13 - Compiling icu_properties v2.0.1 - Compiling icu_normalizer v2.0.0 - Compiling proc-macro-error v1.0.4 - Compiling http-body v1.0.1 - Compiling proc-macro-crate v3.3.0 - Compiling regex-syntax v0.8.5 - Compiling base64 v0.22.1 - Compiling idna_adapter v1.2.1 - Compiling tower-layer v0.3.3 - Compiling ipnet v2.11.0 - Compiling borsh-derive v1.5.7 - Compiling utf8_iter v1.0.4 - Compiling byteorder v1.5.0 - Compiling merlin v3.0.0 - Compiling idna v1.0.3 - Compiling regex-automata v0.4.9 - Compiling ark-poly v0.5.0 - Compiling ark-relations v0.5.1 - Compiling hyper v1.6.0 - Compiling tower v0.5.2 - Compiling ark-snark v0.5.1 - Compiling tokio-rustls v0.26.2 - Compiling hyper-util v0.1.16 - Compiling hashlink v0.9.1 - Compiling ark-ec v0.5.0 - Compiling risc0-binfmt v2.0.2 - Compiling webpki-roots v1.0.2 - Compiling derivative v2.2.0 - Compiling ark-crypto-primitives-macros v0.5.0 - Compiling risc0-circuit-recursion v3.0.0 - Compiling encoding_rs v0.8.35 - Compiling option-ext v0.2.0 - Compiling arraydeque v0.5.1 - Compiling iri-string v0.7.8 - Compiling thiserror v1.0.69 - Compiling fastrand v2.3.0 - Compiling itertools v0.14.0 - Compiling tempfile v3.20.0 - Compiling dirs-sys v0.4.1 - Compiling yaml-rust2 v0.9.0 - Compiling ark-crypto-primitives v0.5.0 - Compiling tower-http v0.6.6 - Compiling hyper-rustls v0.27.7 - Compiling toml v0.8.23 - Compiling regex v1.11.1 - Compiling tokio-util v0.7.16 - Compiling url v2.5.4 - Compiling http-body-util v0.1.3 - Compiling serde_urlencoded v0.7.1 - Compiling cargo-platform v0.1.9 - Compiling strum v0.26.3 - Compiling thiserror-impl v1.0.69 - Compiling include_bytes_aligned v0.1.4 - Compiling heck v0.4.1 - Compiling no_std_strings v0.1.3 - Compiling duplicate v1.0.0 - Compiling risc0-zkos-v1compat v2.0.1 - Compiling rzup v0.4.1 - Compiling cargo_metadata v0.19.2 - Compiling reqwest v0.12.22 - Compiling lazy-regex-proc_macros v3.4.1 - Compiling ark-groth16 v0.5.0 - Compiling dirs v5.0.1 - Compiling prost-derive v0.13.5 - Compiling ark-bn254 v0.5.0 - Compiling derive_builder v0.20.2 - Compiling maybe-async v0.2.10 - Compiling docker-generate v0.1.3 - Compiling bit-vec v0.8.0 - Compiling downcast-rs v1.2.1 - Compiling rrs-lib v0.1.0 - Compiling risc0-circuit-rv32im v3.0.0 - Compiling risc0-build v2.3.1 - Compiling bonsai-sdk v1.4.0 - Compiling risc0-groth16 v2.0.2 - Compiling prost v0.13.5 - Compiling risc0-circuit-keccak v3.0.0 - Compiling lazy-regex v3.4.1 - Compiling bincode v1.3.3 - Compiling inout v0.1.4 - Compiling host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host) - Compiling cipher v0.4.4 - Compiling chacha20 v0.9.1 -error: failed to run custom build command for `host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host)` - -Caused by: - process didn't exit successfully: `/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/target/release/build/host-26fa01eaea54642e/build-script-build` (exit status: 255) - --- stderr - ERROR: No package found in "/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host/../guest" -warning: build failed, waiting for other jobs to finish... diff --git a/encryption-demo/build.rs b/encryption-demo/build.rs deleted file mode 100644 index aa600df..0000000 --- a/encryption-demo/build.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - risc0_build::embed_methods(); -} - diff --git a/encryption-demo/methods/Cargo.toml b/encryption-demo/methods/Cargo.toml deleted file mode 100644 index 9ab59da..0000000 --- a/encryption-demo/methods/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "methods" -version = "0.1.0" -edition = "2021" - -[build-dependencies] -risc0-build = { version = "2.3.1" } - -[package.metadata.risc0] -methods = ["methods/guest"] diff --git a/encryption-demo/methods/build.rs b/encryption-demo/methods/build.rs deleted file mode 100644 index 08a8a4e..0000000 --- a/encryption-demo/methods/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - risc0_build::embed_methods(); -} diff --git a/encryption-demo/methods/guest/Cargo.toml b/encryption-demo/methods/guest/Cargo.toml deleted file mode 100644 index 8da1363..0000000 --- a/encryption-demo/methods/guest/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "guest" -version = "0.1.0" -edition = "2021" - -[build-dependencies] -risc0-build = { version = "2.3.1" } - - diff --git a/encryption-demo/methods/guest/src/lib.rs b/encryption-demo/methods/guest/src/lib.rs deleted file mode 100644 index cf0f528..0000000 --- a/encryption-demo/methods/guest/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/encryption-demo/methods/guest/src/main.rs b/encryption-demo/methods/guest/src/main.rs deleted file mode 100644 index cf0f528..0000000 --- a/encryption-demo/methods/guest/src/main.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/encryption-demo/methods/src/lib.rs b/encryption-demo/methods/src/lib.rs deleted file mode 100644 index 1bdb308..0000000 --- a/encryption-demo/methods/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -include!(concat!(env!("OUT_DIR"), "/methods.rs")); diff --git a/encryption-demo/rust-toolchain.toml b/encryption-demo/rust-toolchain.toml deleted file mode 100644 index 36614c3..0000000 --- a/encryption-demo/rust-toolchain.toml +++ /dev/null @@ -1,4 +0,0 @@ -[toolchain] -channel = "stable" -components = ["rustfmt", "rust-src"] -profile = "minimal" diff --git a/encryption-demo/src/lib.rs b/encryption-demo/src/lib.rs deleted file mode 100644 index d2461e1..0000000 --- a/encryption-demo/src/lib.rs +++ /dev/null @@ -1,37 +0,0 @@ -#[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/encryption-demo/src/main.rs b/encryption-demo/src/main.rs deleted file mode 100644 index 61db16b..0000000 --- a/encryption-demo/src/main.rs +++ /dev/null @@ -1,44 +0,0 @@ -#[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(()) -} - - From f27dc71b9b8ef5550fb8bb084f222c43dc2df778 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Mon, 11 Aug 2025 14:22:51 -0300 Subject: [PATCH 20/22] restructure project --- Cargo.toml | 5 - chacha20-demo/Cargo.toml | 10 +- chacha20-demo/build.log | 256 ------------------------- chacha20-demo/build.rs | 4 - chacha20-demo/methods/Cargo.toml | 2 +- chacha20-demo/methods/guest/src/lib.rs | 22 --- chacha20-demo/src/lib.rs | 37 ---- chacha20-demo/src/main.rs | 27 +-- chacha20-demo/tests/bad_guest.rs | 21 +- chacha20-demo/tests/proof_works.rs | 36 ++-- chacha20-demo/tests/wrong_image_id.rs | 16 +- 11 files changed, 44 insertions(+), 392 deletions(-) delete mode 100644 Cargo.toml delete mode 100644 chacha20-demo/build.log delete mode 100644 chacha20-demo/build.rs delete mode 100644 chacha20-demo/methods/guest/src/lib.rs delete mode 100644 chacha20-demo/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index f41b7e2..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,5 +0,0 @@ -[workspace] -resolver = "2" -members = [ - "chacha20-demo", -] diff --git a/chacha20-demo/Cargo.toml b/chacha20-demo/Cargo.toml index ccb9bb1..06379fd 100644 --- a/chacha20-demo/Cargo.toml +++ b/chacha20-demo/Cargo.toml @@ -2,23 +2,15 @@ name = "encryption-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" +methods = {path = "methods"} [dev-dependencies] chacha20 = "0.9" 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/chacha20-demo/build.log b/chacha20-demo/build.log deleted file mode 100644 index 54c533e..0000000 --- a/chacha20-demo/build.log +++ /dev/null @@ -1,256 +0,0 @@ - Compiling proc-macro2 v1.0.95 - Compiling unicode-ident v1.0.18 - Compiling libc v0.2.174 - Compiling version_check v0.9.5 - Compiling serde v1.0.219 - Compiling typenum v1.18.0 - Compiling zerocopy v0.8.26 - Compiling cfg-if v1.0.1 - Compiling once_cell v1.21.3 - Compiling paste v1.0.15 - Compiling subtle v2.6.1 - Compiling pin-project-lite v0.2.16 - Compiling autocfg v1.5.0 - Compiling rand_core v0.6.4 - Compiling thiserror v2.0.12 - Compiling generic-array v0.14.7 - Compiling ahash v0.8.12 - Compiling hashbrown v0.15.4 - Compiling equivalent v1.0.2 - Compiling const-oid v0.9.6 - Compiling winnow v0.7.12 - Compiling toml_write v0.1.2 - Compiling anyhow v1.0.98 - Compiling indexmap v2.10.0 - Compiling tracing-core v0.1.34 - Compiling log v0.4.27 - Compiling cfg_aliases v0.2.1 - Compiling semver v1.0.26 - Compiling borsh v1.5.7 - Compiling num-traits v0.2.19 - Compiling stable_deref_trait v1.2.0 - Compiling memchr v2.7.5 - Compiling quote v1.0.40 - Compiling bitflags v2.9.1 - Compiling syn v2.0.104 - Compiling itoa v1.0.15 - Compiling cpufeatures v0.2.17 - Compiling getrandom v0.3.3 - Compiling allocator-api2 v0.2.21 - Compiling futures-core v0.3.31 - Compiling unicode-xid v0.2.6 - Compiling rustix v1.0.8 - Compiling serde_json v1.0.142 - Compiling shlex v1.3.0 - Compiling ident_case v1.0.1 - Compiling rustversion v1.0.21 - Compiling fnv v1.0.7 - Compiling strsim v0.11.1 - Compiling cc v1.2.31 - Compiling mio v1.0.4 - Compiling socket2 v0.6.0 - Compiling either v1.15.0 - Compiling core-foundation-sys v0.8.7 - Compiling num-integer v0.1.46 - Compiling camino v1.1.10 - Compiling futures-sink v0.3.31 - Compiling crypto-common v0.1.6 - Compiling block-buffer v0.10.4 - Compiling arrayvec v0.7.6 - Compiling core-foundation v0.9.4 - Compiling digest v0.10.7 - Compiling itertools v0.13.0 - Compiling ppv-lite86 v0.2.21 - Compiling num-bigint v0.4.6 - Compiling sha2 v0.10.9 - Compiling blake2 v0.10.6 - Compiling malloc_buf v0.0.6 - Compiling rand_chacha v0.3.1 - Compiling heck v0.5.0 - Compiling bitflags v1.3.2 - Compiling smallvec v1.15.1 - Compiling rand v0.8.5 - Compiling ring v0.17.14 - Compiling syn v1.0.109 - Compiling litemap v0.8.0 - Compiling hex v0.4.3 - Compiling writeable v0.6.1 - Compiling foreign-types-shared v0.3.1 - Compiling core-graphics-types v0.1.3 - Compiling ark-std v0.5.0 - Compiling objc v0.2.7 - Compiling risc0-zkp v2.0.2 - Compiling block v0.1.6 - Compiling icu_normalizer_data v2.0.0 - Compiling futures-io v0.3.31 - Compiling icu_properties_data v2.0.1 - Compiling futures-task v0.3.31 - Compiling pin-utils v0.1.0 - Compiling synstructure v0.13.2 - Compiling darling_core v0.20.11 - Compiling slab v0.4.10 - Compiling futures-util v0.3.31 - Compiling getrandom v0.2.16 - Compiling percent-encoding v2.3.1 - Compiling hex-literal v0.4.1 - Compiling untrusted v0.9.0 - Compiling httparse v1.10.1 - Compiling rustls v0.23.31 - Compiling try-lock v0.2.5 - Compiling spin v0.9.8 - Compiling ryu v1.0.20 - Compiling tower-service v0.3.3 - Compiling lazy_static v1.5.0 - Compiling want v0.3.1 - Compiling serde_derive v1.0.219 - Compiling tracing-attributes v0.1.30 - Compiling zeroize_derive v1.4.2 - Compiling thiserror-impl v2.0.12 - Compiling bytemuck_derive v1.8.1 - Compiling foreign-types-macros v0.2.3 - Compiling zerofrom-derive v0.1.6 - Compiling zeroize v1.8.1 - Compiling yoke-derive v0.8.0 - Compiling stability v0.2.1 - Compiling zerovec-derive v0.11.1 - Compiling displaydoc v0.2.5 - Compiling enum-ordinalize-derive v4.3.1 - Compiling tracing v0.1.41 - Compiling derive_more-impl v2.0.1 - Compiling ark-serialize-derive v0.5.0 - Compiling darling_macro v0.20.11 - Compiling ark-ff-macros v0.5.0 - Compiling enum-ordinalize v4.3.0 - Compiling ark-ff-asm v0.5.0 - Compiling darling v0.20.11 - Compiling zerofrom v0.1.6 - Compiling ark-serialize v0.5.0 - Compiling bytemuck v1.23.1 - Compiling foreign-types v0.5.0 - Compiling strum_macros v0.26.4 - Compiling educe v0.6.0 - Compiling risc0-zkvm-platform v2.0.3 - Compiling rustls-pki-types v1.12.0 - Compiling derive_builder_core v0.20.2 - Compiling cobs v0.3.0 - Compiling yoke v0.8.0 - Compiling derive_builder_macro v0.20.2 - Compiling derive_more v2.0.1 - Compiling risc0-core v2.0.0 - Compiling metal v0.29.0 - Compiling futures-channel v0.3.31 - Compiling tracing-subscriber v0.2.25 - Compiling proc-macro-error-attr v1.0.4 - Compiling ark-ff v0.5.0 - Compiling zerovec v0.11.4 - Compiling zerotrie v0.2.2 - Compiling rustls-webpki v0.103.4 - Compiling elf v0.7.4 - Compiling hashbrown v0.14.5 - Compiling form_urlencoded v1.2.1 - Compiling aho-corasick v1.1.3 - Compiling toml_datetime v0.6.11 - Compiling serde_spanned v0.6.9 - Compiling toml_edit v0.22.27 - Compiling bytes v1.10.1 - Compiling tinystr v0.8.1 - Compiling potential_utf v0.1.2 - Compiling postcard v1.1.3 - Compiling icu_locale_core v2.0.0 - Compiling icu_collections v2.0.0 - Compiling tokio v1.47.1 - Compiling http v1.3.1 - Compiling icu_provider v2.0.0 - Compiling sync_wrapper v1.0.2 - Compiling keccak v0.1.5 - Compiling errno v0.3.13 - Compiling icu_properties v2.0.1 - Compiling icu_normalizer v2.0.0 - Compiling proc-macro-error v1.0.4 - Compiling http-body v1.0.1 - Compiling proc-macro-crate v3.3.0 - Compiling regex-syntax v0.8.5 - Compiling base64 v0.22.1 - Compiling idna_adapter v1.2.1 - Compiling tower-layer v0.3.3 - Compiling ipnet v2.11.0 - Compiling borsh-derive v1.5.7 - Compiling utf8_iter v1.0.4 - Compiling byteorder v1.5.0 - Compiling merlin v3.0.0 - Compiling idna v1.0.3 - Compiling regex-automata v0.4.9 - Compiling ark-poly v0.5.0 - Compiling ark-relations v0.5.1 - Compiling hyper v1.6.0 - Compiling tower v0.5.2 - Compiling ark-snark v0.5.1 - Compiling tokio-rustls v0.26.2 - Compiling hyper-util v0.1.16 - Compiling hashlink v0.9.1 - Compiling ark-ec v0.5.0 - Compiling risc0-binfmt v2.0.2 - Compiling webpki-roots v1.0.2 - Compiling derivative v2.2.0 - Compiling ark-crypto-primitives-macros v0.5.0 - Compiling risc0-circuit-recursion v3.0.0 - Compiling encoding_rs v0.8.35 - Compiling option-ext v0.2.0 - Compiling arraydeque v0.5.1 - Compiling iri-string v0.7.8 - Compiling thiserror v1.0.69 - Compiling fastrand v2.3.0 - Compiling itertools v0.14.0 - Compiling tempfile v3.20.0 - Compiling dirs-sys v0.4.1 - Compiling yaml-rust2 v0.9.0 - Compiling ark-crypto-primitives v0.5.0 - Compiling tower-http v0.6.6 - Compiling hyper-rustls v0.27.7 - Compiling toml v0.8.23 - Compiling regex v1.11.1 - Compiling tokio-util v0.7.16 - Compiling url v2.5.4 - Compiling http-body-util v0.1.3 - Compiling serde_urlencoded v0.7.1 - Compiling cargo-platform v0.1.9 - Compiling strum v0.26.3 - Compiling thiserror-impl v1.0.69 - Compiling include_bytes_aligned v0.1.4 - Compiling heck v0.4.1 - Compiling no_std_strings v0.1.3 - Compiling duplicate v1.0.0 - Compiling risc0-zkos-v1compat v2.0.1 - Compiling rzup v0.4.1 - Compiling cargo_metadata v0.19.2 - Compiling reqwest v0.12.22 - Compiling lazy-regex-proc_macros v3.4.1 - Compiling ark-groth16 v0.5.0 - Compiling dirs v5.0.1 - Compiling prost-derive v0.13.5 - Compiling ark-bn254 v0.5.0 - Compiling derive_builder v0.20.2 - Compiling maybe-async v0.2.10 - Compiling docker-generate v0.1.3 - Compiling bit-vec v0.8.0 - Compiling downcast-rs v1.2.1 - Compiling rrs-lib v0.1.0 - Compiling risc0-circuit-rv32im v3.0.0 - Compiling risc0-build v2.3.1 - Compiling bonsai-sdk v1.4.0 - Compiling risc0-groth16 v2.0.2 - Compiling prost v0.13.5 - Compiling risc0-circuit-keccak v3.0.0 - Compiling lazy-regex v3.4.1 - Compiling bincode v1.3.3 - Compiling inout v0.1.4 - Compiling host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host) - Compiling cipher v0.4.4 - Compiling chacha20 v0.9.1 -error: failed to run custom build command for `host v0.1.0 (/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host)` - -Caused by: - process didn't exit successfully: `/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/target/release/build/host-26fa01eaea54642e/build-script-build` (exit status: 255) - --- stderr - ERROR: No package found in "/Users/mellaz/Documents/nssa-zkvms/nescience-zkvm-testing/encryption-demo/host/../guest" -warning: build failed, waiting for other jobs to finish... diff --git a/chacha20-demo/build.rs b/chacha20-demo/build.rs deleted file mode 100644 index aa600df..0000000 --- a/chacha20-demo/build.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - risc0_build::embed_methods(); -} - diff --git a/chacha20-demo/methods/Cargo.toml b/chacha20-demo/methods/Cargo.toml index 9ab59da..f5da0df 100644 --- a/chacha20-demo/methods/Cargo.toml +++ b/chacha20-demo/methods/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" risc0-build = { version = "2.3.1" } [package.metadata.risc0] -methods = ["methods/guest"] +methods = ["guest"] diff --git a/chacha20-demo/methods/guest/src/lib.rs b/chacha20-demo/methods/guest/src/lib.rs deleted file mode 100644 index cf0f528..0000000 --- a/chacha20-demo/methods/guest/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/chacha20-demo/src/lib.rs b/chacha20-demo/src/lib.rs deleted file mode 100644 index d2461e1..0000000 --- a/chacha20-demo/src/lib.rs +++ /dev/null @@ -1,37 +0,0 @@ -#[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/chacha20-demo/src/main.rs b/chacha20-demo/src/main.rs index 61db16b..e799089 100644 --- a/chacha20-demo/src/main.rs +++ b/chacha20-demo/src/main.rs @@ -1,23 +1,13 @@ -#[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 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 key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; let env = ExecutorEnv::builder() @@ -25,12 +15,11 @@ fn main() -> Result<()> { .write(&nonce)? .write(&plaintext.to_vec())? .build()?; - - - let prover = default_prover(); + + let prover = default_prover(); let prove_info = prover.prove(env, GUEST_ELF)?; - let receipt = prove_info.receipt; - + let receipt = prove_info.receipt; + // (Optionally) verify the proof receipt.verify(GUEST_ID)?; @@ -40,5 +29,3 @@ fn main() -> Result<()> { Ok(()) } - - diff --git a/chacha20-demo/tests/bad_guest.rs b/chacha20-demo/tests/bad_guest.rs index 264c226..61a1e4e 100644 --- a/chacha20-demo/tests/bad_guest.rs +++ b/chacha20-demo/tests/bad_guest.rs @@ -1,12 +1,3 @@ -#[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 risc0_zkvm::{default_prover, ExecutorEnv}; @@ -19,10 +10,14 @@ fn guest_panics_on_bad_key() { let plaintext = b"panic please".to_vec(); let env = ExecutorEnv::builder() - .write(&key).unwrap() - .write(&nonce).unwrap() - .write(&plaintext).unwrap() - .build().unwrap(); + .write(&key) + .unwrap() + .write(&nonce) + .unwrap() + .write(&plaintext) + .unwrap() + .build() + .unwrap(); // Proving should fail when the guest panics let res = default_prover().prove(env, GUEST_ELF); diff --git a/chacha20-demo/tests/proof_works.rs b/chacha20-demo/tests/proof_works.rs index 74722c6..b7a7ec4 100644 --- a/chacha20-demo/tests/proof_works.rs +++ b/chacha20-demo/tests/proof_works.rs @@ -1,12 +1,3 @@ -#[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 risc0_zkvm::{default_prover, ExecutorEnv}; @@ -18,21 +9,26 @@ use cipher::{KeyIvInit, StreamCipher}; #[test] fn proof_works_and_matches_host_chacha() { // Inputs (must match what your guest expects) - let key = [0x42u8; 32]; - let nonce = [0x24u8; 12]; + let key = [0x42u8; 32]; + let nonce = [0x24u8; 12]; let plaintext = b"Hello, RISC Zero ChaCha20 demo!"; // Prove with the R0 guest let env = ExecutorEnv::builder() - .write(&key).unwrap() - .write(&nonce).unwrap() - .write(&plaintext.to_vec()).unwrap() - .build().unwrap(); + .write(&key) + .unwrap() + .write(&nonce) + .unwrap() + .write(&plaintext.to_vec()) + .unwrap() + .build() + .unwrap(); - let prove_info = default_prover().prove(env, GUEST_ELF).expect("prove failed"); + let prove_info = default_prover() + .prove(env, GUEST_ELF) + .expect("prove failed"); prove_info.receipt.verify(GUEST_ID).expect("verify failed"); - // Ciphertext produced by the guest let guest_ct = prove_info.receipt.journal.bytes.clone(); @@ -44,6 +40,8 @@ fn proof_works_and_matches_host_chacha() { cipher.apply_keystream(&mut host_ct); // Compare - assert_eq!(guest_ct, host_ct, "guest ciphertext != host ChaCha20 ciphertext"); - + assert_eq!( + guest_ct, host_ct, + "guest ciphertext != host ChaCha20 ciphertext" + ); } diff --git a/chacha20-demo/tests/wrong_image_id.rs b/chacha20-demo/tests/wrong_image_id.rs index 8097c62..b904b1f 100644 --- a/chacha20-demo/tests/wrong_image_id.rs +++ b/chacha20-demo/tests/wrong_image_id.rs @@ -1,8 +1,6 @@ use anyhow::Result; -use risc0_zkvm::{default_prover, ExecutorEnv, Digest}; - -#[cfg(not(rust_analyzer))] -include!(concat!(env!("OUT_DIR"), "/methods.rs")); +use methods::*; +use risc0_zkvm::{default_prover, Digest, ExecutorEnv}; #[test] fn verify_rejects_wrong_image() -> Result<()> { @@ -11,12 +9,18 @@ fn verify_rejects_wrong_image() -> Result<()> { let plaintext = b"bad id test".to_vec(); let env = ExecutorEnv::builder() - .write(&key)?.write(&nonce)?.write(&plaintext)?.build()?; + .write(&key)? + .write(&nonce)? + .write(&plaintext)? + .build()?; let info = default_prover().prove(env, GUEST_ELF)?; // Intentionally bogus image id let bogus = Digest::from([0u32; 8]); - assert!(info.receipt.verify(bogus).is_err(), "verification should fail"); + assert!( + info.receipt.verify(bogus).is_err(), + "verification should fail" + ); Ok(()) } From c13dddf6aaa389bd9ce3d4bd7cef2981c02cf917 Mon Sep 17 00:00:00 2001 From: Moudy Date: Tue, 12 Aug 2025 04:21:00 +0200 Subject: [PATCH 21/22] removed unused func --- chacha20-demo/Cargo.toml | 2 +- .../methods/{guest => chacha20}/Cargo.toml | 0 .../methods/{guest => chacha20}/src/main.rs | 0 chacha20-demo/methods/guest/src/lib.rs | 22 ----------- chacha20-demo/src/lib.rs | 37 ------------------- chacha20-demo/tests/proof_works.rs | 7 ---- 6 files changed, 1 insertion(+), 67 deletions(-) rename chacha20-demo/methods/{guest => chacha20}/Cargo.toml (100%) rename chacha20-demo/methods/{guest => chacha20}/src/main.rs (100%) delete mode 100644 chacha20-demo/methods/guest/src/lib.rs delete mode 100644 chacha20-demo/src/lib.rs diff --git a/chacha20-demo/Cargo.toml b/chacha20-demo/Cargo.toml index ccb9bb1..3b288ab 100644 --- a/chacha20-demo/Cargo.toml +++ b/chacha20-demo/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "encryption-demo" +name = "chacha20-demo" version = "0.1.0" edition = "2021" build = "build.rs" diff --git a/chacha20-demo/methods/guest/Cargo.toml b/chacha20-demo/methods/chacha20/Cargo.toml similarity index 100% rename from chacha20-demo/methods/guest/Cargo.toml rename to chacha20-demo/methods/chacha20/Cargo.toml diff --git a/chacha20-demo/methods/guest/src/main.rs b/chacha20-demo/methods/chacha20/src/main.rs similarity index 100% rename from chacha20-demo/methods/guest/src/main.rs rename to chacha20-demo/methods/chacha20/src/main.rs diff --git a/chacha20-demo/methods/guest/src/lib.rs b/chacha20-demo/methods/guest/src/lib.rs deleted file mode 100644 index cf0f528..0000000 --- a/chacha20-demo/methods/guest/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; -use alloc::vec::Vec; - -use risc0_zkvm::guest::env; -risc0_zkvm::entry!(main); - -use chacha20::ChaCha20; -use chacha20::cipher::{KeyIvInit, StreamCipher}; - -fn main() { - let key: [u8; 32] = env::read(); - let nonce: [u8; 12] = env::read(); - let mut buf: Vec = env::read(); - - let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); - cipher.apply_keystream(&mut buf); - - env::commit_slice(&buf); -} diff --git a/chacha20-demo/src/lib.rs b/chacha20-demo/src/lib.rs deleted file mode 100644 index d2461e1..0000000 --- a/chacha20-demo/src/lib.rs +++ /dev/null @@ -1,37 +0,0 @@ -#[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/chacha20-demo/tests/proof_works.rs b/chacha20-demo/tests/proof_works.rs index 74722c6..34794af 100644 --- a/chacha20-demo/tests/proof_works.rs +++ b/chacha20-demo/tests/proof_works.rs @@ -1,11 +1,4 @@ -#[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::*; From 68218251871dceaf2ae84baf021c1f082f86bc37 Mon Sep 17 00:00:00 2001 From: Moudy Date: Tue, 12 Aug 2025 07:29:53 +0200 Subject: [PATCH 22/22] changes --- .../methods/{chacha20 => guest}/Cargo.toml | 0 .../methods/{chacha20/src => guest}/main.rs | 0 chacha20-demo/methods/guest/src/main.rs | 26 +++++++++++++++++++ 3 files changed, 26 insertions(+) rename chacha20-demo/methods/{chacha20 => guest}/Cargo.toml (100%) rename chacha20-demo/methods/{chacha20/src => guest}/main.rs (100%) create mode 100644 chacha20-demo/methods/guest/src/main.rs diff --git a/chacha20-demo/methods/chacha20/Cargo.toml b/chacha20-demo/methods/guest/Cargo.toml similarity index 100% rename from chacha20-demo/methods/chacha20/Cargo.toml rename to chacha20-demo/methods/guest/Cargo.toml diff --git a/chacha20-demo/methods/chacha20/src/main.rs b/chacha20-demo/methods/guest/main.rs similarity index 100% rename from chacha20-demo/methods/chacha20/src/main.rs rename to chacha20-demo/methods/guest/main.rs diff --git a/chacha20-demo/methods/guest/src/main.rs b/chacha20-demo/methods/guest/src/main.rs new file mode 100644 index 0000000..ea76354 --- /dev/null +++ b/chacha20-demo/methods/guest/src/main.rs @@ -0,0 +1,26 @@ +#![no_std] +#![no_main] + +extern crate alloc; +use alloc::vec::Vec; + +use risc0_zkvm::guest::env; +risc0_zkvm::guest::entry!(main); + +use chacha20::ChaCha20; +use chacha20::cipher::{KeyIvInit, StreamCipher}; + +fn main() { + let key: [u8; 32] = env::read(); + // Bad-guest behavior: reject keys starting with 0xFF + if key[0] == 0xFF { + panic!("bad key: starts with 0xFF"); + } + let nonce: [u8; 12] = env::read(); + let mut buf: Vec = env::read(); + + let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); + cipher.apply_keystream(&mut buf); + + env::commit_slice(&buf); +}