Skip to content

zk-examples/zk-starknet-examples

Repository files navigation

zk-starknet-examples

Examples of zk-SNARK verification on Starknet using Garaga.

Description

This repository demonstrates the process of verifying Groth16 proofs on Starknet:

  1. Using arkworks-examples repository: Import verification key and proof in JSON format from the arkworks-examples submodule
  2. Verifier contract generation: Use Garaga to generate a Cairo Groth16 verifier contract
  3. Fork testing on Sepolia: Tests use Sepolia testnet fork to access pre-declared garaga contracts

Installation

# Clone the repository with submodules
git clone --recurse-submodules https://github.com/zk-examples/zk-starknet-examples.git

# Install garaga (requires Python 3.10+)
pip install garaga

Usage

Arkworks

Step 1: Generate verifier contract

Using the verification key from arkworks-examples, generate a Cairo Groth16 verifier contract:

garaga gen --system groth16 --vk arkworks-examples/merkle/export/verification_key.json

This command will create the verifier contract in the merkle/src/ directory.

Step 2: Generate calldata for tests

Generate calldata for snforge tests from the verification key and proof:

cd merkle

garaga calldata \
  --system groth16 \
  --vk ../arkworks-examples/merkle/export/verification_key.json \
  --proof ../arkworks-examples/merkle/export/proof.json \
  --format snforge \
  --output-path tests

This command will create the tests/proof_calldata.txt file with verification data.

Step 3: Run tests

Run tests with Sepolia testnet fork:

snforge test

Make sure the SEPOLIA_RPC_URL environment variable is set before running the tests.

Gnark

Gnark examples use verification keys and proofs from the gnark/ directory for BLS12_381 and BN254 curves.

Step 1: Generate verifier contracts

Generate Cairo Groth16 verifier contracts for both curves:

garaga gen --system groth16 --vk gnark-to-snarkjs/test/cubic/keys/verification_key_gnark_bls12381.json
garaga gen --system groth16 --vk gnark-to-snarkjs/test/cubic/keys/verification_key_gnark_bn254.json

Step 2: Generate calldata for tests

BLS12_381:

cd gnark_bls12_381

garaga calldata \
  --system groth16 \
  --vk ../gnark-to-snarkjs/test/cubic/keys/verification_key_gnark_bls12381.json \
  --proof ../gnark-to-snarkjs/test/cubic/proofs/proof_gnark_bls12381.json \
  --public-inputs ../gnark-to-snarkjs/test/cubic/proofs/public.json \
  --format snforge \
  --output-path tests

cd ..

BN254:

cd gnark_bn254

garaga calldata \
  --system groth16 \
  --vk ../gnark-to-snarkjs/test/cubic/keys/verification_key_gnark_bn254.json \
  --proof ../gnark-to-snarkjs/test/cubic/proofs/proof_gnark_bn254.json \
  --public-inputs ../gnark-to-snarkjs/test/cubic/proofs/public.json \
  --format snforge \
  --output-path tests

cd ..

Step 3: Run tests

Run tests for each project:

source .evn

cd gnark_bls12_381 && snforge test && cd ..
cd gnark_bn254 && snforge test

Make sure the SEPOLIA_RPC_URL environment variable is set before running the tests.

About

Examples of using zero-knowledge proofs on Starknet with Garaga

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages