Concrete — Zama's Fully Homomorphic Encryption Compiler
The importance of compilers for homomorphic encryption has been clear since the rise of practical Fully Homomorphic Encryption (FHE) schemes. Managing noise, choosing appropriate crypto parameters, and finding the best set and order of operations for a specific computation are problems for cryptographers. A homomorphic encryption compiler for developers is supposed to take all that complexity away.
At Zama, we started prototyping compilers a while back using different techniques and technologies. HNP was one of our prototypes that was made available to the community, but it didn’t solve major FHE compilation challenges. So we kept digging.
Following the announcement of Concrete v1.0.0, we released the compiler Zama have been working on for almost two years. This blog post explains how you can use it. A later blog post will be dedicated to the Compiler’s internals.
A First Simple Example
The first thing you need to know is what the input to the Compiler looks like. The Compiler expects an input program in MLIR. Below is a simple example doing an addition of two encrypted unsigned integers.
It’s pretty easy to read. We define the function [.c-inline-code]main[.c-inline-code] that gets two arguments of type [.c-inline-code]FHE.eint<6>[.c-inline-code], which then defines an encrypted unsigned integer of 6 bits and returns a value of the same type. The function itself makes a call to the [.c-inline-code]FHE.add_eint[.c-inline-code] operation, which will add the two operands.
You can use the Compiler via different APIs and tools. Currently available APIs are Python, Cpp, and C. You also have a CLI tool that is quite handy for debugging the compilation pipeline at different stages and intermediate representations.
The Python API is the easiest to set up, so we can begin to compile and run the previous MLIR example. Start with installing [.c-inline-code]concrete-python[.c-inline-code] via [.c-inline-code]pip install concrete-python[.c-inline-code]. This doesn’t just install the Compiler, but the Python frontend as well (we will talk about frontends later in this post). You should be able to import the Compiler module if installation was successful.
One of the main entry points to the Compiler is the [.c-inline-code]LibrarySupport[.c-inline-code] class, which allows you to compile and run FHE programs while storing artifacts on disk. A similar class ([.c-inline-code]JITSupport[.c-inline-code]) is available which keeps artifacts in memory.
At this point, you have completed the compilation, and should find both the compiled library [.c-inline-code]sharedlib.so[.c-inline-code], as well as the [.c-inline-code]client_parameters.concrete.params.json[.c-inline-code] under the [.c-inline-code]out[.c-inline-code] directory. The shared library contains the executable function, which can be loaded and executed. The json file is a description of the inputs and outputs, as well as crypto parameters for the compiled function.
These parameters allow you to generate the right key set, including both secret keys and evaluation keys. Secret keys have the capability of decrypting ciphertexts, and thus should only be accessible to parties doing decryption. Evaluation keys are public material that can be sent to a server in order to run an encrypted computation. Crypto parameters are the keystone for FHE computation to ensure security, correctness, and efficiency. FHE libraries require these parameters to be chosen by the user, but the crypto parameters are an output of the compilation in the Compiler, so you don’t need to provide them.
Now you can make use of the compiled function by generating keys, encrypting the arguments, calling the compiled function, and finally decrypting the result.
The client parameters are first loaded from the compilation output directory and keys are generated accordingly (different functions may require a different set of keys). The [.c-inline-code]server_lambda[.c-inline-code] refers to the entry-point of the compiled library. It will be loaded from the [.c-inline-code]sharedlib.so[.c-inline-code]. You get the set of evaluation keys and call the compiled function. The result of the addition is still encrypted, so you should decrypt it at the end.
While the previous code does compile, encrypt, run, and decrypt on the same machine, encrypted inputs and outputs and keys are serializable. This means you can implement a use case (see Typical Workflow here) where the client holds the keys and does all the encryption/decryption, while the server’s job is to run the encrypted computation.
Some Linear Algebra
The previous FHE program is quite simple, but you can still use the same Python code to compile a more advanced example that works on tensors of encrypted integers. You only have to change the [.c-inline-code]mlir_input[.c-inline-code] and the input arguments.
The above MLIR function defines a matrix multiplication between a tensor of encrypted values, and a tensor of clear values. It then applies a lookup table representing a nonlinear random function.
If you are not familiar with MLIR, [.c-inline-code]FHE[.c-inline-code] and [.c-inline-code]FHELinalg[.c-inline-code] are two dialects— a grouping of functionality which can be used to extend the MLIR system—which define a set of operations. Using operations from these dialects is how you would define an FHE computation. You can read more about the available operations here and here.
There is also a CLI tool that allows you to compile and debug FHE programs. You can build it from here. We will consider [.c-inline-code]fhe.mlir[.c-inline-code] to contain our first example program.
This is the same as compiling it using the Python API.
Now the CLI provides some more options for debugging intermediate representations. You can try to run it with the [.c-inline-code]–help[.c-inline-code] option to get all the possible options, but interesting options to check intermediate representations are listed below:
The dump-[dialect_name] action allows you to see what the IR looks like at a certain level of the compilation, making sure lowering from one dialect to the other works as expected. This is interesting, especially if you want to understand the compilation pipeline. For example, you can see how our previous example program looks in the Concrete dialect:
At this level, the Compiler has already figured out crypto parameters, and especially the size of LWE ciphertexts, which is apparent in the IR.
The second example program is more verbose as it makes use of more complex operations that work on tensors. [.c-inline-code]fhe2.mlir[.c-inline-code] should contain the second example program:
One last set of options to try is [.c-inline-code]--debug[.c-inline-code] (of course!) and [.c-inline-code]--verbose[.c-inline-code]. This tells you a lot about what’s happening during compilation, especially lowering passes.
Seeing your first program, you might wonder if you have to write the MLIR for any circuit you want to run. Thankfully, that’s not the case. You could do it, but it would be more user friendly to build a frontend layer on top of the Compiler, just like Concrete Python. The goal of the frontend would be to go from any representation of your choice to the equivalent MLIR representation from where the Compiler could take over. An extreme example could be to build a drag and drop tool that lets you build a computation graph visually, which then translates it to MLIR. As of today, Concrete Python is the only frontend built on top of the Compiler, but with Concrete being completely open-source, external users are encouraged to build custom frontends for their needs.
There are a CAPI, Python Bindings, and Rust Bindings that let you build an MLIR representation, so you could integrate them with pretty much everything. Below, we show a Python script that generates an example MLIR:
MLIR Python bindings are also packaged as part of Concrete Python. We can combine them with custom FHE dialects to generate a function that takes as input two encrypted integers of 8 bits, subtract one from the other, and return the result. If you run this script, you should get the following MLIR:
The CAPI differs from the Python API in terms of MLIR code generation, but can be used with much more than just Python. It opens up the ability to build new and creative frontends on top of the Concrete Compiler.
You can use the Concrete Compiler to compile an encrypted computation, but it's also possible to build any frontend you like, as long as it can generate an MLIR program at the end. Using the examples provided above, you are able to use the Compiler to suit your needs and complete your builds without a thorough understanding of cryptography.
Subscribe to the Zama newsletter if you liked this blog post, we will soon share more about the internals of the Zama Concrete compiler and how the parameters are being chosen.
- Star the Concrete Github repository to endorse our work.
- Review the Concrete documentation.
- Get support on our community channels.
- Help advance the FHE space with the Zama Bounty Program.