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.

145 lines
4.1 KiB

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