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.

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