Skip to content

Commit fdd90cf

Browse files
committed
MVP working lz4 compression workflow - adds ~50% overhead on cycles
1 parent 3c61fd4 commit fdd90cf

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

Cargo.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ common = { path = "common" }
1515
chacha20 = "0.9.1"
1616
hex-literal = "1.0.0"
1717

18+
lz4_flex = "0.11"
19+
1820
risc0-zkvm = { version = "2.0", default-features = false, features = ['std'] }
1921
risc0-build = "2.0"
2022
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

host/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ hex-literal.workspace = true
1212
risc0-zkvm = { workspace = true, default-features = true }
1313
tracing-subscriber.workspace = true
1414
serde.workspace = true
15+
lz4_flex.workspace = true
1516

1617
[features]
1718
cuda = ["risc0-zkvm/cuda"]

host/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use chacha20::cipher::{KeyIvInit, StreamCipher};
22
use chacha20::ChaCha20;
33

4+
use lz4_flex::decompress_size_prepended;
45
// These constants represent the RISC-V ELF and the image ID generated by risc0-build.
56
// The ELF is used for proving and the ID is used for verification.
67
use methods::GUEST_ENCRYPT_ELF;
@@ -74,12 +75,15 @@ fn main() {
7475
// decrypt ciphertext by applying keystream again
7576
cipher.apply_keystream(&mut output_buffer);
7677

77-
assert_eq!(output_buffer, plaintext);
78+
let decompressed_output =
79+
decompress_size_prepended(&output_buffer).expect("Unable to decompress output");
80+
81+
assert_eq!(decompressed_output, plaintext);
7882
println!("Decryption of zkVM ciphertext matches input!");
7983
println!(
8084
"Output size to publish to DA = {} bytes (seal), {} bytes (ciphertext)",
8185
receipt.seal_size(),
82-
output_buffer.len()
86+
decompressed_output.len()
8387
);
8488
}
8589

methods/guest/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ edition.workspace = true
66
[dependencies]
77
common.workspace = true
88

9+
lz4_flex.workspace = true
10+
911
risc0-zkvm = { workspace = true, default-features = false, features = ['std'] }
1012
chacha20.workspace = true

methods/guest/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use chacha20::cipher::{KeyIvInit, StreamCipher};
22
use chacha20::ChaCha20;
3+
use lz4_flex::compress_prepend_size;
34
use risc0_zkvm::{
45
guest::env,
56
sha::{Impl, Sha256},
@@ -25,15 +26,16 @@ fn main() {
2526
// TODO:
2627
// - Hash key and/or nonce & commit?
2728

29+
let mut compressed_input = compress_prepend_size(&buffer);
2830
// Key and IV must be references to the `GenericArray` type.
2931
// Here we use the `Into` trait to convert arrays into it.
3032
let mut cipher = ChaCha20::new(&key.into(), &nonce.into());
3133

3234
// Write ciphertext into buffer by applying keystream to plaintext
33-
cipher.apply_keystream(&mut buffer);
35+
cipher.apply_keystream(&mut compressed_input);
3436

3537
// write public output to the journal
36-
env::commit_slice(&buffer);
38+
env::commit_slice(&compressed_input);
3739

3840
let end = env::cycle_count();
3941
eprintln!("*-*-*-* CYCLE COUNT: {}", end - start);

0 commit comments

Comments
 (0)