test_serial_cc.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import logging
  2. import openfhe as fhe
  3. LOGGER = logging.getLogger("test_serial_cc")
  4. def test_serial_cryptocontext(tmp_path):
  5. parameters = fhe.CCParamsBFVRNS()
  6. parameters.SetPlaintextModulus(65537)
  7. parameters.SetMultiplicativeDepth(2)
  8. cryptoContext = fhe.GenCryptoContext(parameters)
  9. cryptoContext.Enable(fhe.PKESchemeFeature.PKE)
  10. keypair = cryptoContext.KeyGen()
  11. vectorOfInts1 = list(range(12))
  12. plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
  13. ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
  14. assert fhe.SerializeToFile(str(tmp_path / "cryptocontext.json"), cryptoContext, fhe.JSON)
  15. LOGGER.debug("The cryptocontext has been serialized.")
  16. assert fhe.SerializeToFile(str(tmp_path / "ciphertext1.json"), ciphertext1, fhe.JSON)
  17. cryptoContext.ClearEvalMultKeys()
  18. cryptoContext.ClearEvalAutomorphismKeys()
  19. fhe.ReleaseAllContexts()
  20. cc, success = fhe.DeserializeCryptoContext(str(tmp_path / "cryptocontext.json"), fhe.JSON)
  21. assert success
  22. assert isinstance(cc, fhe.CryptoContext)
  23. assert fhe.SerializeToFile(str(tmp_path / "cryptocontext2.json"), cc, fhe.JSON)
  24. LOGGER.debug("The cryptocontext has been serialized.")
  25. ct1, success = fhe.DeserializeCiphertext(str(tmp_path / "ciphertext1.json"), fhe.JSON)
  26. assert success
  27. assert isinstance(ct1, fhe.Ciphertext)
  28. LOGGER.debug("Cryptocontext deserializes to %s %s", success, ct1)
  29. assert fhe.SerializeToFile(str(tmp_path / "ciphertext12.json"), ct1, fhe.JSON)