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.43k stars 369 forks source link

Issue: Unable to run the example in the readme #764

Closed aarav22 closed 1 year ago

aarav22 commented 1 year ago

I'm new to both gnark and Go. I created a go module and created a file cubic.go and pasted the code from the readme (attached below).

Step 1:

go mod

runs without error and installs all the packages.

Step 2:

go run .

errors:

# github.com/consensys/gnark/internal/backend/bn254/groth16
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bn254/groth16/setup.go:55:25: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bn254/groth16/setup.go:78:26: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bn254/groth16/setup.go:258:27: assignment mismatch: 2 variables but pedersen.Setup returns 3 values
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bn254/groth16/prove.go:337:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bn254/groth16/prove.go:338:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bn254/groth16/prove.go:339:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bn254/groth16/prove.go:357:32: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFTInverse
# github.com/consensys/gnark/internal/backend/bls12-381/groth16
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-381/groth16/setup.go:55:25: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-381/groth16/setup.go:78:26: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-381/groth16/setup.go:258:27: assignment mismatch: 2 variables but pedersen.Setup returns 3 values
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-381/groth16/prove.go:337:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-381/groth16/prove.go:338:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-381/groth16/prove.go:339:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-381/groth16/prove.go:357:32: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFTInverse
# github.com/consensys/gnark/internal/backend/bls24-317/groth16
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-317/groth16/setup.go:55:25: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-317/groth16/setup.go:78:26: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-317/groth16/setup.go:258:27: assignment mismatch: 2 variables but pedersen.Setup returns 3 values
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-317/groth16/prove.go:337:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-317/groth16/prove.go:338:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-317/groth16/prove.go:339:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-317/groth16/prove.go:357:32: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFTInverse
# github.com/consensys/gnark/internal/backend/bw6-633/groth16
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-633/groth16/setup.go:55:25: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-633/groth16/setup.go:78:26: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-633/groth16/setup.go:258:27: assignment mismatch: 2 variables but pedersen.Setup returns 3 values
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-633/groth16/prove.go:337:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-633/groth16/prove.go:338:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-633/groth16/prove.go:339:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-633/groth16/prove.go:357:32: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFTInverse
# github.com/consensys/gnark/internal/backend/bls12-377/groth16
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-377/groth16/setup.go:55:25: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-377/groth16/setup.go:78:26: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-377/groth16/setup.go:258:27: assignment mismatch: 2 variables but pedersen.Setup returns 3 values
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-377/groth16/prove.go:337:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-377/groth16/prove.go:338:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-377/groth16/prove.go:339:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls12-377/groth16/prove.go:357:32: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFTInverse
# github.com/consensys/gnark/internal/backend/bls24-315/groth16
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-315/groth16/setup.go:55:25: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-315/groth16/setup.go:78:26: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-315/groth16/setup.go:258:27: assignment mismatch: 2 variables but pedersen.Setup returns 3 values
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-315/groth16/prove.go:337:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-315/groth16/prove.go:338:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-315/groth16/prove.go:339:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bls24-315/groth16/prove.go:357:32: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFTInverse
# github.com/consensys/gnark/internal/backend/bw6-761/groth16
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-761/groth16/setup.go:55:25: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-761/groth16/setup.go:78:26: undefined: pedersen.Key
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-761/groth16/setup.go:258:27: assignment mismatch: 2 variables but pedersen.Setup returns 3 values
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-761/groth16/prove.go:337:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-761/groth16/prove.go:338:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-761/groth16/prove.go:339:25: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFT
../../../go/pkg/mod/github.com/consensys/gnark@v0.8.0/internal/backend/bw6-761/groth16/prove.go:357:32: cannot use true (untyped bool constant) as fft.Option value in argument to domain.FFTInverse

Environment: Windows Subsystem for Linux


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 {
    // 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)
    groth16.Verify(proof, vk, publicWitness)
}
aarav22 commented 1 year ago

Not sure if it's the ideal approach but downgrading the gnark-crypto dependency to v0.9.1 works.

ivokub commented 1 year ago

Hi - see the solution in https://github.com/Consensys/gnark/issues/780, maybe it is similar issue? I.e. when not explicitly go getting gnark, then go mod tidy upgrades all implicit dependencies to the latest and there is mismatch between gnark and gnark-crypto.