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.

128 lines
2.6 KiB

  1. package ecc
  2. import (
  3. "fmt"
  4. "math/big"
  5. "testing"
  6. )
  7. func TestECC(t *testing.T) {
  8. ec := NewEC(0, 7, 11)
  9. p1, p1i, err := ec.At(big.NewInt(int64(7)))
  10. if err != nil {
  11. t.Errorf(err.Error())
  12. }
  13. if !p1.Equal(Point{big.NewInt(int64(7)), big.NewInt(int64(3))}) {
  14. t.Errorf("p1!=(7, 11)")
  15. }
  16. if !p1i.Equal(Point{big.NewInt(int64(7)), big.NewInt(int64(8))}) {
  17. t.Errorf("p1i!=(7, 8)")
  18. }
  19. }
  20. func TestNeg(t *testing.T) {
  21. ec := NewEC(0, 7, 11)
  22. p1, p1i, err := ec.At(big.NewInt(int64(7)))
  23. if err != nil {
  24. t.Errorf(err.Error())
  25. }
  26. p1Neg := ec.Neg(p1)
  27. if !p1Neg.Equal(p1i) {
  28. t.Errorf("p1Neg!=p1i")
  29. }
  30. }
  31. func TestAdd(t *testing.T) {
  32. ec := NewEC(0, 7, 11)
  33. p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(7))}
  34. p2 := Point{big.NewInt(int64(2)), big.NewInt(int64(2))}
  35. q, err := ec.Add(p1, p2)
  36. if err != nil {
  37. t.Errorf(err.Error())
  38. }
  39. if !q.Equal(Point{big.NewInt(int64(3)), big.NewInt(int64(1))}) {
  40. t.Errorf("q!=(3, 1)")
  41. }
  42. // check that q exists on the elliptic curve
  43. pt, pti, err := ec.At(q.X)
  44. if err != nil {
  45. t.Errorf(err.Error())
  46. }
  47. if !q.Equal(pt) && !q.Equal(pti) {
  48. t.Errorf("q not exist on the elliptic curve")
  49. }
  50. }
  51. func TestAddSamePoint(t *testing.T) {
  52. ec := NewEC(0, 7, 11)
  53. p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(7))}
  54. p1i := Point{big.NewInt(int64(4)), big.NewInt(int64(4))}
  55. q, err := ec.Add(p1, p1)
  56. if err != nil {
  57. t.Errorf(err.Error())
  58. }
  59. if !q.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(5))}) {
  60. t.Errorf("q!=(6, 5)")
  61. }
  62. q_, err := ec.Add(p1i, p1i)
  63. if err != nil {
  64. t.Errorf(err.Error())
  65. }
  66. if !q_.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(6))}) {
  67. t.Errorf("q_!=(6, 6)")
  68. }
  69. }
  70. func TestMulEqualSelfAdd(t *testing.T) {
  71. ec := NewEC(0, 7, 29)
  72. p1 := Point{big.NewInt(int64(11)), big.NewInt(int64(27))}
  73. p1p1, err := ec.Add(p1, p1)
  74. if err != nil {
  75. t.Errorf(err.Error())
  76. }
  77. p1p1, err = ec.Add(p1p1, p1)
  78. if err != nil {
  79. t.Errorf(err.Error())
  80. }
  81. q, err := ec.Mul(p1, 3)
  82. if err != nil {
  83. t.Errorf(err.Error())
  84. }
  85. if !q.Equal(p1p1) {
  86. fmt.Println(q)
  87. fmt.Println(p1p1)
  88. t.Errorf("q!=p1*p1")
  89. }
  90. }
  91. func TestMul(t *testing.T) {
  92. ec := NewEC(0, 7, 29)
  93. p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(19))}
  94. q3, err := ec.Mul(p1, 3)
  95. if err != nil {
  96. t.Errorf(err.Error())
  97. }
  98. if !q3.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(7))}) {
  99. t.Errorf("q3!=(6, 7)")
  100. }
  101. q7, err := ec.Mul(p1, 7)
  102. if err != nil {
  103. t.Errorf(err.Error())
  104. }
  105. if !q7.Equal(Point{big.NewInt(int64(19)), big.NewInt(int64(14))}) {
  106. t.Errorf("q7!=(19, 14)")
  107. }
  108. q8, err := ec.Mul(p1, 8)
  109. if err != nil {
  110. t.Errorf(err.Error())
  111. }
  112. if !q8.Equal(Point{big.NewInt(int64(19)), big.NewInt(int64(15))}) {
  113. t.Errorf("q8!=(19, 15)")
  114. }
  115. }