소스 검색

Introduce CC serialization tests

Erik Takke 1 년 전
부모
커밋
d5fab4207e
1개의 변경된 파일33개의 추가작업 그리고 0개의 파일을 삭제
  1. 33 0
      tests/test_serial_cc.py

+ 33 - 0
tests/test_serial_cc.py

@@ -1,4 +1,5 @@
 import logging
+import pytest
 
 import openfhe as fhe
 
@@ -37,3 +38,35 @@ def test_serial_cryptocontext(tmp_path):
     assert isinstance(ct1, fhe.Ciphertext)
     LOGGER.debug("Cryptocontext deserializes to %s %s", success, ct1)
     assert fhe.SerializeToFile(str(tmp_path / "ciphertext12.json"), ct1, fhe.JSON)
+
+
+@pytest.mark.parametrize("mode", [fhe.JSON, fhe.BINARY])
+def test_serial_cryptocontext_str(mode):
+    parameters = fhe.CCParamsBFVRNS()
+    parameters.SetPlaintextModulus(65537)
+    parameters.SetMultiplicativeDepth(2)
+
+    cryptoContext = fhe.GenCryptoContext(parameters)
+    cryptoContext.Enable(fhe.PKESchemeFeature.PKE)
+
+    keypair = cryptoContext.KeyGen()
+    vectorOfInts = list(range(12))
+    plaintext = cryptoContext.MakePackedPlaintext(vectorOfInts)
+    ciphertext = cryptoContext.Encrypt(keypair.publicKey, plaintext)
+
+    cryptoContext_ser = fhe.Serialize(cryptoContext, mode)
+    LOGGER.debug("The cryptocontext has been serialized.")
+    ciphertext_ser = fhe.Serialize(ciphertext, mode)
+    LOGGER.debug("The ciphertext has been serialized.")
+
+    cryptoContext.ClearEvalMultKeys()
+    cryptoContext.ClearEvalAutomorphismKeys()
+    fhe.ReleaseAllContexts()
+
+    cc = fhe.DeserializeCryptoContextString(cryptoContext_ser, mode)
+    assert isinstance(cc, fhe.CryptoContext)
+    LOGGER.debug("The cryptocontext has been deserialized.")
+
+    ct = fhe.DeserializeCiphertextString(ciphertext_ser, mode)
+    assert isinstance(ct, fhe.Ciphertext)
+    LOGGER.debug("The ciphertext has been reserialized.")