sum
Engine.sum(ciphertext, rotation_key, axis)
- Returns a ciphertext that is the sum of the input ciphertext along a given axis. A level is consumed only if the operation is not a multiparty computation and the ciphertext is at the maximum allowable level for a given parameter.
- Input:
- Ciphertext
- RotationKey
- RotationKey is not required for sum over the 0 axis.
- Axis: int | None
- Axis along which a sum is performed. The default, axis=None, will sum all of the elements of the input ciphertext.
- Output:
- Ciphertext
- The sum value will be in every single slot.
- The length of the ciphertext will be reduced to 1.
- Ciphertext
Examples
Sum along all axes
from desilofhe import Engine
engine = Engine(slot_count=4)
secret_key = engine.create_secret_key()
rotation_key = engine.create_rotation_key(secret_key)
message_4 = [1, 2, 3, 4]
ciphertext_4 = engine.encrypt(message_4, secret_key)
# contents of ciphertext_4:
# [[1, 2, 3, 4]]
sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key)
# contents of sum_of_ciphertext_4:
# [[10, 10, 10, 10]]
message_8 = [1, 2, 3, 4, 5, 6, 7, 8]
ciphertext_8 = engine.encrypt(message_8, secret_key)
# contents of ciphertext_8:
# [[1, 2, 3, 4],
# [5, 6, 7, 8]]
sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key)
# contents of sum_of_ciphertext_8:
# [[36, 36, 36, 36]]
Sum along the 0-axis
from desilofhe import Engine
engine = Engine(slot_count=4)
secret_key = engine.create_secret_key()
rotation_key = engine.create_rotation_key(secret_key)
message_4 = [1, 2, 3, 4]
ciphertext_4 = engine.encrypt(message_4, secret_key)
# contents of ciphertext_4:
# [[1, 2, 3, 4]]
sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key, axis=0)
# contents of sum_of_ciphertext_4:
# [[1, 2, 3, 4]]
message_8 = [1, 2, 3, 4, 5, 6, 7, 8]
ciphertext_8 = engine.encrypt(message_8, secret_key)
# contents of ciphertext_8:
# [[1, 2, 3, 4],
# [5, 6, 7, 8]]
sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key, axis=0)
# contents of sum_of_ciphertext_8:
# [[6, 8, 10, 12]]
Sum along the 1-axis
from desilofhe import Engine
engine = Engine(slot_count=4)
secret_key = engine.create_secret_key()
rotation_key = engine.create_rotation_key(secret_key)
message_4 = [1, 2, 3, 4]
ciphertext_4 = engine.encrypt(message_4, secret_key)
# contents of ciphertext_4:
# [[1, 2, 3, 4]]
sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key, axis=1)
# contents of sum_of_ciphertext_4:
# [[10, 10, 10, 10]]
message_8 = [1, 2, 3, 4, 5, 6, 7, 8]
ciphertext_8 = engine.encrypt(message_8, secret_key)
# contents of ciphertext_8:
# [[1, 2, 3, 4],
# [5, 6, 7, 8]]
sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key, axis=1)
# contents of sum_of_ciphertext_8:
# [[10, 10, 10, 10],
# [26, 26, 26, 26]]