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.

171 lines
5.6 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. 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. ### CLI usage
  31. #### Compile circuit
  32. Having a circuit file `test.circuit`:
  33. ```
  34. func test(private s0, public s1):
  35. s2 = s0 * s0
  36. s3 = s2 * s0
  37. s4 = s3 + s0
  38. s5 = s4 + 5
  39. equals(s1, s5)
  40. out = 1 * 1
  41. ```
  42. And a private inputs file `privateInputs.json`
  43. ```
  44. [
  45. 3
  46. ]
  47. ```
  48. And a public inputs file `publicInputs.json`
  49. ```
  50. [
  51. 35
  52. ]
  53. ```
  54. In the command line, execute:
  55. ```
  56. > ./go-snark-cli compile test.circuit
  57. ```
  58. This will output the `compiledcircuit.json` file.
  59. #### Trusted Setup
  60. Having the `compiledcircuit.json`, now we can generate the `TrustedSetup`:
  61. ```
  62. > ./go-snark-cli trustedsetup
  63. ```
  64. 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`.
  65. #### Generate Proofs
  66. 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:
  67. ```
  68. > ./go-snark-cli genproofs
  69. ```
  70. This will store the file `proofs.json`, that contains all the SNARK proofs.
  71. #### Verify Proofs
  72. 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.
  73. ```
  74. > ./go-snark-cli verify
  75. ```
  76. This will return a `true` if the proofs are verified, or a `false` if the proofs are not verified.
  77. ### Library usage
  78. Warning: not finished.
  79. Example:
  80. ```go
  81. // compile circuit and get the R1CS
  82. flatCode := `
  83. func test(private s0, public s1):
  84. s2 = s0 * s0
  85. s3 = s2 * s0
  86. s4 = s3 + s0
  87. s5 = s4 + 5
  88. equals(s1, s5)
  89. out = 1 * 1
  90. `
  91. // parse the code
  92. parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
  93. circuit, err := parser.Parse()
  94. assert.Nil(t, err)
  95. fmt.Println(circuit)
  96. b3 := big.NewInt(int64(3))
  97. privateInputs := []*big.Int{b3}
  98. b35 := big.NewInt(int64(35))
  99. publicSignals := []*big.Int{b35}
  100. // witness
  101. w, err := circuit.CalculateWitness(privateInputs, publicSignals)
  102. assert.Nil(t, err)
  103. fmt.Println("witness", w)
  104. // now we have the witness:
  105. // w = [1 35 3 9 27 30 35 1]
  106. // flat code to R1CS
  107. fmt.Println("generating R1CS from flat code")
  108. a, b, c := circuit.GenerateR1CS()
  109. /*
  110. now we have the R1CS from the circuit:
  111. 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]]
  112. 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]]
  113. 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]]
  114. */
  115. alphas, betas, gammas, _ := snark.Utils.PF.R1CSToQAP(a, b, c)
  116. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  117. // calculate trusted setup
  118. setup, err := GenerateTrustedSetup(len(w), *circuit, alphas, betas, gammas)
  119. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  120. proof, err := GenerateProofs(*circuit, setup, w, px)
  121. b35Verif := big.NewInt(int64(35))
  122. publicSignalsVerif := []*big.Int{b35Verif}
  123. assert.True(t, VerifyProof(*circuit, setup, proof, publicSignalsVerif, true))
  124. ```
  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.