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.
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.
- 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
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.
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) * G1without revealing scalarsaandb - Matrix Multiplication Verification: Verifies
M * s = owhereMis an n×n matrix ands,oare 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.
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 operationsBilinearPairingVerifier.sol- Proof verification contract
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*yinto 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.
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.
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.
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.
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
- 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