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.

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