1- use chacha20:: cipher:: { KeyIvInit , StreamCipher , StreamCipherSeek } ;
1+ use chacha20:: cipher:: { KeyIvInit , StreamCipher } ;
22use 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
1012fn 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}
0 commit comments