Prove computation with the RISC Zero zkVM and verify the results in your Ethereum contract.
This repository implements an example application on Ethereum utilizing RISC Zero as a coprocessor to the smart contract application. It provides a starting point for building powerful new applications on Ethereum that offload work that is computationally intensive (i.e. gas expensive), or difficult to implement in Solidity (e.g. ed25519 signature verification, or HTML parsing).
Integrate with Steel to execute view calls and simulate transactions on Ethereum. Check out the ERC-20 counter demo to see an example.
Here is a simplified overview of how devs can integrate RISC Zero, with Bonsai proving, into their Ethereum smart contracts:
First, install Rust and Foundry, and then restart your terminal.
# Install Rust
curl https://sh.rustup.rs -sSf | sh
# Install Foundry
curl -L https://foundry.paradigm.xyz | bash
Next, you will use rzup
to install cargo-risczero
.
To install rzup
, run the following command and follow the instructions:
curl -L https://risczero.com/install | bash
Next we can install the RISC Zero toolchain by running rzup
:
rzup
You can verify the installation was successful by running:
cargo risczero --version
Now you have all the tools you need to develop and deploy an application with RISC Zero.
First, install the RISC Zero toolchain using the instructions above.
Now, you can initialize a new RISC Zero project at a location of your choosing:
forge init -t risc0/bonsai-foundry-template ./my-project
Congratulations! You've just started your first RISC Zero project.
Your new project consists of:
Update git submodules.
git submodule update --init
Builds for zkVM program, the publisher app, and any other Rust code.
cargo build
Build your Solidity smart contracts.
NOTE:
cargo build
needs to run first to generate theImageID.sol
contract.
forge build
Tests your zkVM program.
cargo test
Test your Solidity contracts, integrated with your zkVM program.
RISC0_DEV_MODE=true forge test -vvv
Run the same tests, with the full zkVM prover rather than dev-mode, by setting RISC0_DEV_MODE=false
.
RISC0_DEV_MODE=false forge test -vvv
Producing the Groth16 SNARK proofs for this test requires running on an x86 machine with Docker installed, or using Bonsai. Apple silicon is currently unsupported for local proving, you can find out more info in the relevant issues here and here.
To build your application using the RISC Zero Foundry Template, you’ll need to make changes in three main areas:
Note: To request an API key complete the form here.
With the Bonsai proving service, you can produce a Groth16 SNARK proof that is verifiable on-chain. You can get started by setting the following environment variables with your API key and associated URL.
export BONSAI_API_KEY="YOUR_API_KEY" # see form linked above
export BONSAI_API_URL="BONSAI_URL" # provided with your api key
Now if you run forge test
with RISC0_DEV_MODE=false
, the test will run as before, but will additionally use the fully verifying RiscZeroGroth16Verifier
contract instead of MockRiscZeroVerifier
and will request a SNARK receipt from Bonsai.
RISC0_DEV_MODE=false forge test -vvv
By setting the environment variable RISC0_USE_DOCKER
a containerized build process via Docker will ensure that all builds of your guest code, regardless of the machine or local environment, will produce the same image ID.
The image ID, and its importance to security, is explained in more detail in our developer FAQ.
RISC0_USE_DOCKER=1 cargo build
Note: This requires having Docker installed and in your PATH. To install Docker see Get Docker.
When you're ready, follow the deployment guide to get your application running on Sepolia or Ethereum Mainnet.
Below are the primary files in the project directory
.
├── Cargo.toml // Configuration for Cargo and Rust
├── foundry.toml // Configuration for Foundry
├── apps
│ ├── Cargo.toml
│ └── src
│ └── lib.rs // Utility functions
│ └── bin
│ └── publisher.rs // Example app to publish program results into your app contract
├── contracts
│ ├── EvenNumber.sol // Basic example contract for you to modify
│ └── ImageID.sol // Generated contract with the image ID for your zkVM program
├── methods
│ ├── Cargo.toml
│ ├── guest
│ │ ├── Cargo.toml
│ │ └── src
│ │ └── bin // You can add additional guest programs to this folder
│ │ └── is_even.rs // Example guest program for checking if a number is even
│ └── src
│ └── lib.rs // Compiled image IDs and tests for your guest programs
└── tests
├── EvenNumber.t.sol // Tests for the basic example contract
└── Elf.sol // Generated contract with paths the guest program ELF files.