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 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
 36Example:
 37```go
 38bn, err := bn128.NewBn128()
 39assert.Nil(t, err)
 40
 41// new Finite Field
 42fqR := fields.NewFq(bn.R)
 43
 44// new Polynomial Field
 45pf := r1csqap.NewPolynomialField(f)
 46
 47// compile circuit and get the R1CS
 48flatCode := `
 49func test(x):
 50	aux = x*x
 51	y = aux*x
 52	z = x + y
 53	out = z + 5
 54`
 55// parse the code
 56parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
 57circuit, err := parser.Parse()
 58assert.Nil(t, err)
 59fmt.Println(circuit)
 60// flat code to R1CS
 61fmt.Println("generating R1CS from flat code")
 62a, b, c := circuit.GenerateR1CS()
 63
 64/*
 65now we have the R1CS from the circuit:
 66a == [[0 1 0 0 0 0] [0 0 0 1 0 0] [0 1 0 0 1 0] [5 0 0 0 0 1]]
 67b == [[0 1 0 0 0 0] [0 1 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 0 0]]
 68c == [[0 0 0 1 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [0 0 1 0 0 0]]
 69*/
 70
 71
 72alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
 73
 74// wittness
 75b3 := big.NewInt(int64(3))
 76inputs := []*big.Int{b3}
 77w := circuit.CalculateWitness(inputs)
 78fmt.Println("\nwitness", w)
 79
 80ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
 81
 82hx := pf.DivisorPolinomial(px, zx)
 83
 84// hx==px/zx so px==hx*zx
 85assert.Equal(t, px, pf.Mul(hx, zx))
 86
 87// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
 88abc := pf.Sub(pf.Mul(ax, bx), cx)
 89assert.Equal(t, abc, px)
 90hz := pf.Mul(hx, zx)
 91assert.Equal(t, abc, hz)
 92	
 93div, rem := pf.Div(px, zx)
 94assert.Equal(t, hx, div)
 95assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
 96
 97// calculate trusted setup
 98setup, err := GenerateTrustedSetup(bn, fqR, pf, len(w), circuit, alphas, betas, gammas, zx)
 99assert.Nil(t, err)
100fmt.Println("t", setup.Toxic.T)
101
102// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
103proof, err := GenerateProofs(bn, fqR, circuit, setup, hx, w)
104assert.Nil(t, err)
105
106assert.True(t, VerifyProof(bn, circuit, setup, proof))
107```
108
109### Test
110```
111go test ./... -v
112```
113
114---
115
116
117Thanks 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.