A somewhat-homomorphic (insecure) cryptosystem
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.

trostle-parrish_test.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package trostle_parrish
  2. import (
  3. "math/big"
  4. "testing"
  5. )
  6. func TestEncryptDecrypt(t *testing.T) {
  7. // These are representative encryption params
  8. pk := Generate(30, 255)
  9. plaintext := big.NewInt(14)
  10. ciphertext := pk.Encrypt(plaintext)
  11. t.Logf("Encrypted: %v", ciphertext)
  12. decrypted := pk.Decrypt(ciphertext)
  13. t.Logf("Decrypted: %v", decrypted)
  14. if decrypted.Cmp(plaintext) != 0 {
  15. t.Error("Failed to Decrypt")
  16. }
  17. }
  18. func TestMultipleAdditions(t *testing.T) {
  19. // These params are ridiculous low, but they enable us to
  20. // check that addition is working as expected.
  21. pk := Generate(6, 32)
  22. plaintext := big.NewInt(1)
  23. total := pk.Encrypt(plaintext)
  24. for i := 0; i < 128; i++ {
  25. plaintext := big.NewInt(5)
  26. ciphertext := pk.Encrypt(plaintext)
  27. total = Add(total, ciphertext)
  28. t.Logf("Encrypted: %v", total)
  29. decrypted := pk.Decrypt(total)
  30. t.Logf("Decrypted: %v", decrypted)
  31. // We should see a wrap around at 16 based on our encryption params
  32. if decrypted.Cmp(big.NewInt(int64(((i+1)*5)+1))) != 0 {
  33. t.Errorf("Should have been able to do ~2^6 additions = 64, instead: %v", i)
  34. }
  35. }
  36. }