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.

169 lines
3.9 KiB

  1. package fields
  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), fq1.Affine(res))
  20. res = fq1.Double(iToBig(5))
  21. assert.Equal(t, iToBig(3), fq1.Affine(res))
  22. res = fq1.Sub(iToBig(5), iToBig(7))
  23. assert.Equal(t, iToBig(5), fq1.Affine(res))
  24. res = fq1.Neg(iToBig(5))
  25. assert.Equal(t, iToBig(2), fq1.Affine(res))
  26. res = fq1.Mul(iToBig(5), iToBig(11))
  27. assert.Equal(t, iToBig(6), fq1.Affine(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/j
  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), fq2.Affine(res))
  42. res = fq2.Double(iiToBig(5, 3))
  43. assert.Equal(t, iiToBig(3, 6), fq2.Affine(res))
  44. res = fq2.Sub(iiToBig(5, 3), iiToBig(7, 2))
  45. assert.Equal(t, iiToBig(5, 1), fq2.Affine(res))
  46. res = fq2.Neg(iiToBig(4, 4))
  47. assert.Equal(t, iiToBig(3, 3), fq2.Affine(res))
  48. res = fq2.Mul(iiToBig(4, 4), iiToBig(3, 4))
  49. assert.Equal(t, iiToBig(3, 0), fq2.Affine(res))
  50. res = fq2.Inverse(iiToBig(4, 4))
  51. assert.Equal(t, iiToBig(1, 6), fq2.Affine(res))
  52. res = fq2.Square(iiToBig(4, 4))
  53. assert.Equal(t, iiToBig(0, 4), fq2.Affine(res))
  54. res2 := fq2.Mul(iiToBig(4, 4), iiToBig(4, 4))
  55. assert.Equal(t, fq2.Affine(res), fq2.Affine(res2))
  56. assert.True(t, fq2.Equal(res, res2))
  57. res = fq2.Square(iiToBig(3, 5))
  58. assert.Equal(t, iiToBig(5, 2), fq2.Affine(res))
  59. res2 = fq2.Mul(iiToBig(3, 5), iiToBig(3, 5))
  60. assert.Equal(t, fq2.Affine(res), fq2.Affine(res2))
  61. }
  62. func TestFq6(t *testing.T) {
  63. // bn128, err := NewBn128()
  64. // assert.Nil(t, err)
  65. q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
  66. assert.True(t, ok)
  67. fq1 := NewFq(q)
  68. nonResidueFq2, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
  69. assert.True(t, ok)
  70. nonResidueFq6 := iiToBig(9, 1)
  71. fq2 := Fq2{fq1, nonResidueFq2}
  72. fq6 := Fq6{fq2, nonResidueFq6}
  73. a := [3][2]*big.Int{
  74. iiToBig(1, 2),
  75. iiToBig(3, 4),
  76. iiToBig(5, 6)}
  77. b := [3][2]*big.Int{
  78. iiToBig(12, 11),
  79. iiToBig(10, 9),
  80. iiToBig(8, 7)}
  81. mulRes := fq6.Mul(a, b)
  82. divRes := fq6.Div(mulRes, b)
  83. assert.Equal(t, fq6.Affine(a), fq6.Affine(divRes))
  84. }
  85. func TestFq12(t *testing.T) {
  86. q, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) // i
  87. assert.True(t, ok)
  88. fq1 := NewFq(q)
  89. nonResidueFq2, ok := new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208582", 10) // i
  90. assert.True(t, ok)
  91. nonResidueFq6 := iiToBig(9, 1)
  92. fq2 := Fq2{fq1, nonResidueFq2}
  93. fq6 := Fq6{fq2, nonResidueFq6}
  94. fq12 := Fq12{fq6, fq2, nonResidueFq6}
  95. a := [2][3][2]*big.Int{
  96. {
  97. iiToBig(1, 2),
  98. iiToBig(3, 4),
  99. iiToBig(5, 6),
  100. },
  101. {
  102. iiToBig(7, 8),
  103. iiToBig(9, 10),
  104. iiToBig(11, 12),
  105. },
  106. }
  107. b := [2][3][2]*big.Int{
  108. {
  109. iiToBig(12, 11),
  110. iiToBig(10, 9),
  111. iiToBig(8, 7),
  112. },
  113. {
  114. iiToBig(6, 5),
  115. iiToBig(4, 3),
  116. iiToBig(2, 1),
  117. },
  118. }
  119. res := fq12.Add(a, b)
  120. assert.Equal(t,
  121. [2][3][2]*big.Int{
  122. {
  123. iiToBig(13, 13),
  124. iiToBig(13, 13),
  125. iiToBig(13, 13),
  126. },
  127. {
  128. iiToBig(13, 13),
  129. iiToBig(13, 13),
  130. iiToBig(13, 13),
  131. },
  132. },
  133. res)
  134. mulRes := fq12.Mul(a, b)
  135. divRes := fq12.Div(mulRes, b)
  136. assert.Equal(t, fq12.Affine(a), fq12.Affine(divRes))
  137. }