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.

190 lines
3.8 KiB

  1. package bn128
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func iToBig(a int) *big.Int {
  8. return big.NewInt(int64(a))
  9. }
  10. func iiToBig(a, b int) [2]*big.Int {
  11. return [2]*big.Int{iToBig(a), iToBig(b)}
  12. }
  13. func iiiToBig(a, b int) [2]*big.Int {
  14. return [2]*big.Int{iToBig(a), iToBig(b)}
  15. }
  16. func TestFq1(t *testing.T) {
  17. fq1 := NewFq(iToBig(7))
  18. res := fq1.Add(iToBig(4), iToBig(4))
  19. assert.Equal(t, iToBig(1), res)
  20. res = fq1.Double(iToBig(5))
  21. assert.Equal(t, iToBig(3), res)
  22. res = fq1.Sub(iToBig(5), iToBig(7))
  23. assert.Equal(t, iToBig(5), res)
  24. res = fq1.Neg(iToBig(5))
  25. assert.Equal(t, iToBig(2), res)
  26. res = fq1.Mul(iToBig(5), iToBig(11))
  27. assert.Equal(t, iToBig(6), res)
  28. res = fq1.Inverse(iToBig(4))
  29. assert.Equal(t, iToBig(2), res)
  30. res = fq1.Square(iToBig(5))
  31. assert.Equal(t, iToBig(4), res)
  32. }
  33. func TestFq2(t *testing.T) {
  34. fq1 := NewFq(iToBig(7))
  35. nonResidueFq2str := "-1" // i / Beta
  36. nonResidueFq2, ok := new(big.Int).SetString(nonResidueFq2str, 10)
  37. assert.True(t, ok)
  38. assert.Equal(t, nonResidueFq2.String(), nonResidueFq2str)
  39. fq2 := Fq2{fq1, nonResidueFq2}
  40. res := fq2.Add(iiToBig(4, 4), iiToBig(3, 4))
  41. assert.Equal(t, iiToBig(0, 1), res)
  42. res = fq2.Double(iiToBig(5, 3))
  43. assert.Equal(t, iiToBig(3, 6), res)
  44. res = fq2.Sub(iiToBig(5, 3), iiToBig(7, 2))
  45. assert.Equal(t, iiToBig(5, 1), res)
  46. res = fq2.Neg(iiToBig(4, 4))
  47. assert.Equal(t, iiToBig(3, 3), res)
  48. res = fq2.Mul(iiToBig(4, 4), iiToBig(3, 4))
  49. assert.Equal(t, iiToBig(3, 0), res)
  50. res = fq2.Inverse(iiToBig(4, 4))
  51. assert.Equal(t, iiToBig(1, 6), res)
  52. res = fq2.Div(iiToBig(4, 4), iiToBig(3, 4))
  53. assert.Equal(t, iiToBig(0, 6), res)
  54. res = fq2.Square(iiToBig(4, 4))
  55. assert.Equal(t, iiToBig(0, 4), res)
  56. res2 := fq2.Mul(iiToBig(4, 4), iiToBig(4, 4))
  57. assert.Equal(t, res, res2)
  58. res = fq2.Square(iiToBig(3, 5))
  59. assert.Equal(t, iiToBig(5, 2), res)
  60. res2 = fq2.Mul(iiToBig(3, 5), iiToBig(3, 5))
  61. assert.Equal(t, res, res2)
  62. }
  63. func TestFq6(t *testing.T) {
  64. fq1 := NewFq(big.NewInt(int64(7)))
  65. nonResidueFq2, ok := new(big.Int).SetString("-1", 10) // i
  66. assert.True(t, ok)
  67. nonResidueFq6 := iiToBig(9, 1) // TODO
  68. fq2 := Fq2{fq1, nonResidueFq2}
  69. fq6 := Fq6{fq2, nonResidueFq6}
  70. a := [3][2]*big.Int{
  71. iiToBig(1, 2),
  72. iiToBig(3, 4),
  73. iiToBig(5, 6)}
  74. b := [3][2]*big.Int{
  75. iiToBig(12, 11),
  76. iiToBig(10, 9),
  77. iiToBig(8, 7)}
  78. res := fq6.Add(a, b)
  79. assert.Equal(t,
  80. [3][2]*big.Int{
  81. iiToBig(6, 6),
  82. iiToBig(6, 6),
  83. iiToBig(6, 6)},
  84. res)
  85. res = fq6.Sub(a, b)
  86. assert.Equal(t,
  87. [3][2]*big.Int{
  88. iiToBig(3, 5),
  89. iiToBig(0, 2),
  90. iiToBig(4, 6)},
  91. res)
  92. res = fq6.Mul(a, b)
  93. assert.Equal(t,
  94. [3][2]*big.Int{
  95. iiToBig(5, 0),
  96. iiToBig(2, 1),
  97. iiToBig(3, 0)},
  98. res)
  99. mulRes := fq6.Mul(a, b)
  100. divRes := fq6.Div(mulRes, b)
  101. assert.Equal(t, a, divRes)
  102. }
  103. func TestFq12(t *testing.T) {
  104. q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
  105. assert.True(t, ok)
  106. fq1 := NewFq(q)
  107. nonResidueFq2, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
  108. assert.True(t, ok)
  109. nonResidueFq6 := iiToBig(9, 1)
  110. fq2 := Fq2{fq1, nonResidueFq2}
  111. fq6 := Fq6{fq2, nonResidueFq6}
  112. fq12 := Fq12{fq6, fq2, nonResidueFq6}
  113. a := [2][3][2]*big.Int{
  114. {
  115. iiToBig(1, 2),
  116. iiToBig(3, 4),
  117. iiToBig(5, 6),
  118. },
  119. {
  120. iiToBig(7, 8),
  121. iiToBig(9, 10),
  122. iiToBig(11, 12),
  123. },
  124. }
  125. b := [2][3][2]*big.Int{
  126. {
  127. iiToBig(12, 11),
  128. iiToBig(10, 9),
  129. iiToBig(8, 7),
  130. },
  131. {
  132. iiToBig(6, 5),
  133. iiToBig(4, 3),
  134. iiToBig(2, 1),
  135. },
  136. }
  137. res := fq12.Add(a, b)
  138. assert.Equal(t,
  139. [2][3][2]*big.Int{
  140. {
  141. iiToBig(13, 13),
  142. iiToBig(13, 13),
  143. iiToBig(13, 13),
  144. },
  145. {
  146. iiToBig(13, 13),
  147. iiToBig(13, 13),
  148. iiToBig(13, 13),
  149. },
  150. },
  151. res)
  152. mulRes := fq12.Mul(a, b)
  153. divRes := fq12.Div(mulRes, b)
  154. assert.Equal(t, a, divRes)
  155. }