Consensys / gnark

gnark is a fast zk-SNARK library that offers a high-level API to design circuits. The library is open source and developed under the Apache 2.0 license
https://hackmd.io/@gnark
Apache License 2.0
1.41k stars 361 forks source link

How does gnark start GPU acceleration? #1130

Open weijizhen opened 4 months ago

weijizhen commented 4 months ago

I am at gnark/.../example, a main. go was created, such as the following code: package main

import ( "github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark/backend" "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 { // struct tags on a variable is optional // default uses variable name and secret visibility. X frontend.Variable gnark:"x" Y frontend.Variable gnark:",public" }

// Define declares the circuit constraints // x*3 + x + 5 == y 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() { // compiles our circuit into a R1CS var circuit CubicCircuit ccs, _ := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit)

// groth16 zkSNARK: Setup
pk, vk, _ := groth16.Setup(ccs)

// witness definition
assignment := CubicCircuit{X: 3, Y: 35}
witness, _ := frontend.NewWitness(&assignment, ecc.BN254.ScalarField())
publicWitness, _ := witness.Public()

// groth16: Prove & Verify
proof, _ := groth16.Prove(ccs, pk, witness, backend.WithIcicleAcceleration())
//proof, _ := groth16.Prove(ccs, pk, witness)
groth16.Verify(proof, vk, publicWitness)

}

errors: /usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /usr/bin/ld: cannot find -lbn254: No such file or directory collect2: error: ld returned 1 exit status

Should I download the article first and then compile it? Finally, to gnark/.../example and to execute "go run -tags=icicle main.go" What should I do?

ivokub commented 4 months ago

Hi, indeed setting up the GPU support is a bit cumbersome and depends on the ICICLE library being set up correctly. Have a look at https://github.com/ingonyama-zk/icicle/tree/main/wrappers/golang on how to build ICICLE.