simple_integers.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Homomorphic additions, multiplications, and rotations for vectors of integers via BFV
  2. =======================================================================================
  3. Overview
  4. --------
  5. 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 <https://github.com/fairmath/openfhe-rs/blob/master/examples/simple_integers.rs>`_.
  6. Code breakdown
  7. --------------
  8. Importing libraries
  9. ~~~~~~~~~~~~~~~~~~
  10. We start by importing the necessary libraries and modules:
  11. .. code-block:: rust
  12. use openfhe::cxx::{CxxVector};
  13. use openfhe::ffi as ffi;
  14. The code example
  15. ~~~~~~~~~~~~~~~~
  16. The `main` function contains the entire workflow for setting up the BFV scheme, performing encryption, executing homomorphic operations, and decrypting the results.
  17. Generating Parameters
  18. ~~~~~~~~~~~~~~~~~~~~
  19. We define the cryptographic parameters for the BFV scheme, namely plaintext modulus and multiplicative depth.
  20. .. code-block:: rust
  21. let mut _cc_params_bfvrns = ffi::GenParamsBFVRNS();
  22. _cc_params_bfvrns.pin_mut().SetPlaintextModulus(65537);
  23. _cc_params_bfvrns.pin_mut().SetMultiplicativeDepth(2);
  24. Creating Crypto Context
  25. ~~~~~~~~~~~~~~~~~~~~~~~
  26. We create a crypto context based on the defined parameters and enable necessary features.
  27. .. code-block:: rust
  28. let _cc = ffi::DCRTPolyGenCryptoContextByParamsBFVRNS(&_cc_params_bfvrns);
  29. _cc.EnableByFeature(ffi::PKESchemeFeature::PKE);
  30. _cc.EnableByFeature(ffi::PKESchemeFeature::KEYSWITCH);
  31. _cc.EnableByFeature(ffi::PKESchemeFeature::LEVELEDSHE);
  32. Key Generation
  33. ~~~~~~~~~~~~~~
  34. We generate the necessary keys for encryption, including evaluation keys for multiplication and rotation.
  35. .. code-block:: rust
  36. let _key_pair = _cc.KeyGen();
  37. _cc.EvalMultKeyGen(&_key_pair.GetPrivateKey());
  38. let mut _index_list = CxxVector::<i32>::new();
  39. _index_list.pin_mut().push(1);
  40. _index_list.pin_mut().push(2);
  41. _index_list.pin_mut().push(-1);
  42. _index_list.pin_mut().push(-2);
  43. _cc.EvalRotateKeyGen(&_key_pair.GetPrivateKey(), &_index_list, &ffi::DCRTPolyGenNullPublicKey());
  44. Plaintext Vector Creation
  45. ~~~~~~~~~~~~~~~~~~~~~~~~~
  46. .. code-block:: rust
  47. let mut _vector_of_ints_1 = CxxVector::<i64>::new();
  48. _vector_of_ints_1.pin_mut().push(1);
  49. _vector_of_ints_1.pin_mut().push(2);
  50. ...
  51. let _plain_text_1 = _cc.MakePackedPlaintext(&_vector_of_ints_1, 1, 0);
  52. Encrypting Plaintext Vectors
  53. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  54. We encrypt the plaintext vectors using the generated public key.
  55. .. code-block:: rust
  56. let _cipher_text_1 = _cc.EncryptByPublicKey(&_key_pair.GetPublicKey(), &_plain_text_1);
  57. Performing Homomorphic Operations
  58. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. We perform various homomorphic operations on the encrypted data, including addition, multiplication, and rotations.
  60. .. code-block:: rust
  61. let _cipher_text_add_1_2 = _cc.EvalAddByCiphertexts(&_cipher_text_1, &_cipher_text_2);
  62. let _cipher_text_mult_result = _cc.EvalMultByCiphertexts(&_cipher_text_mul_1_2, &_cipher_text_3);
  63. let _cipher_text_rot_1 = _cc.EvalRotate(&_cipher_text_1, 1);
  64. Decrypting and Printing Results
  65. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  66. Finally, we decrypt the results of the homomorphic computations and print them.
  67. .. code-block:: rust
  68. let mut _plain_text_add_result = ffi::GenNullPlainText();
  69. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_cipher_text_add_result, _plain_text_add_result.pin_mut());
  70. println!("Plaintext #1: {}", _plain_text_1.GetString());
  71. Running the example
  72. ~~~~~~~~~~~~~~~~~~~~
  73. 1. Ensure the `openfhe-rs` library is installed and properly configured, see the :doc:`intro` section.
  74. 2. Go to the `openfhe-rs` directory.
  75. 3. Compile and run the `simple_integers.rs` example:
  76. .. code-block:: sh
  77. cargo run --example simple_integers
  78. This should output the results of the homomorphic computations to the console.