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.

173 lines
5.7 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. UNDER CONSTRUCTION!
  7. 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.
  8. This fork aims to extend its functionalities s.t. one can prove set-membership in zero knowledge.
  9. Current implementation status:
  10. - [x] Finite Fields (1, 2, 6, 12) operations
  11. - [x] G1 and G2 curve operations
  12. - [x] BN128 Pairing (to be replaced with less unsecure curve)
  13. - [x] circuit code compiler
  14. - [ ] code to flat code (improve circuit compiler) (in progress)
  15. - [x] flat code compiler
  16. - [x] circuit to R1CS with gate reduction optimisation
  17. - [x] polynomial operations
  18. - [x] R1CS to QAP
  19. - [x] generate trusted setup
  20. - [x] generate proofs
  21. - [x] verify proofs with BN128 pairing
  22. - [x] move witness calculation outside the setup phase
  23. - [ ] Groth16 (in progress)
  24. - [ ] multiple optimizations
  25. ## Usage
  26. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
  27. - [![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)
  28. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
  29. - [![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)
  30. - [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
  31. ### CLI usage
  32. #### Compile circuit
  33. Having a circuit file `test.circuit`:
  34. ```
  35. func test(private s0, public s1):
  36. s2 = s0 * s0
  37. s3 = s2 * s0
  38. s4 = s3 + s0
  39. s5 = s4 + 5
  40. equals(s1, s5)
  41. out = 1 * 1
  42. ```
  43. And a private inputs file `privateInputs.json`
  44. ```
  45. [
  46. 3
  47. ]
  48. ```
  49. And a public inputs file `publicInputs.json`
  50. ```
  51. [
  52. 35
  53. ]
  54. ```
  55. In the command line, execute:
  56. ```
  57. > ./go-snark-cli compile test.circuit
  58. ```
  59. This will output the `compiledcircuit.json` file.
  60. #### Trusted Setup
  61. Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
  62. ```
  63. > ./go-snark-cli trustedsetup
  64. ```
  65. 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`.
  66. #### Generate Proofs
  67. 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:
  68. ```
  69. > ./go-snark-cli genproofs
  70. ```
  71. This will store the file `proofs.json`, that contains all the SNARK proofs.
  72. #### Verify Proofs
  73. 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.
  74. ```
  75. > ./go-snark-cli verify
  76. ```
  77. This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
  78. ### Library usage
  79. Warning: not finished.
  80. Example:
  81. ```go
  82. // compile circuit and get the R1CS
  83. flatCode := `
  84. func test(private s0, public s1):
  85. s2 = s0 * s0
  86. s3 = s2 * s0
  87. s4 = s3 + s0
  88. s5 = s4 + 5
  89. equals(s1, s5)
  90. out = 1 * 1
  91. `
  92. // parse the code
  93. parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
  94. circuit, err := parser.Parse()
  95. assert.Nil(t, err)
  96. fmt.Println(circuit)
  97. b3 := big.NewInt(int64(3))
  98. privateInputs := []*big.Int{b3}
  99. b35 := big.NewInt(int64(35))
  100. publicSignals := []*big.Int{b35}
  101. // witness
  102. w, err := circuit.CalculateWitness(privateInputs, publicSignals)
  103. assert.Nil(t, err)
  104. fmt.Println("witness", w)
  105. // now we have the witness:
  106. // w = [1 35 3 9 27 30 35 1]
  107. // flat code to R1CS
  108. fmt.Println("generating R1CS from flat code")
  109. a, b, c := circuit.GenerateR1CS()
  110. /*
  111. now we have the R1CS from the circuit:
  112. 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]]
  113. 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]]
  114. 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]]
  115. */
  116. alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
  117. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  118. // calculate trusted setup
  119. setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
  120. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  121. proof, err := GenerateProofs(*circuit, setup, w, px)
  122. b35Verif := big.NewInt(int64(35))
  123. publicSignalsVerif := []*big.Int{b35Verif}
  124. assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
  125. ```
  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.