5 releases

new 0.1.5 Dec 7, 2024
0.1.4 Dec 7, 2024
0.1.3 Sep 9, 2024

#563 in Magic Beans

Download history 276/week @ 2024-09-09 30/week @ 2024-09-16 82/week @ 2024-09-30 1/week @ 2024-10-07 228/week @ 2024-12-02

228 downloads per month

MIT license

28KB
368 lines

spawn-zk-snarks

spawn-zk-snarks is a Rust library that provides a robust implementation of Zero-Knowledge Proofs (ZKPs) using the Groth16 proving system. Built on top of the arkworks ecosystem, it offers both high performance and EVM compatibility.

Features

  • Groth16 Proving System

    • Generate proving and verification keys
    • Create verifiable proofs for given witnesses
    • Verify proofs with the generated verification keys
    • Efficient constraint system implementation
  • Circuit Implementation

    • Arithmetic circuit support
    • Constraint generation
    • Witness computation
    • Flexible circuit configuration
  • EVM Compatibility

    • Generate Solidity verifier contracts
    • EVM-compatible proof format
    • Gas cost estimation
    • Optimized for Ethereum deployment
  • Performance

    • Fast proof generation (~1.35ms)
    • Efficient verification (~0.76ms)
    • Quick EVM conversion (~0.29µs)
    • Benchmarked and optimized

Installation

Add this to your Cargo.toml:

[dependencies]
spawn-zk-snarks = "0.1.4"

Quick Start

use spawn_zk_snarks::{Groth16Setup, ArithmeticCircuit};
use ark_bn254::Fr;
use ark_ff::One;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Setup phase
    let setup = Groth16Setup::new(3, 2)?;

    // Generate proof
    let inputs = vec![Fr::one()];
    let witness = vec![Fr::one(), Fr::one()];
    let proof = setup.prove(&inputs, &witness)?;

    // Verify proof
    let is_valid = setup.verify(&proof, &inputs)?;
    assert!(is_valid);

    // Generate Solidity verifier
    let contract = generate_verifier_contract(&setup.verifying_key)?;

    Ok(())
}

Benchmarks

Performance on M1 MacBook Pro:

proof generation    time:   [1.3244 ms 1.3519 ms 1.3931 ms]
proof verification  time:   [760.30 µs 762.93 µs 765.25 µs]
evm conversion     time:   [289.96 ns 291.12 ns 292.43 ns]
  • For detailed benchmark results, analysis and comparisons, see BENCHMARKS.md.
  • Highlights

    • 2x faster verification than generation
    • Sub-millisecond verification time
    • Nanosecond EVM conversion
    • Competitive gas costs

EVM Integration

// Generate Solidity verifier contract
let contract = generate_verifier_contract(&setup.verifying_key)?;

// Convert proof to EVM format
let proof_bytes = Groth16Setup::proof_to_evm_format(&proof)?;

// Estimate gas cost
let gas = Groth16Setup::estimate_verification_gas(
    proof_bytes.len(), 
    inputs.len()
);

Testing

Run unit tests:

cargo test

Run benchmarks:

cargo bench

Security Considerations

  • Uses the battle-tested Groth16 proving system
  • Built on the arkworks cryptographic library
  • Constant-time operations for core cryptographic functions
  • Regular security audits recommended before production use

License

MIT License. See LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

  • Built on top of the arkworks ecosystem
  • Uses the BN254 curve implementation from ark-bn254
  • Inspired by various ZKP implementations in the blockchain space

Disclaimer

This library is provided as is, without any security guarantees. Please perform your own security audit before using in production.

Dependencies

~8.5MB
~150K SLoC