Skip to content

An efficient zk-proof model to estimate vault unitary value at a fixed gas cost regardless of number of tokens and positions owned by the vault

License

Notifications You must be signed in to change notification settings

RigoBlock/unitary-value-zk

Repository files navigation

unitary-value-zk

An efficient zero-knowledge proof system for RigoBlock vault unitary value calculation with constant gas cost verification.

License

Overview

This project uses SP1 and sp1-contract-call to prove RigoBlock vault unitary value offchain for later onchain verification across multiple blockchain networks.

Traditional onchain calculation of vault unitary value has gas costs that scale linearly with the number of tokens and positions held by the vault. This ZK proof system provides:

  • Constant Gas Cost: Proof verification costs the same regardless of vault complexity
  • Offchain Computation: Heavy calculations happen offchain in the zkVM
  • Cryptographic Guarantee: Zero-knowledge proofs ensure computation correctness
  • Scalability: Works efficiently for vaults with any number of tokens/positions
  • Multi-Chain Support: Works on Ethereum, Optimism, Base, Arbitrum, BNB Chain, and Unichain

Supported Chains

  • Ethereum Mainnet (Chain ID: 1)
  • Optimism (Chain ID: 10)
  • BNB Chain (Chain ID: 56)
  • Base (Chain ID: 8453)
  • Arbitrum One (Chain ID: 42161)
  • Unichain (Chain ID: 130)

See MULTI_CHAIN_GUIDE.md for detailed multi-chain usage instructions.

How It Works

The program simulates the following contract calls offchain:

  1. updateUnitaryValue() - Updates the vault's unitary value based on current holdings
  2. getPoolTokens() - Retrieves the updated unitary value and total supply

These calls are executed in SP1's zero-knowledge virtual machine, generating a succinct proof that can be verified onchain at constant cost.

Prerequisites

  • Rust (The project will automatically use the correct nightly version via rust-toolchain file)
  • SP1 - Install the SP1 toolchain with:
    curl -L https://sp1.succinct.xyz | bash
    sp1up
    This installs the succinct Rust toolchain required to build the zkVM client program.

Setup

  1. Clone the repository:
git clone https://github.com/RigoBlock/unitary-value-zk.git
cd unitary-value-zk
  1. Install SP1 toolchain (if not already installed):
curl -L https://sp1.succinct.xyz | bash
sp1up
  1. Copy the example environment file and configure it:
cp .env.example .env

Edit .env and set RPC URLs for the chains you want to use:

# Choose the chains you need
ETHEREUM_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR-API-KEY
OPTIMISM_RPC_URL=https://opt-mainnet.g.alchemy.com/v2/YOUR-API-KEY
BASE_RPC_URL=https://base-mainnet.g.alchemy.com/v2/YOUR-API-KEY
ARBITRUM_RPC_URL=https://arb-mainnet.g.alchemy.com/v2/YOUR-API-KEY
BNB_RPC_URL=https://bsc-dataseed.binance.org/
UNICHAIN_RPC_URL=https://unichain-rpc.example.com/

# Vault address
VAULT_ADDRESS=0x1234567890123456789012345678901234567890

Usage

Quick Start

Run the proof generator on your chosen chain:

# On Ethereum
cargo run --bin rigoblock-basic --release -- --chain ethereum

# On Optimism
cargo run --bin rigoblock-basic --release -- --chain optimism

# On Base
cargo run --bin rigoblock-basic --release -- --chain base

For detailed multi-chain usage, see MULTI_CHAIN_GUIDE.md.

Build the project

First, build the project (this will compile both the host and client programs):

cargo build --release

Note: The first build may take a while as it downloads and compiles dependencies, including SP1 circuits.

Execute without proof generation

This runs the program in the zkVM but doesn't generate a proof. Specify the chain:

cargo run --bin rigoblock-basic --release -- --chain optimism --vault-address $VAULT_ADDRESS

Generate and verify a proof

This generates a zero-knowledge proof of the computation:

cargo run --bin rigoblock-basic --release -- --chain base --vault-address $VAULT_ADDRESS --prove

The proof will be saved to host/fixtures/plonk-fixture.json.

Using environment variables

If you've set up the .env file with your chain RPC URLs, you can run:

# Execute without proving
cargo run --bin rigoblock-basic --release -- --chain optimism

# With proof generation
cargo run --bin rigoblock-basic --release -- --chain optimism --prove

Troubleshooting

SP1 toolchain not found

If you get an error about the succinct toolchain not being installed, make sure you've run:

curl -L https://sp1.succinct.xyz | bash
sp1up

Then restart your terminal or run source ~/.bashrc (or ~/.zshrc for zsh).

Network issues downloading SP1 circuits

If you encounter network issues downloading SP1 verification keys during build, you can:

  1. Ensure you have a stable internet connection
  2. Try building again - the downloader will retry failed downloads
  3. Check the SP1 documentation for alternative installation methods

Project Structure

  • client/: The zkVM program that runs inside the SP1 zkVM
  • host/: The host program that orchestrates the proof generation
    • src/basic.rs: Main program for proof generation
    • src/onchain_verify.rs: Program for testing onchain proof verification
  • host/fixtures/: Generated proofs are saved here
  • contracts/: Solidity contracts for onchain verification
    • src/RigoBlockVaultVerifier.sol: Main verifier contract for vault proofs

How it works

Architecture

The project consists of two main components:

  1. Client Program (client/src/main.rs):

    • Runs inside the SP1 zkVM (zero-knowledge virtual machine)
    • Receives an EVM state sketch from the host
    • Validates the EVM state against the provided state root
    • Simulates the following contract calls:
      1. updateUnitaryValue() - Updates the vault's unitary value calculation
      2. getPoolTokens() - Retrieves the updated unitary value and total supply
    • Commits the output for verification
  2. Host Program (host/src/basic.rs):

    • Connects to an Ethereum RPC endpoint
    • Prepares an EVM state sketch at a specific block
    • Simulates the same contract calls offchain to verify correctness
    • Feeds the state sketch into the zkVM client
    • Generates a zero-knowledge proof of the computation
    • Verifies the proof and extracts public values

Proof Generation Flow

┌─────────────────┐
│  Ethereum RPC   │
└────────┬────────┘
         │
         │ Fetch state at block
         ▼
┌─────────────────┐
│  Host Executor  │ 1. Call updateUnitaryValue()
│                 │ 2. Call getPoolTokens()
│                 │ 3. Build EVM state sketch
└────────┬────────┘
         │
         │ State sketch
         ▼
┌─────────────────┐
│   SP1 zkVM      │ 1. Validate state root
│  (Client Code)  │ 2. Execute calls in zkVM
│                 │ 3. Commit outputs
└────────┬────────┘
         │
         │ Public values
         ▼
┌─────────────────┐
│  Proof Generator│ Generate ZK proof
└────────┬────────┘
         │
         ▼
   plonk-fixture.json

Why ZK Proofs?

Traditional onchain calculation of vault unitary value has gas costs that scale with the number of tokens and positions. By using ZK proofs:

  • Gas cost is constant regardless of vault complexity
  • Computation happens offchain
  • Onchain verification is minimal and efficient
  • The proof cryptographically guarantees the computation was done correctly

Onchain Verification

The project includes Solidity contracts for verifying proofs onchain. The RigoBlockVaultVerifier contract can be used via delegatecall from a vault to verify proofs in the vault's own context.

Key Features

  • Vault-Specific Verification: Ensures proofs are generated for the specific vault
  • Chain ID Validation: Verifies proofs are for the correct blockchain
  • Block Recency Checks: Validates that proofs are recent enough to be trusted (within 256 blocks by default)
  • Delegatecall Support: Can be called from the vault itself

Deploying the Verifier Contract

See the contracts README for detailed deployment instructions.

Testing Onchain Verification

cargo run --bin rigoblock-onchain-verify --release -- \
    --chain-id 11155111 \
    --vault-address 0x... \
    --block-number 12345678 \
    --verifier-contract 0x... \
    --active-fork-name cancun

License

Apache-2.0

About

An efficient zk-proof model to estimate vault unitary value at a fixed gas cost regardless of number of tokens and positions owned by the vault

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •