function_evaluation.rst 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Arbitrary smooth function evaluation in CKKS via OpenFHE-rs
  2. ===========================================================
  3. Overview
  4. --------
  5. This document describes how to evaluate an arbitrary smooth function on a ciphertext in CKKS using `Chebyshev approximation <https://www.gnu.org/software/gsl/doc/html/cheb.html>`_.
  6. The Chebyshev approximation is a method of approximating a smooth function using polynomials, see more `on Wiki <https://en.wikipedia.org/wiki/Chebyshev_polynomials>`_.
  7. Rust example
  8. ------------
  9. The example for this code is located in `examples/function_evaluation.rs <https://github.com/fairmath/openfhe-rs/blob/master/examples/function_evaluation.rs>`_.
  10. The file gives examples of how to run `EvalLogistic`, the logistic function $\frac{1}{1 + e^{-x}}$, and an arbitrary function using `EvalChebyshevFunction`.
  11. We use the square root function in our example for `EvalChebyshevFunction`.
  12. Input parameters
  13. ----------------
  14. Our Rust wrapper is based on the original OpenFHE interface; `EvalLogistic` function requires the following input parameters:
  15. - `ciphertext`: the ciphertext we wish to operate on.
  16. - `a`: the lower bound of underlying plaintext values we could have.
  17. - `b`: the upper bound of underlying plaintext values we could have.
  18. - `degree`: the desired polynomial degree of the Chebyshev approximation.
  19. A higher degree gives a more precise estimate but takes longer to run.
  20. Running the example
  21. ~~~~~~~~~~~~~~~~~~~~
  22. 1. Ensure the `openfhe-rs` library is installed and properly configured, see the :doc:`intro` section.
  23. 2. Go to the `openfhe-rs` directory.
  24. 3. Compile and run the `function_evaluation.rs` example:
  25. .. code-block:: sh
  26. cargo run --example function_evaluation
  27. This should output the results of the homomorphic computations to the console.
  28. How to choose a multiplicative depth
  29. -------------------------------------
  30. Each run of `EvalChebyshevFunction` requires a certain number of multiplications which depends on the input polynomial degree.
  31. We provide a table below to map polynomial degrees to multiplicative depths.
  32. +-------------+---------------------+
  33. | Degree | Multiplicative Depth|
  34. +=============+=====================+
  35. | 3-5 | 4 |
  36. +-------------+---------------------+
  37. | 6-13 | 5 |
  38. +-------------+---------------------+
  39. | 14-27 | 6 |
  40. +-------------+---------------------+
  41. | 28-59 | 7 |
  42. +-------------+---------------------+
  43. | 60-119 | 8 |
  44. +-------------+---------------------+
  45. | 120-247 | 9 |
  46. +-------------+---------------------+
  47. | 248-495 | 10 |
  48. +-------------+---------------------+
  49. | 496-1007 | 11 |
  50. +-------------+---------------------+
  51. | 1008-2031 | 12 |
  52. +-------------+---------------------+
  53. Note that for a range $(a, b) = (-1, 1)$, the multiplicative depth is 1 less than the depths listed in the table.