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- `On the Size of Pairing-based Non-interactive Arguments`, Jens Groth https://eprint.iacr.org/2016/260.pdf
9
10## Caution & Warning
11Implementation of the zkSNARK [Pinocchio protocol](https://eprint.iacr.org/2013/279.pdf) and [Groth16 protocol](https://eprint.iacr.org/2016/260.pdf) from scratch in Go to understand the concepts. Do not use in production.
12
13Not finished, implementing this in my free time to understand it better, so I don't have much time.
14
15Currently allows to do the complete path with [Pinocchio protocol](https://eprint.iacr.org/2013/279.pdf) and [Groth16 protocol](https://eprint.iacr.org/2016/260.pdf) :
16
170. write circuit
181. compile circuit
192. generate trusted setup
203. calculate witness
214. generate proofs
225. verify proofs
23
24Minimal complete flow implementation:
25- [x] Finite Fields (1, 2, 6, 12) operations
26- [x] G1 and G2 curve operations
27- [x] BN128 Pairing
28- [x] circuit flat code compiler
29- [x] circuit to R1CS
30- [x] polynomial operations
31- [x] R1CS to QAP
32- [x] generate trusted setup
33- [x] generate proofs
34- [x] verify proofs with BN128 pairing
35
36Improvements from the minimal implementation:
37- [x] allow to call functions in circuits language
38- [x] allow `import` in circuits language
39- [ ] allow `for` in circuits language
40- [ ] move witness values calculation outside the setup phase
41- [x] Groth16
42- [ ] multiple optimizations
43- [x] wasm proof generation
44- [x] wasm proof verification
45
46## WASM usage
47Ongoing experimentation with go-snark compiled to wasm: https://github.com/arnaucube/go-snark/tree/master/wasm
48
49## Usage
50- [](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
51- [](https://godoc.org/github.com/arnaucube/go-snark/groth16) zkSnark Groth16
52- [](https://godoc.org/github.com/arnaucube/go-snark/bn128) bn128 (more details: https://github.com/arnaucube/go-snark/tree/master/bn128)
53- [](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
54- [](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
55- [](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
56
57### CLI usage
58*The cli still needs some improvements, such as seting input files, etc.*
59
60In 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`.
61
62#### Compile circuit
63Having a circuit file `test.circuit`:
64```
65func exp3(private a):
66 b = a * a
67 c = a * b
68 return c
69
70func main(private s0, public s1):
71 s3 = exp3(s0)
72 s4 = s3 + s0
73 s5 = s4 + 5
74 equals(s1, s5)
75 out = 1 * 1
76```
77And a private inputs file `privateInputs.json`
78```
79[
80 3
81]
82```
83And a public inputs file `publicInputs.json`
84```
85[
86 35
87]
88```
89
90In the command line, execute:
91```
92> ./go-snark-cli compile test.circuit
93```
94If you want to have the wasm input ready also, add the flag `wasm`
95```
96> ./go-snark-cli compile test.circuit wasm
97```
98
99This will output the `compiledcircuit.json` file.
100
101#### Trusted Setup
102Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
103```
104> ./go-snark-cli trustedsetup
105```
106This 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`.
107
108If you want to have the wasm input ready also, add the flag `wasm`
109```
110> ./go-snark-cli trustedsetup wasm
111```
112
113#### Generate Proofs
114Assumming that we have the `compiledcircuit.json`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.json` we can now generate the `Proofs` with the following command:
115```
116> ./go-snark-cli genproofs
117```
118
119This will store the file `proofs.json`, that contains all the SNARK proofs.
120
121#### Verify Proofs
122Having 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.
123```
124> ./go-snark-cli verify
125```
126This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
127
128### Cli using Groth16
129All this process can be done using [Groth16 protocol](https://eprint.iacr.org/2016/260.pdf) protocol:
130```
131> ./go-snark-cli compile test.circuit
132> ./go-snark-cli groth16 trustedsetup
133> ./go-snark-cli groth16 genproofs
134> ./go-snark-cli verify
135```
136
137
138
139### Library usage
140
141Example:
142```go
143// compile circuit and get the R1CS
144flatCode := `
145func exp3(private a):
146 b = a * a
147 c = a * b
148 return c
149
150func main(private s0, public s1):
151 s3 = exp3(s0)
152 s4 = s3 + s0
153 s5 = s4 + 5
154 equals(s1, s5)
155 out = 1 * 1
156`
157
158// parse the code
159parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
160circuit, err := parser.Parse()
161assert.Nil(t, err)
162fmt.Println(circuit)
163
164
165b3 := big.NewInt(int64(3))
166privateInputs := []*big.Int{b3}
167b35 := big.NewInt(int64(35))
168publicSignals := []*big.Int{b35}
169
170// witness
171w, err := circuit.CalculateWitness(privateInputs, publicSignals)
172assert.Nil(t, err)
173fmt.Println("witness", w)
174
175// now we have the witness:
176// w = [1 35 3 9 27 30 35 1]
177
178// flat code to R1CS
179fmt.Println("generating R1CS from flat code")
180a, b, c := circuit.GenerateR1CS()
181
182/*
183now we have the R1CS from the circuit:
184a: [[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]]
185b: [[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]]
186c: [[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]]
187*/
188
189
190alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
191
192
193ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
194
195// calculate trusted setup
196setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
197
198hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
199
200proof, err := GenerateProofs(*circuit, setup, w, px)
201
202b35Verif := big.NewInt(int64(35))
203publicSignalsVerif := []*big.Int{b35Verif}
204assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
205```
206
207##### Verify Proof generated from [snarkjs](https://github.com/iden3/snarkjs)
208Is possible with `go-snark` to verify proofs generated by `snarkjs`
209
210Example:
211```go
212verified, err := VerifyFromCircom("circom-test/verification_key.json", "circom-test/proof.json", "circom-test/public.json")
213assert.Nil(t, err)
214assert.True(t, verified)
215```
216
217
218## Versions
219History of versions & tags of this project:
220- v0.0.1: zkSnark complete flow working with Pinocchio protocol
221- v0.0.2: circuit language improved (allow function calls and file imports)
222- v0.0.3: Groth16 zkSnark protocol added
223
224## Test
225```
226go test ./... -v
227```
228
229## vim/nvim circuit syntax highlighter
230For more details and installation instructions see https://github.com/arnaucube/go-snark/tree/master/vim-syntax
231
232---
233
234
235Thanks 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.