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.

54 lines
1.1 KiB

5 years ago
  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. // key harcoded for tests
  25. pubK := PublicKey{
  26. N: big.NewInt(204223),
  27. G: big.NewInt(24929195694),
  28. }
  29. privK := PrivateKey{
  30. Lambda: big.NewInt(101660),
  31. Mu: big.NewInt(117648),
  32. }
  33. key := Key{
  34. PubK: pubK,
  35. PrivK: privK,
  36. }
  37. n1 := big.NewInt(int64(110))
  38. n2 := big.NewInt(int64(150))
  39. c1 := Encrypt(n1, key.PubK)
  40. c2 := Encrypt(n2, key.PubK)
  41. c3c4 := HomomorphicAddition(c1, c2, key.PubK)
  42. d := Decrypt(c3c4, key.PubK, key.PrivK)
  43. if !bytes.Equal(new(big.Int).Add(n1, n2).Bytes(), d.Bytes()) {
  44. fmt.Println(key)
  45. t.Errorf("decrypted result not equal to original result")
  46. }
  47. }