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.

212 lines
5.6 KiB

  1. package ecc
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestECC(t *testing.T) {
  8. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(11)))
  9. p1, p1i, err := ec.At(big.NewInt(int64(7)))
  10. assert.Nil(t, err)
  11. if !p1.Equal(Point{big.NewInt(int64(7)), big.NewInt(int64(3))}) {
  12. t.Errorf("p1!=(7, 11)")
  13. }
  14. if !p1i.Equal(Point{big.NewInt(int64(7)), big.NewInt(int64(8))}) {
  15. t.Errorf("p1i!=(7, 8)")
  16. }
  17. }
  18. func TestNeg(t *testing.T) {
  19. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(11)))
  20. p1, p1i, err := ec.At(big.NewInt(int64(7)))
  21. assert.Nil(t, err)
  22. p1Neg := ec.Neg(p1)
  23. if !p1Neg.Equal(p1i) {
  24. t.Errorf("p1Neg!=p1i")
  25. }
  26. }
  27. func TestAdd(t *testing.T) {
  28. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(11)))
  29. p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(7))}
  30. p2 := Point{big.NewInt(int64(2)), big.NewInt(int64(2))}
  31. q, err := ec.Add(p1, p2)
  32. assert.Nil(t, err)
  33. if !q.Equal(Point{big.NewInt(int64(3)), big.NewInt(int64(1))}) {
  34. t.Errorf("q!=(3, 1)")
  35. }
  36. // check that q exists on the elliptic curve
  37. pt, pti, err := ec.At(q.X)
  38. assert.Nil(t, err)
  39. if !q.Equal(pt) && !q.Equal(pti) {
  40. t.Errorf("q not exist on the elliptic curve")
  41. }
  42. }
  43. func TestAddSamePoint(t *testing.T) {
  44. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(11)))
  45. p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(7))}
  46. p1i := Point{big.NewInt(int64(4)), big.NewInt(int64(4))}
  47. q, err := ec.Add(p1, p1)
  48. assert.Nil(t, err)
  49. if !q.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(5))}) {
  50. t.Errorf(q.String() + " == q != (6, 5)")
  51. }
  52. q_, err := ec.Add(p1i, p1i)
  53. assert.Nil(t, err)
  54. if !q_.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(6))}) {
  55. t.Errorf(q_.String() + " == q_ != (6, 6)")
  56. }
  57. }
  58. func TestMulPoint1(t *testing.T) {
  59. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(29)))
  60. p := Point{big.NewInt(int64(11)), big.NewInt(int64(27))}
  61. q, err := ec.Mul(p, big.NewInt(int64(1)))
  62. assert.Nil(t, err)
  63. if !q.Equal(Point{big.NewInt(int64(11)), big.NewInt(int64(27))}) {
  64. t.Errorf(q.String() + " == q != (11, 27)")
  65. }
  66. q, err = ec.Mul(p, big.NewInt(int64(2)))
  67. assert.Nil(t, err)
  68. if !q.Equal(Point{big.NewInt(int64(12)), big.NewInt(int64(13))}) {
  69. t.Errorf(q.String() + " == q != (12, 13)")
  70. }
  71. q, err = ec.Mul(p, big.NewInt(int64(3)))
  72. assert.Nil(t, err)
  73. if !q.Equal(Point{big.NewInt(int64(28)), big.NewInt(int64(8))}) {
  74. t.Errorf(q.String() + " == q != (28, 8)")
  75. }
  76. q, err = ec.Mul(p, big.NewInt(int64(4)))
  77. assert.Nil(t, err)
  78. if !q.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(22))}) {
  79. t.Errorf(q.String() + " == q != (6, 22)")
  80. }
  81. }
  82. func TestMulPoint2(t *testing.T) {
  83. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(29)))
  84. p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(19))}
  85. q3, err := ec.Mul(p1, big.NewInt(int64(3)))
  86. assert.Nil(t, err)
  87. if !q3.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(7))}) {
  88. t.Errorf(q3.String() + " == q3 != (6, 7)")
  89. }
  90. q7, err := ec.Mul(p1, big.NewInt(int64(7)))
  91. assert.Nil(t, err)
  92. if !q7.Equal(Point{big.NewInt(int64(19)), big.NewInt(int64(14))}) {
  93. t.Errorf(q7.String() + " == q7 != (19, 14)")
  94. }
  95. q8, err := ec.Mul(p1, big.NewInt(int64(8)))
  96. assert.Nil(t, err)
  97. if !q8.Equal(Point{big.NewInt(int64(19)), big.NewInt(int64(15))}) {
  98. t.Errorf(q8.String() + " == q8 != (12, 16)")
  99. }
  100. }
  101. func TestMulPoint3(t *testing.T) {
  102. // in this test we will multiply by a high number
  103. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(11)))
  104. p := Point{big.NewInt(int64(7)), big.NewInt(int64(3))}
  105. q, err := ec.Mul(p, big.NewInt(int64(100)))
  106. assert.Nil(t, err)
  107. if !q.Equal(Point{big.NewInt(int64(3)), big.NewInt(int64(1))}) {
  108. t.Errorf(q.String() + " == q != (3, 1)")
  109. }
  110. q, err = ec.Mul(p, big.NewInt(int64(100)))
  111. assert.Nil(t, err)
  112. if !q.Equal(Point{big.NewInt(int64(3)), big.NewInt(int64(1))}) {
  113. t.Errorf(q.String() + " == q != (3, 1)")
  114. }
  115. }
  116. func TestMulEqualSelfAdd(t *testing.T) {
  117. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(29)))
  118. p1 := Point{big.NewInt(int64(11)), big.NewInt(int64(27))}
  119. p1_2, err := ec.Add(p1, p1)
  120. assert.Nil(t, err)
  121. p1_3, err := ec.Add(p1_2, p1)
  122. assert.Nil(t, err)
  123. // q * 3
  124. q, err := ec.Mul(p1, big.NewInt(int64(3)))
  125. assert.Nil(t, err)
  126. if !q.Equal(Point{big.NewInt(int64(28)), big.NewInt(int64(8))}) {
  127. t.Errorf(q.String() + " == q != (28, 8)")
  128. }
  129. if !q.Equal(p1_3) {
  130. t.Errorf("p*3 == " + q.String() + ", p+p+p == " + p1_3.String())
  131. }
  132. // q * 4
  133. p1_4, err := ec.Add(p1_3, p1)
  134. assert.Nil(t, err)
  135. q, err = ec.Mul(p1, big.NewInt(int64(4)))
  136. assert.Nil(t, err)
  137. if !q.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(22))}) {
  138. t.Errorf(q.String() + " == q != (6, 22)")
  139. }
  140. if !q.Equal(p1_4) {
  141. t.Errorf("p*4 == " + q.String() + ", p+p+p+p == " + p1_4.String())
  142. }
  143. }
  144. func TestOrder(t *testing.T) {
  145. ec := NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(11)))
  146. g := Point{big.NewInt(int64(7)), big.NewInt(int64(8))}
  147. order, err := ec.Order(g)
  148. assert.Nil(t, err)
  149. assert.Equal(t, order.Int64(), int64(12))
  150. // another test
  151. g = Point{big.NewInt(int64(2)), big.NewInt(int64(9))}
  152. order, err = ec.Order(g)
  153. assert.Nil(t, err)
  154. assert.Equal(t, order.Int64(), int64(4))
  155. // another test with another curve
  156. ec = NewEC(big.NewInt(int64(0)), big.NewInt(int64(7)), big.NewInt(int64(29)))
  157. g = Point{big.NewInt(int64(6)), big.NewInt(int64(22))}
  158. order, err = ec.Order(g)
  159. assert.Nil(t, err)
  160. assert.Equal(t, order.Int64(), int64(5))
  161. // another test
  162. g = Point{big.NewInt(int64(23)), big.NewInt(int64(9))}
  163. order, err = ec.Order(g)
  164. assert.Nil(t, err)
  165. assert.Equal(t, order.Int64(), int64(30))
  166. }