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.

170 lines
5.5 KiB

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