.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_autoexamples/Demo_1_HelloWorld.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__autoexamples_Demo_1_HelloWorld.py: HelloWorld =========================== This basic example shows the simplest use of the library by encrypting two integers, operating with them (+,-,*) and decrypting the results. .. GENERATED FROM PYTHON SOURCE LINES 10-17 1. Imports --------------------------- We start by importing the library with the three main classes: * Pyfhel class contains most of the functions. * PyPtxt is the plaintext class * PyCtxt is the ciphertext class .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. code-block:: Python import numpy as np from Pyfhel import Pyfhel print("1. Import Pyfhel class, and numpy for the inputs to encrypt.") .. rst-class:: sphx-glr-script-out .. code-block:: none 1. Import Pyfhel class, and numpy for the inputs to encrypt. .. GENERATED FROM PYTHON SOURCE LINES 21-25 2. Context and key setup --------------------------- We will start the Helloworld by generating a context and a public/secret key pair. This is all managed by a Pyfhel instance under the hood. .. GENERATED FROM PYTHON SOURCE LINES 25-31 .. code-block:: Python HE = Pyfhel() # Creating empty Pyfhel object HE.contextGen(scheme='bfv', n=2**14, t_bits=20) # Generate context for 'bfv'/'ckks' scheme # The n defines the number of plaintext slots. # There are many configurable parameters on this step # More info in Demo_2, Demo_3, and Pyfhel.contextGen() HE.keyGen() # Key Generation: generates a pair of public/secret keys .. GENERATED FROM PYTHON SOURCE LINES 32-33 The best way to obtain information from a created Pyfhel object is to print it: .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: Python print("2. Context and key setup") print(HE) .. rst-class:: sphx-glr-script-out .. code-block:: none 2. Context and key setup .. GENERATED FROM PYTHON SOURCE LINES 37-40 3. Integer Encryption --------------------------- we will define two integers and encrypt them using `encryptInt`: .. GENERATED FROM PYTHON SOURCE LINES 40-47 .. code-block:: Python integer1 = np.array([127], dtype=np.int64) integer2 = np.array([-2], dtype=np.int64) ctxt1 = HE.encryptInt(integer1) # Encryption makes use of the public key ctxt2 = HE.encryptInt(integer2) # For integers, encryptInt function is used. print("3. Integer Encryption, ") print(" int ",integer1,'-> ctxt1 ', type(ctxt1)) print(" int ",integer2,'-> ctxt2 ', type(ctxt2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 3. Integer Encryption, int [127] -> ctxt1 int [-2] -> ctxt2 .. GENERATED FROM PYTHON SOURCE LINES 48-49 # The best way to obtain information from a ciphertext is to print it: .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: Python print(ctxt1) print(ctxt2) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 53-57 4. Operating with encrypted integers -------------------------------------- Relying on the context defined before, we will now operate (addition, substaction, multiplication) the two ciphertexts: .. GENERATED FROM PYTHON SOURCE LINES 57-65 .. code-block:: Python ctxtSum = ctxt1 + ctxt2 # `ctxt1 += ctxt2` for inplace operation ctxtSub = ctxt1 - ctxt2 # `ctxt1 -= ctxt2` for inplace operation ctxtMul = ctxt1 * ctxt2 # `ctxt1 *= ctxt2` for inplace operation print("4. Operating with encrypted integers") print(f"Sum: {ctxtSum}") print(f"Sub: {ctxtSub}") print(f"Mult:{ctxtMul}") .. rst-class:: sphx-glr-script-out .. code-block:: none 4. Operating with encrypted integers Sum: Sub: Mult: .. GENERATED FROM PYTHON SOURCE LINES 66-70 5. Decrypting integers --------------------------- Once we're finished with the encrypted operations, we can use the Pyfhel instance to decrypt the results using `decryptInt`: .. GENERATED FROM PYTHON SOURCE LINES 70-81 .. code-block:: Python resSum = HE.decryptInt(ctxtSum) # Decryption must use the corresponding function # decryptInt. resSub = HE.decryptInt(ctxtSub) resMul = HE.decryptInt(ctxtMul) print("#. Decrypting result:") print(" addition: decrypt(ctxt1 + ctxt2) = ", resSum) print(" substraction: decrypt(ctxt1 - ctxt2) = ", resSub) print(" multiplication: decrypt(ctxt1 + ctxt2) = ", resMul) .. rst-class:: sphx-glr-script-out .. code-block:: none #. Decrypting result: addition: decrypt(ctxt1 + ctxt2) = [125 0 0 ... 0 0 0] substraction: decrypt(ctxt1 - ctxt2) = [129 0 0 ... 0 0 0] multiplication: decrypt(ctxt1 + ctxt2) = [-254 0 0 ... 0 0 0] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.294 seconds) **Estimated memory usage:** 118 MB .. _sphx_glr_download__autoexamples_Demo_1_HelloWorld.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Demo_1_HelloWorld.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Demo_1_HelloWorld.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_