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.

62 lines
1.3 KiB

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