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, etc
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
14Current implementation status:
15- [x] Finite Fields (1, 2, 6, 12) operations
16- [x] G1 and G2 curve operations
17- [x] BN128 Pairing
18- [ ] circuit code compiler
19 - [ ] code to flat code (improve circuit compiler)
20 - [x] flat code compiler
21 - [ ] private & public inputs. fix circuit compiler
22- [x] circuit to R1CS
23- [x] polynomial operations
24- [x] R1CS to QAP
25- [x] generate trusted setup
26- [x] generate proofs
27- [x] verify proofs with BN128 pairing
28 - [ ] fix 4th pairing proofs generation & verification: ê(Vkx+piA, piB) == ê(piH, Vkz) * ê(piC, G2)
29- [ ] move witness calculation outside the setup phase
30- [ ] Groth16
31- [ ] multiple optimizations
32
33
34## Usage
35- [](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
36- [](https://godoc.org/github.com/arnaucube/go-snark/bn128) bn128 (more details: https://github.com/arnaucube/go-snark/tree/master/bn128)
37- [](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
38- [](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
39- [](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
40
41### Library usage
42Warning: not finished.
43
44Example:
45```go
46// compile circuit and get the R1CS
47flatCode := `
48func test(x):
49 aux = x*x
50 y = aux*x
51 z = x + y
52 out = z + 5
53`
54
55// parse the code
56parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
57circuit, err := parser.Parse()
58assert.Nil(t, err)
59fmt.Println(circuit)
60
61// witness
62b3 := big.NewInt(int64(3))
63inputs := []*big.Int{b3}
64w := circuit.CalculateWitness(inputs)
65fmt.Println("\nwitness", w)
66/*
67now we have the witness:
68w = [1 3 35 9 27 30]
69*/
70
71// flat code to R1CS
72fmt.Println("generating R1CS from flat code")
73a, b, c := circuit.GenerateR1CS()
74
75/*
76now we have the R1CS from the circuit:
77a == [[0 1 0 0 0 0] [0 0 0 1 0 0] [0 1 0 0 1 0] [5 0 0 0 0 1]]
78b == [[0 1 0 0 0 0] [0 1 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 0 0]]
79c == [[0 0 0 1 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [0 0 1 0 0 0]]
80*/
81
82
83alphas, betas, gammas, zx := snark.Utils.PF.R1CSToQAP(a, b, c)
84
85
86ax, bx, cx, px := snark.Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
87
88hx := snark.Utils.PF.DivisorPolinomial(px, zx)
89
90// hx==px/zx so px==hx*zx
91assert.Equal(t, px, snark.Utils.PF.Mul(hx, zx))
92
93// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
94abc := snark.Utils.PF.Sub(pf.Mul(ax, bx), cx)
95assert.Equal(t, abc, px)
96hz := snark.Utils.PF.Mul(hx, zx)
97assert.Equal(t, abc, hz)
98
99div, rem := snark.Utils.PF.Div(px, zx)
100assert.Equal(t, hx, div)
101assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
102
103// calculate trusted setup
104setup, err := snark.GenerateTrustedSetup(len(w), circuit, alphas, betas, gammas, zx)
105assert.Nil(t, err)
106fmt.Println("t", setup.Toxic.T)
107
108// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
109proof, err := snark.GenerateProofs(circuit, setup, hx, w)
110assert.Nil(t, err)
111
112assert.True(t, snark.VerifyProof(circuit, setup, proof))
113```
114
115### CLI usage
116
117#### Compile circuit
118Having a circuit file `test.circuit`:
119```
120func test(x):
121 aux = x*x
122 y = aux*x
123 z = x + y
124 out = z + 5
125```
126And a inputs file `inputs.json`
127```
128[
129 3
130]
131```
132
133In the command line, execute:
134```
135> go-snark-cli compile test.circuit
136```
137
138This will output the `compiledcircuit.json` file.
139
140#### Trusted Setup
141Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
142```
143> go-snark-cli trustedsetup
144```
145This 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`.
146
147
148#### Generate Proofs
149Assumming that we have the `compiledcircuit.json` and the `trustedsetup.json`, we can now generate the `Proofs` with the following command:
150```
151> go-snark-cli genproofs
152```
153
154This will store the file `proofs.json`, that contains all the SNARK proofs.
155
156#### Verify Proofs
157Having the `proofs.json`, `compiledcircuit.json`, `trustedsetup.json` files, we can now verify the `Pairings` of the proofs, in order to verify the proofs.
158```
159> go-snark-cli verify
160```
161This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
162
163
164
165## Test
166```
167go test ./... -v
168```
169
170---
171
172
173Thanks 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.