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

Circuit frontend.Variable name must be capitalized? #643

Closed yuquan1210 closed 1 year ago

yuquan1210 commented 1 year ago

I am using the cube example, and simply changed circuit variables "X" and "Y" to "x" and "y". Then I got this error: I ran go mod tidy and go run .

17:16:51 INF compiling circuit
17:16:51 INF parsed circuit inputs nbPublic=1 nbSecret=1
17:16:51 ERR parsing circuit error="can't set val x"
panic: unrecognized R1CS curve type

goroutine 1 [running]:
github.com/consensys/gnark/backend/groth16.Setup({0x0?, 0x0?})
        /home/ethan/go/pkg/mod/github.com/consensys/gnark@v0.8.0/backend/groth16/groth16.go:286 +0x4a5
main.main()
        /home/ethan/go_tutorial/gnark-example/cube.go:33 +0x8d
exit status 2

Here are the codes I used:

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)
}

Similar error occurred when I tried writing a circuit with names like "sym_0" and "sym_1" but only works if I changed them to "Sym_0" and "Sym_1". I am not sure whether this is a variable naming rule or a bug. Please help. Thank you!

gbotrel commented 1 year ago

yes struct variables must be capitalized -- it makes them public; that's not a gnark thing, and is quite common in Go 👍

yuquan1210 commented 1 year ago

Ohh, I just learned that. Sorry, my bad.