|
| 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