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.

84 lines
3.0 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. `Pinocchio: Nearly practical verifiable computation`, Bryan Parno, Craig Gentry, Jon Howell, Mariana Raykova https://eprint.iacr.org/2013/279.pdf
  5. ### Usage
  6. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
  7. - [![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)
  8. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
  9. - [![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)
  10. Example:
  11. ```go
  12. bn, err := bn128.NewBn128()
  13. assert.Nil(t, err)
  14. // new Finite Field
  15. fqR := fields.NewFq(bn.R)
  16. // new Polynomial Field
  17. pf := r1csqap.NewPolynomialField(f)
  18. /*
  19. suppose that we have the following variables with *big.Int elements:
  20. 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]]
  21. 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]]
  22. 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]]
  23. w = [1, 3, 35, 9, 27, 30]
  24. */
  25. alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
  26. // wittness = 1, 3, 35, 9, 27, 30
  27. w := []*big.Int{b1, b3, b35, b9, b27, b30}
  28. circuit := compiler.Circuit{
  29. NVars: 6,
  30. NPublic: 0,
  31. NSignals: len(w),
  32. }
  33. ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
  34. hx := pf.DivisorPolinomial(px, zx)
  35. // hx==px/zx so px==hx*zx
  36. assert.Equal(t, px, pf.Mul(hx, zx))
  37. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  38. abc := pf.Sub(pf.Mul(ax, bx), cx)
  39. assert.Equal(t, abc, px)
  40. hz := pf.Mul(hx, zx)
  41. assert.Equal(t, abc, hz)
  42. div, rem := pf.Div(px, zx)
  43. assert.Equal(t, hx, div)
  44. assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
  45. // calculate trusted setup
  46. setup, err := GenerateTrustedSetup(bn, fqR, pf, len(w), circuit, alphas, betas, gammas, zx)
  47. assert.Nil(t, err)
  48. fmt.Println("t", setup.Toxic.T)
  49. // piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
  50. proof, err := GenerateProofs(bn, fqR, circuit, setup, hx, w)
  51. assert.Nil(t, err)
  52. assert.True(t, VerifyProof(bn, circuit, setup, proof))
  53. ```
  54. ### Test
  55. ```
  56. go test ./... -v
  57. ```
  58. ---
  59. ## Caution
  60. Not finished, work in progress (implementing this in my free time to understand it better, so I don't have much time).
  61. 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.