Comparisons, Branching, and Loops
Programs are often written with control flow structures such as conditionals and loops in order to repeat actions or change the flow of execution based on certain conditions. Here will will go into what control flow mechanisms can be expressed in an FHE program.
Comparisons
We support the common comparison operators between plaintext and encrypted values: <, >, <=, >=, ==, and !=.
#include <parasol.h>
[[clang::fhe_program]]
void compare([[clang::encrypted]] uint32_t a,
[[clang::encrypted]] uint32_t b,
[[clang::encrypted]] bool *result_lt,
[[clang::encrypted]] bool *result_gt,
[[clang::encrypted]] bool *result_le,
[[clang::encrypted]] bool *result_ge,
[[clang::encrypted]] bool *result_eq,
[[clang::encrypted]] bool *result_ne) {
*result_lt = (a < b);
*result_gt = (a > b);
*result_le = (a <= b);
*result_ge = (a >= b);
*result_eq = (a == b);
*result_ne = (a != b);
}
Branching and the select/iselect operation
We do not support if statements in FHE programs at the moment. However, we do support choosing between two different values based on a condition using the selectX (unsigned) and iselectX (signed) function, where X is the bit width of the values being selected. This enables writing more complex logic in FHE programs.
#include <parasol.h>
[[clang::fhe_program]]
void max([[clang::encrypted]] uint8_t a,
[[clang::encrypted]] uint8_t b,
[[clang::encrypted]] uint8_t *result) {
// Use select function to choose the larger of a and b
*result = select8((a > b), a, b);
}
Loops
Loops where the loop condition is in plaintext are supported in FHE programs, which includes both while loops and for loops.
#include <parasol.h>
[[clang::fhe_program]]
void sum([[clang::encrypted]] uint32_t *a,
uint32_t len,
[[clang::encrypted]] uint8_t *result) {
uint32_t x = 0;
for (uint32_t i = 0; i < len; i++) {
x += a[i];
}
*result = x;
}