This repository contains a Rust implementation of KZG Commitments, a cryptographic primitive used for polynomial commitments. The library provides functionality to generate, commit, and verify polynomial commitments.
Blog post on implementing KZG commitment scheme You can find the python version of this code here
- Vector to Polynomial: Convert a vector of elements to polynomial form.
- Polynomial Commitment: Commit to a polynomial
- Proof Generation: Generate a proof for a given set of points from the vector.
- Proof Verification: Verify the correctness of the proofs generated.
To use this library, add the following to your Cargo.toml
kzg-commitment = "0.1.3"
Here is a basic example of how to use the library:
use kzg_commitment::KZGCommitment;
use ark_bls12_381::{Fr as F};
fn main() {
let kzg = KZGCommitment::new(50);
let vector = vec![F::from(120), F::from(-15), F::from(60), F::from(80)];
let polynomial = KZGCommitment::vector_to_polynomial(&vector);
let commitment = kzg.commit_polynomial(&polynomial);
let points = vec![(F::from(0), F::from(120)), (F::from(1), F::from(-15))];
let proof = kzg.generate_proof(&polynomial, &random_points).unwrap();
let verification = kzg.verify_proof(&commitment, &random_points, &proof);
assert!(verification, "Verification failed");
}
To run the tests, use the following command:
cargo test
test_vec_to_poly
: Tests the conversion of a vector to a polynomial and evaluates it at random points.test_verify_proof_valid
: Tests the generation and verification of a valid proof.test_invalid_proof_generation
: Tests the generation of an invalid proof and ensures the correct error is returned.test_invalid_proof_verification
: Tests the verification of an invalid proof and ensures the correct error is returned.
- Use Finite Field for vectors instead of i32
- Add support for precomputed powers_of_tau (trusted_setup)
- Add more tests for invalid proof verification
- Add benchmarks
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
KZG Commitments - More information about KZG Commitments.
This project was inspired by Dankrad Feist's blog post on KZG Commitments.