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
853 B

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