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