go-snark/README.md
README.mdRaw
1# go-snark [](https://goreportcard.com/report/github.com/arnaucube/go-snark)
2
3zkSNARK library implementation in Go
4
5
6- `Succinct Non-Interactive Zero Knowledge for a von Neumann Architecture`, Eli Ben-Sasson, Alessandro Chiesa, Eran Tromer, Madars Virza https://eprint.iacr.org/2013/879.pdf
7- `Pinocchio: Nearly practical verifiable computation`, Bryan Parno, Craig Gentry, Jon Howell, Mariana Raykova https://eprint.iacr.org/2013/279.pdf
8
9## Caution, Warning
10Implementation of the zkSNARK [Pinocchio protocol](https://eprint.iacr.org/2013/279.pdf) from scratch in Go to understand the concepts. Do not use in production.
11
12Not finished, implementing this in my free time to understand it better, so I don't have much time.
13
14Currently allows to do the complete path with [Pinocchio protocol](https://eprint.iacr.org/2013/279.pdf) :
15- compile circuuit
16 - parsers
17 - R1CS
18 - QAP
19- generate trusted setup
20- calculate witness
21- generate proofs
22- verify proofs
23 - with BN128 pairing
24
25Current implementation status:
26- [x] Finite Fields (1, 2, 6, 12) operations
27- [x] G1 and G2 curve operations
28- [x] BN128 Pairing
29- [x] circuit code compiler
30 - [ ] code to flat code (improve circuit compiler)
31 - [x] flat code compiler
32- [x] circuit to R1CS
33- [x] polynomial operations
34- [x] R1CS to QAP
35- [x] generate trusted setup
36- [x] generate proofs
37- [x] verify proofs with BN128 pairing
38- [ ] move witness calculation outside the setup phase
39- [ ] Groth16
40- [ ] multiple optimizations
41
42
43## Usage
44- [](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
45- [](https://godoc.org/github.com/arnaucube/go-snark/bn128) bn128 (more details: https://github.com/arnaucube/go-snark/tree/master/bn128)
46- [](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
47- [](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
48- [](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
49
50### CLI usage
51
52#### Compile circuit
53Having a circuit file `test.circuit`:
54```
55func test(private s0, public s1):
56 s2 = s0 * s0
57 s3 = s2 * s0
58 s4 = s3 + s0
59 s5 = s4 + 5
60 equals(s1, s5)
61 out = 1 * 1
62```
63And a private inputs file `privateInputs.json`
64```
65[
66 3
67]
68```
69And a public inputs file `publicInputs.json`
70```
71[
72 35
73]
74```
75
76In the command line, execute:
77```
78> ./go-snark-cli compile test.circuit
79```
80
81This will output the `compiledcircuit.json` file.
82
83#### Trusted Setup
84Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
85```
86> ./go-snark-cli trustedsetup
87```
88This will create the file `trustedsetup.json` with the TrustedSetup data, and also a `toxic.json` file, with the parameters to delete from the `Trusted Setup`.
89
90
91#### Generate Proofs
92Assumming that we have the `compiledcircuit.json`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.json` we can now generate the `Proofs` with the following command:
93```
94> ./go-snark-cli genproofs
95```
96
97This will store the file `proofs.json`, that contains all the SNARK proofs.
98
99#### Verify Proofs
100Having the `proofs.json`, `compiledcircuit.json`, `trustedsetup.json` `publicInputs.json` files, we can now verify the `Pairings` of the proofs, in order to verify the proofs.
101```
102> ./go-snark-cli verify
103```
104This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
105
106
107### Library usage
108Warning: not finished.
109
110Example:
111```go
112// compile circuit and get the R1CS
113flatCode := `
114func test(private s0, public s1):
115 s2 = s0 * s0
116 s3 = s2 * s0
117 s4 = s3 + s0
118 s5 = s4 + 5
119 equals(s1, s5)
120 out = 1 * 1
121`
122
123// parse the code
124parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
125circuit, err := parser.Parse()
126assert.Nil(t, err)
127fmt.Println(circuit)
128
129
130b3 := big.NewInt(int64(3))
131privateInputs := []*big.Int{b3}
132b35 := big.NewInt(int64(35))
133publicSignals := []*big.Int{b35}
134
135// witness
136w, err := circuit.CalculateWitness(privateInputs, publicSignals)
137assert.Nil(t, err)
138fmt.Println("witness", w)
139
140// now we have the witness:
141// w = [1 35 3 9 27 30 35 1]
142
143// flat code to R1CS
144fmt.Println("generating R1CS from flat code")
145a, b, c := circuit.GenerateR1CS()
146
147/*
148now we have the R1CS from the circuit:
149a: [[0 0 1 0 0 0 0 0] [0 0 0 1 0 0 0 0] [0 0 1 0 1 0 0 0] [5 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0] [0 1 0 0 0 0 0 0] [1 0 0 0 0 0 0 0]]
150b: [[0 0 1 0 0 0 0 0] [0 0 1 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0]]
151c: [[0 0 0 1 0 0 0 0] [0 0 0 0 1 0 0 0] [0 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0] [0 1 0 0 0 0 0 0] [0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 1]]
152*/
153
154
155alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
156
157
158ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
159
160// calculate trusted setup
161setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
162
163hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
164
165proof, err := GenerateProofs(*circuit, setup, w, px)
166
167b35Verif := big.NewInt(int64(35))
168publicSignalsVerif := []*big.Int{b35Verif}
169assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
170```
171
172
173
174## Test
175```
176go test ./... -v
177```
178
179## vim/nvim circuit syntax highlighter
180For more details and installation instructions see https://github.com/arnaucube/go-snark/tree/master/vim-syntax
181
182---
183
184
185Thanks to [@jbaylina](https://github.com/jbaylina), [@bellesmarta](https://github.com/bellesmarta), [@adriamb](https://github.com/adriamb) for their explanations that helped to understand this a little bit. Also thanks to [@vbuterin](https://github.com/vbuterin) for all the published articles explaining the zkSNARKs.