Skip to content

Frenchkebab/zero-knowledge

Repository files navigation

Zero Knowledge Proof Implementation

A comprehensive implementation of zero knowledge proof systems from foundational cryptography primitives to on-chain verification contracts. This project demonstrates the complete pipeline from R1CS (Rank-1 Constraint System) to encrypted QAP (Quadratic Arithmetic Program) verification on Ethereum.

Overview

This project implements the theoretical foundations of zk-SNARKs through six progressive modules, each building upon the previous to create a complete zero knowledge proof system. Starting with basic elliptic curve cryptography, the implementation progresses through constraint systems, polynomial transformations, and finally on-chain verification using Solidity smart contracts.

Technical Stack

  • Cryptography: secp256k1, BN254 (BN128) curve, bilinear pairings
  • Languages: Python 3, Solidity 0.8.x
  • Libraries: NumPy, galois, py_ecc, eth-ape
  • Testing: pytest, ape framework

Project Modules

1. ECDSA Implementation

Location: 1-implement-ecdsa-from-scratch/

Implements ECDSA signature scheme from scratch using secp256k1 curve (used in Bitcoin and Ethereum).

Key Features:

  • Custom elliptic curve point operations (ECPoint)
  • Curve arithmetic implementation (ECurve)
  • ECDSA signing and verification algorithms
  • Private/public key pair generation
  • Message signing and signature validation

Implementation: Pure Python implementation without external crypto libraries for core operations.

2. Elliptic Curve Proof Verification

Location: 2-zk-proof-solidity/

Solidity smart contract for verifying elliptic curve operations and matrix multiplications without revealing private values.

Key Features:

  • Rational Addition Proof: Verifies (a + b) * G1 = (num/den) * G1 without revealing scalars a and b
  • Matrix Multiplication Verification: Verifies M * s = o where M is an n×n matrix and s, o are vectors
  • Uses BN254 curve (G1 group) with precompiled contracts
  • Modular inverse computation in finite fields

Contract: contracts/ECProof.sol - Deployable Solidity contract with gas-optimized operations.

3. Bilinear Pairing Verification

Location: 3-bilinear-parings/

Implementation of bilinear pairing verification for zero knowledge proofs on Ethereum.

Key Features:

  • Verifies complex pairing equation: e(A1, B2) = e(Alpha1, Beta2) + e(X1, Gamma2) + e(C1, Delta2)
  • Works with both G1 and G2 points on BN254 curve
  • Uses Ethereum precompiled contract at address 0x08 for pairing operations
  • Point validation and curve order checks

Contracts:

  • BilinearPairing.sol - Core pairing operations
  • BilinearPairingVerifier.sol - Proof verification contract

4. Rank-1 Constraint System (R1CS)

Location: 4-R1CS/

Converts arithmetic circuits and conditional logic into R1CS format for zero knowledge proofs.

Key Features:

  • Problem 1: Converts polynomial 5*x³ - 4*y²*x² + 13*x*y² + x² - 10*y into R1CS matrices
  • Problem 2: Converts conditional logic (if-else statements) into R1CS constraints
  • R1CS Verifier: Validates constraint satisfaction Aw ⊙ Bw - Cw = 0 (Hadamard product)
  • Witness vector generation and validation

Implementation: Python with NumPy for matrix operations and finite field arithmetic.

5. Quadratic Arithmetic Program (QAP)

Location: 5-qap/

Transforms R1CS into QAP using Lagrange interpolation over finite fields.

Key Features:

  • Converts R1CS matrices A, B, C into polynomials U(x), V(x), W(x)
  • Lagrange interpolation for polynomial construction
  • Polynomial evaluation at specific points
  • QAP correctness verification

Implementation: Python implementation with galois library for finite field operations.

6. Encrypted QAP Verification

Location: 6-encrypted-qap/

Encrypted evaluation of QAP polynomials with on-chain verification using bilinear pairings.

Key Features:

  • Encrypts QAP polynomials on elliptic curves: [A]_1, [B]_2, [C]_1
  • Trusted setup for powers of tau generation
  • Verifies pairing equation: e([A]_1, [B]_2) = e([C]_1, [G]_2)
  • On-chain Solidity verification contract
  • Complete test suite with pytest

Contracts: contracts/EncryptedQAP.sol - Production-ready verification contract

Tests: Comprehensive test suite verifying encrypted QAP evaluation and on-chain verification.

Project Flow

ECDSA → EC Proofs → Bilinear Pairings → R1CS → QAP → Encrypted QAP Verification

Each module builds upon the previous, creating a complete zero knowledge proof system that can verify computations without revealing the underlying data.

Getting Started

Each module contains detailed setup instructions in its respective README. General requirements:

  • Python 3.8+
  • Solidity compiler 0.8.x
  • eth-ape framework (for Solidity projects)
  • Required Python packages: numpy, galois, py_ecc

Key Achievements

  • Implemented complete zk-SNARK pipeline from scratch
  • Deployed and tested Solidity verification contracts
  • Converted complex arithmetic circuits to R1CS
  • Implemented polynomial interpolation and encrypted evaluation
  • Created production-ready on-chain verification system

Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published