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.

166 lines
5.2 KiB

5 years ago
5 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. ## 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 curve 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. #### Library usage
  28. Example:
  29. ```go
  30. // compile circuit and get the R1CS
  31. flatCode := `
  32. func test(x):
  33. aux = x*x
  34. y = aux*x
  35. z = x + y
  36. out = z + 5
  37. `
  38. // parse the code
  39. parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
  40. circuit, err := parser.Parse()
  41. assert.Nil(t, err)
  42. fmt.Println(circuit)
  43. // witness
  44. b3 := big.NewInt(int64(3))
  45. inputs := []*big.Int{b3}
  46. w := circuit.CalculateWitness(inputs)
  47. fmt.Println("\nwitness", w)
  48. /*
  49. now we have the witness:
  50. w = [1 3 35 9 27 30]
  51. */
  52. // flat code to R1CS
  53. fmt.Println("generating R1CS from flat code")
  54. a, b, c := circuit.GenerateR1CS()
  55. /*
  56. now we have the R1CS from the circuit:
  57. 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]]
  58. 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]]
  59. 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]]
  60. */
  61. alphas, betas, gammas, zx := snark.Utils.PF.R1CSToQAP(a, b, c)
  62. ax, bx, cx, px := snark.Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  63. hx := snark.Utils.PF.DivisorPolinomial(px, zx)
  64. // hx==px/zx so px==hx*zx
  65. assert.Equal(t, px, snark.Utils.PF.Mul(hx, zx))
  66. // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
  67. abc := snark.Utils.PF.Sub(pf.Mul(ax, bx), cx)
  68. assert.Equal(t, abc, px)
  69. hz := snark.Utils.PF.Mul(hx, zx)
  70. assert.Equal(t, abc, hz)
  71. div, rem := snark.Utils.PF.Div(px, zx)
  72. assert.Equal(t, hx, div)
  73. assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
  74. // calculate trusted setup
  75. setup, err := snark.GenerateTrustedSetup(len(w), circuit, alphas, betas, gammas, zx)
  76. assert.Nil(t, err)
  77. fmt.Println("t", setup.Toxic.T)
  78. // piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
  79. proof, err := snark.GenerateProofs(circuit, setup, hx, w)
  80. assert.Nil(t, err)
  81. assert.True(t, snark.VerifyProof(circuit, setup, proof))
  82. ```
  83. #### CLI usage
  84. ##### Compile circuit
  85. Having a circuit file `test.circuit`:
  86. ```
  87. func test(x):
  88. aux = x*x
  89. y = aux*x
  90. z = x + y
  91. out = z + 5
  92. ```
  93. And a inputs file `inputs.json`
  94. ```
  95. [
  96. 3
  97. ]
  98. ```
  99. In the command line, execute:
  100. ```
  101. > go-snark compile test.circuit
  102. ```
  103. This will output the `compiledcircuit.json` file.
  104. ##### Trusted Setup
  105. Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
  106. ```
  107. > go-snark trustedsetup compiledcircuit.json
  108. ```
  109. This will create the file `trustedsetup.json` with the TrustedSetup data, and also a `toxic.json` file, with the parameters to delete from the `Trusted Setup`.
  110. ##### Generate Proofs
  111. Assumming that we have the `compiledcircuit.json` and the `trustedsetup.json`, we can now generate the `Proofs` with the following command:
  112. ```
  113. > go-snark genproofs
  114. ```
  115. This will store the file `proofs.json`, that contains all the SNARK proofs.
  116. ##### Verify Proofs
  117. Having the `proofs.json`, `compiledcircuit.json`, `trustedsetup.json` files, we can now verify the `Pairings` of the proofs, in order to verify the proofs.
  118. ```
  119. > go-snark verify
  120. ```
  121. This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
  122. ### Test
  123. ```
  124. go test ./... -v
  125. ```
  126. ---
  127. 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.