Skip to content

Quickstart

In this section we will build and run a fibonacci program.

Setup

First, create a new Rust project.

cargo openvm init fibonacci

This will generate an OpenVM-specific starter package. Notice Cargo.toml has the following dependency:

[dependencies]
openvm = { git = "https://github.com/openvm-org/openvm.git", tag = "v1.4.0", features = ["std"] }

Note that std is not enabled by default, so explicitly enabling it is required.

The fibonacci program

The read function takes input from the stdin (it also works with OpenVM runtime).

// src/main.rs
use openvm::io::{read, reveal_u32};
 
fn main() {
    let n: u64 = read();
    let mut a: u64 = 0;
    let mut b: u64 = 1;
    for _ in 0..n {
        let c: u64 = a.wrapping_add(b);
        a = b;
        b = c;
    }
    reveal_u32(a as u32, 0);
    reveal_u32((a >> 32) as u32, 1);
}

Build

To build the program, run:

cargo openvm build

This will output:

  • an RV32IM ELF containing custom OpenVM instructions at ./target/riscv32im-risc0-zkvm-elf/release/fibonacci
  • an OpenVM executable binary at ./target/openvm/release/fibonacci.vmexe

Keygen

Before generating proofs, we will also need to generate the proving and verification keys using:

cargo openvm keygen

This will output a serialized proving key to ./target/openvm/app.pk and a verification key to ./target/openvm/app.vk.

Proof Generation

Now we are ready to generate a proof! Simply run:

cargo openvm prove app --input "0x010A00000000000000"

The --input field is passed to the program which receives it via the io::read function. In our main.rs we called read() to get n: u64. The input here is n = 10u64 in little endian. Note that this value must be padded to exactly 8 bytes (64 bits) and is prefixed with 0x01 to indicate that the input is composed of raw bytes.

The serialized proof will be output to ./fibonacci.app.proof.

Proof Verification

Finally, the proof can be verified.

cargo openvm verify app

The process should exit with no errors.

Runtime Execution

The OpenVM binary can also be executed without generating a proof, which can be useful for testing purposes.

cargo openvm run --input "0x010A00000000000000"