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.

186 lines
5.5 KiB

  1. ## Bn128
  2. Implementation of the bn128 pairing in Go.
  3. Implementation followng the information and the implementations from:
  4. - `Multiplication and Squaring on Pairing-Friendly
  5. Fields`, Augusto Jun Devegili, Colm Ó hÉigeartaigh, Michael Scott, and Ricardo Dahab https://pdfs.semanticscholar.org/3e01/de88d7428076b2547b60072088507d881bf1.pdf
  6. - `Optimal Pairings`, Frederik Vercauteren https://www.cosic.esat.kuleuven.be/bcrypt/optimal.pdf , https://eprint.iacr.org/2008/096.pdf
  7. - `Double-and-Add with Relative Jacobian
  8. Coordinates`, Björn Fay https://eprint.iacr.org/2014/1014.pdf
  9. - `Fast and Regular Algorithms for Scalar Multiplication
  10. over Elliptic Curves`, Matthieu Rivain https://eprint.iacr.org/2011/338.pdf
  11. - `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
  12. - `New software speed records for cryptographic pairings`, Michael Naehrig, Ruben Niederhagen, Peter Schwabe https://cryptojedi.org/papers/dclxvi-20100714.pdf
  13. - `Implementing Cryptographic Pairings over Barreto-Naehrig Curves`, Augusto Jun Devegili, Michael Scott, Ricardo Dahab https://eprint.iacr.org/2007/390.pdf
  14. - https://github.com/zcash/zcash/tree/master/src/snark
  15. - https://github.com/iden3/snarkjs
  16. - https://github.com/ethereum/py_ecc/tree/master/py_ecc/bn128
  17. - [x] Fq, Fq2, Fq6, Fq12 operations
  18. - [x] G1, G2 operations
  19. - [x] preparePairing
  20. - [x] PreComupteG1, PreComupteG2
  21. - [x] DoubleStep, AddStep
  22. - [x] MillerLoop
  23. - [x] Pairing
  24. ### Installation
  25. ```
  26. go get github.com/arnaucube/bn128
  27. ```
  28. #### Usage
  29. - Pairing
  30. ```go
  31. bn128, err := NewBn128()
  32. assert.Nil(t, err)
  33. big25 := big.NewInt(int64(25))
  34. big30 := big.NewInt(int64(30))
  35. g1a := bn128.G1.MulScalar(bn128.G1.G, big25)
  36. g2a := bn128.G2.MulScalar(bn128.G2.G, big30)
  37. g1b := bn128.G1.MulScalar(bn128.G1.G, big30)
  38. g2b := bn128.G2.MulScalar(bn128.G2.G, big25)
  39. pA, err := bn128.Pairing(g1a, g2a)
  40. assert.Nil(t, err)
  41. pB, err := bn128.Pairing(g1b, g2b)
  42. assert.Nil(t, err)
  43. assert.True(t, bn128.Fq12.Equal(pA, pB))
  44. ```
  45. #### Test
  46. ```
  47. go test -v
  48. ```
  49. ##### Internal operations more deeply
  50. First let's assume that we have these three basic functions to convert integer compositions to big integer compositions:
  51. ```go
  52. func iToBig(a int) *big.Int {
  53. return big.NewInt(int64(a))
  54. }
  55. func iiToBig(a, b int) [2]*big.Int {
  56. return [2]*big.Int{iToBig(a), iToBig(b)}
  57. }
  58. func iiiToBig(a, b int) [2]*big.Int {
  59. return [2]*big.Int{iToBig(a), iToBig(b)}
  60. }
  61. ```
  62. - Finite Fields (1, 2, 6, 12) operations
  63. ```go
  64. // new finite field of order 1
  65. fq1 := NewFq(iToBig(7))
  66. // basic operations of finite field 1
  67. res := fq1.Add(iToBig(4), iToBig(4))
  68. res = fq1.Double(iToBig(5))
  69. res = fq1.Sub(iToBig(5), iToBig(7))
  70. res = fq1.Neg(iToBig(5))
  71. res = fq1.Mul(iToBig(5), iToBig(11))
  72. res = fq1.Inverse(iToBig(4))
  73. res = fq1.Square(iToBig(5))
  74. // new finite field of order 2
  75. nonResidueFq2str := "-1" // i/j
  76. nonResidueFq2, ok := new(big.Int).SetString(nonResidueFq2str, 10)
  77. fq2 := Fq2{fq1, nonResidueFq2}
  78. nonResidueFq6 := iiToBig(9, 1)
  79. // basic operations of finite field of order 2
  80. res := fq2.Add(iiToBig(4, 4), iiToBig(3, 4))
  81. res = fq2.Double(iiToBig(5, 3))
  82. res = fq2.Sub(iiToBig(5, 3), iiToBig(7, 2))
  83. res = fq2.Neg(iiToBig(4, 4))
  84. res = fq2.Mul(iiToBig(4, 4), iiToBig(3, 4))
  85. res = fq2.Inverse(iiToBig(4, 4))
  86. res = fq2.Div(iiToBig(4, 4), iiToBig(3, 4))
  87. res = fq2.Square(iiToBig(4, 4))
  88. // new finite field of order 6
  89. nonResidueFq6 := iiToBig(9, 1) // TODO
  90. fq6 := Fq6{fq2, nonResidueFq6}
  91. // define two new values of Finite Field 6, in order to be able to perform the operations
  92. a := [3][2]*big.Int{
  93. iiToBig(1, 2),
  94. iiToBig(3, 4),
  95. iiToBig(5, 6)}
  96. b := [3][2]*big.Int{
  97. iiToBig(12, 11),
  98. iiToBig(10, 9),
  99. iiToBig(8, 7)}
  100. // basic operations of finite field order 6
  101. res := fq6.Add(a, b)
  102. res = fq6.Sub(a, b)
  103. res = fq6.Mul(a, b)
  104. divRes := fq6.Div(mulRes, b)
  105. // new finite field of order 12
  106. q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
  107. if !ok {
  108. fmt.Println("error parsing string to big integer")
  109. }
  110. fq1 := NewFq(q)
  111. nonResidueFq2, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
  112. assert.True(t, ok)
  113. nonResidueFq6 := iiToBig(9, 1)
  114. fq2 := Fq2{fq1, nonResidueFq2}
  115. fq6 := Fq6{fq2, nonResidueFq6}
  116. fq12 := Fq12{fq6, fq2, nonResidueFq6}
  117. ```
  118. - G1 operations
  119. ```go
  120. bn128, err := NewBn128()
  121. assert.Nil(t, err)
  122. r1 := big.NewInt(int64(33))
  123. r2 := big.NewInt(int64(44))
  124. gr1 := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(r1))
  125. gr2 := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(r2))
  126. grsum1 := bn128.G1.Add(gr1, gr2)
  127. r1r2 := bn128.Fq1.Add(r1, r2)
  128. grsum2 := bn128.G1.MulScalar(bn128.G1.G, r1r2)
  129. a := bn128.G1.Affine(grsum1)
  130. b := bn128.G1.Affine(grsum2)
  131. assert.Equal(t, a, b)
  132. assert.Equal(t, "0x2f978c0ab89ebaa576866706b14787f360c4d6c3869efe5a72f7c3651a72ff00", utils.BytesToHex(a[0].Bytes()))
  133. assert.Equal(t, "0x12e4ba7f0edca8b4fa668fe153aebd908d322dc26ad964d4cd314795844b62b2", utils.BytesToHex(a[1].Bytes()))
  134. ```
  135. - G2 operations
  136. ```go
  137. bn128, err := NewBn128()
  138. assert.Nil(t, err)
  139. r1 := big.NewInt(int64(33))
  140. r2 := big.NewInt(int64(44))
  141. gr1 := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(r1))
  142. gr2 := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(r2))
  143. grsum1 := bn128.G2.Add(gr1, gr2)
  144. r1r2 := bn128.Fq1.Add(r1, r2)
  145. grsum2 := bn128.G2.MulScalar(bn128.G2.G, r1r2)
  146. a := bn128.G2.Affine(grsum1)
  147. b := bn128.G2.Affine(grsum2)
  148. assert.Equal(t, a, b)
  149. ```