HelloWorld

This basic example shows the simplest use of the library by encrypting two integers, operating with them (+,-,*) and decrypting the results.

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

import numpy as np
from Pyfhel import Pyfhel
print("1. Import Pyfhel class, and numpy for the inputs to encrypt.")
1. Import Pyfhel class, and numpy for the inputs to encrypt.

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.

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

The best way to obtain information from a created Pyfhel object is to print it:

print("2. Context and key setup")
print(HE)
2. Context and key setup
<bfv Pyfhel obj at 0x7fecaf3f96c0, [pk:Y, sk:Y, rtk:-, rlk:-, contx(n=16384, t=786433, sec=128, qi=[], scale=1.0, )]>

3. Integer Encryption

we will define two integers and encrypt them using encryptInt:

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))
3. Integer Encryption,
    int  [127] -> ctxt1  <class 'Pyfhel.PyCtxt.PyCtxt'>
    int  [-2] -> ctxt2  <class 'Pyfhel.PyCtxt.PyCtxt'>

# The best way to obtain information from a ciphertext is to print it:

print(ctxt1)
print(ctxt2)
<Pyfhel Ciphertext at 0x7fecaf426ef0, scheme=bfv, size=2/2, noiseBudget=361>
<Pyfhel Ciphertext at 0x7fecaf24b7c0, scheme=bfv, size=2/2, noiseBudget=361>

4. Operating with encrypted integers

Relying on the context defined before, we will now operate (addition, substaction, multiplication) the two ciphertexts:

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}")
4. Operating with encrypted integers
Sum: <Pyfhel Ciphertext at 0x7fecaf429900, scheme=bfv, size=2/2, noiseBudget=360>
Sub: <Pyfhel Ciphertext at 0x7fecaf3d56d0, scheme=bfv, size=2/2, noiseBudget=360>
Mult:<Pyfhel Ciphertext at 0x7fecaf3f9680, scheme=bfv, size=3/3, noiseBudget=329>

5. Decrypting integers

Once we’re finished with the encrypted operations, we can use the Pyfhel instance to decrypt the results using decryptInt:

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)
#. 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]

Total running time of the script: (0 minutes 1.294 seconds)

Estimated memory usage: 118 MB

Gallery generated by Sphinx-Gallery