Encrypted Computations using Google Colab

Alex Shpurov
3 min readJul 27, 2020

In this tutorial, I show how to make a computation on encrypted data using Python and Google Collab, a free machine learning platform offered by Google. I liked Collab since you can test something very quickly before installing it on your computer or in the cloud.

The type of encryption I am using is called fully homomorphic encryption and it allows us to encrypt data first and send data to another party to perform some calculations keeping data still encrypted. Once you get a result back, you and only you can decrypt it because you hold a secret key.

Fully homomorphic encryption (FHE) is a type of encryption which performs only two operations on encrypted data: addition and multiplication. In practice, you need to convert your task in a way that you have only these two operations for encrypted computations.

In this tutorial, I will be using a Python wrapper for the SEAL library. This library was developed by Microsoft a few years ago and It implements the widely used BFV FHE encryption schema.

Let’s open a new collab notebook, or you can use the one I already have [link].

You need to install and build the SEAL library by going through the following steps:

  1. Downloading all the dependencies:
!git clone -b 3.3.2 https://github.com/Huelse/SEAL-Python

2. Build the SEAL C++ library

!cd SEAL-Python/SEAL/native/src;cmake .;make

3. Python-SEAL dependency

!pip3 install -r SEAL-Python/requirements.txt

4. Setup Python-SEAL

!cd SEAL-Python;python3 setup.py build_ext -i

5. Install Python-SEAL

!cd SEAL-Python;python3 setup.py install

This is it. We have completed the installation.

Now let’s try to generate a key pair:

from seal import *parms = EncryptionParameters(scheme_type.BFV)poly_modulus_degree = 4096parms.set_poly_modulus_degree(poly_modulus_degree)parms.set_coeff_modulus(CoeffModulus.BFVDefault(poly_modulus_degree))parms.set_plain_modulus(256)context = SEALContext.Create(parms)keygen = KeyGenerator(context)public_key = keygen.public_key()secret_key = keygen.secret_key()encryptor = Encryptor(context, public_key)evaluator = Evaluator(context)decryptor = Decryptor(context, secret_key)

The secret key here is a private key you will be using for decryption while the public key is used to perform calculations and it could be given to anyone. Please note, that calculations are causing noise to grow in encrypted values and at some point, you won’t be able to decrypt those values anymore. Also, calculations perform against fixed modulus. Please refer to the official Microsoft documentation.

Now, let’s proceed to the demo where we are encrypting two values. (Numbers should be hexadecimal.)

enc1 = Ciphertext()enc2 = Ciphertext()encryptor.encrypt(Plaintext(“5”), enc1)encryptor.encrypt(Plaintext(“7”), enc2)

Here we are adding two numbers and decrypt to see the result

evaluator.add_inplace(enc1, enc2)# decryptresult = Plaintext()decryptor.decrypt(enc1, result)print(f”result: 0x{result.to_string()}”)

>> result: 0xC

Then we are multiplying the encrypted numbers and decrypt them

evaluator.multiply_inplace(enc1, enc2)result_mul = Plaintext()decryptor.decrypt(enc1, result_mul)print(f”result: 0x{result_mul.to_string()}”)

> result: 0x54

You can run more examples from the Python-Seal repository.

The Colab notebook with the code above is available as well as the GitHub page.

Next: Encrypted Computations, Implementation Tricks >