test_serial_cc.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. cryptoContext.Enable(fhe.PKESchemeFeature.PRE)
  39. keypair = cryptoContext.KeyGen()
  40. vectorOfInts = list(range(12))
  41. plaintext = cryptoContext.MakePackedPlaintext(vectorOfInts)
  42. ciphertext = cryptoContext.Encrypt(keypair.publicKey, plaintext)
  43. evalKey = cryptoContext.ReKeyGen(keypair.secretKey, keypair.publicKey)
  44. cryptoContext_ser = fhe.Serialize(cryptoContext, mode)
  45. LOGGER.debug("The cryptocontext has been serialized.")
  46. publickey_ser = fhe.Serialize(keypair.publicKey, mode)
  47. LOGGER.debug("The public key has been serialized.")
  48. secretkey_ser = fhe.Serialize(keypair.secretKey, mode)
  49. LOGGER.debug("The private key has been serialized.")
  50. ciphertext_ser = fhe.Serialize(ciphertext, mode)
  51. LOGGER.debug("The ciphertext has been serialized.")
  52. evalKey_ser = fhe.Serialize(evalKey, mode)
  53. LOGGER.debug("The evaluation key has been serialized.")
  54. cryptoContext.ClearEvalMultKeys()
  55. cryptoContext.ClearEvalAutomorphismKeys()
  56. fhe.ReleaseAllContexts()
  57. cc = fhe.DeserializeCryptoContextString(cryptoContext_ser, mode)
  58. assert isinstance(cc, fhe.CryptoContext)
  59. LOGGER.debug("The cryptocontext has been deserialized.")
  60. pk = fhe.DeserializePublicKeyString(publickey_ser, mode)
  61. assert isinstance(pk, fhe.PublicKey)
  62. LOGGER.debug("The public key has been deserialized.")
  63. sk = fhe.DeserializePrivateKeyString(secretkey_ser, mode)
  64. assert isinstance(sk, fhe.PrivateKey)
  65. LOGGER.debug("The private key has been deserialized.")
  66. ct = fhe.DeserializeCiphertextString(ciphertext_ser, mode)
  67. assert isinstance(ct, fhe.Ciphertext)
  68. LOGGER.debug("The ciphertext has been reserialized.")
  69. ek = fhe.DeserializeEvalKeyString(evalKey_ser, mode)
  70. assert isinstance(ek, fhe.EvalKey)
  71. LOGGER.debug("The evaluation key has been deserialized.")