You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7 KiB

6 years ago
  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. zkSNARK library implementation in Go
  3. `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
  4. ### Usage
  5. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
  6. - [![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)
  7. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
  8. - [![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)
  9. Example:
  10. ```go
  11. bn, err := bn128.NewBn128()
  12. assert.Nil(t, err)
  13. // new Finite Field
  14. f := fields.NewFq(bn.R)
  15. // new Polynomial Field
  16. pf := r1csqap.NewPolynomialField(f)
  17. /*
  18. suppose that we have the following variables with *big.Int elements:
  19. a = [[0 1 0 0 0 0] [0 0 0 1 0 0] [0 1 0 0 1 0] [5 0 0 0 0 1]]
  20. b = [[0 1 0 0 0 0] [0 1 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 0 0]]
  21. c = [[0 0 0 1 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [0 0 1 0 0 0]]
  22. w = [1, 3, 35, 9, 27, 30]
  23. */
  24. alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
  25. ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
  26. hx := pf.DivisorPolinomial(px, zx)
  27. // hx==px/zx so px==hx*zx
  28. assert.Equal(t, px, pf.Mul(hx, zx))
  29. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  30. abc := pf.Sub(pf.Mul(ax, bx), cx)
  31. assert.Equal(t, abc, px)
  32. hz := pf.Mul(hx, zx)
  33. assert.Equal(t, abc, hz)
  34. // calculate trusted setup
  35. setup, err := GenerateTrustedSetup(bn, len(ax))
  36. assert.Nil(t, err)
  37. fmt.Println("trusted setup:")
  38. fmt.Println(setup.G1T)
  39. fmt.Println(setup.G2T)
  40. // piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
  41. proof, err := GenerateProofs(bn, f, setup, ax, bx, cx, hx, zx)
  42. assert.Nil(t, err)
  43. // verify the proofs with the bn128 pairing
  44. verified := VerifyProof(bn, publicSetup, proof)
  45. assert.True(t, verified)
  46. ```
  47. ### Test
  48. ```
  49. go test ./... -v
  50. ```
  51. ---
  52. ## Caution
  53. Not finished, work in progress (implementing this in my free time to understand it better, so I don't have much time).
  54. Thanks 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.