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