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.

115 lines
4.0 KiB

  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. zk-SNARK library implementation in Go
  3. #### Test
  4. ```
  5. go test ./... -v
  6. ```
  7. ## R1CS to Quadratic Arithmetic Program
  8. - `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
  9. - Vitalik Buterin blog post about QAP https://medium.com/@VitalikButerin/quadratic-arithmetic-programs-from-zero-to-hero-f6d558cea649
  10. - Ariel Gabizon in Zcash blog https://z.cash/blog/snark-explain5
  11. - Lagrange polynomial Wikipedia article https://en.wikipedia.org/wiki/Lagrange_polynomial
  12. #### Usage
  13. - R1CS to QAP
  14. ```go
  15. pf := NewPolynomialField(f)
  16. b0 := big.NewInt(int64(0))
  17. b1 := big.NewInt(int64(1))
  18. b3 := big.NewInt(int64(3))
  19. b5 := big.NewInt(int64(5))
  20. b9 := big.NewInt(int64(9))
  21. b27 := big.NewInt(int64(27))
  22. b30 := big.NewInt(int64(30))
  23. b35 := big.NewInt(int64(35))
  24. a := [][]*big.Int{
  25. []*big.Int{b0, b1, b0, b0, b0, b0},
  26. []*big.Int{b0, b0, b0, b1, b0, b0},
  27. []*big.Int{b0, b1, b0, b0, b1, b0},
  28. []*big.Int{b5, b0, b0, b0, b0, b1},
  29. }
  30. b := [][]*big.Int{
  31. []*big.Int{b0, b1, b0, b0, b0, b0},
  32. []*big.Int{b0, b1, b0, b0, b0, b0},
  33. []*big.Int{b1, b0, b0, b0, b0, b0},
  34. []*big.Int{b1, b0, b0, b0, b0, b0},
  35. }
  36. c := [][]*big.Int{
  37. []*big.Int{b0, b0, b0, b1, b0, b0},
  38. []*big.Int{b0, b0, b0, b0, b1, b0},
  39. []*big.Int{b0, b0, b0, b0, b0, b1},
  40. []*big.Int{b0, b0, b1, b0, b0, b0},
  41. }
  42. alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
  43. fmt.Println(alphas)
  44. fmt.Println(betas)
  45. fmt.Println(gammas)
  46. fmt.Println(z)
  47. w := []*big.Int{b1, b3, b35, b9, b27, b30}
  48. ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
  49. fmt.Println(ax)
  50. fmt.Println(bx)
  51. fmt.Println(cx)
  52. fmt.Println(px)
  53. hx := pf.DivisorPolinomial(px, zx)
  54. fmt.Println(hx)
  55. ```
  56. ## Bn128
  57. Implementation of the bn128 pairing in Go.
  58. Implementation followng the information and the implementations from:
  59. - `Multiplication and Squaring on Pairing-Friendly
  60. Fields`, Augusto Jun Devegili, Colm Ó hÉigeartaigh, Michael Scott, and Ricardo Dahab https://pdfs.semanticscholar.org/3e01/de88d7428076b2547b60072088507d881bf1.pdf
  61. - `Optimal Pairings`, Frederik Vercauteren https://www.cosic.esat.kuleuven.be/bcrypt/optimal.pdf , https://eprint.iacr.org/2008/096.pdf
  62. - `Double-and-Add with Relative Jacobian
  63. Coordinates`, Björn Fay https://eprint.iacr.org/2014/1014.pdf
  64. - `Fast and Regular Algorithms for Scalar Multiplication
  65. over Elliptic Curves`, Matthieu Rivain https://eprint.iacr.org/2011/338.pdf
  66. - `High-Speed Software Implementation of the Optimal Ate Pairing over Barreto–Naehrig Curves`, Jean-Luc Beuchat, Jorge E. González-Díaz, Shigeo Mitsunari, Eiji Okamoto, Francisco Rodríguez-Henríquez, and Tadanori Teruya https://eprint.iacr.org/2010/354.pdf
  67. - `New software speed records for cryptographic pairings`, Michael Naehrig, Ruben Niederhagen, Peter Schwabe https://cryptojedi.org/papers/dclxvi-20100714.pdf
  68. - `Implementing Cryptographic Pairings over Barreto-Naehrig Curves`, Augusto Jun Devegili, Michael Scott, Ricardo Dahab https://eprint.iacr.org/2007/390.pdf
  69. - https://github.com/zcash/zcash/tree/master/src/snark
  70. - https://github.com/iden3/snarkjs
  71. - https://github.com/ethereum/py_ecc/tree/master/py_ecc/bn128
  72. #### Usage
  73. - Pairing
  74. ```go
  75. bn128, err := NewBn128()
  76. assert.Nil(t, err)
  77. big25 := big.NewInt(int64(25))
  78. big30 := big.NewInt(int64(30))
  79. g1a := bn128.G1.MulScalar(bn128.G1.G, big25)
  80. g2a := bn128.G2.MulScalar(bn128.G2.G, big30)
  81. g1b := bn128.G1.MulScalar(bn128.G1.G, big30)
  82. g2b := bn128.G2.MulScalar(bn128.G2.G, big25)
  83. pA, err := bn128.Pairing(g1a, g2a)
  84. assert.Nil(t, err)
  85. pB, err := bn128.Pairing(g1b, g2b)
  86. assert.Nil(t, err)
  87. assert.True(t, bn128.Fq12.Equal(pA, pB))
  88. ```
  89. ---
  90. ## Caution
  91. Not finished, work in progress (implementing this in my free time to understand it better, so I don't have much time).
  92. Thanks to @jbaylina, @bellesmarta, @adriamb for their explanations that helped to understand a little bit this.