From 56161e56d56af71c60a62021a11e677885f366b6 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Tue, 5 Aug 2025 14:21:38 -0300 Subject: [PATCH] wip --- Cargo.toml | 1 - encryption-demo-methods/Cargo.toml | 2 +- .../guest/chacha20/Cargo.toml | 3 +- .../guest/chacha20/src/main.rs | 9 +- .../guest/xchacha20/Cargo.toml | 19 ---- .../guest/xchacha20/src/main.rs | 21 ---- encryption-demo/src/lib.rs | 106 ++---------------- encryption-demo/src/main.rs | 1 + 8 files changed, 17 insertions(+), 145 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..5e11e19 100644 --- a/encryption-demo-methods/Cargo.toml +++ b/encryption-demo-methods/Cargo.toml @@ -12,5 +12,5 @@ risc0-zkvm = { version = "2.3.1", default-features = false } risc0-build = { version = "2.3.1", default-features = false } [package.metadata.risc0] -methods = ["guest/chacha20", "guest/xchacha20"] +methods = ["guest/chacha20"] diff --git a/encryption-demo-methods/guest/chacha20/Cargo.toml b/encryption-demo-methods/guest/chacha20/Cargo.toml index fdf0bfb..b34a262 100644 --- a/encryption-demo-methods/guest/chacha20/Cargo.toml +++ b/encryption-demo-methods/guest/chacha20/Cargo.toml @@ -4,14 +4,13 @@ version = "0.1.0" edition = "2021" publish = false # Ensure staticlib for RISC-V -crate-type = ["staticlib"] [dependencies] # no_std stream cipher chacha20 = { version = "0.9", default-features = false } cipher = { version = "0.4", default-features = false } +risc0-zkvm = { version = "2.2.0", default-features = false, features = ['std'] } -risc0-zkvm-guest = "2.3.1" [features] default = ["alloc"] # pull in alloc inside guest diff --git a/encryption-demo-methods/guest/chacha20/src/main.rs b/encryption-demo-methods/guest/chacha20/src/main.rs index 1ab9c08..795c5ff 100644 --- a/encryption-demo-methods/guest/chacha20/src/main.rs +++ b/encryption-demo-methods/guest/chacha20/src/main.rs @@ -1,10 +1,6 @@ -#![no_std] -#![no_main] - use chacha20::cipher::{KeyIvInit, StreamCipher}; use chacha20::ChaCha20; use risc0_zkvm::guest::env; -risc0_zkvm_guest::entry!(main); pub fn main() { // Read 32-byte key @@ -14,14 +10,13 @@ pub fn main() { // Read plaintext length let len: u32 = env::read(); // Read plaintext bytes - let mut plaintext = vec![0u8; len as usize]; - env::read_slice(&mut plaintext).unwrap(); + let mut plaintext: Vec<_> = env::read(); // Encrypt in-place let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); cipher.apply_keystream(&mut plaintext); // Commit ciphertext - env::commit_slice(&plaintext); + env::commit(&plaintext); } 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/src/lib.rs b/encryption-demo/src/lib.rs index feaab80..9316124 100644 --- a/encryption-demo/src/lib.rs +++ b/encryption-demo/src/lib.rs @@ -4,7 +4,7 @@ use risc0_zkvm::{ default_prover, ExecutorEnv, Receipt, }; use encryption_demo_methods::{ - CHACHA20_ELF, CHACHA20_ID, XCHACHA20_ELF, XCHACHA20_ID, + CHACHA20_ELF, CHACHA20_ID }; /// Encrypt `plaintext` with ChaCha20 inside the zkVM. @@ -13,102 +13,20 @@ 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()?; + .write(key).unwrap() + .write(nonce).unwrap() + .write(&(plaintext.len() as u32)).unwrap() + .write(&plaintext).unwrap() + .build().unwrap(); 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)) + let prove_info = prover.prove(env, CHACHA20_ELF).unwrap(); + // receipt.verify(CHACHA20_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 index 1a4baf5..f2771b3 100644 --- a/encryption-demo/src/main.rs +++ b/encryption-demo/src/main.rs @@ -1 +1,2 @@ +fn main() {}