sum

Engine.sum(ciphertext, rotation_key, axis)
입력 암호문을 지정된 축을 기준으로 합 연산을 수행한 암호문을 반환합니다. 다자간 연산이 아니면서 최대 레벨인 경우에만 레벨을 소모합니다.
  • 인풋:
    • Ciphertext
    • RotationKey
      • 0-축 의 경우에는 필요하지 않습니다.
    • Axis: int | None
      • 합 연산을 수행할 축입니다. 전달하지 않을 경우 모든 값의 총합이 모든 슬롯에 기입됩니다.
  • 아웃풋:
    • Ciphertext
      • 합의 값들이 모든 슬롯에 저장되어있습니다.
      • 암호문의 길이가 1로 줄어듭니다.

Examples

모든 축에 대한 합 연산

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)

# ciphertext_4의 내부:
# [[1, 2, 3, 4]]

sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key)

# 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)

# ciphertext_8의 내부:
# [[1, 2, 3, 4],
#  [5, 6, 7, 8]]

sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key)

# sum_of_ciphertext_8의 내부:
# [[36, 36, 36, 36]]

0-축에 대한 합 연산

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)

# ciphertext_4의 내부:
# [[1, 2, 3, 4]]

sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key, axis=0)

# 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)

# ciphertext_8의 내부:
# [[1, 2, 3, 4],
#  [5, 6, 7, 8]]

sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key, axis=0)

# sum_of_ciphertext_8의 내부:
# [[6, 8, 10, 12]]

1-축에 대한 합 연산

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)

# ciphertext_4의 내부:
# [[1, 2, 3, 4]]

sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key, axis=1)

# 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)

# ciphertext_8의 내부:
# [[1, 2, 3, 4],
#  [5, 6, 7, 8]]

sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key, axis=1)

# sum_of_ciphertext_8의 내부:
# [[10, 10, 10, 10],
#  [26, 26, 26, 26]]