Skip to content

Commit 9a7cf7c

Browse files
docs: installation, types and algorithms; int and real examples
1 parent 753597b commit 9a7cf7c

8 files changed

Lines changed: 399 additions & 67 deletions

File tree

docs/source/api.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.

docs/source/base_types_and_alg.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Base types and algorithms
2+
=========================
3+
4+
There are a couple of base types used around all the FHE-enabled applications. You can check this section or jump directly into our examples and figure out everything through experience.
5+
6+
Remember: if you have any questions not covered on this page - feel free to ping us on our `Discord server <https://discord.com/invite/NfhXwyr9M5>`_.
7+
8+
Cryptography schemes supported
9+
-------------------------------
10+
11+
We currently support three so-called PKE schemes:
12+
13+
- CKKS
14+
- BFV
15+
- BGV
16+
17+
Many of our methods and algorithms contain the suffix referring to the scheme used. This section provides examples.
18+
19+
Params
20+
------
21+
22+
The Params policy type stores flags and parameters for the FHE algorithms to use.
23+
24+
There are different ways to generate a Params type instance, but the main way is to create a mutable object by calling ``GetParams%scheme_name%`` and setting the options after that. For the CKKS scheme, it will be ``GetParamsCKKSRNS``.
25+
26+
CryptoContext
27+
-------------
28+
29+
CryptoContext type, as its name stands, stores the metadata of your FHE context. You usually create its instance using one of the following functions:
30+
31+
- By generating the CryptoContext directly - ``ffi::GenCryptoContextByParamsCKKSRNS``
32+
- By deserializing a CryptoContext generated somewhere else - ``ffi::DeserializeCryptoContextFromFile``
33+

docs/source/index.rst

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
Welcome to OpenFHE Rust wrapper's documentation!
22
===================================
33

4+
OpenFHE
5+
--------
6+
7+
Fully Homomorphic Encryption (FHE) is a powerful cryptographic primitive that enables performing computations over encrypted data without having access to the secret key.
8+
OpenFHE is an open-source FHE library that includes efficient implementations of all common FHE schemes:
9+
- Brakerski/Fan-Vercauteren (BFV) scheme for integer arithmetic
10+
- Brakerski-Gentry-Vaikuntanathan (BGV) scheme for integer arithmetic
11+
- Cheon-Kim-Kim-Song (CKKS) scheme for real-number arithmetic (includes approximate bootstrapping)
12+
- Ducas-Micciancio (DM/FHEW) and Chillotti-Gama-Georgieva-Izabachene (CGGI/TFHE), and Lee-Micciancio-Kim-Choi-Deryabin-Eom-Yoo (LMKCDEY) schemes for evaluating Boolean circuits and arbitrary functions over larger plaintext spaces using lookup tables
13+
414
About OpenFHE-rs
515
================
616

7-
☀️ *OpenFHE-rs is a joint project by `FairMath <https://fairmath.xyz/>`__ & `OpenFHE <https://www.openfhe.org/>`__.*
8-
9-
---
17+
OpenFHE-rs is a joint project by `Fair Math <https://fairmath.xyz/>`_ & `OpenFHE <https://www.openfhe.org/>`_
1018

1119
.. image:: https://img.shields.io/discord/1163764915803279360?logo=discord&label=Fair%20Math
1220
:target: https://discord.com/invite/NfhXwyr9M5
1321

1422
.. image:: https://img.shields.io/twitter/follow/FairMath
1523
:target: https://twitter.com/FairMath
1624

17-
---
25+
.. note::
26+
27+
🔔 Keep in mind that the library is WIP and may contain some unpolished interfaces. If you encounter any issues or have any suggestions, feel free to ping us on our Discord server or open a new issue in the `GitHub repository <https://github.com/fairmath/openfhe-rs/tree/master>`_.
1828

1929
OpenFHE-rs is a Rust interface for the OpenFHE library, which is renowned for its comprehensive suite of Fully Homomorphic Encryption (FHE) schemes, all implemented in C++. By providing a Rust wrapper for OpenFHE, we aim to make these advanced FHE capabilities easily accessible to Rust developers.
2030

2131
Whether you're developing secure data processing applications or privacy-focused tools, OpenFHE-rs enables you to leverage the powerful encryption technologies of OpenFHE seamlessly within your Rust projects.
2232

23-
24-
25-
Check out the :doc:`intro` section for further information, including installation instructions.
26-
27-
.. note::
28-
29-
🔔 *Keep in mind that the library is WIP and may contain some unpolished interfaces. If you encounter any issues or have any suggestions, feel free to ping us on our Discord server or open a new issue in the `GitHub repository <https://github.com/fairmath/openfhe-rs/tree/master>`__.*
33+
Check out the :doc:`intro` section for quick start information, including installation instructions.
3034

3135
Contents
3236
--------
3337

3438
.. toctree::
3539

36-
usage
37-
api
38-
examples
40+
intro
41+
limitations
42+
base_types_and_alg
43+
simple_integers
44+
simple_real_numbers

docs/source/intro.rst

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
21
About OpenFHE-rs
32
================
43

5-
☀️ *OpenFHE-rs is a joint project by `FairMath <https://fairmath.xyz/>`__ & `OpenFHE <https://www.openfhe.org/>`__.*
6-
7-
---
4+
OpenFHE-rs is a joint project by `Fair Math <https://fairmath.xyz/>`_ & `OpenFHE <https://www.openfhe.org/>`_
85

9-
🔔 *Keep in mind that the library is WIP and may contain some unpolished interfaces. If you encounter any issues or have any suggestions, feel free to ping us on our Discord server or open a new issue in the `GitHub repository <https://github.com/fairmath/openfhe-rs/tree/master>`__.*
6+
.. note::
107

11-
---
8+
🔔 Keep in mind that the library is WIP and may contain some unpolished interfaces. If you encounter any issues or have any suggestions, feel free to ping us on our Discord server or open a new issue in the `GitHub repository <https://github.com/fairmath/openfhe-rs/tree/master>`_.
129

1310
OpenFHE-rs is a Rust interface for the OpenFHE library, which is renowned for its comprehensive suite of Fully Homomorphic Encryption (FHE) schemes, all implemented in C++. By providing a Rust wrapper for OpenFHE, we aim to make these advanced FHE capabilities easily accessible to Rust developers.
1411

@@ -44,7 +41,7 @@ Installation process
4441
Core OpenFHE library installation
4542
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4643

47-
To build and install the OpenFHE library, follow the steps below or refer to `OpenFHE's installation documentation <https://openfhe-development.readthedocs.io/en/latest/sphinx_rsts/intro/installation/installation.html>`__.
44+
To build and install the OpenFHE library, follow the steps below or refer to `OpenFHE's installation documentation <https://openfhe-development.readthedocs.io/en/latest/sphinx_rsts/intro/installation/installation.html>`_.
4845

4946
1. Clone the repository
5047

@@ -75,7 +72,7 @@ To build and install the OpenFHE library, follow the steps below or refer to `Op
7572
Configuring your project to use the crate
7673
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7774

78-
To use the OpenFHE crate in your Rust project, add it as a dependency from `crates.io <https://crates.io/crates/openfhe>`__:
75+
To use the OpenFHE crate in your Rust project, add it as a dependency from `crates.io <https://crates.io/crates/openfhe>`_:
7976

8077
.. code-block:: bash
8178
@@ -101,12 +98,12 @@ You also need to add a small piece of code for the core dependencies' configurat
10198
Template repository
10299
~~~~~~~~~~~~~~~~~~~
103100

104-
Instead of doing it manually, you can start your project by forking our `template repository <https://github.com/fairmath/openfhe-rs-template/tree/main>`__.
101+
Instead of doing it manually, you can start your project by forking our `template repository <https://github.com/fairmath/openfhe-rs-template/tree/main>`_.
105102

106103
Custom crate installation from the source
107104
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108105

109-
You can adjust the installation process by building the crate manually. In that case, you need to clone the Fair Math's `openfhe-rs <https://github.com/fairmath/openfhe-rs>`__ repo to your local machine and build it:
106+
You can adjust the installation process by building the crate manually. In that case, you need to clone the Fair Math's `openfhe-rs <https://github.com/fairmath/openfhe-rs>`_ repo to your local machine and build it:
110107

111108
1. Clone the repository
112109

@@ -139,10 +136,10 @@ You can adjust the installation process by building the crate manually. In that
139136
Contributing
140137
============
141138

142-
Contributions are always welcome! If you find bugs, have feature requests, or want to contribute code, please open an issue or pull request on the `GitHub repository <https://github.com/fairmath/openfhe-rs/tree/master>`__.
139+
Contributions are always welcome! If you find bugs, have feature requests, or want to contribute code, please open an issue or pull request on the `GitHub repository <https://github.com/fairmath/openfhe-rs/tree/master>`_.
143140

144141
License
145142
=======
146143

147-
`OpenFHE-rs` is licensed under the **BSD 2-Clause License**. See the `LICENSE <LICENSE>`__ file for more details.
144+
`OpenFHE-rs` is licensed under the **BSD 2-Clause License**. See the `LICENSE <LICENSE>`_ file for more details.
148145

docs/source/limitations.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Limitations
2+
===========
3+
4+
OpenFHE
5+
-------
6+
7+
See the `Security Notes for Homomorphic Encryption <https://openfhe-development.readthedocs.io/en/latest/sphinx_rsts/intro/security.html>`_ in OpenFHE's documentation.
8+
9+
Rust wrapper
10+
------------
11+
12+
The library is WIP and may contain some unpolished interfaces. If you struggle with anything or have suggestions, feel free to ping us on our Discord server or open a new issue in the `GitHub repository <https://github.com/fairmath/openfhe-rs/tree/master>`_.
13+
14+
At the moment, only a certain set of OpenFHE functionality has been implemented.
15+
16+
We use the `CXX crate <https://cxx.rs/>`_ for our Rust binding to reduce the amount of abstraction levels. OpenFHE types are represented as opaque types on the Rust side using `cxx::UniquePtr <https://docs.rs/cxx/latest/cxx/struct.UniquePtr.html>`_ from the CXX crate. Instead of the usual `std::vec <https://doc.rust-lang.org/std/vec/>`_, we use `cxx::CxxVector <https://docs.rs/cxx/latest/cxx/struct.CxxVector.html>`_ for primitive types, and also `CxxVector<ComplexPair>` for representing `std::vector<std::complex<double>>` type.
17+
18+
The main difference lies in the current `cxx::CxxVector` supported functional, limited compared to `std::vector`, e.g. you need to init `cxx::CxxVector` element by element. Since `cxx` currently does not support `cxx::CxxVector` of opaque types, we are using a separate type for each vector of opaque type. Since `cxx` currently does not support alternatives for `std::unordered_map` and `std::map`, we are using separate types for them. The implementations of these types are very simple because we are planning to switch `cxx` variants further. But before switching these types can be extended to support basic functional of `std::vector`, `std::map` and `std::unordered_map` if required.
19+
20+
Since Rust does not support default parameters and function overloading, default parameters are mentioned in comments after the corresponding parameter name, and overloaded functions are represented by slightly different names. Calling member functions of generated null types (generated by GenNull... functions) results in undefined behavior. Since `CXX` crate has limited support for function pointers - only functions without a return type are supported) - EvalChebychevFunction expects `fn(f64, ret: &mut f64)` as the callable object.
21+

docs/source/simple_integers.rst

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Homomorphic additions, multiplications, and rotations for vectors of integers via BFV
2+
=======================================================================================
3+
4+
Overview
5+
--------
6+
7+
This Rust example demonstrates basic homomorphic encryption operations such as addition, multiplication, and rotation on vectors of integers using the BFVrns3 scheme provided by the `openfhe` library. The example walks through the setup of cryptographic parameters, key generation, encryption of plaintext vectors, performing homomorphic operations, and decrypting the results. The example for this code is located in :code:`examples/simple_integers.rs`.
8+
9+
Code breakdown
10+
--------------
11+
12+
Importing libraries
13+
~~~~~~~~~~~~~~~~~~
14+
15+
We start by importing the necessary libraries and modules:
16+
17+
.. code-block:: rust
18+
19+
use openfhe::cxx::{CxxVector};
20+
use openfhe::ffi as ffi;
21+
22+
The code example
23+
~~~~~~~~~~~~~~~~
24+
25+
The `main` function contains the entire workflow for setting up the BFV scheme, performing encryption, executing homomorphic operations, and decrypting the results.
26+
27+
Generating Parameters
28+
~~~~~~~~~~~~~~~~~~~~
29+
30+
We define the cryptographic parameters for the BFV scheme, namely plaintext modulus and multiplicative depth.
31+
32+
.. code-block:: rust
33+
34+
let mut _cc_params_bfvrns = ffi::GenParamsBFVRNS();
35+
_cc_params_bfvrns.pin_mut().SetPlaintextModulus(65537);
36+
_cc_params_bfvrns.pin_mut().SetMultiplicativeDepth(2);
37+
38+
Creating Crypto Context
39+
~~~~~~~~~~~~~~~~~~~~~~~
40+
41+
We create a crypto context based on the defined parameters and enable necessary features.
42+
43+
.. code-block:: rust
44+
45+
let _cc = ffi::GenCryptoContextByParamsBFVRNS(&_cc_params_bfvrns);
46+
_cc.Enable(ffi::PKESchemeFeature::PKE);
47+
_cc.Enable(ffi::PKESchemeFeature::KEYSWITCH);
48+
_cc.Enable(ffi::PKESchemeFeature::LEVELEDSHE);
49+
50+
Key Generation
51+
~~~~~~~~~~~~~~
52+
53+
We generate the necessary keys for encryption, including evaluation keys for multiplication and rotation.
54+
55+
.. code-block:: rust
56+
57+
let _key_pair = _cc.KeyGen();
58+
_cc.EvalMultKeyGen(&_key_pair.GetPrivateKey());
59+
60+
let mut _index_list = CxxVector::<i32>::new();
61+
_index_list.pin_mut().push(1);
62+
_index_list.pin_mut().push(2);
63+
_index_list.pin_mut().push(-1);
64+
_index_list.pin_mut().push(-2);
65+
_cc.EvalRotateKeyGen(&_key_pair.GetPrivateKey(), &_index_list, &ffi::GenNullPublicKey());
66+
67+
Plaintext Vector Creation
68+
~~~~~~~~~~~~~~~~~~~~~~~~~
69+
70+
.. code-block:: rust
71+
72+
let mut _vector_of_ints_1 = CxxVector::<i64>::new();
73+
_vector_of_ints_1.pin_mut().push(1);
74+
_vector_of_ints_1.pin_mut().push(2);
75+
...
76+
let _plain_text_1 = _cc.MakePackedPlaintext(&_vector_of_ints_1, 1, 0);
77+
78+
Encrypting Plaintext Vectors
79+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80+
81+
We encrypt the plaintext vectors using the generated public key.
82+
83+
.. code-block:: rust
84+
85+
let _cipher_text_1 = _cc.EncryptByPublicKey(&_key_pair.GetPublicKey(), &_plain_text_1);
86+
87+
Performing Homomorphic Operations
88+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
We perform various homomorphic operations on the encrypted data, including addition, multiplication, and rotations.
91+
92+
.. code-block:: rust
93+
94+
let _cipher_text_add_1_2 = _cc.EvalAddByCiphertexts(&_cipher_text_1, &_cipher_text_2);
95+
let _cipher_text_mult_result = _cc.EvalMultByCiphertexts(&_cipher_text_mul_1_2, &_cipher_text_3);
96+
let _cipher_text_rot_1 = _cc.EvalRotate(&_cipher_text_1, 1);
97+
98+
Decrypting and Printing Results
99+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100+
101+
Finally, we decrypt the results of the homomorphic computations and print them.
102+
103+
.. code-block:: rust
104+
105+
let mut _plain_text_add_result = ffi::GenNullPlainText();
106+
_cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_cipher_text_add_result, _plain_text_add_result.pin_mut());
107+
println!("Plaintext #1: {}", _plain_text_1.GetString());
108+
109+
Running the example
110+
~~~~~~~~~~~~~~~~~~~~
111+
112+
1. Ensure the `openfhe-rs` library is installed and properly configured, see the :doc:`intro` section.
113+
2. Go to the `examples` directory and make sure that the needed example is there - `simple_integers.rs`.
114+
3. Compile and run the Rust file:
115+
116+
.. code-block:: sh
117+
118+
rustc simple_integers.rs -o simple_integers
119+
./simple_integers
120+
121+
This should output the results of the homomorphic computations to the console.

0 commit comments

Comments
 (0)