diff --git a/Cargo.lock b/Cargo.lock index 8593ddb..7247052 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1336,6 +1336,7 @@ version = "0.1.0" dependencies = [ "chacha20", "common", + "lz4_flex", "risc0-zkvm", ] @@ -1435,6 +1436,7 @@ dependencies = [ "chacha20", "common", "hex-literal 1.0.0", + "lz4_flex", "methods", "risc0-zkvm", "serde", @@ -1886,6 +1888,15 @@ version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" +[[package]] +name = "lz4_flex" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75761162ae2b0e580d7e7c390558127e5f01b4194debd6221fd8c207fc80e3f5" +dependencies = [ + "twox-hash", +] + [[package]] name = "lzma-sys" version = "0.1.20" @@ -3059,7 +3070,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.4.15", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 649d4b3..4bc527d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,8 @@ common = { path = "common" } chacha20 = "0.9.1" hex-literal = "1.0.0" +lz4_flex = "0.11" + risc0-zkvm = { version = "2.0", default-features = false, features = ['std'] } risc0-build = "2.0" tracing-subscriber = { version = "0.3", features = ["env-filter"] } diff --git a/host/Cargo.toml b/host/Cargo.toml index 0b049f8..98608d9 100644 --- a/host/Cargo.toml +++ b/host/Cargo.toml @@ -12,6 +12,7 @@ hex-literal.workspace = true risc0-zkvm = { workspace = true, default-features = true } tracing-subscriber.workspace = true serde.workspace = true +lz4_flex.workspace = true [features] cuda = ["risc0-zkvm/cuda"] diff --git a/host/src/main.rs b/host/src/main.rs index a59c700..b14f6d6 100644 --- a/host/src/main.rs +++ b/host/src/main.rs @@ -1,6 +1,7 @@ use chacha20::cipher::{KeyIvInit, StreamCipher}; use chacha20::ChaCha20; +use lz4_flex::decompress_size_prepended; // These constants represent the RISC-V ELF and the image ID generated by risc0-build. // The ELF is used for proving and the ID is used for verification. use methods::GUEST_ENCRYPT_ELF; @@ -74,7 +75,10 @@ fn main() { // decrypt ciphertext by applying keystream again cipher.apply_keystream(&mut output_buffer); - assert_eq!(output_buffer, plaintext); + let decompressed_output = + decompress_size_prepended(&output_buffer).expect("Unable to decompress output"); + + assert_eq!(decompressed_output, plaintext); println!("Decryption of zkVM ciphertext matches input!"); println!( "Output size to publish to DA = {} bytes (seal), {} bytes (ciphertext)", diff --git a/methods/guest/Cargo.toml b/methods/guest/Cargo.toml index f2964e3..0e98988 100644 --- a/methods/guest/Cargo.toml +++ b/methods/guest/Cargo.toml @@ -6,5 +6,7 @@ edition.workspace = true [dependencies] common.workspace = true +lz4_flex.workspace = true + risc0-zkvm = { workspace = true, default-features = false, features = ['std'] } chacha20.workspace = true diff --git a/methods/guest/src/main.rs b/methods/guest/src/main.rs index a6572da..ffe54c7 100644 --- a/methods/guest/src/main.rs +++ b/methods/guest/src/main.rs @@ -1,5 +1,6 @@ use chacha20::cipher::{KeyIvInit, StreamCipher}; use chacha20::ChaCha20; +use lz4_flex::compress_prepend_size; use risc0_zkvm::{ guest::env, sha::{Impl, Sha256}, @@ -25,15 +26,16 @@ fn main() { // TODO: // - Hash key and/or nonce & commit? + let mut compressed_input = compress_prepend_size(&buffer); // Key and IV must be references to the `GenericArray` type. // Here we use the `Into` trait to convert arrays into it. let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); // Write ciphertext into buffer by applying keystream to plaintext - cipher.apply_keystream(&mut buffer); + cipher.apply_keystream(&mut compressed_input); // write public output to the journal - env::commit_slice(&buffer); + env::commit_slice(&compressed_input); let end = env::cycle_count(); eprintln!("*-*-*-* CYCLE COUNT: {}", end - start);