Explorar el Código

#53: add initial unittests
- iterate through boolean AND OR NOT test
- test all the examples from the unittest for run through

- run black code formatter
- update CMakeLists.txt for the situation of python3.12 build
- add tests

Muthu Annamalai hace 1 año
padre
commit
ad69449d5b

+ 16 - 1
CMakeLists.txt

@@ -69,11 +69,26 @@ else()
     # Set Python_EXECUTABLE to the specified path
     set(Python_EXECUTABLE "${PYTHON_EXECUTABLE_PATH}")
 endif()
+
+# Find Python interpreter
+find_package(PythonInterp REQUIRED)
+
+# Check Python version
+if(${PYTHON_VERSION_MAJOR} EQUAL 3 AND ${PYTHON_VERSION_MINOR} GREATER_EQUAL 10)
+execute_process(
+    COMMAND "${Python_EXECUTABLE}" -c "from sys import exec_prefix; print(exec_prefix)"
+    OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
+    OUTPUT_STRIP_TRAILING_WHITESPACE
+ )       
+else()
 execute_process(
     COMMAND "${Python_EXECUTABLE}" -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
     OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
     OUTPUT_STRIP_TRAILING_WHITESPACE
-)
+ )    
+endif()
+
+
 
 message(STATUS "Python site packages directory: ${PYTHON_SITE_PACKAGES}")
 install(TARGETS openfhe LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES})

+ 77 - 67
examples/pke/simple-integers-bgvrns.py

@@ -1,94 +1,104 @@
 # Initial Settings
 from openfhe import *
+import os
+
 # import openfhe.PKESchemeFeature as Feature
 
 
-# Sample Program: Step 1: Set CryptoContext
-parameters = CCParamsBGVRNS()
-parameters.SetPlaintextModulus(65537)
-parameters.SetMultiplicativeDepth(2)
+def main():
+    # Sample Program: Step 1: Set CryptoContext
+    parameters = CCParamsBGVRNS()
+    parameters.SetPlaintextModulus(65537)
+    parameters.SetMultiplicativeDepth(2)
+
+    crypto_context = GenCryptoContext(parameters)
+    # Enable features that you wish to use
+    crypto_context.Enable(PKESchemeFeature.PKE)
+    crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
+    crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
 
-crypto_context = GenCryptoContext(parameters)
-# Enable features that you wish to use
-crypto_context.Enable(PKESchemeFeature.PKE)
-crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
-crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
+    # Sample Program: Step 2: Key Generation
 
-# Sample Program: Step 2: Key Generation
+    # Generate a public/private key pair
+    key_pair = crypto_context.KeyGen()
 
-# Generate a public/private key pair
-key_pair = crypto_context.KeyGen()
+    # Generate the relinearization key
+    crypto_context.EvalMultKeyGen(key_pair.secretKey)
 
-# Generate the relinearization key
-crypto_context.EvalMultKeyGen(key_pair.secretKey)
+    # Generate the rotation evaluation keys
+    crypto_context.EvalRotateKeyGen(key_pair.secretKey, [1, 2, -1, -2])
 
-# Generate the rotation evaluation keys
-crypto_context.EvalRotateKeyGen(key_pair.secretKey, [1, 2, -1, -2])
+    # Sample Program: Step 3: Encryption
 
-# Sample Program: Step 3: Encryption
+    # First plaintext vector is encoded
+    vector_of_ints1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext1 = crypto_context.MakePackedPlaintext(vector_of_ints1)
 
-# First plaintext vector is encoded
-vector_of_ints1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext1 = crypto_context.MakePackedPlaintext(vector_of_ints1)
+    # Second plaintext vector is encoded
+    vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)
 
-# Second plaintext vector is encoded
-vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)
+    # Third plaintext vector is encoded
+    vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)
 
-# Third plaintext vector is encoded
-vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)
+    # The encoded vectors are encrypted
+    ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
+    ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
+    ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)
 
-# The encoded vectors are encrypted
-ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
-ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
-ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)
+    #  Sample Program: Step 4: Evaluation
 
-#  Sample Program: Step 4: Evaluation
+    # Homomorphic additions
+    ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
+    ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)
 
-# Homomorphic additions
-ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
-ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)
+    # Homomorphic Multiplication
+    ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
+    ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)
 
-# Homomorphic Multiplication
-ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
-ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)
+    # Homomorphic Rotations
+    ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
+    ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
+    ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
+    ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)
 
-# Homomorphic Rotations
-ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
-ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
-ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
-ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)
+    # Sample Program: Step 5: Decryption
 
-# Sample Program: Step 5: Decryption
+    # Decrypt the result of additions
+    plaintext_add_result = crypto_context.Decrypt(
+        ciphertext_add_result, key_pair.secretKey
+    )
 
-# Decrypt the result of additions
-plaintext_add_result = crypto_context.Decrypt(ciphertext_add_result,key_pair.secretKey)
+    # Decrypt the result of multiplications
+    plaintext_mult_result = crypto_context.Decrypt(
+        ciphertext_mult_result, key_pair.secretKey
+    )
 
-# Decrypt the result of multiplications
-plaintext_mult_result = crypto_context.Decrypt(ciphertext_mult_result,key_pair.secretKey)
+    # Decrypt the result of rotations
+    plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1, key_pair.secretKey)
+    plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2, key_pair.secretKey)
+    plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3, key_pair.secretKey)
+    plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4, key_pair.secretKey)
 
-# Decrypt the result of rotations
-plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1,key_pair.secretKey)
-plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2,key_pair.secretKey)
-plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3,key_pair.secretKey)
-plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4,key_pair.secretKey)
+    plaintextRot1.SetLength(len(vector_of_ints1))
+    plaintextRot2.SetLength(len(vector_of_ints1))
+    plaintextRot3.SetLength(len(vector_of_ints1))
+    plaintextRot4.SetLength(len(vector_of_ints1))
 
+    print("Plaintext #1: " + str(plaintext1))
+    print("Plaintext #2: " + str(plaintext2))
+    print("Plaintext #3: " + str(plaintext3))
 
-plaintextRot1.SetLength(len(vector_of_ints1))
-plaintextRot2.SetLength(len(vector_of_ints1))
-plaintextRot3.SetLength(len(vector_of_ints1))
-plaintextRot4.SetLength(len(vector_of_ints1))
+    # Output Results
+    print("\nResults of homomorphic computations")
+    print("#1 + #2 + #3 = " + str(plaintext_add_result))
+    print("#1 * #2 * #3 = " + str(plaintext_mult_result))
+    print("Left rotation of #1 by 1 = " + str(plaintextRot1))
+    print("Left rotation of #1 by 2 = " + str(plaintextRot2))
+    print("Right rotation of #1 by 1 = " + str(plaintextRot3))
+    print("Right rotation of #1 by 2 = " + str(plaintextRot4))
 
-print("Plaintext #1: " + str(plaintext1))
-print("Plaintext #2: " + str(plaintext2))
-print("Plaintext #3: " + str(plaintext3))
 
-# Output Results
-print("\nResults of homomorphic computations")
-print("#1 + #2 + #3 = " + str(plaintext_add_result))
-print("#1 * #2 * #3 = " + str(plaintext_mult_result))
-print("Left rotation of #1 by 1 = " + str(plaintextRot1))
-print("Left rotation of #1 by 2 = " + str(plaintextRot2))
-print("Right rotation of #1 by 1 = " + str(plaintextRot3))
-print("Right rotation of #1 by 2 = " + str(plaintextRot4))
+if __name__ == "__main__":
+    main()

+ 221 - 180
examples/pke/simple-integers-serial-bgvrns.py

@@ -1,193 +1,234 @@
 # Initial Settings
 from openfhe import *
-# import openfhe.PKESchemeFeature as Feature
-
-datafolder = 'demoData'
-serType = BINARY # BINARY or JSON 
-print("This program requres the subdirectory `" + datafolder + "' to exist, otherwise you will get an error writing serializations.")
-
-# Sample Program: Step 1: Set CryptoContext
-parameters = CCParamsBGVRNS()
-parameters.SetPlaintextModulus(65537)
-parameters.SetMultiplicativeDepth(2)
-
-cryptoContext = GenCryptoContext(parameters)
-# Enable features that you wish to use
-cryptoContext.Enable(PKESchemeFeature.PKE)
-cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
-cryptoContext.Enable(PKESchemeFeature.LEVELEDSHE)
-
-# Serialize cryptocontext
-if not SerializeToFile(datafolder + "/cryptocontext.txt", cryptoContext, serType):
-   raise Exception("Error writing serialization of the crypto context to cryptocontext.txt")
-print("The cryptocontext has been serialized.")
-
-# Sample Program: Step 2: Key Generation
-
-# Generate a public/private key pair
-keypair = cryptoContext.KeyGen()
-print("The keypair has been generated.")
-
-# Serialize the public key
-if not SerializeToFile(datafolder + "/key-public.txt", keypair.publicKey, serType):
-   raise Exception("Error writing serialization of the public key to key-public.txt")
-print("The public key has been serialized.")
-
-# Serialize the secret key
-if not SerializeToFile(datafolder + "/key-private.txt", keypair.secretKey, serType):
-   raise Exception("Error writing serialization of the secret key to key-private.txt")
-print("The secret key has been serialized.")
-
-# Generate the relinearization key
-cryptoContext.EvalMultKeyGen(keypair.secretKey)
-print("The relinearization key has been generated.")
-
-# Serialize the relinearization key
-if not cryptoContext.SerializeEvalMultKey(datafolder + "/key-eval-mult.txt",serType):
-   raise Exception("Error writing serialization of the eval mult keys to \"key-eval-mult.txt\"")
-print("The relinearization key has been serialized.")
-
-# Generate the rotation evaluation keys
-cryptoContext.EvalRotateKeyGen(keypair.secretKey, [1, 2, -1, -2])
-print("The rotation evaluation keys have been generated.")
-
-# Serialize the rotation evaluation keys
-if not cryptoContext.SerializeEvalAutomorphismKey(datafolder + "/key-eval-rot.txt",serType):
-   raise Exception("Error writing serialization of the eval rotate keys to \"key-eval-rot.txt\"")
-print("The rotation evaluation keys have been serialized.")
-
-# Sample Program: Step 3: Encryption
-
-# First plaintext vector is encoded
-vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
-
-# Second plaintext vector is encoded
-vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
-
-# Third plaintext vector is encoded
-vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
-
-
-# The encoded vectors are encrypted
-ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
-ciphertext2 = cryptoContext.Encrypt(keypair.publicKey, plaintext2)
-ciphertext3 = cryptoContext.Encrypt(keypair.publicKey, plaintext3)
-print("The plaintexts have been encrypted.")
+import tempfile
+import os
 
-if not SerializeToFile(datafolder + "/ciphertext1.txt", ciphertext1, serType):
-   raise Exception("Error writing serialization of ciphertext 1 to ciphertext1.txt")
-print("The first ciphertext has been serialized.")
-
-if not SerializeToFile(datafolder + "/ciphertext2.txt", ciphertext2, serType):
-   raise Exception("Error writing serialization of ciphertext2 to ciphertext2.txt")
-print("The second ciphertext has been serialized.")
-
-if not SerializeToFile(datafolder + "/ciphertext3.txt", ciphertext3, serType):   
-   raise Exception("Error writing serialization of ciphertext3 to ciphertext3.txt")
-print("The third ciphertext has been serialized.")
-
-# Sample Program: Step 4: Evaluation
-
-# OpenFHE maintains an internal map of CryptoContext objects which are
-# indexed by a tag and the tag is applied to both the CryptoContext and some
-# of the keys. When deserializing a context, OpenFHE checks for the tag and
-# if it finds it in the CryptoContext map, it will return the stored version.
-# Hence, we need to clear the context and clear the keys.
-cryptoContext.ClearEvalMultKeys()
-cryptoContext.ClearEvalAutomorphismKeys()
-ReleaseAllContexts()
-
-# Deserialize the crypto context
-
-cc, res = DeserializeCryptoContext(datafolder + "/cryptocontext.txt", serType)
-if not res:
-   raise Exception("Error reading serialization of the crypto context from cryptocontext.txt")
-print("The cryptocontext has been deserialized.")
-
-# Deserialize the public key
-pk, res = DeserializePublicKey(datafolder + "/key-public.txt", serType)
-
-if not res:
-   raise Exception("Error reading serialization of the public key from key-public.txt")
-print("The public key has been deserialized.")
-
-if not cc.DeserializeEvalMultKey(datafolder + "/key-eval-mult.txt",serType):
-   raise Exception("Could not deserialize the eval mult key file")
-
-print("The relinearization key has been deserialized.")
-
-if not cc.DeserializeEvalAutomorphismKey(datafolder + "/key-eval-rot.txt",serType):
-   raise Exception("Could not deserialize the eval rotation key file")
-
-print("Deserialized the eval rotation keys.")
-
-# Deserialize the ciphertexts
-ct1, res =  DeserializeCiphertext(datafolder + "/ciphertext1.txt", serType)
-
-if not res:
-    raise Exception("Could not read the ciphertext")
-print("The first ciphertext has been deserialized.")
-
-ct2, res = DeserializeCiphertext(datafolder + "/ciphertext2.txt", serType)
-
-if not res:
-    raise Exception("Could not read the ciphertext")
-print("The second ciphertext has been deserialized.")
-
-ct3, res = DeserializeCiphertext(datafolder + "/ciphertext3.txt", serType)
-if not res:   
-    raise Exception("Could not read the ciphertext")
-print("The third ciphertext has been deserialized.")
-
-# Homomorphic addition
+# import openfhe.PKESchemeFeature as Feature
 
-ciphertextAdd12 = cc.EvalAdd(ct1, ct2)
-ciphertextAddResult = cc.EvalAdd(ciphertextAdd12, ct3)
+datafolder = "demoData"
+
+
+def main_action():
+    serType = BINARY  # BINARY or JSON
+    print(
+        "This program requres the subdirectory `"
+        + datafolder
+        + "' to exist, otherwise you will get an error writing serializations."
+    )
+
+    # Sample Program: Step 1: Set CryptoContext
+    parameters = CCParamsBGVRNS()
+    parameters.SetPlaintextModulus(65537)
+    parameters.SetMultiplicativeDepth(2)
+
+    cryptoContext = GenCryptoContext(parameters)
+    # Enable features that you wish to use
+    cryptoContext.Enable(PKESchemeFeature.PKE)
+    cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
+    cryptoContext.Enable(PKESchemeFeature.LEVELEDSHE)
+
+    # Serialize cryptocontext
+    if not SerializeToFile(datafolder + "/cryptocontext.txt", cryptoContext, serType):
+        raise Exception(
+            "Error writing serialization of the crypto context to cryptocontext.txt"
+        )
+    print("The cryptocontext has been serialized.")
+
+    # Sample Program: Step 2: Key Generation
+
+    # Generate a public/private key pair
+    keypair = cryptoContext.KeyGen()
+    print("The keypair has been generated.")
+
+    # Serialize the public key
+    if not SerializeToFile(datafolder + "/key-public.txt", keypair.publicKey, serType):
+        raise Exception(
+            "Error writing serialization of the public key to key-public.txt"
+        )
+    print("The public key has been serialized.")
+
+    # Serialize the secret key
+    if not SerializeToFile(datafolder + "/key-private.txt", keypair.secretKey, serType):
+        raise Exception(
+            "Error writing serialization of the secret key to key-private.txt"
+        )
+    print("The secret key has been serialized.")
+
+    # Generate the relinearization key
+    cryptoContext.EvalMultKeyGen(keypair.secretKey)
+    print("The relinearization key has been generated.")
+
+    # Serialize the relinearization key
+    if not cryptoContext.SerializeEvalMultKey(
+        datafolder + "/key-eval-mult.txt", serType
+    ):
+        raise Exception(
+            'Error writing serialization of the eval mult keys to "key-eval-mult.txt"'
+        )
+    print("The relinearization key has been serialized.")
+
+    # Generate the rotation evaluation keys
+    cryptoContext.EvalRotateKeyGen(keypair.secretKey, [1, 2, -1, -2])
+    print("The rotation evaluation keys have been generated.")
+
+    # Serialize the rotation evaluation keys
+    if not cryptoContext.SerializeEvalAutomorphismKey(
+        datafolder + "/key-eval-rot.txt", serType
+    ):
+        raise Exception(
+            'Error writing serialization of the eval rotate keys to "key-eval-rot.txt"'
+        )
+    print("The rotation evaluation keys have been serialized.")
+
+    # Sample Program: Step 3: Encryption
+
+    # First plaintext vector is encoded
+    vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
+
+    # Second plaintext vector is encoded
+    vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
+
+    # Third plaintext vector is encoded
+    vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
+
+    # The encoded vectors are encrypted
+    ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
+    ciphertext2 = cryptoContext.Encrypt(keypair.publicKey, plaintext2)
+    ciphertext3 = cryptoContext.Encrypt(keypair.publicKey, plaintext3)
+    print("The plaintexts have been encrypted.")
+
+    if not SerializeToFile(datafolder + "/ciphertext1.txt", ciphertext1, serType):
+        raise Exception(
+            "Error writing serialization of ciphertext 1 to ciphertext1.txt"
+        )
+    print("The first ciphertext has been serialized.")
+
+    if not SerializeToFile(datafolder + "/ciphertext2.txt", ciphertext2, serType):
+        raise Exception("Error writing serialization of ciphertext2 to ciphertext2.txt")
+    print("The second ciphertext has been serialized.")
+
+    if not SerializeToFile(datafolder + "/ciphertext3.txt", ciphertext3, serType):
+        raise Exception("Error writing serialization of ciphertext3 to ciphertext3.txt")
+    print("The third ciphertext has been serialized.")
+
+    # Sample Program: Step 4: Evaluation
+
+    # OpenFHE maintains an internal map of CryptoContext objects which are
+    # indexed by a tag and the tag is applied to both the CryptoContext and some
+    # of the keys. When deserializing a context, OpenFHE checks for the tag and
+    # if it finds it in the CryptoContext map, it will return the stored version.
+    # Hence, we need to clear the context and clear the keys.
+    cryptoContext.ClearEvalMultKeys()
+    cryptoContext.ClearEvalAutomorphismKeys()
+    ReleaseAllContexts()
+
+    # Deserialize the crypto context
+
+    cc, res = DeserializeCryptoContext(datafolder + "/cryptocontext.txt", serType)
+    if not res:
+        raise Exception(
+            "Error reading serialization of the crypto context from cryptocontext.txt"
+        )
+    print("The cryptocontext has been deserialized.")
+
+    # Deserialize the public key
+    pk, res = DeserializePublicKey(datafolder + "/key-public.txt", serType)
+
+    if not res:
+        raise Exception(
+            "Error reading serialization of the public key from key-public.txt"
+        )
+    print("The public key has been deserialized.")
+
+    if not cc.DeserializeEvalMultKey(datafolder + "/key-eval-mult.txt", serType):
+        raise Exception("Could not deserialize the eval mult key file")
+
+    print("The relinearization key has been deserialized.")
+
+    if not cc.DeserializeEvalAutomorphismKey(datafolder + "/key-eval-rot.txt", serType):
+        raise Exception("Could not deserialize the eval rotation key file")
+
+    print("Deserialized the eval rotation keys.")
+
+    # Deserialize the ciphertexts
+    ct1, res = DeserializeCiphertext(datafolder + "/ciphertext1.txt", serType)
+
+    if not res:
+        raise Exception("Could not read the ciphertext")
+    print("The first ciphertext has been deserialized.")
+
+    ct2, res = DeserializeCiphertext(datafolder + "/ciphertext2.txt", serType)
+
+    if not res:
+        raise Exception("Could not read the ciphertext")
+    print("The second ciphertext has been deserialized.")
+
+    ct3, res = DeserializeCiphertext(datafolder + "/ciphertext3.txt", serType)
+    if not res:
+        raise Exception("Could not read the ciphertext")
+    print("The third ciphertext has been deserialized.")
+
+    # Homomorphic addition
+
+    ciphertextAdd12 = cc.EvalAdd(ct1, ct2)
+    ciphertextAddResult = cc.EvalAdd(ciphertextAdd12, ct3)
+
+    # Homomorphic multiplication
+    ciphertextMult12 = cc.EvalMult(ct1, ct2)
+    ciphertextMultResult = cc.EvalMult(ciphertextMult12, ct3)
 
-# Homomorphic multiplication
-ciphertextMult12 = cc.EvalMult(ct1, ct2)
-ciphertextMultResult = cc.EvalMult(ciphertextMult12, ct3)
+    # Homomorphic rotation
+    ciphertextRot1 = cc.EvalRotate(ct1, 1)
+    ciphertextRot2 = cc.EvalRotate(ct2, 2)
+    ciphertextRot3 = cc.EvalRotate(ct3, -1)
+    ciphertextRot4 = cc.EvalRotate(ct3, -2)
 
-# Homomorphic rotation
-ciphertextRot1 = cc.EvalRotate(ct1, 1)
-ciphertextRot2 = cc.EvalRotate(ct2, 2)
-ciphertextRot3 = cc.EvalRotate(ct3, -1)
-ciphertextRot4 = cc.EvalRotate(ct3, -2)
+    # Sample Program: Step 5: Decryption
+
+    sk, res = DeserializePrivateKey(datafolder + "/key-private.txt", serType)
+    if not res:
+        raise Exception("Could not read secret key")
+    print("The secret key has been deserialized.")
+
+    # Decrypt the result of additions
+    plaintextAddResult = cc.Decrypt(sk, ciphertextAddResult)
+
+    # Decrypt the result of multiplications
+    plaintextMultResult = cc.Decrypt(sk, ciphertextMultResult)
 
-# Sample Program: Step 5: Decryption
+    # Decrypt the result of rotations
+    plaintextRot1 = cc.Decrypt(sk, ciphertextRot1)
+    plaintextRot2 = cc.Decrypt(sk, ciphertextRot2)
+    plaintextRot3 = cc.Decrypt(sk, ciphertextRot3)
+    plaintextRot4 = cc.Decrypt(sk, ciphertextRot4)
 
-sk, res = DeserializePrivateKey(datafolder + "/key-private.txt", serType)
-if not res:
-      raise Exception("Could not read secret key")
-print("The secret key has been deserialized.")
+    # Shows only the same number of elements as in the original plaintext vector
+    # By default it will show all coefficients in the BFV-encoded polynomial
+    plaintextRot1.SetLength(len(vectorOfInts1))
+    plaintextRot2.SetLength(len(vectorOfInts1))
+    plaintextRot3.SetLength(len(vectorOfInts1))
+    plaintextRot4.SetLength(len(vectorOfInts1))
 
-# Decrypt the result of additions
-plaintextAddResult = cc.Decrypt(sk, ciphertextAddResult)
+    # Output results
+    print("\nResults of homomorphic computations")
+    print("#1 + #2 + #3: " + str(plaintextAddResult))
+    print("#1 * #2 * #3: " + str(plaintextMultResult))
+    print("Left rotation of #1 by 1: " + str(plaintextRot1))
+    print("Left rotation of #1 by 2: " + str(plaintextRot2))
+    print("Right rotation of #1 by 1: " + str(plaintextRot3))
+    print("Right rotation of #1 by 2: " + str(plaintextRot4))
 
-# Decrypt the result of multiplications
-plaintextMultResult = cc.Decrypt(sk, ciphertextMultResult)
 
-# Decrypt the result of rotations
-plaintextRot1 = cc.Decrypt(sk, ciphertextRot1)
-plaintextRot2 = cc.Decrypt(sk, ciphertextRot2)
-plaintextRot3 = cc.Decrypt(sk, ciphertextRot3)
-plaintextRot4 = cc.Decrypt(sk, ciphertextRot4)
+def main():
+    global datafolder
+    with tempfile.TemporaryDirectory() as td:
+        datafolder = td + "/" + datafolder
+        os.mkdir(datafolder)
+        main_action()
 
-# Shows only the same number of elements as in the original plaintext vector
-# By default it will show all coefficients in the BFV-encoded polynomial
-plaintextRot1.SetLength(len(vectorOfInts1))
-plaintextRot2.SetLength(len(vectorOfInts1))
-plaintextRot3.SetLength(len(vectorOfInts1))
-plaintextRot4.SetLength(len(vectorOfInts1))
 
-# Output results
-print("\nResults of homomorphic computations")
-print("#1 + #2 + #3: " + str(plaintextAddResult))
-print("#1 * #2 * #3: " + str(plaintextMultResult))
-print("Left rotation of #1 by 1: " + str(plaintextRot1))
-print("Left rotation of #1 by 2: " + str(plaintextRot2))
-print("Right rotation of #1 by 1: " + str(plaintextRot3))
-print("Right rotation of #1 by 2: " + str(plaintextRot4))
+if __name__ == "__main__":
+    main()

+ 222 - 181
examples/pke/simple-integers-serial.py

@@ -1,193 +1,234 @@
 # Initial Settings
 from openfhe import *
-# import openfhe.PKESchemeFeature as Feature
-
-datafolder = 'demoData'
-serType = BINARY # BINARY or JSON 
-print("This program requres the subdirectory `" + datafolder + "' to exist, otherwise you will get an error writing serializations.")
-
-# Sample Program: Step 1: Set CryptoContext
-parameters = CCParamsBFVRNS()
-parameters.SetPlaintextModulus(65537)
-parameters.SetMultiplicativeDepth(2)
-
-cryptoContext = GenCryptoContext(parameters)
-# Enable features that you wish to use
-cryptoContext.Enable(PKESchemeFeature.PKE)
-cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
-cryptoContext.Enable(PKESchemeFeature.LEVELEDSHE)
-
-# Serialize cryptocontext
-if not SerializeToFile(datafolder + "/cryptocontext.txt", cryptoContext, serType):
-   raise Exception("Error writing serialization of the crypto context to cryptocontext.txt")
-print("The cryptocontext has been serialized.")
-
-# Sample Program: Step 2: Key Generation
-
-# Generate a public/private key pair
-keypair = cryptoContext.KeyGen()
-print("The keypair has been generated.")
-
-# Serialize the public key
-if not SerializeToFile(datafolder + "/key-public.txt", keypair.publicKey, serType):
-   raise Exception("Error writing serialization of the public key to key-public.txt")
-print("The public key has been serialized.")
-
-# Serialize the secret key
-if not SerializeToFile(datafolder + "/key-private.txt", keypair.secretKey, serType):
-   raise Exception("Error writing serialization of the secret key to key-private.txt")
-print("The secret key has been serialized.")
-
-# Generate the relinearization key
-cryptoContext.EvalMultKeyGen(keypair.secretKey)
-print("The relinearization key has been generated.")
-
-# Serialize the relinearization key
-if not cryptoContext.SerializeEvalMultKey(datafolder + "/key-eval-mult.txt",serType):
-   raise Exception("Error writing serialization of the eval mult keys to \"key-eval-mult.txt\"")
-print("The relinearization key has been serialized.")
-
-# Generate the rotation evaluation keys
-cryptoContext.EvalRotateKeyGen(keypair.secretKey, [1, 2, -1, -2])
-print("The rotation evaluation keys have been generated.")
-
-# Serialize the rotation evaluation keys
-if not cryptoContext.SerializeEvalAutomorphismKey(datafolder + "/key-eval-rot.txt",serType):
-   raise Exception("Error writing serialization of the eval rotate keys to \"key-eval-rot.txt\"")
-print("The rotation evaluation keys have been serialized.")
-
-# Sample Program: Step 3: Encryption
-
-# First plaintext vector is encoded
-vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
-
-# Second plaintext vector is encoded
-vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
-
-# Third plaintext vector is encoded
-vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
-
-
-# The encoded vectors are encrypted
-ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
-ciphertext2 = cryptoContext.Encrypt(keypair.publicKey, plaintext2)
-ciphertext3 = cryptoContext.Encrypt(keypair.publicKey, plaintext3)
-print("The plaintexts have been encrypted.")
-
-if not SerializeToFile(datafolder + "/ciphertext1.txt", ciphertext1, serType):
-   raise Exception("Error writing serialization of ciphertext 1 to ciphertext1.txt")
-print("The first ciphertext has been serialized.")
 
-if not SerializeToFile(datafolder + "/ciphertext2.txt", ciphertext2, serType):
-   raise Exception("Error writing serialization of ciphertext2 to ciphertext2.txt")
-print("The second ciphertext has been serialized.")
-
-if not SerializeToFile(datafolder + "/ciphertext3.txt", ciphertext3, serType):   
-   raise Exception("Error writing serialization of ciphertext3 to ciphertext3.txt")
-print("The third ciphertext has been serialized.")
-
-# Sample Program: Step 4: Evaluation
-
-# OpenFHE maintains an internal map of CryptoContext objects which are
-# indexed by a tag and the tag is applied to both the CryptoContext and some
-# of the keys. When deserializing a context, OpenFHE checks for the tag and
-# if it finds it in the CryptoContext map, it will return the stored version.
-# Hence, we need to clear the context and clear the keys.
-cryptoContext.ClearEvalMultKeys()
-cryptoContext.ClearEvalAutomorphismKeys()
-ReleaseAllContexts()
-
-# Deserialize the crypto context
-
-cc, res = DeserializeCryptoContext(datafolder + "/cryptocontext.txt", serType)
-if not res:
-   raise Exception("Error reading serialization of the crypto context from cryptocontext.txt")
-print("The cryptocontext has been deserialized.")
-
-# Deserialize the public key
-pk, res = DeserializePublicKey(datafolder + "/key-public.txt", serType)
-
-if not res:
-   raise Exception("Error reading serialization of the public key from key-public.txt")
-print("The public key has been deserialized.")
-
-if not cc.DeserializeEvalMultKey(datafolder + "/key-eval-mult.txt",serType):
-   raise Exception("Could not deserialize the eval mult key file")
-
-print("The relinearization key has been deserialized.")
-
-if not cc.DeserializeEvalAutomorphismKey(datafolder + "/key-eval-rot.txt",serType):
-   raise Exception("Could not deserialize the eval rotation key file")
-
-print("Deserialized the eval rotation keys.")
-
-# Deserialize the ciphertexts
-ct1, res =  DeserializeCiphertext(datafolder + "/ciphertext1.txt", serType)
-
-if not res:
-    raise Exception("Could not read the ciphertext")
-print("The first ciphertext has been deserialized.")
-
-ct2, res = DeserializeCiphertext(datafolder + "/ciphertext2.txt", serType)
-
-if not res:
-    raise Exception("Could not read the ciphertext")
-print("The second ciphertext has been deserialized.")
-
-ct3, res = DeserializeCiphertext(datafolder + "/ciphertext3.txt", serType)
-if not res:   
-    raise Exception("Could not read the ciphertext")
-print("The third ciphertext has been deserialized.")
-
-# Homomorphic addition
-
-ciphertextAdd12 = cc.EvalAdd(ct1, ct2)
-ciphertextAddResult = cc.EvalAdd(ciphertextAdd12, ct3)
+# import openfhe.PKESchemeFeature as Feature
+import tempfile
+import os
+
+datafolder = "demoData"
+
+
+def main_action():
+    serType = BINARY  # BINARY or JSON
+    print(
+        "This program requres the subdirectory `"
+        + datafolder
+        + "' to exist, otherwise you will get an error writing serializations."
+    )
+
+    # Sample Program: Step 1: Set CryptoContext
+    parameters = CCParamsBFVRNS()
+    parameters.SetPlaintextModulus(65537)
+    parameters.SetMultiplicativeDepth(2)
+
+    cryptoContext = GenCryptoContext(parameters)
+    # Enable features that you wish to use
+    cryptoContext.Enable(PKESchemeFeature.PKE)
+    cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
+    cryptoContext.Enable(PKESchemeFeature.LEVELEDSHE)
+
+    # Serialize cryptocontext
+    if not SerializeToFile(datafolder + "/cryptocontext.txt", cryptoContext, serType):
+        raise Exception(
+            "Error writing serialization of the crypto context to cryptocontext.txt"
+        )
+    print("The cryptocontext has been serialized.")
+
+    # Sample Program: Step 2: Key Generation
+
+    # Generate a public/private key pair
+    keypair = cryptoContext.KeyGen()
+    print("The keypair has been generated.")
+
+    # Serialize the public key
+    if not SerializeToFile(datafolder + "/key-public.txt", keypair.publicKey, serType):
+        raise Exception(
+            "Error writing serialization of the public key to key-public.txt"
+        )
+    print("The public key has been serialized.")
+
+    # Serialize the secret key
+    if not SerializeToFile(datafolder + "/key-private.txt", keypair.secretKey, serType):
+        raise Exception(
+            "Error writing serialization of the secret key to key-private.txt"
+        )
+    print("The secret key has been serialized.")
+
+    # Generate the relinearization key
+    cryptoContext.EvalMultKeyGen(keypair.secretKey)
+    print("The relinearization key has been generated.")
+
+    # Serialize the relinearization key
+    if not cryptoContext.SerializeEvalMultKey(
+        datafolder + "/key-eval-mult.txt", serType
+    ):
+        raise Exception(
+            'Error writing serialization of the eval mult keys to "key-eval-mult.txt"'
+        )
+    print("The relinearization key has been serialized.")
+
+    # Generate the rotation evaluation keys
+    cryptoContext.EvalRotateKeyGen(keypair.secretKey, [1, 2, -1, -2])
+    print("The rotation evaluation keys have been generated.")
+
+    # Serialize the rotation evaluation keys
+    if not cryptoContext.SerializeEvalAutomorphismKey(
+        datafolder + "/key-eval-rot.txt", serType
+    ):
+        raise Exception(
+            'Error writing serialization of the eval rotate keys to "key-eval-rot.txt"'
+        )
+    print("The rotation evaluation keys have been serialized.")
+
+    # Sample Program: Step 3: Encryption
+
+    # First plaintext vector is encoded
+    vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
+
+    # Second plaintext vector is encoded
+    vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
+
+    # Third plaintext vector is encoded
+    vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
+
+    # The encoded vectors are encrypted
+    ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
+    ciphertext2 = cryptoContext.Encrypt(keypair.publicKey, plaintext2)
+    ciphertext3 = cryptoContext.Encrypt(keypair.publicKey, plaintext3)
+    print("The plaintexts have been encrypted.")
+
+    if not SerializeToFile(datafolder + "/ciphertext1.txt", ciphertext1, serType):
+        raise Exception(
+            "Error writing serialization of ciphertext 1 to ciphertext1.txt"
+        )
+    print("The first ciphertext has been serialized.")
+
+    if not SerializeToFile(datafolder + "/ciphertext2.txt", ciphertext2, serType):
+        raise Exception("Error writing serialization of ciphertext2 to ciphertext2.txt")
+    print("The second ciphertext has been serialized.")
+
+    if not SerializeToFile(datafolder + "/ciphertext3.txt", ciphertext3, serType):
+        raise Exception("Error writing serialization of ciphertext3 to ciphertext3.txt")
+    print("The third ciphertext has been serialized.")
+
+    # Sample Program: Step 4: Evaluation
+
+    # OpenFHE maintains an internal map of CryptoContext objects which are
+    # indexed by a tag and the tag is applied to both the CryptoContext and some
+    # of the keys. When deserializing a context, OpenFHE checks for the tag and
+    # if it finds it in the CryptoContext map, it will return the stored version.
+    # Hence, we need to clear the context and clear the keys.
+    cryptoContext.ClearEvalMultKeys()
+    cryptoContext.ClearEvalAutomorphismKeys()
+    ReleaseAllContexts()
+
+    # Deserialize the crypto context
+
+    cc, res = DeserializeCryptoContext(datafolder + "/cryptocontext.txt", serType)
+    if not res:
+        raise Exception(
+            "Error reading serialization of the crypto context from cryptocontext.txt"
+        )
+    print("The cryptocontext has been deserialized.")
+
+    # Deserialize the public key
+    pk, res = DeserializePublicKey(datafolder + "/key-public.txt", serType)
+
+    if not res:
+        raise Exception(
+            "Error reading serialization of the public key from key-public.txt"
+        )
+    print("The public key has been deserialized.")
+
+    if not cc.DeserializeEvalMultKey(datafolder + "/key-eval-mult.txt", serType):
+        raise Exception("Could not deserialize the eval mult key file")
+
+    print("The relinearization key has been deserialized.")
+
+    if not cc.DeserializeEvalAutomorphismKey(datafolder + "/key-eval-rot.txt", serType):
+        raise Exception("Could not deserialize the eval rotation key file")
+
+    print("Deserialized the eval rotation keys.")
+
+    # Deserialize the ciphertexts
+    ct1, res = DeserializeCiphertext(datafolder + "/ciphertext1.txt", serType)
+
+    if not res:
+        raise Exception("Could not read the ciphertext")
+    print("The first ciphertext has been deserialized.")
+
+    ct2, res = DeserializeCiphertext(datafolder + "/ciphertext2.txt", serType)
+
+    if not res:
+        raise Exception("Could not read the ciphertext")
+    print("The second ciphertext has been deserialized.")
+
+    ct3, res = DeserializeCiphertext(datafolder + "/ciphertext3.txt", serType)
+    if not res:
+        raise Exception("Could not read the ciphertext")
+    print("The third ciphertext has been deserialized.")
+
+    # Homomorphic addition
+
+    ciphertextAdd12 = cc.EvalAdd(ct1, ct2)
+    ciphertextAddResult = cc.EvalAdd(ciphertextAdd12, ct3)
+
+    # Homomorphic multiplication
+    ciphertextMult12 = cc.EvalMult(ct1, ct2)
+    ciphertextMultResult = cc.EvalMult(ciphertextMult12, ct3)
 
-# Homomorphic multiplication
-ciphertextMult12 = cc.EvalMult(ct1, ct2)
-ciphertextMultResult = cc.EvalMult(ciphertextMult12, ct3)
+    # Homomorphic rotation
+    ciphertextRot1 = cc.EvalRotate(ct1, 1)
+    ciphertextRot2 = cc.EvalRotate(ct2, 2)
+    ciphertextRot3 = cc.EvalRotate(ct3, -1)
+    ciphertextRot4 = cc.EvalRotate(ct3, -2)
 
-# Homomorphic rotation
-ciphertextRot1 = cc.EvalRotate(ct1, 1)
-ciphertextRot2 = cc.EvalRotate(ct2, 2)
-ciphertextRot3 = cc.EvalRotate(ct3, -1)
-ciphertextRot4 = cc.EvalRotate(ct3, -2)
+    # Sample Program: Step 5: Decryption
+
+    sk, res = DeserializePrivateKey(datafolder + "/key-private.txt", serType)
+    if not res:
+        raise Exception("Could not read secret key")
+    print("The secret key has been deserialized.")
+
+    # Decrypt the result of additions
+    plaintextAddResult = cc.Decrypt(sk, ciphertextAddResult)
+
+    # Decrypt the result of multiplications
+    plaintextMultResult = cc.Decrypt(sk, ciphertextMultResult)
 
-# Sample Program: Step 5: Decryption
+    # Decrypt the result of rotations
+    plaintextRot1 = cc.Decrypt(sk, ciphertextRot1)
+    plaintextRot2 = cc.Decrypt(sk, ciphertextRot2)
+    plaintextRot3 = cc.Decrypt(sk, ciphertextRot3)
+    plaintextRot4 = cc.Decrypt(sk, ciphertextRot4)
 
-sk, res = DeserializePrivateKey(datafolder + "/key-private.txt", serType)
-if not res:
-      raise Exception("Could not read secret key")
-print("The secret key has been deserialized.")
+    # Shows only the same number of elements as in the original plaintext vector
+    # By default it will show all coefficients in the BFV-encoded polynomial
+    plaintextRot1.SetLength(len(vectorOfInts1))
+    plaintextRot2.SetLength(len(vectorOfInts1))
+    plaintextRot3.SetLength(len(vectorOfInts1))
+    plaintextRot4.SetLength(len(vectorOfInts1))
 
-# Decrypt the result of additions
-plaintextAddResult = cc.Decrypt(sk, ciphertextAddResult)
+    # Output results
+    print("\nResults of homomorphic computations")
+    print("#1 + #2 + #3: " + str(plaintextAddResult))
+    print("#1 * #2 * #3: " + str(plaintextMultResult))
+    print("Left rotation of #1 by 1: " + str(plaintextRot1))
+    print("Left rotation of #1 by 2: " + str(plaintextRot2))
+    print("Right rotation of #1 by 1: " + str(plaintextRot3))
+    print("Right rotation of #1 by 2: " + str(plaintextRot4))
 
-# Decrypt the result of multiplications
-plaintextMultResult = cc.Decrypt(sk, ciphertextMultResult)
 
-# Decrypt the result of rotations
-plaintextRot1 = cc.Decrypt(sk, ciphertextRot1)
-plaintextRot2 = cc.Decrypt(sk, ciphertextRot2)
-plaintextRot3 = cc.Decrypt(sk, ciphertextRot3)
-plaintextRot4 = cc.Decrypt(sk, ciphertextRot4)
+def main():
+    global datafolder
+    with tempfile.TemporaryDirectory() as td:
+        datafolder = td + "/" + datafolder
+        os.mkdir(datafolder)
+        main_action()
 
-# Shows only the same number of elements as in the original plaintext vector
-# By default it will show all coefficients in the BFV-encoded polynomial
-plaintextRot1.SetLength(len(vectorOfInts1))
-plaintextRot2.SetLength(len(vectorOfInts1))
-plaintextRot3.SetLength(len(vectorOfInts1))
-plaintextRot4.SetLength(len(vectorOfInts1))
 
-# Output results
-print("\nResults of homomorphic computations")
-print("#1 + #2 + #3: " + str(plaintextAddResult))
-print("#1 * #2 * #3: " + str(plaintextMultResult))
-print("Left rotation of #1 by 1: " + str(plaintextRot1))
-print("Left rotation of #1 by 2: " + str(plaintextRot2))
-print("Right rotation of #1 by 1: " + str(plaintextRot3))
-print("Right rotation of #1 by 2: " + str(plaintextRot4))
+if __name__ == "__main__":
+    main()

+ 76 - 67
examples/pke/simple-integers.py

@@ -1,94 +1,103 @@
 # Initial Settings
 from openfhe import *
+
 # import openfhe.PKESchemeFeature as Feature
 
 
-# Sample Program: Step 1: Set CryptoContext
-parameters = CCParamsBFVRNS()
-parameters.SetPlaintextModulus(65537)
-parameters.SetMultiplicativeDepth(2)
+def main():
+    # Sample Program: Step 1: Set CryptoContext
+    parameters = CCParamsBFVRNS()
+    parameters.SetPlaintextModulus(65537)
+    parameters.SetMultiplicativeDepth(2)
+
+    crypto_context = GenCryptoContext(parameters)
+    # Enable features that you wish to use
+    crypto_context.Enable(PKESchemeFeature.PKE)
+    crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
+    crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
 
-crypto_context = GenCryptoContext(parameters)
-# Enable features that you wish to use
-crypto_context.Enable(PKESchemeFeature.PKE)
-crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
-crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
+    # Sample Program: Step 2: Key Generation
 
-# Sample Program: Step 2: Key Generation
+    # Generate a public/private key pair
+    key_pair = crypto_context.KeyGen()
 
-# Generate a public/private key pair
-key_pair = crypto_context.KeyGen()
+    # Generate the relinearization key
+    crypto_context.EvalMultKeyGen(key_pair.secretKey)
 
-# Generate the relinearization key
-crypto_context.EvalMultKeyGen(key_pair.secretKey)
+    # Generate the rotation evaluation keys
+    crypto_context.EvalRotateKeyGen(key_pair.secretKey, [1, 2, -1, -2])
 
-# Generate the rotation evaluation keys
-crypto_context.EvalRotateKeyGen(key_pair.secretKey, [1, 2, -1, -2])
+    # Sample Program: Step 3: Encryption
 
-# Sample Program: Step 3: Encryption
+    # First plaintext vector is encoded
+    vector_of_ints1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext1 = crypto_context.MakePackedPlaintext(vector_of_ints1)
 
-# First plaintext vector is encoded
-vector_of_ints1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext1 = crypto_context.MakePackedPlaintext(vector_of_ints1)
+    # Second plaintext vector is encoded
+    vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)
 
-# Second plaintext vector is encoded
-vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)
+    # Third plaintext vector is encoded
+    vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
+    plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)
 
-# Third plaintext vector is encoded
-vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
-plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)
+    # The encoded vectors are encrypted
+    ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
+    ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
+    ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)
 
-# The encoded vectors are encrypted
-ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
-ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
-ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)
+    #  Sample Program: Step 4: Evaluation
 
-#  Sample Program: Step 4: Evaluation
+    # Homomorphic additions
+    ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
+    ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)
 
-# Homomorphic additions
-ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
-ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)
+    # Homomorphic Multiplication
+    ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
+    ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)
 
-# Homomorphic Multiplication
-ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
-ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)
+    # Homomorphic Rotations
+    ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
+    ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
+    ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
+    ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)
 
-# Homomorphic Rotations
-ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
-ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
-ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
-ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)
+    # Sample Program: Step 5: Decryption
 
-# Sample Program: Step 5: Decryption
+    # Decrypt the result of additions
+    plaintext_add_result = crypto_context.Decrypt(
+        ciphertext_add_result, key_pair.secretKey
+    )
 
-# Decrypt the result of additions
-plaintext_add_result = crypto_context.Decrypt(ciphertext_add_result,key_pair.secretKey)
+    # Decrypt the result of multiplications
+    plaintext_mult_result = crypto_context.Decrypt(
+        ciphertext_mult_result, key_pair.secretKey
+    )
 
-# Decrypt the result of multiplications
-plaintext_mult_result = crypto_context.Decrypt(ciphertext_mult_result,key_pair.secretKey)
+    # Decrypt the result of rotations
+    plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1, key_pair.secretKey)
+    plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2, key_pair.secretKey)
+    plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3, key_pair.secretKey)
+    plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4, key_pair.secretKey)
 
-# Decrypt the result of rotations
-plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1,key_pair.secretKey)
-plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2,key_pair.secretKey)
-plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3,key_pair.secretKey)
-plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4,key_pair.secretKey)
+    plaintextRot1.SetLength(len(vector_of_ints1))
+    plaintextRot2.SetLength(len(vector_of_ints1))
+    plaintextRot3.SetLength(len(vector_of_ints1))
+    plaintextRot4.SetLength(len(vector_of_ints1))
 
+    print("Plaintext #1: " + str(plaintext1))
+    print("Plaintext #2: " + str(plaintext2))
+    print("Plaintext #3: " + str(plaintext3))
 
-plaintextRot1.SetLength(len(vector_of_ints1))
-plaintextRot2.SetLength(len(vector_of_ints1))
-plaintextRot3.SetLength(len(vector_of_ints1))
-plaintextRot4.SetLength(len(vector_of_ints1))
+    # Output Results
+    print("\nResults of homomorphic computations")
+    print("#1 + #2 + #3 = " + str(plaintext_add_result))
+    print("#1 * #2 * #3 = " + str(plaintext_mult_result))
+    print("Left rotation of #1 by 1 = " + str(plaintextRot1))
+    print("Left rotation of #1 by 2 = " + str(plaintextRot2))
+    print("Right rotation of #1 by 1 = " + str(plaintextRot3))
+    print("Right rotation of #1 by 2 = " + str(plaintextRot4))
 
-print("Plaintext #1: " + str(plaintext1))
-print("Plaintext #2: " + str(plaintext2))
-print("Plaintext #3: " + str(plaintext3))
 
-# Output Results
-print("\nResults of homomorphic computations")
-print("#1 + #2 + #3 = " + str(plaintext_add_result))
-print("#1 * #2 * #3 = " + str(plaintext_mult_result))
-print("Left rotation of #1 by 1 = " + str(plaintextRot1))
-print("Left rotation of #1 by 2 = " + str(plaintextRot2))
-print("Right rotation of #1 by 1 = " + str(plaintextRot3))
-print("Right rotation of #1 by 2 = " + str(plaintextRot4))
+if __name__ == "__main__":
+    main()

+ 114 - 62
examples/pke/simple-real-numbers-serial.py

@@ -1,27 +1,30 @@
 from openfhe import *
-
+import os
+from pathlib import Path
+import tempfile
 
 # NOTE:
 # If running locally, you may want to replace the "hardcoded" datafolder with
 # the datafolder location below which gets the current working directory
 
 # Save-Load locations for keys
-datafolder = 'demoData'
-ccLocation = '/cryptocontext.txt'
-pubKeyLocation = '/key_pub.txt' # Pub key
-multKeyLocation = '/key_mult.txt' # relinearization key
-rotKeyLocation = '/key_rot.txt' # automorphism / rotation key
+datafolder = "demoData"
+ccLocation = "/cryptocontext.txt"
+pubKeyLocation = "/key_pub.txt"  # Pub key
+multKeyLocation = "/key_mult.txt"  # relinearization key
+rotKeyLocation = "/key_rot.txt"  # automorphism / rotation key
 
 # Save-load locations for RAW ciphertexts
-cipherOneLocation = '/ciphertext1.txt'
-cipherTwoLocation = '/ciphertext2.txt'
+cipherOneLocation = "/ciphertext1.txt"
+cipherTwoLocation = "/ciphertext2.txt"
 
 # Save-load locations for evaluated ciphertexts
-cipherMultLocation = '/ciphertextMult.txt'
-cipherAddLocation = '/ciphertextAdd.txt'
-cipherRotLocation = '/ciphertextRot.txt'
-cipherRotNegLocation = '/ciphertextRotNegLocation.txt'
-clientVectorLocation = '/clientVectorFromClient.txt'
+cipherMultLocation = "/ciphertextMult.txt"
+cipherAddLocation = "/ciphertextAdd.txt"
+cipherRotLocation = "/ciphertextRot.txt"
+cipherRotNegLocation = "/ciphertextRotNegLocation.txt"
+clientVectorLocation = "/clientVectorFromClient.txt"
+
 
 # Demarcate - Visual separator between the sections of code
 def demarcate(msg):
@@ -29,6 +32,7 @@ def demarcate(msg):
     print(msg)
     print("**************************************************\n")
 
+
 """
 serverSetupAndWrite(multDepth, scaleModSize, batchSize)
     simulates a server at startup where we generate a cryptocontext and keys.
@@ -40,6 +44,8 @@ serverSetupAndWrite(multDepth, scaleModSize, batchSize)
     :param batchSize: batch size to use
     :return Tuple<cryptoContext, keyPair>
 """
+
+
 def serverSetupAndWrite(multDepth, scaleModSize, batchSize):
 
     parameters = CCParamsCKKSRNS()
@@ -75,7 +81,7 @@ def serverSetupAndWrite(multDepth, scaleModSize, batchSize):
     serverP2 = serverCC.MakeCKKSPackedPlaintext(vec2)
     serverP3 = serverCC.MakeCKKSPackedPlaintext(vec3)
 
-    print("Plaintext version of first vector: "+ str(serverP1))
+    print("Plaintext version of first vector: " + str(serverP1))
 
     print("Plaintexts have been generated from complex-double vectors")
 
@@ -93,7 +99,7 @@ def serverSetupAndWrite(multDepth, scaleModSize, batchSize):
     #      relinearization (eval mult keys)
     #      rotation keys
     #      Some of the ciphertext
-    #    
+    #
     #      We serialize all of them to files
     ###
     demarcate("Part 2: Data Serialization (server)")
@@ -116,22 +122,24 @@ def serverSetupAndWrite(multDepth, scaleModSize, batchSize):
 
     if not SerializeToFile(datafolder + cipherOneLocation, serverC1, BINARY):
         raise Exception("Error writing ciphertext 1")
-    
+
     if not SerializeToFile(datafolder + cipherTwoLocation, serverC2, BINARY):
         raise Exception("Error writing ciphertext 2")
-    
+
     return (serverCC, serverKP, len(vec1))
 
+
 ###
- # clientProcess
- #  - deserialize data from a file which simulates receiving data from a server
- # after making a request
- #  - we then process the data by doing operations (multiplication, addition,
- # rotation, etc)
- #  - !! We also create an object and encrypt it in this function before sending
- # it off to the server to be decrypted
+# clientProcess
+#  - deserialize data from a file which simulates receiving data from a server
+# after making a request
+#  - we then process the data by doing operations (multiplication, addition,
+# rotation, etc)
+#  - !! We also create an object and encrypt it in this function before sending
+# it off to the server to be decrypted
 ###
 
+
 def clientProcess():
     # clientCC = CryptoContext()
     # clientCC.ClearEvalMultKeys()
@@ -140,33 +148,45 @@ def clientProcess():
 
     clientCC, res = DeserializeCryptoContext(datafolder + ccLocation, BINARY)
     if not res:
-        raise Exception(f"I cannot deserialize the cryptocontext from {datafolder+ccLocation}")
+        raise Exception(
+            f"I cannot deserialize the cryptocontext from {datafolder+ccLocation}"
+        )
 
     print("Client CC deserialized")
 
-    #clientKP = KeyPair()
+    # clientKP = KeyPair()
     # We do NOT have a secret key. The client
     # should not have access to this
     clientPuclicKey, res = DeserializePublicKey(datafolder + pubKeyLocation, BINARY)
     if not res:
-        raise Exception(f"I cannot deserialize the public key from {datafolder+pubKeyLocation}")
+        raise Exception(
+            f"I cannot deserialize the public key from {datafolder+pubKeyLocation}"
+        )
     print("Client KP deserialized\n")
 
     if not clientCC.DeserializeEvalMultKey(datafolder + multKeyLocation, BINARY):
-        raise Exception(f"Cannot deserialize eval mult keys from {datafolder+multKeyLocation}")
+        raise Exception(
+            f"Cannot deserialize eval mult keys from {datafolder+multKeyLocation}"
+        )
     print("Deserialized eval mult keys\n")
 
     if not clientCC.DeserializeEvalAutomorphismKey(datafolder + rotKeyLocation, BINARY):
-        raise Exception(f"Cannot deserialize eval automorphism keys from {datafolder+rotKeyLocation}")
-    
+        raise Exception(
+            f"Cannot deserialize eval automorphism keys from {datafolder+rotKeyLocation}"
+        )
+
     clientC1, res = DeserializeCiphertext(datafolder + cipherOneLocation, BINARY)
     if not res:
-        raise Exception(f"Cannot deserialize the ciphertext from {datafolder+cipherOneLocation}")
+        raise Exception(
+            f"Cannot deserialize the ciphertext from {datafolder+cipherOneLocation}"
+        )
     print("Deserialized ciphertext 1\n")
 
     clientC2, res = DeserializeCiphertext(datafolder + cipherTwoLocation, BINARY)
     if not res:
-        raise Exception(f"Cannot deserialize the ciphertext from {datafolder+cipherTwoLocation}")
+        raise Exception(
+            f"Cannot deserialize the ciphertext from {datafolder+cipherTwoLocation}"
+        )
     print("Deserialized ciphertext 2\n")
 
     clientCiphertextMult = clientCC.EvalMult(clientC1, clientC2)
@@ -185,10 +205,13 @@ def clientProcess():
     SerializeToFile(datafolder + cipherAddLocation, clientCiphertextAdd, BINARY)
     SerializeToFile(datafolder + cipherRotLocation, clientCiphertextRot, BINARY)
     SerializeToFile(datafolder + cipherRotNegLocation, clientCiphertextRotNeg, BINARY)
-    SerializeToFile(datafolder + clientVectorLocation, clientInitializedEncryption, BINARY)
+    SerializeToFile(
+        datafolder + clientVectorLocation, clientInitializedEncryption, BINARY
+    )
 
     print("Serialized all ciphertexts from client\n")
 
+
 ###
 #  serverVerification
 #  - deserialize data from the client.
@@ -199,22 +222,42 @@ def clientProcess():
 # @return
 #  5-tuple of the plaintexts of various operations
 ##
-def serverVerification(cc,kp,vectorSize):
-    
-    serverCiphertextFromClient_Mult, res = DeserializeCiphertext(datafolder + cipherMultLocation, BINARY)
-    serverCiphertextFromClient_Add, res = DeserializeCiphertext(datafolder + cipherAddLocation, BINARY)
-    serverCiphertextFromClient_Rot, res = DeserializeCiphertext(datafolder + cipherRotLocation, BINARY)
-    serverCiphertextFromClient_RotNeg, res = DeserializeCiphertext(datafolder + cipherRotNegLocation, BINARY)
-    serverCiphertextFromClient_Vec, res = DeserializeCiphertext(datafolder + clientVectorLocation, BINARY)
+def serverVerification(cc, kp, vectorSize):
+
+    serverCiphertextFromClient_Mult, res = DeserializeCiphertext(
+        datafolder + cipherMultLocation, BINARY
+    )
+    serverCiphertextFromClient_Add, res = DeserializeCiphertext(
+        datafolder + cipherAddLocation, BINARY
+    )
+    serverCiphertextFromClient_Rot, res = DeserializeCiphertext(
+        datafolder + cipherRotLocation, BINARY
+    )
+    serverCiphertextFromClient_RotNeg, res = DeserializeCiphertext(
+        datafolder + cipherRotNegLocation, BINARY
+    )
+    serverCiphertextFromClient_Vec, res = DeserializeCiphertext(
+        datafolder + clientVectorLocation, BINARY
+    )
     print("Deserialized all data from client on server\n")
 
     print("Part 5: Correctness Verification")
 
-    serverPlaintextFromClient_Mult = cc.Decrypt(kp.secretKey, serverCiphertextFromClient_Mult)
-    serverPlaintextFromClient_Add = cc.Decrypt(kp.secretKey, serverCiphertextFromClient_Add)
-    serverPlaintextFromClient_Rot = cc.Decrypt(kp.secretKey, serverCiphertextFromClient_Rot)
-    serverPlaintextFromClient_RotNeg = cc.Decrypt(kp.secretKey, serverCiphertextFromClient_RotNeg)
-    serverPlaintextFromClient_Vec = cc.Decrypt(kp.secretKey, serverCiphertextFromClient_Vec)
+    serverPlaintextFromClient_Mult = cc.Decrypt(
+        kp.secretKey, serverCiphertextFromClient_Mult
+    )
+    serverPlaintextFromClient_Add = cc.Decrypt(
+        kp.secretKey, serverCiphertextFromClient_Add
+    )
+    serverPlaintextFromClient_Rot = cc.Decrypt(
+        kp.secretKey, serverCiphertextFromClient_Rot
+    )
+    serverPlaintextFromClient_RotNeg = cc.Decrypt(
+        kp.secretKey, serverCiphertextFromClient_RotNeg
+    )
+    serverPlaintextFromClient_Vec = cc.Decrypt(
+        kp.secretKey, serverCiphertextFromClient_Vec
+    )
 
     serverPlaintextFromClient_Mult.SetLength(vectorSize)
     serverPlaintextFromClient_Add.SetLength(vectorSize)
@@ -222,14 +265,27 @@ def serverVerification(cc,kp,vectorSize):
     serverPlaintextFromClient_Rot.SetLength(vectorSize + 1)
     serverPlaintextFromClient_RotNeg.SetLength(vectorSize + 1)
 
-    return (serverPlaintextFromClient_Mult, 
-            serverPlaintextFromClient_Add, 
-            serverPlaintextFromClient_Vec, 
-            serverPlaintextFromClient_Rot, 
-            serverPlaintextFromClient_RotNeg)
+    return (
+        serverPlaintextFromClient_Mult,
+        serverPlaintextFromClient_Add,
+        serverPlaintextFromClient_Vec,
+        serverPlaintextFromClient_Rot,
+        serverPlaintextFromClient_RotNeg,
+    )
+
 
 def main():
-    print(f"This program requires the subdirectory `{datafolder}' to exist, otherwise you will get\n an error writing serializations.")
+    global datafolder
+    with tempfile.TemporaryDirectory() as td:
+        datafolder = td + "/" + datafolder
+        os.makedirs(datafolder)
+        main_action()
+
+
+def main_action():
+    print(
+        f"This program requires the subdirectory `{datafolder}' to exist, otherwise you will get\n an error writing serializations."
+    )
 
     # Set main params
     multDepth = 5
@@ -246,7 +302,9 @@ def main():
     cipherRotResIdx = 3
     cipherRotNegResIdx = 4
 
-    demarcate("Part 1: Cryptocontext generation, key generation, data encryption \n(server)")
+    demarcate(
+        "Part 1: Cryptocontext generation, key generation, data encryption \n(server)"
+    )
 
     tupleCryptoContext_KeyPair = serverSetupAndWrite(multDepth, scaleModSize, batchSize)
     cc = tupleCryptoContext_KeyPair[cryptoContextIdx]
@@ -269,19 +327,13 @@ def main():
     # vec2: [12.5, 13.5, 14.5, 15.5]
 
     print(multRes)  # EXPECT: 12.5, 27.0, 43.5, 62
-    print(addRes)   # EXPECT: 13.5, 15.5, 17.5, 19.5
-    print(vecRes)   # EXPECT:  [1,2,3,4]
+    print(addRes)  # EXPECT: 13.5, 15.5, 17.5, 19.5
+    print(vecRes)  # EXPECT:  [1,2,3,4]
 
     print("Displaying 5 elements of a 4-element vector to illustrate rotation")
-    print(rotRes)     # EXPECT: [2, 3, 4, noise, noise]
+    print(rotRes)  # EXPECT: [2, 3, 4, noise, noise]
     print(rotNegRes)  # EXPECT: [noise, 1, 2, 3, 4]
 
+
 if __name__ == "__main__":
     main()
-
-
-
-
-
-
-

+ 85 - 86
examples/pke/simple-real-numbers.py

@@ -1,89 +1,88 @@
 from openfhe import *
 
-mult_depth = 1
-scale_mod_size = 50
-batch_size = 8
-
-parameters = CCParamsCKKSRNS()
-parameters.SetMultiplicativeDepth(mult_depth)
-parameters.SetScalingModSize(scale_mod_size)
-parameters.SetBatchSize(batch_size)
-
-cc = GenCryptoContext(parameters)
-cc.Enable(PKESchemeFeature.PKE)
-cc.Enable(PKESchemeFeature.KEYSWITCH)
-cc.Enable(PKESchemeFeature.LEVELEDSHE)
-
-print("The CKKS scheme is using ring dimension: " + str(cc.GetRingDimension()))
-
-keys = cc.KeyGen()
-cc.EvalMultKeyGen(keys.secretKey)
-cc.EvalRotateKeyGen(keys.secretKey, [1, -2])
-
-x1 = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
-x2 = [5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25]
-
-ptx1 = cc.MakeCKKSPackedPlaintext(x1)
-ptx2 = cc.MakeCKKSPackedPlaintext(x2)
-
-print("Input x1: " + str(ptx1))
-print("Input x2: " + str(ptx2))
-
-# Encrypt the encoded vectors
-c1 = cc.Encrypt(keys.publicKey, ptx1)
-c2 = cc.Encrypt(keys.publicKey, ptx2)
-
-# Step 4: Evaluation
-# Homomorphic additions
-c_add = cc.EvalAdd(c1, c2)
-# Homomorphic subtraction
-c_sub = cc.EvalSub(c1, c2)
-# Homomorphic scalar multiplication
-c_scalar = cc.EvalMult(c1,4)
-# Homomorphic multiplication
-c_mult = cc.EvalMult(c1, c2)
-# Homomorphic rotations
-c_rot1 = cc.EvalRotate(c1, 1)
-c_rot2 = cc.EvalRotate(c1, -2)
-
-# Step 5: Decryption and output
-# Decrypt the result of additions
-ptAdd = cc.Decrypt(c_add,keys.secretKey)
-
-# We set the precision to 8 decimal digits for a nicer output.
-# If you want to see the error/noise introduced by CKKS, bump it up
-# to 15 and it should become visible.
-
-precision = 8
-print("Results of homomorphic computations:")
-result = cc.Decrypt(c1, keys.secretKey)
-result.SetLength(batch_size)
-print("x1 = " + str(result))
-print("Estimated precision in bits: " + str(result.GetLogPrecision()))
-
-# Decrypt the result of scalar multiplication
-result = cc.Decrypt(c_scalar,keys.secretKey)
-result.SetLength(batch_size)
-print("4 * x1 = " + str(result))
-
-# Decrypt the result of multiplication
-result = cc.Decrypt(c_mult,keys.secretKey)
-result.SetLength(batch_size)
-print("x1 * x2 = " + str(result))
-
-# Decrypt the result of rotations
-result = cc.Decrypt(c_rot1,keys.secretKey)
-result.SetLength(batch_size)
-print("In rotations, very small outputs (~10^-10 here) correspond to 0's:")
-print("x1 rotated by 1 = " + str(result))
-
-result = cc.Decrypt(c_rot2,keys.secretKey)
-result.SetLength(batch_size)
-print("x1 rotated by -2 = " + str(result))
-
-
-
-
-
-
 
+def main():
+    mult_depth = 1
+    scale_mod_size = 50
+    batch_size = 8
+
+    parameters = CCParamsCKKSRNS()
+    parameters.SetMultiplicativeDepth(mult_depth)
+    parameters.SetScalingModSize(scale_mod_size)
+    parameters.SetBatchSize(batch_size)
+
+    cc = GenCryptoContext(parameters)
+    cc.Enable(PKESchemeFeature.PKE)
+    cc.Enable(PKESchemeFeature.KEYSWITCH)
+    cc.Enable(PKESchemeFeature.LEVELEDSHE)
+
+    print("The CKKS scheme is using ring dimension: " + str(cc.GetRingDimension()))
+
+    keys = cc.KeyGen()
+    cc.EvalMultKeyGen(keys.secretKey)
+    cc.EvalRotateKeyGen(keys.secretKey, [1, -2])
+
+    x1 = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
+    x2 = [5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25]
+
+    ptx1 = cc.MakeCKKSPackedPlaintext(x1)
+    ptx2 = cc.MakeCKKSPackedPlaintext(x2)
+
+    print("Input x1: " + str(ptx1))
+    print("Input x2: " + str(ptx2))
+
+    # Encrypt the encoded vectors
+    c1 = cc.Encrypt(keys.publicKey, ptx1)
+    c2 = cc.Encrypt(keys.publicKey, ptx2)
+
+    # Step 4: Evaluation
+    # Homomorphic additions
+    c_add = cc.EvalAdd(c1, c2)
+    # Homomorphic subtraction
+    c_sub = cc.EvalSub(c1, c2)
+    # Homomorphic scalar multiplication
+    c_scalar = cc.EvalMult(c1, 4)
+    # Homomorphic multiplication
+    c_mult = cc.EvalMult(c1, c2)
+    # Homomorphic rotations
+    c_rot1 = cc.EvalRotate(c1, 1)
+    c_rot2 = cc.EvalRotate(c1, -2)
+
+    # Step 5: Decryption and output
+    # Decrypt the result of additions
+    ptAdd = cc.Decrypt(c_add, keys.secretKey)
+
+    # We set the precision to 8 decimal digits for a nicer output.
+    # If you want to see the error/noise introduced by CKKS, bump it up
+    # to 15 and it should become visible.
+
+    precision = 8
+    print("Results of homomorphic computations:")
+    result = cc.Decrypt(c1, keys.secretKey)
+    result.SetLength(batch_size)
+    print("x1 = " + str(result))
+    print("Estimated precision in bits: " + str(result.GetLogPrecision()))
+
+    # Decrypt the result of scalar multiplication
+    result = cc.Decrypt(c_scalar, keys.secretKey)
+    result.SetLength(batch_size)
+    print("4 * x1 = " + str(result))
+
+    # Decrypt the result of multiplication
+    result = cc.Decrypt(c_mult, keys.secretKey)
+    result.SetLength(batch_size)
+    print("x1 * x2 = " + str(result))
+
+    # Decrypt the result of rotations
+    result = cc.Decrypt(c_rot1, keys.secretKey)
+    result.SetLength(batch_size)
+    print("In rotations, very small outputs (~10^-10 here) correspond to 0's:")
+    print("x1 rotated by 1 = " + str(result))
+
+    result = cc.Decrypt(c_rot2, keys.secretKey)
+    result.SetLength(batch_size)
+    print("x1 rotated by -2 = " + str(result))
+
+
+if __name__ == "__main__":
+    main()

+ 64 - 0
tests/test_boolean.py

@@ -0,0 +1,64 @@
+from openfhe import *
+import pytest
+
+
+## Sample Program: Step 1: Set CryptoContext
+@pytest.mark.parametrize("a", [0, 1])
+@pytest.mark.parametrize("b", [0, 1])
+def test_boolean_AND(a, b):
+    cc = BinFHEContext()
+
+    """
+    STD128 is the security level of 128 bits of security based on LWE Estimator
+    and HE standard. Other common options are TOY, MEDIUM, STD192, and STD256.
+    MEDIUM corresponds to the level of more than 100 bits for both quantum and
+    classical computer attacks
+    """
+    cc.GenerateBinFHEContext(STD128, GINX)
+
+    ## Sample Program: Step 2: Key Generation
+
+    # Generate the secret key
+    sk = cc.KeyGen()
+
+    print("Generating the bootstrapping keys...\n")
+
+    # Generate the bootstrapping keys (refresh and switching keys)
+    cc.BTKeyGen(sk)
+
+    # Sample Program: Step 3: Encryption
+    """
+    Encrypt two ciphertexts representing Boolean True (1).
+    By default, freshly encrypted ciphertexts are bootstrapped.
+    If you wish to get a fresh encryption without bootstrapping, write
+    ct1 = cc.Encrypt(sk, 1, FRESH)
+    """
+
+    ct1 = cc.Encrypt(sk, a)
+    ct2 = cc.Encrypt(sk, b)
+
+    # Sample Program: Step 4: Evaluation
+
+    # Compute (1 AND 1) = 1; Other binary gate options are OR, NAND, and NOR
+    ctAND1 = cc.EvalBinGate(AND, ct1, ct2)
+
+    # Compute (NOT 1) = 0
+    ct2Not = cc.EvalNOT(ct2)
+
+    # Compute (1 AND (NOT 1)) = 0
+    ctAND2 = cc.EvalBinGate(AND, ct2Not, ct1)
+
+    # Compute OR of the result in ctAND1 and ctAND2
+    ctResult = cc.EvalBinGate(OR, ctAND1, ctAND2)
+
+    # Sample Program: Step 5: Decryption
+
+    result = cc.Decrypt(sk, ctResult)
+
+    print(
+        f"Result of encrypted computation of ({a} AND {b}) OR ({a} AND (NOT {b})) = {result}"
+    )
+    plaintext_result = (a and b) or (a and (not b))
+    assert (
+        result == plaintext_result
+    ), "Logical AND in plaintext and ciphertext should be same"

+ 57 - 0
tests/test_examples.py

@@ -0,0 +1,57 @@
+import os
+import sys
+from pathlib import Path
+import importlib.util
+import pytest
+import tempfile
+import shutil
+
+EXAMPLES_SCRIPTS_PATH = os.path.join(Path(__file__).parent.parent, "examples", "pke")
+
+
+def importhelper(path, modulename):
+    spec = importlib.util.spec_from_file_location(
+        modulename, os.path.join(path, modulename + ".py")
+    )
+    module = importlib.util.module_from_spec(spec)
+    sys.modules[modulename] = module
+    spec.loader.exec_module(module)
+    return module
+
+
+@pytest.mark.parametrize(
+    "raw_modulename",
+    [
+        "simple-ckks-bootstrapping.py",
+        "simple-integers-serial-bgvrns.py",
+        "function-evaluation.py",
+        "advanced-real-numbers-128.py",
+        "simple-integers-bgvrns.py",
+        "simple-integers-serial.py",
+        "polynomial-evaluation.py",
+        "scheme-switching.py",
+        "tckks-interactive-mp-bootstrapping.py",
+        "advanced-real-numbers.py",
+        "threshold-fhe-5p.py",
+        "simple-integers.py",
+        "simple-real-numbers-serial.py",
+        "iterative-ckks-bootstrapping.py",
+        "tckks-interactive-mp-bootstrapping-Chebyschev.py",
+        "simple-real-numbers.py",
+        "threshold-fhe.py",
+        "pre-buffer.py",
+    ],
+)
+def test_run_scripts(raw_modulename):
+    with tempfile.TemporaryDirectory() as td:
+        os.mkdir(td + "/demoData")
+        modulename_py = raw_modulename.replace("-", "_")
+        shutil.copyfile(
+            os.path.join(EXAMPLES_SCRIPTS_PATH, raw_modulename),
+            os.path.join(td, modulename_py),
+        )
+        sys.path.insert(0, td)
+        modulename = modulename_py.split(".")[0]
+        print(f"-*- running module {modulename} -*-")
+        module = importhelper(td, modulename)
+        module.main()