Skip to content

Matrix (GL) Quickstart

In this page you will learn how to encrypt / decrypt data, and compute simple operations on encrypted data such as addition, subtraction, multiplication and matrix multiplication using the GL scheme. You can run all the codes in Google Colab

Encrypt / Decrypt

Encryption and decryption can be done with a secret key. You can encrypt and decrypt data as shown below.

import numpy as np
from desilofhe import Engine

engine = Engine(form="matrix")

secret_key = engine.create_secret_key()

data = np.ones(engine.shape)
encrypted = engine.encrypt(data, secret_key)
decrypted = engine.decrypt(encrypted, secret_key)

print(decrypted[0])  # ones

Add / Subtract

To add or subtract ciphertext, no additional keys are required beyond the secret keys. You can encrypt data separately and then perform addition and subtraction as follows:

import numpy as np
from desilofhe import Engine

engine = Engine(form="matrix")

secret_key = engine.create_secret_key()

data1 = np.ones(engine.shape) * 2
encrypted1 = engine.encrypt(data1, secret_key)

data2 = np.ones(engine.shape)
encrypted2 = engine.encrypt(data2, secret_key)

added = engine.add(encrypted1, encrypted2)
subtracted = engine.subtract(encrypted1, encrypted2)

decrypted1 = engine.decrypt(added, secret_key)
decrypted2 = engine.decrypt(subtracted, secret_key)

print(decrypted1[0])  # [3. 3. 3.]
print(decrypted2[0])  # [1. 1. 1.]

Multiply

To multiply ciphertexts, a special key called the relinearization key is required due to the nature of homomorphic encryption. You can encrypt data and perform multiplication as shown below. Note that ciphertexts in homomorphic encryption have a maximum multiplication depth, which will decrease after multiplication.

import numpy as np
from desilofhe import Engine

engine = Engine(form="matrix")

secret_key = engine.create_secret_key()
relinearization_key = engine.create_relinearization_key(secret_key)

data1 = np.ones(engine.shape) * 2  # [2. 2. 2.]
encrypted1 = engine.encrypt(data1, secret_key)

data2 = np.ones(engine.shape) * 2  # [2. 2. 2.]
encrypted2 = engine.encrypt(data2, secret_key)

multiplied = engine.multiply(encrypted1, encrypted2, relinearization_key)

decrypted = engine.decrypt(multiplied, secret_key)

print(decrypted[0])  # [4. 4. 4.]
print(encrypted1.level, encrypted2.level, multiplied.level)  # 7 7 6

Matrix Multiply

To matrix multiply ciphertexts, a special key called the matrix multiplication key is required. You can encrypt data and perform matrix multiplication as shown below. Note that ciphertexts in homomorphic encryption have a maximum multiplication depth, which will decrease after multiplication.

import numpy as np
from desilofhe import Engine

engine = Engine(form="matrix")

secret_key = engine.create_secret_key()
matrix_multiplication_key = engine.create_matrix_multiplication_key(secret_key)

data1 = np.ones(engine.shape)
encrypted1 = engine.encrypt(data1, secret_key)

data2 = np.ones(engine.shape)
encrypted2 = engine.encrypt(data2, secret_key)

multiplied = engine.matrix_multiply(
    encrypted1, encrypted2, matrix_multiplication_key
)

decrypted = engine.decrypt(multiplied, secret_key)

print(decrypted[0])  # [32. 32. 32.]
print(encrypted1.level, encrypted2.level, multiplied.level)  # 7 7 6