Reusing arguments

This document explains how to reuse encrypted arguments in applications where the same arguments are used repeatedly.

Encrypting data can be resource-intensive, especially when the same argument or set of arguments is used multiple times. In such cases, it’s inefficient to encrypt and transfer the arguments repeatedly. Instead, you can encrypt the arguments separately and reuse them as needed. By encrypting the arguments once and reusing them, you can optimize performance by reducing encryption time, memory usage, and network bandwidth.

Here is an example:

from concrete import fhe

@fhe.compiler({"x": "encrypted", "y": "encrypted"})
def add(x, y):
    return x + y

inputset = [(2, 3), (0, 0), (1, 6), (7, 7), (7, 1), (3, 2), (6, 1), (1, 7), (4, 5), (5, 4)]
circuit = add.compile(inputset)

sample_y = 4
_, encrypted_y = circuit.encrypt(None, sample_y)

for sample_x in range(3, 6):
    encrypted_x, _ = circuit.encrypt(sample_x, None)

    encrypted_result = circuit.run(encrypted_x, encrypted_y)
    result = circuit.decrypt(encrypted_result)

    assert result == sample_x + sample_y

Note when you use encrypt method:

  • If you have multiple arguments, the encrypt method would return a tuple.

  • If you specify None as one of the arguments, None is placed at the same location in the resulting tuple.

    • For example, circuit.encrypt(a, None, b, c, None) returns (encrypted_a, None, encrypted_b, encrypted_c, None).

  • Each value returned by encrypt can be stored and reused anytime.

The order of arguments must be consistent when encrypting and using them. Encrypting an x and using it as a y could result in undefined behavior.

Last updated