aboutsummaryrefslogtreecommitdiff
path: root/zkvms/sp1/host/src/main.rs
blob: a3952478cff887fcfa79d12a7322252461b0d1fd (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
use sp1_sdk::{EnvProver, ProverClient, SP1ProofWithPublicValues, SP1Stdin, SP1VerifyingKey};
use zkvms_host_io::{
    benchmarkable, foreach_input_field, read_args, Input,
    RunType::{Execute, Prove, Verify},
    output_proof_size,
};

/// The ELF (executable and linkable format) file for the Succinct RISC-V zkVM.
pub const FIBONACCI_ELF: &[u8] = include_bytes!("./guest");

fn build_stdin(input: &Input) -> SP1Stdin {
    let mut stdin = SP1Stdin::new();
    foreach_input_field! {
        stdin.write(&input.yield);
    }
    stdin
}

fn prove(client: &EnvProver, stdin: SP1Stdin) -> (SP1ProofWithPublicValues, SP1VerifyingKey) {
    let (pk, vk) = client.setup(FIBONACCI_ELF);
    let proof = client
        .prove(&pk, &stdin)
        .run()
        .expect("failed to generate proof");
    (proof, vk)
}

fn main() {
    let run_info = read_args();
    let stdin = build_stdin(&run_info.input);

    sp1_sdk::utils::setup_logger();
    let client = ProverClient::new();

    match run_info.run_type {
        Execute => benchmarkable! {
            let (output, report) = client.execute(FIBONACCI_ELF, &stdin).run().unwrap();

            println!("Program executed successfully.");
            println!("{:?}", output);
            println!("Number of cycles: {}", report.total_instruction_count());
        },
        Prove => benchmarkable! {
            let (proof, _) = prove(&client, stdin.clone());

            output_proof_size(&proof);

            println!("Successfully generated proof!");
        },
        Verify => {
            let (proof, vk) = prove(&client, stdin.clone());
            println!("Successfully generated proof!");

            output_proof_size(&proof);

            benchmarkable! {
                client.verify(&proof, &vk).expect("failed to verify proof");
                println!("Successfully verified proof!");
            }
        }
    }
}