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.

213 lines
7.6 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) [![Build Status](https://travis-ci.org/arnaucube/go-snark.svg?branch=master)](https://travis-ci.org/arnaucube/go-snark) [![Gitter](https://badges.gitter.im/go-snark/community.svg)](https://gitter.im/go-snark/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
  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. - `On the Size of Pairing-based Non-interactive Arguments`, Jens Groth https://eprint.iacr.org/2016/260.pdf
  6. ## Caution & Warning
  7. Implementation of the zkSNARK [Pinocchio protocol](https://eprint.iacr.org/2013/279.pdf) and [Groth16 protocol](https://eprint.iacr.org/2016/260.pdf) from scratch in Go to understand the concepts. Do not use in production.
  8. Not finished, implementing this in my free time to understand it better, so I don't have much time.
  9. Currently allows to do the complete path with [Pinocchio protocol](https://eprint.iacr.org/2013/279.pdf) and [Groth16 protocol](https://eprint.iacr.org/2016/260.pdf) :
  10. 0. write circuit
  11. 1. compile circuit
  12. 2. generate trusted setup
  13. 3. calculate witness
  14. 4. generate proofs
  15. 5. verify proofs
  16. Minimal complete flow implementation:
  17. - [x] Finite Fields (1, 2, 6, 12) operations
  18. - [x] G1 and G2 curve operations
  19. - [x] BN128 Pairing
  20. - [x] circuit flat code compiler
  21. - [x] circuit to R1CS
  22. - [x] polynomial operations
  23. - [x] R1CS to QAP
  24. - [x] generate trusted setup
  25. - [x] generate proofs
  26. - [x] verify proofs with BN128 pairing
  27. Improvements from the minimal implementation:
  28. - [x] allow to call functions in circuits language
  29. - [x] allow `import` in circuits language
  30. - [ ] allow `for` in circuits language
  31. - [ ] move witness values calculation outside the setup phase
  32. - [x] Groth16
  33. - [ ] multiple optimizations
  34. ## Usage
  35. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
  36. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/groth16?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/groth16) zkSnark Groth16
  37. - [![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)
  38. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
  39. - [![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)
  40. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
  41. ### CLI usage
  42. *The cli still needs some improvements, such as seting input files, etc.*
  43. In this example we will follow the equation example from [Vitalik](https://medium.com/@VitalikButerin/quadratic-arithmetic-programs-from-zero-to-hero-f6d558cea649)'s article: `y = x^3 + x + 5`, where `y==35` and `x==3`. So we want to prove that we know a secret `x` such as the result of the equation is `35`.
  44. #### Compile circuit
  45. Having a circuit file `test.circuit`:
  46. ```
  47. func exp3(private a):
  48. b = a * a
  49. c = a * b
  50. return c
  51. func main(private s0, public s1):
  52. s3 = exp3(s0)
  53. s4 = s3 + s0
  54. s5 = s4 + 5
  55. equals(s1, s5)
  56. out = 1 * 1
  57. ```
  58. And a private inputs file `privateInputs.json`
  59. ```
  60. [
  61. 3
  62. ]
  63. ```
  64. And a public inputs file `publicInputs.json`
  65. ```
  66. [
  67. 35
  68. ]
  69. ```
  70. In the command line, execute:
  71. ```
  72. > ./go-snark-cli compile test.circuit
  73. ```
  74. This will output the `compiledcircuit.json` file.
  75. #### Trusted Setup
  76. Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
  77. ```
  78. > ./go-snark-cli trustedsetup
  79. ```
  80. 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`.
  81. #### Generate Proofs
  82. Assumming that we have the `compiledcircuit.json`, `trustedsetup.json`, `privateInputs.json` and the `publicInputs.json` we can now generate the `Proofs` with the following command:
  83. ```
  84. > ./go-snark-cli genproofs
  85. ```
  86. This will store the file `proofs.json`, that contains all the SNARK proofs.
  87. #### Verify Proofs
  88. Having the `proofs.json`, `compiledcircuit.json`, `trustedsetup.json` `publicInputs.json` files, we can now verify the `Pairings` of the proofs, in order to verify the proofs.
  89. ```
  90. > ./go-snark-cli verify
  91. ```
  92. This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
  93. ### Cli using Groth16
  94. All this process can be done using [Groth16 protocol](https://eprint.iacr.org/2016/260.pdf) protocol:
  95. ```
  96. > ./go-snark-cli compile test.circuit
  97. > ./go-snark-cli groth16 trustedsetup
  98. > ./go-snark-cli groth16 genproofs
  99. > ./go-snark-cli verify
  100. ```
  101. ### Library usage
  102. Example:
  103. ```go
  104. // compile circuit and get the R1CS
  105. flatCode := `
  106. func exp3(private a):
  107. b = a * a
  108. c = a * b
  109. return c
  110. func main(private s0, public s1):
  111. s3 = exp3(s0)
  112. s4 = s3 + s0
  113. s5 = s4 + 5
  114. equals(s1, s5)
  115. out = 1 * 1
  116. `
  117. // parse the code
  118. parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
  119. circuit, err := parser.Parse()
  120. assert.Nil(t, err)
  121. fmt.Println(circuit)
  122. b3 := big.NewInt(int64(3))
  123. privateInputs := []*big.Int{b3}
  124. b35 := big.NewInt(int64(35))
  125. publicSignals := []*big.Int{b35}
  126. // witness
  127. w, err := circuit.CalculateWitness(privateInputs, publicSignals)
  128. assert.Nil(t, err)
  129. fmt.Println("witness", w)
  130. // now we have the witness:
  131. // w = [1 35 3 9 27 30 35 1]
  132. // flat code to R1CS
  133. fmt.Println("generating R1CS from flat code")
  134. a, b, c := circuit.GenerateR1CS()
  135. /*
  136. now we have the R1CS from the circuit:
  137. a: [[0 0 1 0 0 0 0 0] [0 0 1 0 0 0 0 0] [0 0 1 0 1 0 0 0] [5 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0] [0 1 0 0 0 0 0 0] [1 0 0 0 0 0 0 0]]
  138. b: [[0 0 1 0 0 0 0 0] [0 0 0 1 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0]]
  139. c: [[0 0 0 1 0 0 0 0] [0 0 0 0 1 0 0 0] [0 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0] [0 1 0 0 0 0 0 0] [0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 1]]
  140. */
  141. alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
  142. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  143. // calculate trusted setup
  144. setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
  145. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  146. proof, err := GenerateProofs(*circuit, setup, w, px)
  147. b35Verif := big.NewInt(int64(35))
  148. publicSignalsVerif := []*big.Int{b35Verif}
  149. assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
  150. ```
  151. ## Versions
  152. History of versions & tags of this project:
  153. - v0.0.1: zkSnark complete flow working with Pinocchio protocol
  154. - v0.0.2: circuit language improved (allow function calls and file imports)
  155. - v0.0.3: Groth16 zkSnark protocol added
  156. ## Test
  157. ```
  158. go test ./... -v
  159. ```
  160. ## vim/nvim circuit syntax highlighter
  161. For more details and installation instructions see https://github.com/arnaucube/go-snark/tree/master/vim-syntax
  162. ---
  163. 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.