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.

185 lines
6.0 KiB

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. Currently allows to do the complete path with [Pinocchio protocol](https://eprint.iacr.org/2013/279.pdf) :
  9. - compile circuuit
  10. - parsers
  11. - R1CS
  12. - QAP
  13. - generate trusted setup
  14. - calculate witness
  15. - generate proofs
  16. - verify proofs
  17. - with BN128 pairing
  18. Current implementation status:
  19. - [x] Finite Fields (1, 2, 6, 12) operations
  20. - [x] G1 and G2 curve operations
  21. - [x] BN128 Pairing
  22. - [x] circuit code compiler
  23. - [ ] code to flat code (improve circuit compiler)
  24. - [x] flat code compiler
  25. - [x] circuit to R1CS
  26. - [x] polynomial operations
  27. - [x] R1CS to QAP
  28. - [x] generate trusted setup
  29. - [x] generate proofs
  30. - [x] verify proofs with BN128 pairing
  31. - [ ] move witness calculation outside the setup phase
  32. - [ ] 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/bn128?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/bn128) bn128 (more details: https://github.com/arnaucube/go-snark/tree/master/bn128)
  37. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
  38. - [![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)
  39. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
  40. ### CLI usage
  41. #### Compile circuit
  42. Having a circuit file `test.circuit`:
  43. ```
  44. func test(private s0, public s1):
  45. s2 = s0 * s0
  46. s3 = s2 * s0
  47. s4 = s3 + s0
  48. s5 = s4 + 5
  49. equals(s1, s5)
  50. out = 1 * 1
  51. ```
  52. And a private inputs file `privateInputs.json`
  53. ```
  54. [
  55. 3
  56. ]
  57. ```
  58. And a public inputs file `publicInputs.json`
  59. ```
  60. [
  61. 35
  62. ]
  63. ```
  64. In the command line, execute:
  65. ```
  66. > ./go-snark-cli compile test.circuit
  67. ```
  68. This will output the `compiledcircuit.json` file.
  69. #### Trusted Setup
  70. Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
  71. ```
  72. > ./go-snark-cli trustedsetup
  73. ```
  74. 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`.
  75. #### Generate Proofs
  76. 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:
  77. ```
  78. > ./go-snark-cli genproofs
  79. ```
  80. This will store the file `proofs.json`, that contains all the SNARK proofs.
  81. #### Verify Proofs
  82. 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.
  83. ```
  84. > ./go-snark-cli verify
  85. ```
  86. This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
  87. ### Library usage
  88. Warning: not finished.
  89. Example:
  90. ```go
  91. // compile circuit and get the R1CS
  92. flatCode := `
  93. func test(private s0, public s1):
  94. s2 = s0 * s0
  95. s3 = s2 * s0
  96. s4 = s3 + s0
  97. s5 = s4 + 5
  98. equals(s1, s5)
  99. out = 1 * 1
  100. `
  101. // parse the code
  102. parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
  103. circuit, err := parser.Parse()
  104. assert.Nil(t, err)
  105. fmt.Println(circuit)
  106. b3 := big.NewInt(int64(3))
  107. privateInputs := []*big.Int{b3}
  108. b35 := big.NewInt(int64(35))
  109. publicSignals := []*big.Int{b35}
  110. // witness
  111. w, err := circuit.CalculateWitness(privateInputs, publicSignals)
  112. assert.Nil(t, err)
  113. fmt.Println("witness", w)
  114. // now we have the witness:
  115. // w = [1 35 3 9 27 30 35 1]
  116. // flat code to R1CS
  117. fmt.Println("generating R1CS from flat code")
  118. a, b, c := circuit.GenerateR1CS()
  119. /*
  120. now we have the R1CS from the circuit:
  121. a: [[0 0 1 0 0 0 0 0] [0 0 0 1 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]]
  122. b: [[0 0 1 0 0 0 0 0] [0 0 1 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] [1 0 0 0 0 0 0 0]]
  123. 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]]
  124. */
  125. alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
  126. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  127. // calculate trusted setup
  128. setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
  129. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  130. proof, err := GenerateProofs(*circuit, setup, w, px)
  131. b35Verif := big.NewInt(int64(35))
  132. publicSignalsVerif := []*big.Int{b35Verif}
  133. assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
  134. ```
  135. ## Test
  136. ```
  137. go test ./... -v
  138. ```
  139. ## vim/nvim circuit syntax highlighter
  140. For more details and installation instructions see https://github.com/arnaucube/go-snark/tree/master/vim-syntax
  141. ---
  142. 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.