# HelloWorld Demo for Pyfhel, showing the simplest use of the library by
#   encrypting two integers, operating with them (+,-,*) and decrypting
#   the results. The code is heavily commented.


from Pyfhel import Pyfhel, PyPtxt, PyCtxt
# Pyfhel class contains most of the functions.
# PyPtxt is the plaintext class
# PyCtxt is the ciphertext class


print("==============================================================")
print("===================== Pyfhel HELLO WORLD =====================")
print("==============================================================")


print("1. Creating Context and KeyGen in a Pyfhel Object. ")
HE = Pyfhel()           # Creating empty Pyfhel object
HE.contextGen(p=65537)  # Generating context. The value of p is important.
# Generating context. The value of p is important.
#  There are many configurable parameters on this step
#  More info in Demo_ContextParameters.py, and
#  in the docs of the function (link to docs in README)
HE.keyGen()             # Key Generation.


print("2. Encrypting integers")
integer1 = 127
integer2 = -2
ctxt1 = HE.encryptInt(integer1) # Encryption makes use of the public key
ctxt2 = HE.encryptInt(integer2) # For integers, encryptInt function is used.
print("    int ",integer1,'-> ctxt1 ', type(ctxt1), str(ctxt1))
print("    int ",integer2,'-> ctxt2 ', type(ctxt2), str(ctxt2))

print("3. Operating with encrypted integers")
ctxtSum = ctxt1 + ctxt2         # `ctxt1 += ctxt2` for quicker inplace operation
ctxtSub = ctxt1 - ctxt2         # `ctxt1 -= ctxt2` for quicker inplace operation
ctxtMul = ctxt1 * ctxt2         # `ctxt1 *= ctxt2` for quicker inplace operation


print("4. Decrypting result:")
resSum = HE.decryptInt(ctxtSum) # Decryption must use the corresponding function
#  decryptInt.
resSub = HE.decryptInt(ctxtSub)
resMul = HE.decryptInt(ctxtMul)
print("     addition:       decrypt(ctxt1 + ctxt2) =  ", resSum)
print("     substraction:   decrypt(ctxt1 - ctxt2) =  ", resSub)
print("     multiplication: decrypt(ctxt1 + ctxt2) =  ", resMul)