Programming with USEncrypt®#

When using USEncrypt®, users need to be aware of a few guidelines that affect the way they should write their code. These principles are motivated by the fact that data must be protected at all times, and that a few traditional coding methods should not be used to avoid leaking information.

Tip #1: Loops Cannot Have an Encrypted Condition#

while and for loops are a very important tool, but it is important to know that they cannot be used when the condition itself is an encrypted object. For example, consider the following code snippet:

import usencrypt as ue
value = ue.encrypt(0)

while value < 10:
    # Do something.
    value += 1

Running it outputs the following:

ValueError: Direct boolean evaluation of a Ciphertext is not supported.

Therefore, this is not an option when using encrypted objects, as it would inherently lead the user to be able to iteratively decipher the value of the object. Instead, we recommend, whenever possible, to run all loops strictly n times, as follows:

import usencrypt as ue

n = 10

for i in range(n):
    # Do something.

While this may lead to less efficient scripts, it protects the integrity of the data.

Tip #2: Control-Flow with the usencrypt.ifelse() Ternary Function#

Using tradition if-then-else statements may lead to a leak in data information. For example, consider the following function:

def foo(value):
    """
    If the value is 5, return value * 2.
    """
    if value == 5:
        return value * 2
    else:
        return value

With non-encrypted data, we can do the following:

>>> value = 5
>>> result = foo(value)
>>> result
10

However, this inherently tells us that value is indeed 5, leaking its information. Therefore, the same exercise with an encrypted value results in the following:

>>> import usencrypt as ue
>>> ue_value = ue.encrypt(5)
>>> result = foo(ue_value)
>>> result
ValueError: Direct boolean evaluation of a Ciphertext is not supported.

This is where the usencrypt.ifelse() ternary function comes in. With this function, we can set up our if-then-else statement as a ternary statement, keeping the results encrypted:

>>> import usencrypt as ue
>>> ue_value = ue.encrypt(5)
>>> result = ue.ifelse(ue_value == 5, ue_value * 2, ue_value)
>>> result
(0x7f85100ba310) Float: 0x6ea0d1091f29e8e0bc55893a74419e77092569727a374dcdb4c802e20f9fc5bc
>>> ue.decrypt(result)
10.0

Here, we can continue the original flow of the program while maintaining the confidentiality of the data.