This project demonstrates how to use the RISC Zero zero-knowledge prover with Solana. It provides an example Solana program that implements a simple nonce increment system where users must provide valid ZK proofs to increment a nonce value on-chain.
The project consists of several key components:
-
Solana Programs: Smart contracts that handle on-chain verification and state management
verifier_router
: Routes proof verification requests to appropriate verifiersgroth16_verifier
: The RISC Zero Groth16 verifier that performs the proof verificationsolana-examples
: Example program that uses the data from a RISC Zero Proof
-
RISC Zero Guest Program: Off-chain program that generates proofs
- Located in
hello-world/methods/guest
- Implements nonce increment logic that will be proven
- Located in
-
Host Program: Rust program that acts as the Solana client and submits data for proving
- Located in
hello-world/host
- Initializes the Solana example program account on first run
- Handles proof generation and submission to Solana
- Located in
- Rust and Cargo
- Solana Tools
- Anchor Framework
- Node.js and Yarn
- Configure Local Environment:
# Configure Solana for localhost
solana config set --url localhost
# Generate a new keypair if needed
solana-keygen new
- Start Local Validator:
# Run this in a separate terminal
solana-test-validator -r
- Build and Deploy the Verifier Router System:
# Go to the Solana Verifier programs to deploy them locally
cd solana-verifier
# Install the dependencies for the scripts
yarn install
# Sync keys for local deploy
anchor keys sync
# Build the programs so that IDLs are generated
anchor build
# Generate client code for scripts before first run
yarn run client
# Deploy the programs locally
yarn run deploy
- Build and Deploy the Example Project:
# Go to the Solana Examples directory for everything else
cd solana-examples
# Sync keys for local deploy
anchor keys sync
# Build the program
anchor build
# Deploy the program locally
anchor deploy
# Go to the Client program directory
cd hello-world
# Run the example program
RUST_LOG=info cargo run
This will:
- Generate a fresh keypair and request an airdrop (to demonstrate anyone can submit proofs)
- Initialize the Solana example program on the first run
- Generate a ZK proof for incrementing the nonce
- Submit the proof to the Solana program
- Verify the proof on-chain
- Update the nonce if verification succeeds
-
Initialization
- The Solana program initializes with a starting nonce of 0
- It stores the RISC Zero image ID to ensure proofs come from an unmodified program
-
Proof Generation
- User requests to increment nonce
- Host program loads current nonce from Solana
- Generates proof using RISC Zero guest program
- Proof demonstrates user of the signing account has run the guest program
-
On-chain Verification
- Host submits proof to Solana program
- Program validates:
- Proof is for correct image ID
- Nonce value is incremented correctly
- Sender's public key matches proof
- If valid, nonce is incremented
Handles on-chain state and verification logic:
- Stores current nonce and program image ID
- Verifies proofs through Verifier Router
- Updates state when proofs are valid
Simple program that:
- Takes current nonce as input
- Increments nonce value
- Outputs incremented value and public key into the proofs journal
Orchestrates the proof system:
- Initializes the Solana example program if not currently initialized
- Reads current state from Solana
- Generates proofs using guest program
- Submits proofs to Solana program
- The Verifier Router system must be deployed before the example project
- Use
solana logs
to monitor program output during testing - Program IDs are deterministic based on
Anchor.toml
configuration
If you encounter issues:
- Verify local validator is running:
solana cluster-version
- Check account balance:
solana balance
- Reset local validator if needed:
solana-test-validator -r
- Verify program deployment:
solana program show --programs --all