function_evaluation.rst 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 `Installation guide <../getting-started/installation.md>`.
  23. 2. Go to the `examples` directory and make sure that the needed example is there - `function_evaluation.rs`.
  24. 3. Compile and run the Rust file:
  25. .. code-block:: sh
  26. rustc function_evaluation.rs -o function_evaluation
  27. ./function_evaluation
  28. This should output the results of the homomorphic computations to the console.
  29. How to choose a multiplicative depth
  30. -------------------------------------
  31. Each run of `EvalChebyshevFunction` requires a certain number of multiplications which depends on the input polynomial degree.
  32. We provide a table below to map polynomial degrees to multiplicative depths.
  33. +-------------+---------------------+
  34. | Degree | Multiplicative Depth|
  35. +=============+=====================+
  36. | 3-5 | 4 |
  37. +-------------+---------------------+
  38. | 6-13 | 5 |
  39. +-------------+---------------------+
  40. | 14-27 | 6 |
  41. +-------------+---------------------+
  42. | 28-59 | 7 |
  43. +-------------+---------------------+
  44. | 60-119 | 8 |
  45. +-------------+---------------------+
  46. | 120-247 | 9 |
  47. +-------------+---------------------+
  48. | 248-495 | 10 |
  49. +-------------+---------------------+
  50. | 496-1007 | 11 |
  51. +-------------+---------------------+
  52. | 1008-2031 | 12 |
  53. +-------------+---------------------+
  54. Note that for a range $(a, b) = (-1, 1)$, the multiplicative depth is 1 less than the depths listed in the table.