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### CLI usage
 40
 41#### Compile circuit
 42Having a circuit file `test.circuit`:
 43```
 44func test(private s0, public s1):
 45	s2 = s0 * s0
 46	s3 = s2 * s0
 47	s4 = s3 + s0
 48	s5 = s4 + 5
 49	equals(s1, s5)
 50	out = 1 * 1
 51```
 52And a private inputs file `privateInputs.json`
 53```
 54[
 55	3
 56]
 57```
 58And a public inputs file `publicInputs.json`
 59```
 60[
 61	35
 62]
 63```
 64
 65In the command line, execute:
 66```
 67> ./go-snark-cli compile test.circuit
 68```
 69
 70This will output the `compiledcircuit.json` file.
 71
 72#### Trusted Setup
 73Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
 74```
 75> ./go-snark-cli trustedsetup
 76```
 77This 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`.
 78
 79
 80#### Generate Proofs
 81Assumming that we have the `compiledcircuit.json`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.json` we can now generate the `Proofs` with the following command:
 82```
 83> ./go-snark-cli genproofs
 84```
 85
 86This will store the file `proofs.json`, that contains all the SNARK proofs.
 87
 88#### Verify Proofs
 89Having 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.
 90```
 91> ./go-snark-cli verify
 92```
 93This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
 94
 95
 96### Library usage
 97Warning: not finished.
 98
 99Example:
100```go
101// compile circuit and get the R1CS
102flatCode := `
103func test(private s0, public s1):
104	s2 = s0 * s0
105	s3 = s2 * s0
106	s4 = s3 + s0
107	s5 = s4 + 5
108	equals(s1, s5)
109	out = 1 * 1
110`
111
112// parse the code
113parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
114circuit, err := parser.Parse()
115assert.Nil(t, err)
116fmt.Println(circuit)
117
118
119b3 := big.NewInt(int64(3))
120privateInputs := []*big.Int{b3}
121b35 := big.NewInt(int64(35))
122publicSignals := []*big.Int{b35}
123
124// witness
125w, err := circuit.CalculateWitness(privateInputs, publicSignals)
126assert.Nil(t, err)
127fmt.Println("witness", w)
128
129// now we have the witness:
130// w = [1 35 3 9 27 30 35 1]
131
132// flat code to R1CS
133fmt.Println("generating R1CS from flat code")
134a, b, c := circuit.GenerateR1CS()
135
136/*
137now we have the R1CS from the circuit:
138a: [[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]]
139b: [[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]]
140c: [[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]]
141*/
142
143
144alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
145
146
147ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
148
149// calculate trusted setup
150setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
151
152hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
153
154proof, err := GenerateProofs(*circuit, setup, w, px)
155
156b35Verif := big.NewInt(int64(35))
157publicSignalsVerif := []*big.Int{b35Verif}
158assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
159```
160
161
162
163## Test
164```
165go test ./... -v
166```
167
168---
169
170
171Thanks 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.