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.

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