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.

69 lines
1.8 KiB

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