Try it Live
Run BN254 examples in the interactive playground
Source: bn254.zig • bn254.wasm.tsTests: bn254.test.ts
BN254 (BN128)
Pairing-friendly elliptic curve implementation for zkSNARK verification and Ethereum’s Alt-BN128 precompiles (0x06-0x08).Overview
BN254 (also known as BN128 or Alt-BN128) is a Barreto-Naehrig pairing-friendly elliptic curve widely used in zero-knowledge proof systems. It provides efficient pairing operations essential for zkSNARK verification, privacy-preserving protocols, and cryptographic applications requiring bilinear pairings. Ethereum Use Cases:- zkSNARKs: Zero-knowledge proof verification (Zcash, Tornado Cash, zkSync)
- EIP-196: ECADD precompile (0x06) - G1 point addition
- EIP-196: ECMUL precompile (0x07) - G1 scalar multiplication
- EIP-197: ECPAIRING precompile (0x08) - Optimal ate pairing check
- Privacy protocols: Confidential transactions, private voting systems
Quick Start
Elliptic Curve Pairing Basics
Pairing-based cryptography uses a special bilinear mape: G1 × G2 → GT that enables:
- Bilinearity:
e(aP, bQ) = e(P, Q)^(ab)- scalar multiplication distributes - Non-degeneracy:
e(G1, G2) ≠ 1- generator pairing produces non-trivial result - Computability: Pairing computable in polynomial time (optimal ate pairing)
- Identity-based encryption: Public keys derived from identities
- Short signatures: BLS signatures with signature aggregation
- zkSNARKs: Succinct non-interactive zero-knowledge proofs
- Broadcast encryption: Efficient one-to-many encryption
API Reference
Field Elements
BN254 operates over two finite fields:Base Field (Fp)
Scalar Field (Fr)
Extension Field (Fp2)
Group Elements
G1 Points (Base Field)
y^2 = x^3 + 3 over Fp
G2 Points (Extension Field)
y^2 = x^3 + 3/(9+u) over Fp2
Pairing Operations
Optimal Ate Pairing
Pairing Check (zkSNARK Verification)
Serialization
G1 Point Format (64 bytes)
G2 Point Format (128 bytes)
Use Cases
zkSNARK Verification
EIP-196/197 Precompile Calls
Implementation Details
Rust Implementation (Production - Arkworks)
- Library: arkworks (ark-bn254, ark-ec, ark-ff)
- FFI:
src/crypto/bn254_arkworks.zig - Status: Audited, production-ready
- Performance: 3-5x faster than Zig implementation
- Use: Recommended for production deployments
- Battle-tested in Ethereum ecosystem
- Constant-time operations (side-channel resistant)
- Extensive security audits
- Optimized assembly for critical paths
TypeScript Implementation (Reference)
- Location:
src/crypto/bn254/(.jsfiles) - Purpose: Pure TS reference, browser compatibility
- Features:
- Fp, Fp2 field arithmetic
- G1, G2 point operations
- Pairing computation
- Serialization utilities
WASM Builds
Zig fallback: WASM builds use Zig implementation (arkworks unavailable in WASM). WASM performance is ~50% of native arkworks, but fully functional.Security Considerations
Production Deployments:- Use arkworks (Rust) implementation for native builds
- Audited, constant-time operations
- Resistant to timing side-channels
- Zig implementation suitable for testing
- Pure implementation aids understanding
- No known vulnerabilities, but unaudited
- Verify trusted setup authenticity
- Validate proof inputs (prevent malleability)
- Check subgroup membership for G2 points
- Ensure scalar values in valid range [1, r-1]
Performance
Native (Arkworks Rust):- ECADD: ~0.02ms
- ECMUL: ~0.15ms
- Pairing: ~1.5ms
- Pairing check (2 pairs): ~2.5ms
- ECADD: ~0.05ms
- ECMUL: ~0.3ms
- Pairing: ~3ms
- Pairing check (2 pairs): ~5ms
Constants
Related
- Precompiles: BN254 Add/Mul/Pairing - EIP-196/197 precompile implementations
- BLS12-381 - Alternative pairing curve for Eth2 consensus
- KZG Commitments - Polynomial commitments using BLS12-381
References
- EIP-196: Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128
- EIP-197: Precompiled contracts for optimal ate pairing check on the elliptic curve alt_bn128
- Groth16: On the Size of Pairing-based Non-interactive Arguments
- arkworks-rs/algebra - Audited Rust implementation

