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
 14Currently allows to do the complete path with [Pinocchio protocol](https://eprint.iacr.org/2013/279.pdf) :
 151. compile circuuit
 162. generate trusted setup
 173. calculate witness
 184. generate proofs
 195. verify proofs
 20
 21Minimal complete flow implementation:
 22- [x] Finite Fields (1, 2, 6, 12) operations
 23- [x] G1 and G2 curve operations
 24- [x] BN128 Pairing
 25- [x] circuit flat code compiler
 26- [x] circuit to R1CS
 27- [x] polynomial operations
 28- [x] R1CS to QAP
 29- [x] generate trusted setup
 30- [x] generate proofs
 31- [x] verify proofs with BN128 pairing
 32
 33Improvements from the minimal implementation:
 34- [ ] allow `import` in circuits language
 35- [ ] allow `for` in circuits language
 36- [ ] code to flat code (improve circuit compiler)
 37- [ ] move witness values calculation outside the setup phase
 38- [ ] Groth16
 39- [ ] multiple optimizations
 40
 41
 42## Usage
 43- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
 44- [![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)
 45- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
 46- [![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)
 47- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
 48
 49### CLI usage
 50*The cli still needs some improvements, such as seting input files, etc.*
 51
 52In 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`.
 53
 54#### Compile circuit
 55Having a circuit file `test.circuit`:
 56```
 57func test(private s0, public s1):
 58	s2 = s0 * s0
 59	s3 = s2 * s0
 60	s4 = s3 + s0
 61	s5 = s4 + 5
 62	equals(s1, s5)
 63	out = 1 * 1
 64```
 65And a private inputs file `privateInputs.json`
 66```
 67[
 68	3
 69]
 70```
 71And a public inputs file `publicInputs.json`
 72```
 73[
 74	35
 75]
 76```
 77
 78In the command line, execute:
 79```
 80> ./go-snark-cli compile test.circuit
 81```
 82
 83This will output the `compiledcircuit.json` file.
 84
 85#### Trusted Setup
 86Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
 87```
 88> ./go-snark-cli trustedsetup
 89```
 90This 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`.
 91
 92
 93#### Generate Proofs
 94Assumming that we have the `compiledcircuit.json`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.json` we can now generate the `Proofs` with the following command:
 95```
 96> ./go-snark-cli genproofs
 97```
 98
 99This will store the file `proofs.json`, that contains all the SNARK proofs.
100
101#### Verify Proofs
102Having 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.
103```
104> ./go-snark-cli verify
105```
106This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
107
108
109### Library usage
110
111Example:
112```go
113// compile circuit and get the R1CS
114flatCode := `
115func test(private s0, public s1):
116	s2 = s0 * s0
117	s3 = s2 * s0
118	s4 = s3 + s0
119	s5 = s4 + 5
120	equals(s1, s5)
121	out = 1 * 1
122`
123
124// parse the code
125parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
126circuit, err := parser.Parse()
127assert.Nil(t, err)
128fmt.Println(circuit)
129
130
131b3 := big.NewInt(int64(3))
132privateInputs := []*big.Int{b3}
133b35 := big.NewInt(int64(35))
134publicSignals := []*big.Int{b35}
135
136// witness
137w, err := circuit.CalculateWitness(privateInputs, publicSignals)
138assert.Nil(t, err)
139fmt.Println("witness", w)
140
141// now we have the witness:
142// w = [1 35 3 9 27 30 35 1]
143
144// flat code to R1CS
145fmt.Println("generating R1CS from flat code")
146a, b, c := circuit.GenerateR1CS()
147
148/*
149now we have the R1CS from the circuit:
150a: [[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]]
151b: [[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]]
152c: [[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]]
153*/
154
155
156alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
157
158
159ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
160
161// calculate trusted setup
162setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
163
164hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
165
166proof, err := GenerateProofs(*circuit, setup, w, px)
167
168b35Verif := big.NewInt(int64(35))
169publicSignalsVerif := []*big.Int{b35Verif}
170assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
171```
172
173
174
175## Test
176```
177go test ./... -v
178```
179
180## vim/nvim circuit syntax highlighter
181For more details and installation instructions see https://github.com/arnaucube/go-snark/tree/master/vim-syntax
182
183---
184
185
186Thanks 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.