aboutsummaryrefslogtreecommitdiff
path: root/zkvms/risc0/host/src/main.rs
blob: c214c4e813e5c296e14b0491b0664774f992d107 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use zkvms_host_io::{Input, Output, foreach_input_field, benchmarkable, read_args, RunType::{ Execute, Prove, Verify }};
use risc0_zkvm::{default_prover, default_executor, ExecutorEnv, Receipt};
use risc0_zkp::core::digest::Digest;
use hex::FromHex;

// https://github.com/risc0/risc0/blob/881e512732eca72849b2d0e263a1242aba3158af/risc0/build/src/lib.rs#L280-L284
static HELLO_GUEST_ELF: &[u8] = include_bytes!("./guest");
// https://github.com/risc0/risc0/blob/881e512732eca72849b2d0e263a1242aba3158af/risc0/build/src/lib.rs#L255
// Generated by Nix, see zkvms/risc0/default.nix
static HELLO_GUEST_ID: &str = env!("GUEST_ID");

fn build_env(input: &Input) -> ExecutorEnv {
    let mut builder = ExecutorEnv::builder();
    foreach_input_field!{
        builder.write(&input.yield).unwrap();
    }
    builder.build().unwrap()
}

fn prove(env: ExecutorEnv) -> Receipt {
    default_prover()
        .prove(env, HELLO_GUEST_ELF)
        .expect("Error occured")
        .receipt
}

fn journal(receipt: Receipt) -> Output {
    receipt
        .journal
        .decode()
        .unwrap()
}

fn main() {
    let run_info = read_args();

    match run_info.run_type {
        Execute => benchmarkable! {
            // ExecutorEnv does not derive Clone
            let env = build_env(&run_info.input);
            let exec = default_executor();

            let output = default_executor()
                .execute(env, HELLO_GUEST_ELF)
                .unwrap()
                .receipt_claim
                .unwrap()
                .output
                .value()
                .unwrap();
            println!("{:#?}", output);
        },
        Prove => benchmarkable! {
            // ExecutorEnv does not derive Clone
            let env = build_env(&run_info.input);
            let receipt = prove(env);
            println!("Output from journal: {:?}", journal(receipt));
        },
        Verify => benchmarkable!{
            // https://github.com/risc0/risc0/blob/881e512732eca72849b2d0e263a1242aba3158af/risc0/build/src/lib.rs#L197-L199
            let guest_id: Digest = Digest::from_hex(HELLO_GUEST_ID).unwrap();

            // ExecutorEnv does not derive Clone
            let env = build_env(&run_info.input);
            let receipt = prove(env);

            let receipt = receipt.clone();
            receipt.verify(guest_id.clone()).unwrap();
            println!("Output from verify: {:?}", journal(receipt));
        },
    }
}