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