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.

171 lines
5.4 KiB

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