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.

225 lines
8.3 KiB

5 years ago
5 years ago
  1. ### Warning
  2. 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 done in my free time to understand the concepts. Do not use in production.
  3. If you want to generate proofs & verify them from Go, you can try https://github.com/vocdoni/go-snark, which is implemented using the [bn256](https://github.com/ethereum/go-ethereum/tree/master/crypto/bn256/cloudflare) for the Pairing curve operations for the Groth16 zkSNARK, and it is compatible with [circom](https://github.com/iden3/circom).
  4. # go-snark-study [![Go Report Card](https://goreportcard.com/badge/github.com/arnaucube/go-snark-study)](https://goreportcard.com/report/github.com/arnaucube/go-snark-study) [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark-study?status.svg)](https://godoc.org/github.com/arnaucube/go-snark-study)
  5. zkSNARK library implementation in Go
  6. - `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
  7. - `Pinocchio: Nearly practical verifiable computation`, Bryan Parno, Craig Gentry, Jon Howell, Mariana Raykova https://eprint.iacr.org/2013/279.pdf
  8. - `On the Size of Pairing-based Non-interactive Arguments`, Jens Groth https://eprint.iacr.org/2016/260.pdf
  9. ## Features
  10. 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) :
  11. 0. write circuit
  12. 1. compile circuit
  13. 2. generate trusted setup
  14. 3. calculate witness
  15. 4. generate proofs
  16. 5. verify proofs
  17. Minimal complete flow implementation:
  18. - [x] Finite Fields (1, 2, 6, 12) operations
  19. - [x] G1 and G2 curve operations
  20. - [x] BN128 Pairing
  21. - [x] circuit flat code compiler
  22. - [x] circuit to R1CS
  23. - [x] polynomial operations
  24. - [x] R1CS to QAP
  25. - [x] generate trusted setup
  26. - [x] generate proofs
  27. - [x] verify proofs with BN128 pairing
  28. ## WASM usage
  29. Experimentation with go-snark-study compiled to wasm: https://github.com/arnaucube/go-snark-study/tree/master/wasm
  30. ## Usage
  31. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark-study?status.svg)](https://godoc.org/github.com/arnaucube/go-snark-study) zkSnark
  32. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark-study/groth16?status.svg)](https://godoc.org/github.com/arnaucube/go-snark-study/groth16) zkSnark Groth16
  33. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark-study/bn128?status.svg)](https://godoc.org/github.com/arnaucube/go-snark-study/bn128) bn128 (more details: https://github.com/arnaucube/go-snark-study/tree/master/bn128)
  34. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark-study/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark-study/fields) Finite Fields operations
  35. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark-study/r1csqap?status.svg)](https://godoc.org/github.com/arnaucube/go-snark-study/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark-study/tree/master/r1csqap)
  36. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark-study/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark-study/circuitcompiler) Circuit Compiler
  37. ### CLI usage
  38. *The cli still needs some improvements, such as seting input files, etc.*
  39. 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`.
  40. #### Compile circuit
  41. Having a circuit file `test.circuit`:
  42. ```
  43. func exp3(private a):
  44. b = a * a
  45. c = a * b
  46. return c
  47. func main(private s0, public s1):
  48. s3 = exp3(s0)
  49. s4 = s3 + s0
  50. s5 = s4 + 5
  51. equals(s1, s5)
  52. out = 1 * 1
  53. ```
  54. And a private inputs file `privateInputs.json`
  55. ```
  56. [
  57. 3
  58. ]
  59. ```
  60. And a public inputs file `publicInputs.json`
  61. ```
  62. [
  63. 35
  64. ]
  65. ```
  66. In the command line, execute:
  67. ```
  68. > ./go-snark-cli compile test.circuit
  69. ```
  70. If you want to have the wasm input ready also, add the flag `wasm`
  71. ```
  72. > ./go-snark-cli compile test.circuit wasm
  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. If you want to have the wasm input ready also, add the flag `wasm`
  82. ```
  83. > ./go-snark-cli trustedsetup wasm
  84. ```
  85. #### Generate Proofs
  86. 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:
  87. ```
  88. > ./go-snark-cli genproofs
  89. ```
  90. This will store the file `proofs.json`, that contains all the SNARK proofs.
  91. #### Verify Proofs
  92. 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.
  93. ```
  94. > ./go-snark-cli verify
  95. ```
  96. This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
  97. ### Cli using Groth16
  98. All this process can be done using [Groth16 protocol](https://eprint.iacr.org/2016/260.pdf) protocol:
  99. ```
  100. > ./go-snark-cli compile test.circuit
  101. > ./go-snark-cli groth16 trustedsetup
  102. > ./go-snark-cli groth16 genproofs
  103. > ./go-snark-cli verify
  104. ```
  105. ### Library usage
  106. Example:
  107. ```go
  108. // compile circuit and get the R1CS
  109. flatCode := `
  110. func exp3(private a):
  111. b = a * a
  112. c = a * b
  113. return c
  114. func main(private s0, public s1):
  115. s3 = exp3(s0)
  116. s4 = s3 + s0
  117. s5 = s4 + 5
  118. equals(s1, s5)
  119. out = 1 * 1
  120. `
  121. // parse the code
  122. parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
  123. circuit, err := parser.Parse()
  124. assert.Nil(t, err)
  125. fmt.Println(circuit)
  126. b3 := big.NewInt(int64(3))
  127. privateInputs := []*big.Int{b3}
  128. b35 := big.NewInt(int64(35))
  129. publicSignals := []*big.Int{b35}
  130. // witness
  131. w, err := circuit.CalculateWitness(privateInputs, publicSignals)
  132. assert.Nil(t, err)
  133. fmt.Println("witness", w)
  134. // now we have the witness:
  135. // w = [1 35 3 9 27 30 35 1]
  136. // flat code to R1CS
  137. fmt.Println("generating R1CS from flat code")
  138. a, b, c := circuit.GenerateR1CS()
  139. /*
  140. now we have the R1CS from the circuit:
  141. 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]]
  142. 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]]
  143. 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]]
  144. */
  145. alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
  146. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  147. // calculate trusted setup
  148. setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
  149. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  150. proof, err := GenerateProofs(*circuit, setup, w, px)
  151. b35Verif := big.NewInt(int64(35))
  152. publicSignalsVerif := []*big.Int{b35Verif}
  153. assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
  154. ```
  155. ##### Verify Proof generated from [snarkjs](https://github.com/iden3/snarkjs)
  156. Is possible with `go-snark-study` to verify proofs generated by `snarkjs`
  157. Example:
  158. ```go
  159. verified, err := VerifyFromCircom("circom-test/verification_key.json", "circom-test/proof.json", "circom-test/public.json")
  160. assert.Nil(t, err)
  161. assert.True(t, verified)
  162. ```
  163. ## Versions
  164. History of versions & tags of this project:
  165. - v0.0.1: zkSnark complete flow working with Pinocchio protocol
  166. - v0.0.2: circuit language improved (allow function calls and file imports)
  167. - v0.0.3: Groth16 zkSnark protocol added
  168. ## Test
  169. ```
  170. go test ./... -v
  171. ```
  172. ## vim/nvim circuit syntax highlighter
  173. For more details and installation instructions see https://github.com/arnaucube/go-snark-study/tree/master/vim-syntax
  174. ---
  175. 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.