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.

88 lines
3.4 KiB

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