Rust integration

This document explains how to use Fully Homomorphic Encryption (FHE) modules developed with concrete-python directly in Rust programs using the Concrete toolchain.

This workflow enables rapid prototyping in Python and seamless deployment in Rust, combining the flexibility of Python with the safety and performance of Rust.

Overview

  • Write and compile FHE modules in Python using concrete-python.

  • Import the compiled module artifact into Rust using the concrete-macro crate.

  • Use the generated Rust APIs for encryption, evaluation, and decryption.

Prerequisites

  • Python 3.8+

  • Rust 1.70+

  • concrete-python (>=2.10)

  • concrete and concrete-macro Rust crates (>=2.10.1-rc1)

Regular example

Step 1: Define and Compile a Module in Python

Write your FHE logic in Python and compile it to an artifact compatible with the Rust toolchain. Here is an example of a small and simple module:

This produces a MyModule.zip artifact containing the compiled FHE module.

Step 2: Set Up the Rust Project

Initialize a new Rust project and add the required dependencies.

Place the MyModule.zip artifact in your project directory.

Step 3: Import the Python-Compiled Module in Rust

Use the concrete_macro::from_concrete_python_export_zip! macro to import the module at build time.

This macro unpacks the artifact, triggers recompilation, reads metadata, and generates Rust APIs for the module's functions.

Step 4: Use the Module in Rust

You can now use the FHE functions in Rust. The following example demonstrates a full FHE workflow:

TFHE-rs Ciphertext Interoperability

Starting from Concrete v2.11, you can define and use modules that operate directly on TFHE-rs ciphertexts, enabling seamless interoperability between Concrete and TFHE-rs in Rust.

Step 1: Define and Compile a Module with TFHE-rs Types in Python

You can define a module in Python that uses TFHE-rs integer types as arguments and outputs. For example:

This produces a test_tfhers.zip artifact compatible with Rust and TFHE-rs.

Step 2: Use the Module with TFHE-rs Ciphertexts in Rust

You can import and use the module in Rust, passing and receiving native TFHE-rs ciphertexts:

This workflow allows you to combine the high-level graph optimizations of Concrete with the operator-level flexibility of TFHE-rs, all within Rust.

Notes

  • The module must be compiled with via_mlir=True to be loaded in the Rust program.

  • The Rust API is currently in beta and may evolve in future releases.

  • The Python and Rust environments must use compatible versions of the Concrete toolchain.

  • When using TFHE-rs ciphertext interoperability, ensure that the TFHE-rs client key used for encryption matches the one registered in the Concrete keyset for the corresponding argument.

Last updated

Was this helpful?