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.

99 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. Not finished, work in progress (doing this in my free time, so I don't have much time).
  4. #### Test
  5. ```
  6. go test ./... -v
  7. ```
  8. ## R1CS to Quadratic Arithmetic Program
  9. - `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
  10. - Vitalik Buterin blog post about QAP https://medium.com/@VitalikButerin/quadratic-arithmetic-programs-from-zero-to-hero-f6d558cea649
  11. - Ariel Gabizon in Zcash blog https://z.cash/blog/snark-explain5
  12. - Lagrange polynomial Wikipedia article https://en.wikipedia.org/wiki/Lagrange_polynomial
  13. #### Usage
  14. - R1CS to QAP
  15. ```go
  16. b0 := big.NewFloat(float64(0))
  17. b1 := big.NewFloat(float64(1))
  18. b5 := big.NewFloat(float64(5))
  19. a := [][]*big.Float{
  20. []*big.Float{b0, b1, b0, b0, b0, b0},
  21. []*big.Float{b0, b0, b0, b1, b0, b0},
  22. []*big.Float{b0, b1, b0, b0, b1, b0},
  23. []*big.Float{b5, b0, b0, b0, b0, b1},
  24. }
  25. b := [][]*big.Float{
  26. []*big.Float{b0, b1, b0, b0, b0, b0},
  27. []*big.Float{b0, b1, b0, b0, b0, b0},
  28. []*big.Float{b1, b0, b0, b0, b0, b0},
  29. []*big.Float{b1, b0, b0, b0, b0, b0},
  30. }
  31. c := [][]*big.Float{
  32. []*big.Float{b0, b0, b0, b1, b0, b0},
  33. []*big.Float{b0, b0, b0, b0, b1, b0},
  34. []*big.Float{b0, b0, b0, b0, b0, b1},
  35. []*big.Float{b0, b0, b1, b0, b0, b0},
  36. }
  37. alpha, beta, gamma, z := R1CSToQAP(a, b, c)
  38. fmt.Println(alpha)
  39. fmt.Println(beta)
  40. fmt.Println(gamma)
  41. fmt.Println(z)
  42. /*
  43. out:
  44. alpha: [[-5 9.166666666666666 -5 0.8333333333333334] [8 -11.333333333333332 5 -0.6666666666666666] [0 0 0 0] [-6 9.5 -4 0.5] [4 -7 3.5 -0.5] [-1 1.8333333333333333 -1 0.16666666666666666]]
  45. beta: [[3 -5.166666666666667 2.5 -0.33333333333333337] [-2 5.166666666666667 -2.5 0.33333333333333337] [0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]
  46. gamma: [[0 0 0 0] [0 0 0 0] [-1 1.8333333333333333 -1 0.16666666666666666] [4 -4.333333333333333 1.5 -0.16666666666666666] [-6 9.5 -4 0.5] [4 -7 3.5 -0.5]]
  47. z: [24 -50 35 -10 1]
  48. */
  49. ```
  50. ## Bn128
  51. Implementation of the bn128 pairing in Go.
  52. Implementation followng the information and the implementations from:
  53. - `Multiplication and Squaring on Pairing-Friendly
  54. Fields`, Augusto Jun Devegili, Colm Ó hÉigeartaigh, Michael Scott, and Ricardo Dahab https://pdfs.semanticscholar.org/3e01/de88d7428076b2547b60072088507d881bf1.pdf
  55. - `Optimal Pairings`, Frederik Vercauteren https://www.cosic.esat.kuleuven.be/bcrypt/optimal.pdf , https://eprint.iacr.org/2008/096.pdf
  56. - `Double-and-Add with Relative Jacobian
  57. Coordinates`, Björn Fay https://eprint.iacr.org/2014/1014.pdf
  58. - `Fast and Regular Algorithms for Scalar Multiplication
  59. over Elliptic Curves`, Matthieu Rivain https://eprint.iacr.org/2011/338.pdf
  60. - `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
  61. - `New software speed records for cryptographic pairings`, Michael Naehrig, Ruben Niederhagen, Peter Schwabe https://cryptojedi.org/papers/dclxvi-20100714.pdf
  62. - `Implementing Cryptographic Pairings over Barreto-Naehrig Curves`, Augusto Jun Devegili, Michael Scott, Ricardo Dahab https://eprint.iacr.org/2007/390.pdf
  63. - https://github.com/zcash/zcash/tree/master/src/snark
  64. - https://github.com/iden3/snarkjs
  65. - https://github.com/ethereum/py_ecc/tree/master/py_ecc/bn128
  66. #### Usage
  67. - Pairing
  68. ```go
  69. bn128, err := NewBn128()
  70. assert.Nil(t, err)
  71. big25 := big.NewInt(int64(25))
  72. big30 := big.NewInt(int64(30))
  73. g1a := bn128.G1.MulScalar(bn128.G1.G, big25)
  74. g2a := bn128.G2.MulScalar(bn128.G2.G, big30)
  75. g1b := bn128.G1.MulScalar(bn128.G1.G, big30)
  76. g2b := bn128.G2.MulScalar(bn128.G2.G, big25)
  77. pA, err := bn128.Pairing(g1a, g2a)
  78. assert.Nil(t, err)
  79. pB, err := bn128.Pairing(g1b, g2b)
  80. assert.Nil(t, err)
  81. assert.True(t, bn128.Fq12.Equal(pA, pB))
  82. ```