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
 14
 15### Usage
 16- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
 17- [![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)
 18- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
 19- [![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)
 20- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
 21
 22Example:
 23```go
 24bn, err := bn128.NewBn128()
 25assert.Nil(t, err)
 26
 27// new Finite Field
 28fqR := fields.NewFq(bn.R)
 29
 30// new Polynomial Field
 31pf := r1csqap.NewPolynomialField(f)
 32
 33// compile circuit and get the R1CS
 34flatCode := `
 35func test(x):
 36	aux = x*x
 37	y = aux*x
 38	z = x + y
 39	out = z + 5
 40`
 41// parse the code
 42parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
 43circuit, err := parser.Parse()
 44assert.Nil(t, err)
 45fmt.Println(circuit)
 46// flat code to R1CS
 47fmt.Println("generating R1CS from flat code")
 48a, b, c := circuit.GenerateR1CS()
 49
 50/*
 51now we have the R1CS from the circuit:
 52a == [[0 1 0 0 0 0] [0 0 0 1 0 0] [0 1 0 0 1 0] [5 0 0 0 0 1]]
 53b == [[0 1 0 0 0 0] [0 1 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 0 0]]
 54c == [[0 0 0 1 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [0 0 1 0 0 0]]
 55*/
 56
 57
 58alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
 59
 60// wittness = 1, 3, 35, 9, 27, 30
 61w := []*big.Int{b1, b3, b35, b9, b27, b30}
 62
 63ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
 64
 65hx := pf.DivisorPolinomial(px, zx)
 66
 67// hx==px/zx so px==hx*zx
 68assert.Equal(t, px, pf.Mul(hx, zx))
 69
 70// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
 71abc := pf.Sub(pf.Mul(ax, bx), cx)
 72assert.Equal(t, abc, px)
 73hz := pf.Mul(hx, zx)
 74assert.Equal(t, abc, hz)
 75	
 76div, rem := pf.Div(px, zx)
 77assert.Equal(t, hx, div)
 78assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
 79
 80// calculate trusted setup
 81setup, err := GenerateTrustedSetup(bn, fqR, pf, len(w), circuit, alphas, betas, gammas, zx)
 82assert.Nil(t, err)
 83fmt.Println("t", setup.Toxic.T)
 84
 85// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
 86proof, err := GenerateProofs(bn, fqR, circuit, setup, hx, w)
 87assert.Nil(t, err)
 88
 89assert.True(t, VerifyProof(bn, circuit, setup, proof))
 90```
 91
 92### Test
 93```
 94go test ./... -v
 95```
 96
 97---
 98
 99
100Thanks 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.