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, 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
 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- [ ] move witness calculation outside the setup phase
 28- [ ] Groth16
 29- [ ] multiple optimizations
 30
 31
 32## Usage
 33- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
 34- [![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)
 35- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
 36- [![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)
 37- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
 38
 39### Library usage
 40Warning: not finished.
 41
 42Example:
 43```go
 44// compile circuit and get the R1CS
 45flatCode := `
 46func test(private s0, public s1):
 47	s2 = s0 * s0
 48	s3 = s2 * s0
 49	s4 = s3 + s0
 50	s5 = s4 + 5
 51	equals(s1, s5)
 52	out = 1 * 1
 53`
 54
 55// parse the code
 56parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
 57circuit, err := parser.Parse()
 58assert.Nil(t, err)
 59fmt.Println(circuit)
 60
 61
 62b3 := big.NewInt(int64(3))
 63privateInputs := []*big.Int{b3}
 64b35 := big.NewInt(int64(35))
 65publicSignals := []*big.Int{b35}
 66
 67// witness
 68w, err := circuit.CalculateWitness(privateInputs, publicSignals)
 69assert.Nil(t, err)
 70fmt.Println("witness", w)
 71
 72// now we have the witness:
 73// w = [1 35 3 9 27 30 35 1]
 74
 75// flat code to R1CS
 76fmt.Println("generating R1CS from flat code")
 77a, b, c := circuit.GenerateR1CS()
 78
 79/*
 80now we have the R1CS from the circuit:
 81a: [[0 0 1 0 0 0 0 0] [0 0 0 1 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]]
 82b: [[0 0 1 0 0 0 0 0] [0 0 1 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] [1 0 0 0 0 0 0 0]]
 83c: [[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]]
 84*/
 85
 86
 87alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
 88
 89
 90ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
 91
 92// calculate trusted setup
 93setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
 94
 95hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
 96
 97proof, err := GenerateProofs(*circuit, setup, w, px)
 98
 99b35Verif := big.NewInt(int64(35))
100publicSignalsVerif := []*big.Int{b35Verif}
101assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
102```
103
104### CLI usage
105
106#### Compile circuit
107Having a circuit file `test.circuit`:
108```
109func test(private s0, public s1):
110	s2 = s0 * s0
111	s3 = s2 * s0
112	s4 = s3 + s0
113	s5 = s4 + 5
114	equals(s1, s5)
115	out = 1 * 1
116```
117And a private inputs file `privateInputs.json`
118```
119[
120	3
121]
122```
123And a public inputs file `publicInputs.json`
124```
125[
126	35
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`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.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` `publicInputs.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.