arnaucube / go-snark-study

zkSNARK library implementation in Go from scratch (compiler, setup, prover, verifier)
GNU General Public License v3.0
255 stars 58 forks source link

Compile sample code error #13

Closed KimiWu123 closed 5 years ago

KimiWu123 commented 5 years ago

I followed the instruction here, it show the following error when I compile the circuit.

$ ./cli.exe compile test.circuit
cli

panic: using not declared function

goroutine 1 [running]:
github.com/arnaucube/go-snark/circuitcompiler.(*Parser).parseLine(0xc0000876d8, 0xc000066570, 0xc00005e410, 0x4)
        C:/Users/user/code/00_Projects/zk/go-snark/circuitcompiler/parser.go:168 +0x135e
github.com/arnaucube/go-snark/circuitcompiler.(*Parser).Parse(0xc0000876d8, 0xc00008a030, 0x1000, 0xc0000c0000)
        C:/Users/user/code/00_Projects/zk/go-snark/circuitcompiler/parser.go:222 +0x1d5
main.CompileCircuit(0xc0000ac2c0, 0x0, 0x0)
        C:/Users/user/code/00_Projects/zk/go-snark/cli/main.go:111 +0x2f9
github.com/urfave/cli.HandleAction(0x5e84a0, 0x6334b0, 0xc0000ac2c0, 0xc0000a8000, 0x0)
        C:/Users/user/go/pkg/mod/github.com/urfave/cli@v1.20.0/app.go:490 +0xcf
github.com/urfave/cli.Command.Run(0x625f7c, 0x7, 0x0, 0x0, 0x7e8660, 0x0, 0x0, 0x6282ac, 0x11, 0x0, ...)
        C:/Users/user/go/pkg/mod/github.com/urfave/cli@v1.20.0/command.go:210 +0x99d
github.com/urfave/cli.(*App).Run(0xc000096340, 0xc00005a0c0, 0x3, 0x4, 0x0, 0x0)
        C:/Users/user/go/pkg/mod/github.com/urfave/cli@v1.20.0/app.go:255 +0x6b6
main.main()
        C:/Users/user/code/00_Projects/zk/go-snark/cli/main.go:89 +0x19b

If I use the sample code in snark_test.go as following, it shows another error,

func main(private s0, public s1):
    s2 = s0 * s0
    s3 = s2 * s0
    s4 = s3 + s0
    s5 = s4 + 5
    equals(s1, s5)
    out = 1 * 1
out=1* [] []} {   1 1 [] []}] {[] [] []}} * 1 [] []} {* s1 1 s5 equals(s1, s5): s5==s1 * 1 [] []} {1 = *

witness [1 0 3 0 0 0 0 0 0 0 0 0]

generating R1CS from flat code
panic: using variable before it's set, s5

goroutine 1 [running]:
github.com/arnaucube/go-snark/circuitcompiler.insertVar(0xc0000c5680, 0xc, 0x10, 0xc0000d2100, 0xc, 0x10, 0xc00005e508, 0x2, 0xc000086f48, 0xc0000c4d80, ...)
        C:/Users/user/code/00_Projects/zk/go-snark/circuitcompiler/circuit.go:63 +0x3c7
github.com/arnaucube/go-snark/circuitcompiler.(*Circuit).GenerateR1CS(0xc0000c2000, 0xc00008a008, 0xc0000875b8, 0x1, 0x1, 0x20, 0x0, 0x0, 0xc, 0x10)
        C:/Users/user/code/00_Projects/zk/go-snark/circuitcompiler/circuit.go:122 +0xeb4
main.CompileCircuit(0xc0000ac2c0, 0x0, 0x0)
        C:/Users/user/code/00_Projects/zk/go-snark/cli/main.go:136 +0x78c
github.com/urfave/cli.HandleAction(0x5e84a0, 0x6334b0, 0xc0000ac2c0, 0xc0000a8000, 0x0)
        C:/Users/user/go/pkg/mod/github.com/urfave/cli@v1.20.0/app.go:490 +0xcf
github.com/urfave/cli.Command.Run(0x625f7c, 0x7, 0x0, 0x0, 0x7e8660, 0x0, 0x0, 0x6282ac, 0x11, 0x0, ...)
        C:/Users/user/go/pkg/mod/github.com/urfave/cli@v1.20.0/command.go:210 +0x99d
github.com/urfave/cli.(*App).Run(0xc0000964e0, 0xc00005a0c0, 0x3, 0x4, 0x0, 0x0)
        C:/Users/user/go/pkg/mod/github.com/urfave/cli@v1.20.0/app.go:255 +0x6b6
main.main()
        C:/Users/user/code/00_Projects/zk/go-snark/cli/main.go:89 +0x19b

Did I do anything wrong? It seems some bugs in the parser, but it works well if I run the test directly. Another question, why do you need out = 1 * 1?

Env: Windows 10

Thanks.

arnaucube commented 5 years ago

I understand that this issue was solved by https://github.com/arnaucube/go-snark/pull/14, right?

KimiWu123 commented 5 years ago

Yup! Exactly!