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.

77 lines
1.7 KiB

  1. package elgamal
  2. import (
  3. "math/big"
  4. "testing"
  5. ecc "../ecc"
  6. )
  7. func TestNewEG(t *testing.T) {
  8. ec := ecc.NewEC(1, 18, 19)
  9. g := ecc.Point{big.NewInt(int64(7)), big.NewInt(int64(11))}
  10. eg, err := NewEG(ec, g)
  11. if err != nil {
  12. t.Errorf(err.Error())
  13. }
  14. privK := 5
  15. pubK, err := eg.PubK(privK)
  16. if err != nil {
  17. t.Errorf(err.Error())
  18. }
  19. if !pubK.Equal(ecc.Point{big.NewInt(int64(13)), big.NewInt(int64(9))}) {
  20. t.Errorf("pubK!=(13, 9)")
  21. }
  22. }
  23. func TestEGEncrypt(t *testing.T) {
  24. ec := ecc.NewEC(1, 18, 19)
  25. g := ecc.Point{big.NewInt(int64(7)), big.NewInt(int64(11))}
  26. eg, err := NewEG(ec, g)
  27. if err != nil {
  28. t.Errorf(err.Error())
  29. }
  30. privK := 5
  31. pubK, err := eg.PubK(privK)
  32. if err != nil {
  33. t.Errorf(err.Error())
  34. }
  35. // m: point to encrypt
  36. m := ecc.Point{big.NewInt(int64(11)), big.NewInt(int64(12))}
  37. c, err := eg.Encrypt(m, pubK, 15)
  38. if err != nil {
  39. t.Errorf(err.Error())
  40. }
  41. if !c[0].Equal(ecc.Point{big.NewInt(int64(8)), big.NewInt(int64(5))}) {
  42. t.Errorf("c[0] != (8, 5), encryption failed")
  43. }
  44. if !c[1].Equal(ecc.Point{big.NewInt(int64(2)), big.NewInt(int64(16))}) {
  45. t.Errorf("c[1] != (2, 16), encryption failed")
  46. }
  47. }
  48. func TestEGDecrypt(t *testing.T) {
  49. ec := ecc.NewEC(1, 18, 19)
  50. g := ecc.Point{big.NewInt(int64(7)), big.NewInt(int64(11))}
  51. eg, err := NewEG(ec, g)
  52. if err != nil {
  53. t.Errorf(err.Error())
  54. }
  55. privK := 5
  56. pubK, err := eg.PubK(privK)
  57. if err != nil {
  58. t.Errorf(err.Error())
  59. }
  60. // m: point to encrypt
  61. m := ecc.Point{big.NewInt(int64(11)), big.NewInt(int64(12))}
  62. c, err := eg.Encrypt(m, pubK, 15)
  63. if err != nil {
  64. t.Errorf(err.Error())
  65. }
  66. d, err := eg.Decrypt(c, privK)
  67. if err != nil {
  68. t.Errorf(err.Error())
  69. }
  70. if !m.Equal(d) {
  71. t.Errorf("m != d, decrypting failed")
  72. }
  73. }