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