test_serial_cc.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import logging
  2. import pytest
  3. import openfhe as fhe
  4. LOGGER = logging.getLogger("test_serial_cc")
  5. def test_serial_cryptocontext(tmp_path):
  6. parameters = fhe.CCParamsBFVRNS()
  7. parameters.SetPlaintextModulus(65537)
  8. parameters.SetMultiplicativeDepth(2)
  9. cryptoContext = fhe.GenCryptoContext(parameters)
  10. cryptoContext.Enable(fhe.PKESchemeFeature.PKE)
  11. keypair = cryptoContext.KeyGen()
  12. vectorOfInts1 = list(range(12))
  13. plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
  14. ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
  15. assert fhe.SerializeToFile(str(tmp_path / "cryptocontext.json"), cryptoContext, fhe.JSON)
  16. LOGGER.debug("The cryptocontext has been serialized.")
  17. assert fhe.SerializeToFile(str(tmp_path / "ciphertext1.json"), ciphertext1, fhe.JSON)
  18. cryptoContext.ClearEvalMultKeys()
  19. cryptoContext.ClearEvalAutomorphismKeys()
  20. fhe.ReleaseAllContexts()
  21. cc, success = fhe.DeserializeCryptoContext(str(tmp_path / "cryptocontext.json"), fhe.JSON)
  22. assert success
  23. assert isinstance(cc, fhe.CryptoContext)
  24. assert fhe.SerializeToFile(str(tmp_path / "cryptocontext2.json"), cc, fhe.JSON)
  25. LOGGER.debug("The cryptocontext has been serialized.")
  26. ct1, success = fhe.DeserializeCiphertext(str(tmp_path / "ciphertext1.json"), fhe.JSON)
  27. assert success
  28. assert isinstance(ct1, fhe.Ciphertext)
  29. LOGGER.debug("Cryptocontext deserializes to %s %s", success, ct1)
  30. assert fhe.SerializeToFile(str(tmp_path / "ciphertext12.json"), ct1, fhe.JSON)
  31. @pytest.mark.parametrize("mode", [fhe.JSON, fhe.BINARY])
  32. def test_serial_cryptocontext_str(mode):
  33. parameters = fhe.CCParamsBFVRNS()
  34. parameters.SetPlaintextModulus(65537)
  35. parameters.SetMultiplicativeDepth(2)
  36. cryptoContext = fhe.GenCryptoContext(parameters)
  37. cryptoContext.Enable(fhe.PKESchemeFeature.PKE)
  38. keypair = cryptoContext.KeyGen()
  39. vectorOfInts = list(range(12))
  40. plaintext = cryptoContext.MakePackedPlaintext(vectorOfInts)
  41. ciphertext = cryptoContext.Encrypt(keypair.publicKey, plaintext)
  42. cryptoContext_ser = fhe.Serialize(cryptoContext, mode)
  43. LOGGER.debug("The cryptocontext has been serialized.")
  44. ciphertext_ser = fhe.Serialize(ciphertext, mode)
  45. LOGGER.debug("The ciphertext has been serialized.")
  46. cryptoContext.ClearEvalMultKeys()
  47. cryptoContext.ClearEvalAutomorphismKeys()
  48. fhe.ReleaseAllContexts()
  49. cc = fhe.DeserializeCryptoContextString(cryptoContext_ser, mode)
  50. assert isinstance(cc, fhe.CryptoContext)
  51. LOGGER.debug("The cryptocontext has been deserialized.")
  52. ct = fhe.DeserializeCiphertextString(ciphertext_ser, mode)
  53. assert isinstance(ct, fhe.Ciphertext)
  54. LOGGER.debug("The ciphertext has been reserialized.")