Skip to content

Commit be9ff15

Browse files
committed
prove, use DEV_MODE for testing/debug
1 parent fa72774 commit be9ff15

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ To build all methods and execute the method within the zkVM, run the following
44
command:
55

66
```bash
7-
cargo run
7+
# Run full proof
8+
cargo r -r
9+
10+
# Execution ONLY testing
11+
RISC0_DEV_MODE=1 cargo r
812
```
913

1014
### Executing the Project Locally in Development Mode
@@ -14,6 +18,6 @@ During development, faster iteration upon code changes can be achieved by levera
1418
Put together, the command to run your project in development mode while getting execution statistics is:
1519

1620
```bash
17-
RUST_LOG="[executor]=info" RISC0_DEV_MODE=1 cargo run
21+
RUST_LOG="info" RISC0_DEV_MODE=1 cargo r
1822
```
1923

host/src/main.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use chacha20::cipher::{KeyIvInit, StreamCipher};
1+
use chacha20::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};
22
use chacha20::ChaCha20;
33
use hex_literal::hex;
44

55
// These constants represent the RISC-V ELF and the image ID generated by risc0-build.
66
// The ELF is used for proving and the ID is used for verification.
77
use methods::{GUEST_ENCRYPT_ELF, GUEST_ENCRYPT_ID};
8-
use risc0_zkvm::{default_executor, ExecutorEnv};
8+
use risc0_zkvm::{default_prover, ExecutorEnv};
99

1010
fn main() {
1111
// Initialize tracing. In order to view logs, run `RUST_LOG=info cargo run`
@@ -40,26 +40,32 @@ fn main() {
4040
.unwrap();
4141

4242
// testing Execution ONLY
43-
let prover = default_executor();
43+
let prover = default_prover();
4444

4545
// Proof information by proving the specified ELF binary.
4646
// This struct contains the receipt along with statistics about execution of the guest
47-
let prove_info = prover.execute(env, GUEST_ENCRYPT_ELF).unwrap();
47+
let prove_info = prover.prove(env, GUEST_ENCRYPT_ELF).unwrap();
4848

4949
// extract the receipt.
50-
// let receipt = prove_info.receipt;
50+
let receipt = prove_info.receipt;
5151

52-
// let output: [u8; 16] = receipt.journal.decode().unwrap();
53-
54-
let output: [u8; 16] = prove_info.journal.decode().unwrap();
55-
println!("output bytes: {:?}", output);
52+
let mut output: [u8; 16] = receipt.journal.decode().unwrap();
53+
println!("zkVM output bytes: {:?}", output);
5654

5755
let ciphertext = hex!("e405626e 4f1236b3 670ee428 332ea20e");
5856
println!("expected output bytes: {:?}", ciphertext);
5957

60-
// // The receipt was verified at the end of proving, but the below code is an
61-
// // example of how someone else could verify this receipt.
62-
// receipt
63-
// .verify(GUEST_ENCRYPT_ID)
64-
// .unwrap();
58+
// The receipt was verified at the end of proving, but the below code is an
59+
// example of how someone else could verify this receipt.
60+
receipt
61+
.verify(GUEST_ENCRYPT_ID)
62+
.unwrap();
63+
println!("Reciept from zkVM OK!");
64+
65+
// ChaCha ciphers support seeking
66+
cipher.seek(0u32);
67+
68+
// decrypt ciphertext by applying keystream again
69+
cipher.apply_keystream(&mut output);
70+
assert_eq!(output, plaintext);
6571
}

methods/guest/src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use risc0_zkvm::guest::env;
22

33
fn main() {
4-
// TODO: Implement your guest code here
5-
6-
// read the input
7-
84
let plaintext: [u8; 16] = env::read();
95
let keystream: [u8; 16] = env::read();
6+
7+
// TODO:
8+
// - Hash plaintext & commit?
9+
// - Hash keystream & commit?
1010

1111
let mut buffer = plaintext.clone();
1212
xor_arrays(&plaintext,&keystream, &mut buffer);
@@ -15,13 +15,15 @@ fn main() {
1515
env::commit(&buffer);
1616
}
1717

18+
/// UNSAFE: We demand input and output arrays here are the SAME length.
1819
fn xor_arrays(arr1: &[u8], arr2: &[u8], output: &mut [u8]) {
1920
// assert_eq!(arr1.len(), arr2.len(), "Arrays must have the same length");
2021
// assert_eq!(arr1.len(), output.len(), "Output array must have the same length");
2122

2223
for i in 0..arr1.len() {
24+
// We assume that input arrays are the same length, skipping checks to save cycles
2325
unsafe {
2426
*output.get_unchecked_mut(i) = arr1.get_unchecked(i) ^ arr2.get_unchecked(i);
2527
}
2628
}
27-
}
29+
}

0 commit comments

Comments
 (0)