gnark

repository·master·Indexed 23 days ago

https://github.com/consensys/gnark

A high-performance zk-SNARK library written in Go. It provides a high-level API for defining circuits, compiling them, and generating/verifying proofs using proving systems such as Groth16 and PLONK. Supported elliptic curves include BN254, BLS12-381, BLS12-377, and BW6-761. The library includes experimental GPU acceleration for Groth16 via the Ingonyama ICICLE backend.

Tokens
1.1K
Snippets
3
Records
9
Agent score
33%

What's inside gnark

  1. Experimental GPU acceleration with ICICLE

    master

    gnark supports experimental GPU acceleration for Groth16 via the Ingonyama ICICLE backend. This is available for the following curves:

    • BN254
    • BLS12-377
    • BLS12-381
    • BW6-761

    For more details, refer to the accelerated backend documentation or the ICICLE repository.

  2. Supported ZKP schemes and curves in gnark

    master

    gnark is a Zero Knowledge Proofs (ZKP) library that supports multiple proving schemes and elliptic curves.

    Supported ZKP schemes:

    • Groth16
    • PLONK

    Supported curves:

    • BN254
    • BLS12_377
    • BLS12_381
    • BW6_761
  3. How to define and run a circuit in gnark

    master

    To create a zk-SNARK circuit, you must:

    1. Define a struct that implements the frontend.Circuit interface by providing a Define(api frontend.API) error method.
    2. Use struct tags (e.g., gnark:"x" or gnark:",public") to define circuit variables.
    3. Compile the circuit using frontend.Compile.
    4. Perform a setup (e.g., groth16.Setup) to generate proving and verification keys.
    5. Create a witness using frontend.NewWitness with an assignment struct.
    6. Generate a proof using groth16.Prove and verify it using groth16.Verify.
    package main
    
    import (
    	"github.com/consensys/gnark-crypto/ecc"
    	"github.com/consensys/gnark/backend/groth16"
    	"github.com/consensys/gnark/frontend"
    	"github.com/consensys/gnark/frontend/cs/r1cs"
    )
    
    // CubicCircuit defines a simple circuit.
    // x**3 + x + 5 == y
    type CubicCircuit struct {
    	X frontend.Variable `gnark:"x"`
    	Y frontend.Variable `gnark:",public"`
    }
    
    // Define declares the circuit constraints.
    func (circuit *CubicCircuit) Define(api frontend.API) error {
    	x3 := api.Mul(circuit.X, circuit.X, circuit.X)
    	api.AssertIsEqual(circuit.Y, api.Add(x3, circuit.X, 5))
    	return nil
    }
    
    func main() {
    	var circuit CubicCircuit
    	ccs, _ := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)
    
    	pk, vk, _ := groth16.Setup(ccs)
    
    	assignment := CubicCircuit{X: 3, Y: 35}
    	witness, _ := frontend.NewWitness(&assignment, ecc.BN254.ScalarField())
    	publicWitness, _ := witness.Public()
    
    	proof, _ := groth16.Prove(ccs, pk, witness)
    	_ = groth16.Verify(proof, vk, publicWitness)
    }
  4. Run local tests for gnark

    master

    Use the following commands to run various test suites locally:

    • Short tests: go test -short ./...
    • Release and Solidity checks: go test -tags=release_checks,solccheck .
    • Prover checks: go test -tags=prover_checks ./test/... ./examples/...
    • Fuzz testing: go test -run=NONE -fuzz=FuzzIntcomp -fuzztime=30s ./internal/backend/ioutils
    • Generate code: go generate ./...
    go test -short ./...
    go test -tags=release_checks,solccheck .
    go test -tags=prover_checks ./test/... ./examples/...
    go test -run=NONE -fuzz=FuzzIntcomp -fuzztime=30s ./internal/backend/ioutils
    go generate ./...
  5. Supported proving systems and curves in gnark

    master

    gnark supports the following proving systems and elliptic curves:

    Proving Systems:

    • Groth16
    • PLONK

    Curves:

    • BN254
    • BLS12-381
    • BLS12-377
    • BW6-761

    Important Notes:

    • Solidity verifier export support is curve-dependent; BN254 is the primary target.
    • Serialized formats are not guaranteed to be stable across versions.