Groth16 zero-knowledge proof verifier, implemented in Rust.
Verifies BN128 curve Groth16 proofs generated by the gnark compiler.
bn: BN128 curve pairing operationsanyhow: Error handling
[dependencies]
gnark-bn-verifier = "0.1.0"use gnark_bn_verifier::{proof::Groth16Proof, vk::Groth16VKey};
use bn::Fr;
// Deserialize verification key and proof from bytes
let vk = Groth16VKey::try_from(vk_bytes)?;
let proof = Groth16Proof::try_from(proof_bytes)?;
// Public inputs from the circuit
let public_inputs: Vec<Fr> = vec![
Fr::from(123),
Fr::from(456),
];
// Verify
proof.verify(&vk, &public_inputs)?;
println!("Proof verified successfully!");Byte sequence generated by gnark:
| Offset | Length | Content |
|---|---|---|
| 0 | 32 | α (G1, compressed) |
| 32 | 32 | (unused) |
| 64 | 64 | β (G2, compressed) |
| 128 | 64 | γ (G2, compressed) |
| 224 | 64 | δ (G2, compressed) |
| 288 | 4 | num_k (big-endian u32) |
| 292 | num_k × 32 | K[i] (G1, compressed) |
num_k = number of public inputs + 1
Byte sequence generated by gnark:
| Offset | Length | Content |
|---|---|---|
| 0 | 64 | A (G1, uncompressed) |
| 64 | 128 | B (G2, uncompressed) |
| 192 | 64 | C (G1, uncompressed) |
Public inputs are scalar values of type Fr, corresponding to public parameters in the circuit.
Verification key struct.
// Deserialize from bytes
let vk = Groth16VKey::try_from(bytes)?;
// Get number of public inputs
let num_inputs = vk.num_public_inputs();Proof struct.
// Deserialize from bytes
let proof = Groth16Proof::try_from(bytes)?;
// Verify the proof
proof.verify(&vk, &public_inputs)?;cargo testTo verify real proofs, you need to compile a circuit with gnark and generate proofs:
- Install gnark:
go get github.com/ConsenSys/gnark - Write circuit code
- Run prover to generate vk/proof/public_inputs
- Verify using this library
package main
import (
"github.com/consensys/gnark/std/hash/mimc"
"github.com/consensys/gnark/benchmark"
)
func main() {
// Define circuit
circuit := MyCircuit{}
// Generate test vectors
_ = circuit
}- This library only implements the verification algorithm, not proof generation
- Verification key must be from a trusted source
- Pairing operations are performed on the BN128 curve
MIT