Call FHE circuits from other languages
Last updated
Last updated
After doing a compilation, we endup with a couple of artifacts, including crypto parameters and a binary file containing the executable circuit. In order to be able to encrypt and run the circuit properly, we need to know how to interpret these artifacts, and there are a couple of utility functions to load them. These utility functions can be accessed through a variety of languages, including Python, Cpp, and Rust. (built on top of the ) can be a good example for someone who wants to build bindings for another language.
bindgen
is used to generate Rust FFI bindings to the CAPI are built on top of the CAPI in order to provide a safer, and more Rusty API. Although you can use bindgen
(as we did to build the Rust bindings) to generate the Rust FFI from the CAPI and use it as is, we will here show how to use the Rust API that is built on top of that, as it's easier to use.
We will use a really simple example for a demo, but the same steps can be done for any other circuit. example.mlir
will contain the MLIR below:
You can use the concretecompiler
binary to compile this MLIR program. Same can be done with concrete-python
, as we only need the compilation artifacts at the end.
You should be able to see artifacts listed in the rust-demo
directory
Now we want to use the Rust bindings in order to call the compiled circuit.
The main struct
to manage compilation artifacts is LibrarySypport
. You will have to create one with the path you used during compilation, then load the result of the compilation
Using the compilation result, you can load the server lambda (the entrypoint to the executable compiled circuit) as well as the client parameters (containing crypto parameters)
The client parameters will serve the client to generate keys and encrypt arguments for the circuit
Only evaluation keys are required for the execution of the circuit. You can execute the circuit on the encrypted arguments via server_lambda_call
At this point you have the encrypted result and can decrypt it using the keyset which holds the secret key
There is also a couple of tests in that can show how to both compile and run a circuit between a client and server using serialization.