A minimal implementation of the KZG commitment scheme. Built on top of the arkworks ecosystem.
- Define and operate on univariate polynomials over finite fields.
- Efficient polynomial arithmetic (add, sub, mul, scale, evaluate).
- Divide polynomials by linear factors.
- Minimal implementation of KZG commitments using a trusted setup.
- Fully generic over any pairing-friendly curve (
ark_ec::Pairing). - Suitable for educational, experimental, and constrained environments.
⚠️ Experimental: This crate is designed for educational purposes. Not intended for production use.
Add to your Cargo.toml:
[dependencies]
kzg-mini = "0.1"
ark-ff = "0.4"
ark-ec = "0.4"
ark-std = "0.4"
ark-bls12-381 = "0.4" # Or any curve from arkworksThe following examples demonstrate how to create a polynomial commitment and open a KZG proof using the kzg_mini crate on the BLS12-381 curve. See examples for the full code.
This example constructs a polynomial f(x) = 3x² + 2x + 1, commits to it, and verifies a KZG proof:
fn main() {
let mut rng = test_rng();
let poly = Polynomial::new(vec![Fr::from(1u64), Fr::from(2u64), Fr::from(3u64)]);
let tau = Fr::rand(&mut rng);
let g1 = G1Projective::generator();
let g2 = G2Projective::generator();
let setup = KZGCeremony::<Bls12_381>::setup(2, tau, g1, g2);
let commitment = setup.commit(&poly);
let point = Fr::from(42u64);
let proof = setup.open(&poly, point);
let ok = setup.verify(commitment, &proof);
println!("Reference Polynomial: {:#?}", poly);
println!("Commitment: {}", commitment);
println!("Proof: {:#?}", proof);
println!("KZG proof verified: {}", ok);
}This example shows how to generate a polynomial from a string, commit to it, and verify the corresponding proof:
fn main() {
let mut rng = test_rng();
let poly = Polynomial::from_str("hell0, world!")?;
let tau = Fr::rand(&mut rng);
let g1 = G1Projective::generator();
let g2 = G2Projective::generator();
let setup = KZGCeremony::<Bls12_381>::setup(poly.coeffs.len(), tau, g1, g2);
let commitment = setup.commit(&poly);
let point = Fr::from(42u64);
let proof = setup.open(&poly, point);
let ok = setup.verify(commitment, &proof);
println!("Reference Polynomial: {:#?}", poly);
println!("Commitment: {}", commitment);
println!("Proof: {:#?}", proof);
println!("KZG proof verified: {}", ok);
}Polynomial<F>: A simple polynomial type over anyark_ff::Field, with arithmetic operations and evaluation.KZGCeremony<E>: Holds the trusted setup parameters for a KZG commitment over a pairing engineE.KZGCommitment,KZGProof: Types representing the output of commitment and opening.
MIT or Apache-2.0
Built using arkworks libraries.