Quickstart

In this section, we will show you how to create a simple Fibonacci program using the SP1 zkVM.

Create Project

The first step is to create a new project using the cargo prove new <name> command. This command will create a new folder in your current directory.

cargo prove new fibonacci
cd fibonacci

This will create a new project with the following structure:

.
├── program
│   ├── Cargo.toml
│   ├── elf
│   └── src
│       └── main.rs
└── script
    ├── Cargo.toml
    └── src
        └── main.rs

6 directories, 4 files

There are 2 directories (each a crate) in the project:

  • program: the source code that will be proven inside the zkVM.
  • script: code that contains proof generation and verification code.

We recommend you install the rust-analyzer extension. Note that if you use cargo prove new inside a monorepo, you will need to add the manifest file to rust-analyzer.linkedProjects to get full IDE support.

Generate Proof

The program simply computes the n-th Fibonacci number.

Before we can run the program inside the zkVM, it must be compiled to a RISCV executable using the succinct Rust toolchain. This is called an ELF (Executable and Linkable Format). The build.rs file in the script directory will use the cargo prove toolchain to automatically build the ELF.

To generate a proof, we take the ELF file generated by the build.rs file and execute it within the SP1 zkVM. The code in the script directory is already scaffolded with a script that has logic to generate a proof, save the proof to disk, and verify it.

cd script
cargo run --release

The output should show

...
    Compiling fibonacci-script v0.1.0 (.../fibonacci/script)
    Finished release [optimized] target(s) in 26.14s
    Running `target/release/fibonacci-script`
a: 205697230343233228174223751303346572685
b: 332825110087067562321196029789634457848
successfully generated and verified proof for the program!

The program by default is quite small, so proof generation will only take a few seconds locally. After it completes, the proof will be saved in the proof-with-io.bin file and also be verified for correctness.

Modifying the Program

You can play around with how many rounds of Fibonacci are executed by playing around with n (by default set to 186) in the file script/src/main.rs. Integer overflow will cause larger n to result in non-fibonacci output, although the proof will still be generated and verified.

The ELF will be automatically rebuilt every time you modify the program. You can verify that the ELF was re-generated by looking in the elf directory and for a file called riscv32im-succinct-zkvm-elf:

ls elf # should show riscv32im-succinct-zkvm-elf