To compute on encrypted data, you first need to define the function you want to compute, then compile it into a Concrete Circuit
, which you can use to perform homomorphic evaluation.
Here is the full example that we will walk through:
Everything you need to perform homomorphic evaluation is included in a single module:
In this example, we compile a simple addition function:
To compile the function, you need to create a Compiler
by specifying the function to compile and the encryption status of its inputs:
To set that e.g. y
is in the clear, it would be
An inputset is a collection representing the typical inputs to the function. It is used to determine the bit widths and shapes of the variables within the function.
It should be in iterable, yielding tuples, of the same length as the number of arguments of the function being compiled:
Here, our inputset is made of 10 pairs of integers, whose the minimum pair is (0, 0)
and the maximum is (7, 7)
.
Choosing a representative inputset is critical to allow the compiler to find accurate bounds of all the intermediate values (find more details here. Later if you evaluate the circuit with values that make under or overflows it results to an undefined behavior.
There is a utility function called fhe.inputset(...)
for easily creating random inputsets, see its documentation to learn more!
You can use the compile
method of the Compiler
class with an inputset to perform the compilation and get the resulting circuit back:
You can use the keygen
method of the Circuit
class to generate the keys (public and private):
If you don't call the key generation explicitly keys will be generated lazily when it needed.
Now you can easily perform the homomorphic evaluation using the encrypt
, run
and decrypt
methods of the Circuit
: