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.

235 lines
8.3 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. - [x] wasm proof generation
  35. - [x] wasm proof verification
  36. ## WASM usage
  37. Ongoing experimentation with go-snark compiled to wasm: https://github.com/arnaucube/go-snark/tree/master/wasm
  38. ## Usage
  39. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
  40. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/groth16?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/groth16) zkSnark Groth16
  41. - [![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)
  42. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
  43. - [![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)
  44. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
  45. ### CLI usage
  46. *The cli still needs some improvements, such as seting input files, etc.*
  47. 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`.
  48. #### Compile circuit
  49. Having a circuit file `test.circuit`:
  50. ```
  51. func exp3(private a):
  52. b = a * a
  53. c = a * b
  54. return c
  55. func main(private s0, public s1):
  56. s3 = exp3(s0)
  57. s4 = s3 + s0
  58. s5 = s4 + 5
  59. equals(s1, s5)
  60. out = 1 * 1
  61. ```
  62. And a private inputs file `privateInputs.json`
  63. ```
  64. [
  65. 3
  66. ]
  67. ```
  68. And a public inputs file `publicInputs.json`
  69. ```
  70. [
  71. 35
  72. ]
  73. ```
  74. In the command line, execute:
  75. ```
  76. > ./go-snark-cli compile test.circuit
  77. ```
  78. If you want to have the wasm input ready also, add the flag `wasm`
  79. ```
  80. > ./go-snark-cli compile test.circuit wasm
  81. ```
  82. This will output the `compiledcircuit.json` file.
  83. #### Trusted Setup
  84. Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
  85. ```
  86. > ./go-snark-cli trustedsetup
  87. ```
  88. 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`.
  89. If you want to have the wasm input ready also, add the flag `wasm`
  90. ```
  91. > ./go-snark-cli trustedsetup wasm
  92. ```
  93. #### Generate Proofs
  94. 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:
  95. ```
  96. > ./go-snark-cli genproofs
  97. ```
  98. This will store the file `proofs.json`, that contains all the SNARK proofs.
  99. #### Verify Proofs
  100. 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.
  101. ```
  102. > ./go-snark-cli verify
  103. ```
  104. This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
  105. ### Cli using Groth16
  106. All this process can be done using [Groth16 protocol](https://eprint.iacr.org/2016/260.pdf) protocol:
  107. ```
  108. > ./go-snark-cli compile test.circuit
  109. > ./go-snark-cli groth16 trustedsetup
  110. > ./go-snark-cli groth16 genproofs
  111. > ./go-snark-cli verify
  112. ```
  113. ### Library usage
  114. Example:
  115. ```go
  116. // compile circuit and get the R1CS
  117. flatCode := `
  118. func exp3(private a):
  119. b = a * a
  120. c = a * b
  121. return c
  122. func main(private s0, public s1):
  123. s3 = exp3(s0)
  124. s4 = s3 + s0
  125. s5 = s4 + 5
  126. equals(s1, s5)
  127. out = 1 * 1
  128. `
  129. // parse the code
  130. parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
  131. circuit, err := parser.Parse()
  132. assert.Nil(t, err)
  133. fmt.Println(circuit)
  134. b3 := big.NewInt(int64(3))
  135. privateInputs := []*big.Int{b3}
  136. b35 := big.NewInt(int64(35))
  137. publicSignals := []*big.Int{b35}
  138. // witness
  139. w, err := circuit.CalculateWitness(privateInputs, publicSignals)
  140. assert.Nil(t, err)
  141. fmt.Println("witness", w)
  142. // now we have the witness:
  143. // w = [1 35 3 9 27 30 35 1]
  144. // flat code to R1CS
  145. fmt.Println("generating R1CS from flat code")
  146. a, b, c := circuit.GenerateR1CS()
  147. /*
  148. now we have the R1CS from the circuit:
  149. 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]]
  150. 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]]
  151. 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]]
  152. */
  153. alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
  154. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  155. // calculate trusted setup
  156. setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
  157. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  158. proof, err := GenerateProofs(*circuit, setup, w, px)
  159. b35Verif := big.NewInt(int64(35))
  160. publicSignalsVerif := []*big.Int{b35Verif}
  161. assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
  162. ```
  163. ##### Verify Proof generated from [snarkjs](https://github.com/iden3/snarkjs)
  164. Is possible with `go-snark` to verify proofs generated by `snarkjs`
  165. Example:
  166. ```go
  167. verified, err := VerifyFromCircom("circom-test/verification_key.json", "circom-test/proof.json", "circom-test/public.json")
  168. assert.Nil(t, err)
  169. assert.True(t, verified)
  170. ```
  171. ## Versions
  172. History of versions & tags of this project:
  173. - v0.0.1: zkSnark complete flow working with Pinocchio protocol
  174. - v0.0.2: circuit language improved (allow function calls and file imports)
  175. - v0.0.3: Groth16 zkSnark protocol added
  176. ## Test
  177. ```
  178. go test ./... -v
  179. ```
  180. ## vim/nvim circuit syntax highlighter
  181. For more details and installation instructions see https://github.com/arnaucube/go-snark/tree/master/vim-syntax
  182. ---
  183. 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.