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.

173 lines
5.1 KiB

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