Skip to content

Quickstart

In this page you will learn how to encrypt / decrypt data, and compute simple operations on encrypted data such as additions, subtractions and multiplications.

Encrypt / Decrypt

Encryption and decryption require a public key and a secret key, respectively. The public key is generated from the secret key. You can encrypt and decrypt data as shown below. Note that CKKS is a real-number-based scheme, so a very small error is introduced during encryption and decryption.

from desilofhe import Engine

engine = Engine()

secret_key = engine.create_secret_key()
public_key = engine.create_public_key(secret_key)

data = [1, 2, 3]
encrypted = engine.encrypt(data, public_key)
decrypted = engine.decrypt(encrypted, secret_key)

print(decrypted[:3]) # [1. 2. 3.]

Add / Subtract

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

from desilofhe import Engine

engine = Engine()

secret_key = engine.create_secret_key()
public_key = engine.create_public_key(secret_key)

data1 = [1, 2, 3]
encrypted1 = engine.encrypt(data1, public_key)

data2 = [4, 5, 6]
encrypted2 = engine.encrypt(data2, public_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[:3], decrypted2[:3]) # [5. 7. 9.] [-3. -3. -3.]

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.

from desilofhe import Engine

engine = Engine()

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

data1 = [1, 2, 3]
encrypted1 = engine.encrypt(data1, public_key)

data2 = [4, 5, 6]
encrypted2 = engine.encrypt(data2, public_key)

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

decrypted = engine.decrypt(multiplied, secret_key)

print(decrypted[:3]) # [~4 ~10 ~18]
print(encrypted1.level, encrypted2.level, multiplied.level) # 7 7 6

SEAL-Style API

The examples up until now combine several operations into single functions for convenience. If you are familiar with traditional homomorphic encryption libraries, you can use a SEAL-style API. This approach might be more efficient if you plan to reuse intermediate results.

from desilofhe import Engine

engine = Engine()

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

data1 = [1, 2, 3]
encoded1 = engine.encode(data1)
encrypted1 = engine.encrypt(encoded1, public_key)

data2 = [4, 5, 6]
encoded2 = engine.encode(data2)
encrypted2 = engine.encrypt(encoded2, public_key)

rescaled1 = engine.rescale(encrypted1)
rescaled2 = engine.rescale(encrypted2)
multiplied = engine.multiply(rescaled1, rescaled2)
relinearized = engine.relinearize(multiplied, relinearization_key)

decrypted_plaintext = engine.decrypt_to_plaintext(relinearized, secret_key)
decrypted = engine.decode(decrypted_plaintext)

print(decrypted[:3])  # [~4 ~10 ~18]