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.

40 lines
860 B

  1. package paillier
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/big"
  6. "testing"
  7. )
  8. func TestEncryptDecrypt(t *testing.T) {
  9. key, err := GenerateKeyPair()
  10. if err != nil {
  11. t.Errorf(err.Error())
  12. }
  13. mBytes := []byte("Hi")
  14. m := new(big.Int).SetBytes(mBytes)
  15. c := Encrypt(m, key.PubK)
  16. d := Decrypt(c, key.PubK, key.PrivK)
  17. if m == d {
  18. fmt.Println(key)
  19. t.Errorf("m not equal to decrypted")
  20. }
  21. }
  22. func TestHomomorphicAddition(t *testing.T) {
  23. key, err := GenerateKeyPair()
  24. if err != nil {
  25. t.Errorf(err.Error())
  26. }
  27. n1 := big.NewInt(int64(110))
  28. n2 := big.NewInt(int64(150))
  29. c1 := Encrypt(n1, key.PubK)
  30. c2 := Encrypt(n2, key.PubK)
  31. c3c4 := HomomorphicAddition(c1, c2, key.PubK)
  32. d := Decrypt(c3c4, key.PubK, key.PrivK)
  33. if !bytes.Equal(new(big.Int).Add(n1, n2).Bytes(), d.Bytes()) {
  34. fmt.Println(key)
  35. t.Errorf("decrypted result not equal to original result")
  36. }
  37. }