Skip to content

Commit 3ad7f11

Browse files
authored
Upgrade risczero v2.0 (#1)
* feat: upgrade to risczero 2.0, move to workspace deps * feat: read slice for perf, print hex hash for debug * 3MB input data testing (guest stack overflows?) * feat: crate for shared constants --------- Signed-off-by: Nuke <[email protected]>
1 parent 03add14 commit 3ad7f11

File tree

11 files changed

+692
-196
lines changed

11 files changed

+692
-196
lines changed

Cargo.lock

Lines changed: 586 additions & 140 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
[workspace]
22
resolver = "2"
3-
members = ["demo-ChaCha","host", "methods"]
3+
members = [ "common","demo-ChaCha","host", "methods", "methods/guest"]
44
default-members = ["host"]
55

6+
[workspace.package]
7+
version = "0.1.0"
8+
edition = "2021"
9+
10+
[workspace.dependencies]
11+
methods = { path = "methods" }
12+
host = { path = "host" }
13+
common = { path = "common" }
14+
15+
chacha20 = "0.9.1"
16+
hex-literal = "1.0.0"
17+
18+
risc0-zkvm = { version = "2.0", default-features = false, features = ['std'] }
19+
risc0-build = "2.0"
20+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
21+
serde = "1.0"
22+
623
# Always optimize; building and running the guest takes much longer without optimization.
724
[profile.dev]
825
opt-level = 3

common/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "common"
3+
version.workspace = true
4+
edition.workspace = true
5+
6+
[dependencies]

common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub const INPUT_BYTES_LENGTH: usize = 300_000;

demo-ChaCha/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "demo-ChaCha"
3-
version = "0.1.0"
4-
edition = "2024"
3+
version.workspace = true
4+
edition.workspace = true
55

66
[dependencies]
7-
chacha20 = "0.9.1"
8-
hex-literal = "1.0.0"
7+
chacha20.workspace = true
8+
hex-literal.workspace = true

demo-ChaCha/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use chacha20::ChaCha20;
21
use chacha20::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};
2+
use chacha20::ChaCha20;
33
use hex_literal::hex;
44

55
fn main() {

host/Cargo.toml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[package]
22
name = "host"
3-
version = "0.1.0"
4-
edition = "2021"
3+
version.workspace = true
4+
edition.workspace = true
55

66
[dependencies]
7-
chacha20 = "0.9.1"
8-
hex-literal = "1.0.0"
7+
methods.workspace = true
8+
common.workspace = true
99

10-
methods = { path = "../methods" }
11-
risc0-zkvm = { version = "1.2.5" }
12-
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
13-
serde = "1.0"
10+
chacha20.workspace = true
11+
hex-literal.workspace = true
12+
risc0-zkvm = { workspace = true, default-features = true }
13+
tracing-subscriber.workspace = true
14+
serde.workspace = true

host/src/main.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use chacha20::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};
1+
use chacha20::cipher::{KeyIvInit, StreamCipher};
22
use chacha20::ChaCha20;
3-
use hex_literal::hex;
43

54
// These constants represent the RISC-V ELF and the image ID generated by risc0-build.
65
// The ELF is used for proving and the ID is used for verification.
7-
use methods::{GUEST_ENCRYPT_ELF, GUEST_ENCRYPT_ID};
8-
use risc0_zkvm::{default_prover, sha::Digest, ExecutorEnv};
6+
use methods::GUEST_ENCRYPT_ELF;
7+
use risc0_zkvm::sha::rust_crypto::{Digest, Sha256};
8+
use risc0_zkvm::{default_prover, ExecutorEnv};
9+
10+
use common::INPUT_BYTES_LENGTH;
911

1012
fn main() {
1113
// Initialize tracing. In order to view logs, run `RUST_LOG=info cargo run`
@@ -19,17 +21,13 @@ fn main() {
1921
let nonce = [0x24; 12];
2022

2123
// 16 byte test
22-
let plaintext = hex!("00010203 04050607 08090A0B 0C0D0E0F");
23-
println!("Plaintext bytes: {:?}", plaintext);
24+
let plaintext = [0; INPUT_BYTES_LENGTH];
2425

2526
// zkVM
2627
let env = ExecutorEnv::builder()
27-
.write(&key)
28-
.unwrap()
29-
.write(&nonce)
30-
.unwrap()
31-
.write(&plaintext)
32-
.unwrap()
28+
.write_slice(&key)
29+
.write_slice(&nonce)
30+
.write_slice(&plaintext)
3331
.build()
3432
.unwrap();
3533

@@ -43,26 +41,43 @@ fn main() {
4341
// extract the receipt.
4442
let receipt = prove_info.receipt;
4543

46-
let mut output: (Digest, [u8; 16]) = receipt.journal.decode().unwrap();
47-
println!("zkVM -> plaintext hash: {:?}", output.0);
48-
println!("zkVM -> ciphertext bytes: {:?}", output.1);
44+
let output = receipt.journal.bytes.clone();
45+
// sha256 = 16 bytes committed first
46+
let output_digest: [u8; 32] = output[..32].try_into().expect("sha256 hash reading erorr");
47+
// Ciphertext is the rest of the journal bytes
48+
let mut output_buffer: [u8; INPUT_BYTES_LENGTH] = output[32..]
49+
.try_into()
50+
.expect("Ciphertext unable to populate buffer");
51+
52+
println!(
53+
"zkVM -> plaintext hash: 0x{}",
54+
bytes_to_hex(&output_digest)
55+
);
4956

50-
let ciphertext = hex!("e405626e 4f1236b3 670ee428 332ea20e");
51-
println!("expected output bytes: {:?}", ciphertext);
57+
// Check against the input
58+
let input_plaintext_digest = Sha256::digest(&plaintext);
59+
println!(
60+
"Input -> plaintext hash: 0x{}",
61+
bytes_to_hex(&input_plaintext_digest)
62+
);
5263

53-
// The receipt was verified at the end of proving, but the below code is an
54-
// example of how someone else could verify this receipt.
55-
receipt.verify(GUEST_ENCRYPT_ID).unwrap();
56-
println!("Reciept from zkVM OK!");
64+
let ciphertext_digest = Sha256::digest(&output_buffer);
65+
println!(
66+
"zkVM -> ciphertext hash: 0x{}",
67+
bytes_to_hex(&ciphertext_digest)
68+
);
5769

5870
// Key and IV must be references to the `GenericArray` type.
5971
// Here we use the `Into` trait to convert arrays into it.
6072
let mut cipher = ChaCha20::new(&key.into(), &nonce.into());
6173

62-
// ChaCha ciphers support seeking
63-
cipher.seek(0u32);
64-
6574
// decrypt ciphertext by applying keystream again
66-
cipher.apply_keystream(&mut output.1);
67-
assert_eq!(output.1, plaintext);
75+
cipher.apply_keystream(&mut output_buffer);
76+
assert_eq!(output_buffer, plaintext);
77+
println!("Decryption of zkVM ciphertext matches input!")
78+
}
79+
80+
fn bytes_to_hex(bytes: &[u8]) -> String {
81+
let digest_hex: String = bytes.iter().map(|b| format!("{:02x}", b)).collect();
82+
digest_hex
6883
}

methods/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "methods"
3-
version = "0.1.0"
4-
edition = "2021"
3+
version.workspace = true
4+
edition.workspace = true
55

66
[build-dependencies]
7-
risc0-build = { version = "1.2.5" }
7+
risc0-build.workspace = true
88

99
[package.metadata.risc0]
1010
methods = ["guest"]

methods/guest/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "guest_encrypt"
3-
version = "0.1.0"
4-
edition = "2021"
5-
6-
[workspace]
3+
version.workspace = true
4+
edition.workspace = true
75

86
[dependencies]
9-
risc0-zkvm = { version = "1.2.5", default-features = false, features = ['std'] }
10-
chacha20 = "0.9.1"
7+
common.workspace = true
8+
9+
risc0-zkvm = { workspace = true, default-features = false, features = ['std'] }
10+
chacha20.workspace = true

0 commit comments

Comments
 (0)