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.

59 lines
1.3 KiB

  1. package rsa
  2. import (
  3. "bytes"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestEncryptDecrypt(t *testing.T) {
  9. key, err := GenerateKeyPair()
  10. assert.Nil(t, err)
  11. mBytes := []byte("Hi")
  12. m := new(big.Int).SetBytes(mBytes)
  13. c := Encrypt(m, key.PubK)
  14. d := Decrypt(c, key.PrivK)
  15. if m == d {
  16. t.Errorf("m not equal to decrypted")
  17. }
  18. }
  19. func TestBlindSignature(t *testing.T) {
  20. key, err := GenerateKeyPair()
  21. assert.Nil(t, err)
  22. mBytes := []byte("Hi")
  23. m := new(big.Int).SetBytes(mBytes)
  24. c := Encrypt(m, key.PubK)
  25. d := Decrypt(c, key.PrivK)
  26. if m == d {
  27. t.Errorf("decrypted d not equal to original m")
  28. }
  29. rVal := big.NewInt(int64(101))
  30. mBlinded := Blind(m, rVal, key.PubK)
  31. sigma := BlindSign(mBlinded, key.PrivK)
  32. mSigned := Unblind(sigma, rVal, key.PubK)
  33. verified := Verify(m, mSigned, key.PubK)
  34. if !verified {
  35. t.Errorf("false, signature not verified")
  36. }
  37. }
  38. func TestHomomorphicMultiplication(t *testing.T) {
  39. key, err := GenerateKeyPair()
  40. assert.Nil(t, err)
  41. n1 := big.NewInt(int64(11))
  42. n2 := big.NewInt(int64(15))
  43. c1 := Encrypt(n1, key.PubK)
  44. c2 := Encrypt(n2, key.PubK)
  45. c3c4 := HomomorphicMul(c1, c2, key.PubK)
  46. d := Decrypt(c3c4, key.PrivK)
  47. if !bytes.Equal(new(big.Int).Mul(n1, n2).Bytes(), d.Bytes()) {
  48. t.Errorf("decrypted result not equal to original result")
  49. }
  50. }