go-snark/README.md
README.mdRaw
1# go-snark [](https://goreportcard.com/report/github.com/arnaucube/go-snark) [](https://travis-ci.org/arnaucube/go-snark) [](https://gitter.im/go-snark/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
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) :
151. compile circuuit
162. generate trusted setup
173. calculate witness
184. generate proofs
195. verify proofs
20
21Minimal complete flow implementation:
22- [x] Finite Fields (1, 2, 6, 12) operations
23- [x] G1 and G2 curve operations
24- [x] BN128 Pairing
25- [x] circuit flat code compiler
26- [x] circuit to R1CS
27- [x] polynomial operations
28- [x] R1CS to QAP
29- [x] generate trusted setup
30- [x] generate proofs
31- [x] verify proofs with BN128 pairing
32
33Improvements from the minimal implementation:
34- [x] allow to call functions in circuits language
35- [x] allow `import` in circuits language
36- [ ] allow `for` in circuits language
37- [ ] move witness values calculation outside the setup phase
38- [ ] Groth16
39- [ ] multiple optimizations
40
41
42## Usage
43- [](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
44- [](https://godoc.org/github.com/arnaucube/go-snark/bn128) bn128 (more details: https://github.com/arnaucube/go-snark/tree/master/bn128)
45- [](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
46- [](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
47- [](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
48
49### CLI usage
50*The cli still needs some improvements, such as seting input files, etc.*
51
52In this example we will follow the equation example from [Vitalik](https://medium.com/@VitalikButerin/quadratic-arithmetic-programs-from-zero-to-hero-f6d558cea649)'s article: `y = x^3 + x + 5`, where `y==35` and `x==3`. So we want to prove that we know a secret `x` such as the result of the equation is `35`.
53
54#### Compile circuit
55Having a circuit file `test.circuit`:
56```
57func exp3(private a):
58 b = a * a
59 c = a * b
60 return c
61
62func main(private s0, public s1):
63 s3 = exp3(s0)
64 s4 = s3 + s0
65 s5 = s4 + 5
66 equals(s1, s5)
67 out = 1 * 1
68```
69And a private inputs file `privateInputs.json`
70```
71[
72 3
73]
74```
75And a public inputs file `publicInputs.json`
76```
77[
78 35
79]
80```
81
82In the command line, execute:
83```
84> ./go-snark-cli compile test.circuit
85```
86
87This will output the `compiledcircuit.json` file.
88
89#### Trusted Setup
90Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
91```
92> ./go-snark-cli trustedsetup
93```
94This 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`.
95
96
97#### Generate Proofs
98Assumming that we have the `compiledcircuit.json`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.json` we can now generate the `Proofs` with the following command:
99```
100> ./go-snark-cli genproofs
101```
102
103This will store the file `proofs.json`, that contains all the SNARK proofs.
104
105#### Verify Proofs
106Having 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.
107```
108> ./go-snark-cli verify
109```
110This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
111
112
113### Library usage
114
115Example:
116```go
117// compile circuit and get the R1CS
118flatCode := `
119func exp3(private a):
120 b = a * a
121 c = a * b
122 return c
123
124func main(private s0, public s1):
125 s3 = exp3(s0)
126 s4 = s3 + s0
127 s5 = s4 + 5
128 equals(s1, s5)
129 out = 1 * 1
130`
131
132// parse the code
133parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
134circuit, err := parser.Parse()
135assert.Nil(t, err)
136fmt.Println(circuit)
137
138
139b3 := big.NewInt(int64(3))
140privateInputs := []*big.Int{b3}
141b35 := big.NewInt(int64(35))
142publicSignals := []*big.Int{b35}
143
144// witness
145w, err := circuit.CalculateWitness(privateInputs, publicSignals)
146assert.Nil(t, err)
147fmt.Println("witness", w)
148
149// now we have the witness:
150// w = [1 35 3 9 27 30 35 1]
151
152// flat code to R1CS
153fmt.Println("generating R1CS from flat code")
154a, b, c := circuit.GenerateR1CS()
155
156/*
157now we have the R1CS from the circuit:
158a: [[0 0 1 0 0 0 0 0] [0 0 1 0 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]]
159b: [[0 0 1 0 0 0 0 0] [0 0 0 1 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]]
160c: [[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]]
161*/
162
163
164alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
165
166
167ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
168
169// calculate trusted setup
170setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
171
172hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
173
174proof, err := GenerateProofs(*circuit, setup, w, px)
175
176b35Verif := big.NewInt(int64(35))
177publicSignalsVerif := []*big.Int{b35Verif}
178assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
179```
180
181
182
183## Test
184```
185go test ./... -v
186```
187
188## vim/nvim circuit syntax highlighter
189For more details and installation instructions see https://github.com/arnaucube/go-snark/tree/master/vim-syntax
190
191---
192
193
194Thanks 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.