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.

71 lines
1.3 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, p1_, 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 !p1_.Equal(Point{big.NewInt(int64(7)), big.NewInt(int64(8))}) {
  17. t.Errorf("p1_!=(7, 8)")
  18. }
  19. }
  20. func TestNeg(t *testing.T) {
  21. ec := NewEC(0, 7, 11)
  22. p1, p1_, 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(p1_) {
  28. t.Errorf("p1Neg!=p1_")
  29. }
  30. }
  31. func TestAdd(t *testing.T) {
  32. fmt.Println("y^2 = x^3 + 7")
  33. fmt.Print("ec: ")
  34. ec := NewEC(0, 7, 11)
  35. fmt.Println(ec)
  36. p1, _, err := ec.At(big.NewInt(int64(7)))
  37. if err != nil {
  38. t.Errorf(err.Error())
  39. }
  40. fmt.Print("p1: ")
  41. fmt.Println(p1)
  42. p2, _, err := ec.At(big.NewInt(int64(6)))
  43. if err != nil {
  44. t.Errorf(err.Error())
  45. }
  46. fmt.Print("p2: ")
  47. fmt.Println(p2)
  48. q, err := ec.Add(p1, p2)
  49. if err != nil {
  50. t.Errorf(err.Error())
  51. }
  52. fmt.Print("q: ")
  53. fmt.Println(q)
  54. if !q.Equal(Point{big.NewInt(int64(2)), big.NewInt(int64(9))}) {
  55. t.Errorf("q!=(2, 9)")
  56. }
  57. // check that q exists on the elliptic curve
  58. pt, pt_, err := ec.At(q.X)
  59. if err != nil {
  60. t.Errorf(err.Error())
  61. }
  62. if !q.Equal(pt) && !q.Equal(pt_) {
  63. t.Errorf("q not exist on the elliptic curve")
  64. }
  65. }