GL (Matrix) Quickstart
In this page you will learn how to encrypt / decrypt data, and compute simple operations on encrypted data such as additions, subtractions, matrix multiplications and hadamard 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 GLEngine
engine = GLEngine()
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 GLEngine
engine = GLEngine()
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.]
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 GLEngine
engine = GLEngine()
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) # 6 6 5
Hadamard Multiply
To Hadamard multiply ciphertexts, a special key called the Hadamard multiplication key is required. You can encrypt data and perform Hadamard 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 GLEngine
engine = GLEngine()
secret_key = engine.create_secret_key()
hadamard_multiplication_key = engine.create_hadamard_multiplication_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.hadamard_multiply(
encrypted1, encrypted2, hadamard_multiplication_key
)
decrypted = engine.decrypt(multiplied, secret_key)
print(decrypted[0]) # [4. 4. 4.]
print(encrypted1.level, encrypted2.level, multiplied.level) # 6 6 5