FunctionEvaluator#
- class FunctionEvaluator#
This class is used to evaluate different available functions on CTiles, such as approximate inverse function.
- compare(self: pyhelayers.FunctionEvaluator, a: pyhelayers.CTile, b: pyhelayers.Tile, g_rep: int, f_rep: int, max_possible_abs_of_diff: float) pyhelayers.CTile #
Comparison of two numbers. This method compares two numbers a and b and return 1 if a>b, 0 if b<a and 0.5 if a==b by calculating sign(a-b).
- Parameters:
a (CTile) – First ciphertext to compare
b (CTile) – Second ciphertext to compare
g_rep (int) – How many repetitions of g(x) See further explanation in sign()
f_rep (int) – How many repetitions of f(x) See further explanation in sign()
max_possible_abs_of_diff (double) – An upper upper bound on |a-b|. The tighter this bound is, the more accurate the result will be.
- compareInPlace(self: pyhelayers.FunctionEvaluator, a: pyhelayers.CTile, b: pyhelayers.Tile, g_rep: int, f_rep: int, max_possible_abs_of_diff: float) None #
Comparison of two numbers. This method compares two numbers a and b and return 1 if a>b, 0 if b<a and 0.5 if a==b by calculating sign(a-b).
- Parameters:
a – First CTile to compare
b – Second Tile to compare
g_rep (int) – How many repetitions of g(x) See further explanation in sign()
f_rep (int) – How many repetitions of f(x) See further explanation in sign()
max_possible_abs_of_diff (double) – An upper upper bound on |a-b|. The tighter this bound is, the more accurate the result will be.
- inverse_positive(self: pyhelayers.FunctionEvaluator, src: pyhelayers.CTile, lower_bound: float, upper_bound: float, bit_resolution: int = 5) pyhelayers.CTile #
Calculates (an approximation of) 1/src. src must be between lower_bound and upper_bound, and lower_bound must be non-negative. The tighter the given bounds are, the more accurate the result will be. For more details, see “Homomorphic Encryption for Arithmetic of Approximate Numbers” paper, p. 15: https://eprint.iacr.org/2016/421.pdf.
- Parameters:
src (CTile) – Ciphertext to calculate its inverse.
lower_bound (double) – a lower bound on src. The tighter this bound is, the more accurate the result will be. This lower bound must be non-negative.
upper_bound (double) – an upper upper bound on src. The tighter this bound is, the more accurate the result will be.
bit_resolution – Controls the accuracy of the result. A higher bit_resolution value will increase the accuracy of the result at the account of consuming more multiplication depth. Defaults to 5.
- one_hot(self: pyhelayers.FunctionEvaluator, src: pyhelayers.CTile, possible_values: numpy.ndarray[numpy.float64]) pyhelayers.CTileVector #
Given ciphertext src and a list possible_values of the possible values inside src, calculates possible_values indicator ciphertexts that indicates which slots in src contain the corresponding value. Formally, the i’th indicator ciphertext contains 1 in slot j only if src[j]=possible_values[i], and 0 otherwise.
- Parameters:
src (CTile) – The source ciphertext
possible_values (list) – A list of doubles contain the possible values inside src
- sign(self: pyhelayers.FunctionEvaluator, a: pyhelayers.CTile, g_rep: int, f_rep: int, max_abs_val: float = 1, binary_res: bool = False) pyhelayers.CTile #
Returns the (approximated) sign of a. The sign is computed as the g(g(…g(f(f(…f(x)))))), where g(x) and f(x) are two degree 7 polynomials. The number of times g(x) and f(x) appear in the composition can be controlled by g_rep and f_rep arguments, respectively. a must be in the range [-max_abs_val, max_abs_val].
- Parameters:
a (CTile) – Ciphertext we want to find its sign. a must be in the range [-max_abs_val, max_abs_val].
g_rep (int) – How many repetitions of g(x)
f_rep (int) – How many repetitions of f(x)
max_abs_val (double) – An upper bound on the absolute value of a. Defaults to 1.
binary_res (boolean) – If true the result will be close to 0 when a < 0 and close to 1 when a > 0. If false, the result will be close to -1 when a < 0 and close to 1 when a > 0. Defaults to false.
-
class FunctionEvaluator#
This class is used to evaluate different available functions on CTiles, such as polynomial evaluation and sigmoid.
Public Functions
-
FunctionEvaluator(const HeContext &he, Verbosity verbosity = VERBOSITY_NONE)#
A constructor.
- Parameters:
he – The HeContext.
verbosity – The verbosity level.
-
~FunctionEvaluator()#
-
FunctionEvaluator(const NativeFunctionEvaluator &src) = delete#
-
FunctionEvaluator &operator=(const NativeFunctionEvaluator &src) = delete#
-
void polyEval(CTile &res, const CTile &src, const std::vector<double> &coefs, EvalType type = DEFAULT) const#
Polynomial evaluation.
Evaluates the given polynomial on input “src”, and stores the result in “res”.
- Parameters:
res – Where to store the result.
src – The input of the polynomial.
coefs – The coefficients of the polynomial. coefs[0] is the free coefficient.
type – The type of the evaluation algorithm. See also the documentation of “EvalType”.
- Throws:
runtime_error – If for each i, abs(coefs[i]) < (1 / he.getDefaultScale()).
-
void polyEvalInPlace(CTile &src, const std::vector<double> &coefs, EvalType type = DEFAULT) const#
Polynomial evaluation, in place.
Evaluates the given polynomial on input “src”, and stores the result in “src”.
- Parameters:
src – The input of the polynomial. This will contain the result of the evaluation at the end of the function execution.
coefs – The coefficients of the polynomial. coefs[0] is the free coefficient.
type – The type of the evaluation algorithm. See also the documentation of “EvalType”.
- Throws:
runtime_error – If for each i, abs(coefs[i]) < (1 / he.getDefaultScale()).
-
void polyEvalInPlace(CTile &src, const std::vector<CTile> &coefs, bool normalized = false, bool ignoreFreeCoef = false) const#
Polynomial evaluation, in place.
Evaluates the given polynomial on input “src”, and stores the result in “src”.
- Parameters:
src – The input of the polynomial. This will contain the result of the evaluation at the end of the function execution.
coefs – The (encrypted) coefficients of the polynomial. coefs[0] is the free coefficient.
normalized – If false, the polynomial to evaluate will be composed from the coefficients in “coeffs” vector only. Otherwise, an extra term, whose coefficient is 1 and whose power is coefs.size(), will be added.
ignoreFreeCoef – If true, the first coefficient in “coefs” array is not added to the final result.
-
void compositePolynomialEvaluation(CTile &res, const CTile &src, const std::vector<std::vector<double>> &polys, EvalType type = DEFAULT) const#
Composite polynomial evaluation.
Evaluates the composition of the given polynomials on the given “src”, and stores the result in “res”.
- Parameters:
res – Where to store the result.
src – The input of the composite polynomial.
polys – The coefficients of the composition polynomials. polys[0] is evaluated first, and polys[i][0] is the free coefficient of the i-th polynomial.
type – The type of the evaluation algorithm. See also the documentation of “EvalType”.
- Throws:
runtime_error – If for there exists an i s.t. for each j, abs(polys[i][j]) < (1 / he.getDefaultScale()).
-
void compositePolynomialEvaluationInPlace(CTile &src, const std::vector<std::vector<double>> &polys, EvalType type = DEFAULT) const#
Composite polynomial evaluation, in place.
Evaluates the composition of the given polynomials on the given “src”, and stores the result in “src”.
- Parameters:
src – The input of the composite polynomial. This will contain the result of the evaluation at the end of the function execution.
polys – The coefficients of the composition polynomials. polys[0] is evaluated first, and polys[i][0] is the free coefficient of the i-th polynomial.
type – The type of the evaluation algorithm. See also the documentation of “EvalType”.
- Throws:
runtime_error – If for there exists an i s.t. for each j, abs(polys[i][j]) < (1 / he.getDefaultScale()).
-
void pow(CTile &res, const CTile &src, int degree) const#
Computes src^degree, and stores the result into res.
- Parameters:
res – Where the result should be stored.
src – To compute its power.
degree – The required exponent.
-
void powInPlace(CTile &c, int degree, bool nonLinearizedMonomials = false, bool nonScaledMonomials = false, int CI = 0) const#
Computes c^degree, in place.
- Parameters:
c – Cipher to compute its power. The result will be stored here.
degree – The required exponent.
nonLinearizedMonomials – If true the result is not linearized
nonScaledMonomials – If true the result is not scaled
CI – If not 0 the result’s chain index set to CI
-
void sigmoid(CTile &res, const CTile &src, SigmoidType type) const#
A polynomial approximation of sigmoid.
- Parameters:
res – Where to store the result.
src – Ciphertext to calculate its sigmoid.
type – Specifies the degree of the approximating polynomial. See also SigmoidType documentation.
-
void sigmoidInPlace(CTile &src, SigmoidType type) const#
A polynomial approximation of sigmoid, in place.
- Parameters:
src – Ciphertext to calculate its sigmoid. The result will be stored here.
type – Specifies the degree of the approximating polynomial. See also SigmoidType documentation.
-
CTile abs(const CTile &a, int gRep, int fRep, double maxPossibleAbsValue)#
Calculates absolute value.
This method return the absolute value of a cipher text. maxPossibleValue is the maximum of the absolute value of the possible value of a.
- Parameters:
a – [in] Cipher to calculate absolute for.
gRep – [in] How many repetitions of g(x)
fRep – [in] How many repetitions of f(x)
maxPossibleAbsValue – [in] The maximum absolute value possible for a.
- Returns:
Result of the function.
-
void compareInPlace(CTile &a, const Tile &b, int gRep, int fRep, double maxPossibleAbsOfDiff) const#
Comparison of two numbers.
This method compares two numbers a and b and placed the result in a. The result is 1 if a>b, 0 if b<a and 0.5 if a==b by calculating approximation of sign(a-b).
- Parameters:
a – [in] First CTile to compare and output argument
b – [in] Second Tile to compare
gRep – [in] How many repetitions of g(x). See further explanation in sign()
fRep – [in] How many repetitions of f(x). See further explanation in sign()
maxPossibleAbsOfDiff – [in] An upper upper bound on |a-b|. The tighter this bound is, the more accurate the result will be.
-
CTile compare(const CTile &a, const Tile &b, int gRep, int fRep, double maxPossibleAbsOfDiff) const#
Comparison of two numbers.
This method compares two numbers a and b and return 1 if a>b, 0 if b<a and 0.5 if a==b.
- Parameters:
a – First ciphertext to compare
b – Second ciphertext to compare
gRep – How many repetitions of g(x). See further explanation in sign().
fRep – How many repetitions of f(x). See further explanation in sign().
maxPossibleAbsOfDiff – An upper upper bound on |a-b|. The tighter this bound is, the more accurate the result will be.
-
void min(CTile &minRes, CTile &minIndicator, const CTile &a, const CTile &b, int gRep, int fRep, double maxDiff = 1, bool delayMulByHalf = false) const#
Computes min(a,b), stores it in minRes and stores the minimum indicator in “minIndicator” (1 if a < b and 0 otherwise).
- Parameters:
minRes – min(a,b) will be stored here.
minIndicator – The resulting minimum indicator. This will contain an encryption of 1 if a < b and 0 otherwise.
a – The first CTile for the minimum computation.
b – The second CTile for the minimum computation.
gRep, fRep – Determine the accuracy of the result. Greater gRep and fRep will result with a greater accuracy on the account of slower runtime and more multiplication depth.
maxDiff – The maximum possible absolute difference between a and b.
delayMulByHalf – If true, minRes will contain two times min(a,b) and the function will consume one less chain index in “minRes”.
-
void min(CTile &minRes, std::vector<CTile> &minIndicators, const std::vector<CTile> &cs, int gRep, int fRep, double maxDiff = 1) const#
Stores the minimum of “cs” CTiles in “minRes” and stores in minIndicators[i] a CTile containing 1 if cs[i] is the minimum and 0 otherwise.
- Parameters:
gRep, fRep – Determine the accuracy of the result. Greater gRep and fRep will result with a greater accuracy on the account of slower runtime and more multiplication depth.
maxDiff – The maximum possible absolute difference between two values in cs[i].
-
CTile sign(const CTile &a, int gRep, int fRep, double maxAbsVal = 1, bool binaryRes = false) const#
Returns the (approximated) sign of
a
.The sign is computed as the g(g(…g(f(f(…f(x)))))), where g(x) and f(x) are two degree 7 polynomials. The number of times g(x) and f(x) appear in the composition can be controlled by
gRep
andfRep
arguments, respectively.a
must be in the range [-maxAbsVal, maxAbsVal].- Parameters:
a – Ciphertext we want to find its sign. a must be in the range [-maxAbsVal, maxAbsVal].
gRep – How many repetitions of g(x).
fRep – How many repetitions of f(x).
maxAbsVal – An upper bound on the absolute value of a. Defaults to 1.
binaryRes – If true the result will be close to 0 when a < 0 and close to 1 when a > 0. If false, the result will be close to -1 when a < 0 and close to 1 when a > 0. Defaults to false.
-
CTile compositeRemezDepth12Sign(const CTile &src, double maxAbsVal = 1, bool binaryRes = false) const#
- Parameters:
src – Cipher text we want to find its sign. a must be in the range [-maxAbsVal, maxAbsVal].
maxAbsVal – An upper bound on the absolute value of src. Defaults to 1.
binaryRes – If true the result will be close to 0 when src < 0 and close to 1 when src > 0. If false, the result will be close to -1 when src < 0 and close to 1 when src > 0. Defaults to false.
-
void signInPlace(CTile &src, int gRep, int fRep, double maxAbsVal = 1, bool binaryRes = false) const#
Computes the (approximated) sign of
src
, in place.The sign is computed as the g(g(…g(f(f(…f(x)))))), where g(x) and f(x) are two degree 7 polynomials. The number of times g(x) and f(x) appear in the composition can be controlled by
gRep
andfRep
arguments, respectively.src
must be in the range [-maxAbsVal, maxAbsVal].- Parameters:
src – Cipher text we want to find its sign. a must be in the range [-maxAbsVal, maxAbsVal].
gRep – How many repetitions of g(x).
fRep – How many repetitions of f(x).
maxAbsVal – An upper bound on the absolute value of src. Defaults to 1.
binaryRes – If true the result will be close to 0 when src < 0 and close to 1 when src > 0. If false, the result will be close to -1 when src < 0 and close to 1 when src > 0. Defaults to false.
- Throws:
runtime_error – if gRep or fRep are negative, or if they are both 0.
-
void compositeRemezDepth12SignInPlace(CTile &src, double maxAbsVal = 1, bool binaryRes = false) const#
- Parameters:
src – Cipher text we want to find its sign. a must be in the range [-maxAbsVal, maxAbsVal].
maxAbsVal – An upper bound on the absolute value of src. Defaults to 1.
binaryRes – If true the result will be close to 0 when src < 0 and close to 1 when src > 0. If false, the result will be close to -1 when src < 0 and close to 1 when src > 0. Defaults to false.
-
void inversePositive(CTile &src, double lowerBound, double upperBound, int bitResolution = 5) const#
Calculates (an approximation of) 1/src.
src must be between lowerBound and upperBound. lowerBound must be non-negative and smaller or equal to upperBound, and upperBound must be strictly positive. The tighter the given bounds are, the more accurate the result will be.
- Parameters:
src – ciphertext to calculate its inverse.
lowerBound – a lower bound on src. The tighter this bound is, the more accurate the result will be. This lowerBound must be non-negative.
upperBound – an upper upperBound on src. The tighter this bound is, the more accurate the result will be.
bitResolution – Controls the accuracy of the result. A higher bitResolution value will increase the accuracy of the result at the account of consuming more multiplication depth.
- Throws:
invalid – argument if lowerBound is negative or larger than upperBound, or if upperBound is not positive.
-
void inversePositiveWithoutScaling(CTile &src, int bitResolution = 5) const#
Calculates (an approximation of) 1/src.
src must be in the range(0, 2), and the approximation becomes less accurates near the boundaries.
- Parameters:
src – ciphertext to calculate its inverse.
bitResolution – Controls the accuracy of the result. A higher bitResolution value will increase the accuracy of the result at the expense of consuming more multiplication depth.
-
void sqrt(CTile &src, int bitResolution) const#
Calculates (an approximation of) sqrt(src).
src must be in the range [0,1].
- Parameters:
src – To compute its square root.
bitResolution – Controls the accuracy of the result. A higher value results with a better accuracy at the account of higher multiplication depth and higher latency.
-
void oneHot(std::vector<CTile> &res, const CTile &src, const std::vector<double> &possibleValues) const#
Given ciphertext src and a vector possibleValues of the possible values inside src, calculates |possibleValues| indicator ciphertexts that indicates which slots in src contain the corresponding value.
Formally, the i’th indicator ciphertext contains 1 in slot j only if src[j]=possibleValues[i], and 0 otherwise.
- Parameters:
res – An empty vector of CTiles
src – The source ciphertext
possibleValues – A vector of doubles contain the possible values inside src
-
bool polyEvalNeedBs(const CTile &src, const std::vector<double> &coeffs, EvalType type) const#
Checks whether a bootstrapping will be required to complete the specified polynomial evaluation.
If a bootstrapping is required to evaluate the given polynomial on the given
src
ciphertext and using the given evaluation method, returns true. Otherwise, returns false.- Parameters:
src – The ciphertext to evaluate the polynomial on.
coeffs – The polynomial’s coefficients.
type – The polynomial evaluation method.
-
bool polyEvalNeedBs(const CTile &src, int numEncryptedCoefs, bool normalized) const#
Checks whether bootstrapping will be required to complete the specified polynomial evaluation.
If bootstrapping is required to evaluate the given polynomial on the given
src
ciphertext and using the given evaluation method, returns true. Otherwise, returns false.- Parameters:
src – The ciphertext to evaluate the polynomial on.
numEncryptedCoefs – The number of encrypted coefficients of the polynomial.
normalized – If false, the polynomial to evaluate will be composed from the coefficients in “coeffs” vector only. Otherwise, an extra term, whose coefficient is 1 and whose power is coefs.size(), will be added.
-
void polyEvalByRootsInPlace(CTile &src, const std::vector<std::complex<double>> &roots, double coef, DimInt blockSize = -1) const#
Polynomial evaluation using a polynomial’s roots, in place.
Refer to polyEvalByRoots() documentation for more details.
- Parameters:
src – The input of the polynomial. This will contain the result of the evaluation at the end of the function execution.
roots – The roots of the polynomial.
coef – The evaluated polynomial is coef * \prod(x-r_i) (where r_i are the roots).
blockSize – Before evaluation, each blockSize roots are expanded into one polynomial of degree blockSize. If -1, a blockSize of min(sqrt(n), 16) is used (where n is the number of roots). Defaults to -1. If a positive value is given, it must not be greater than 30.
- Throws:
invalid_argument – if blockSize is greater than 30.
-
void polyEvalByRoots(CTile &res, const CTile &src, const std::vector<std::complex<double>> &roots, double coef, DimInt blockSize = -1) const#
Polynomial evaluation using a polynomial’s roots.
If the roots vector is (r1, …, rn), then the evaluated polynomial is: coef * (x - r1) * … * (x - rn). This polynomial evaluation is usually more stable and faster than other polynomial evaluation methods, although in some cases it might consume one chain index more than the MIN_DEPTH polynomial evaluation (which can be applied by calling polyEval(type=MIN_DEPTH)). During the evaluation, each “blockSize” roots are expanded into one polynomial of degree blockSize. Then, all of the expanded blocks are evaluated separately and multiplied together. A blockSize of sqrt(n) results with the best runtime, while a smaller blockSize may decrease multiplication depth by 1 and slightly enhance stabilitiy.
- Parameters:
res – The result will be stored here.
src – The input of the polynomial.
roots – The roots of the polynomial.
coef – The evaluated polynomial is coef * \prod(x-r_i) (where r_i are the roots).
blockSize – Before evaluation, each blockSize roots are expanded into one polynomial of degree blockSize. If -1, a blockSize of min(sqrt(n), 16) is used (where n is the number of roots). Defaults to -1. If a positive value is given, it must not be greater than 30.
- Throws:
invalid_argument – If blockSize is greater than 30.
Public Static Functions
-
static int getPolyEvalMulDepth(const std::vector<double> &coefs, EvalType type)#
Returns the required multiplication depth to evaluate the given polynomial, using the given evaluation method.
- Parameters:
coefs – The polynomial’s coefficients.
type – The polynomial evaluation method.
-
static int getPolyEvalMulDepth(int numEncryptedCoefs, bool normalized)#
Returns the required multiplication depth to evaluate the described polynomial.
- Parameters:
numEncryptedCoefs – The number of encrypted coefficients of the polynomial
normalized – If false, the polynomial to evaluate will be composed from the coefficients in “coeffs” vector only. Otherwise, an extra term, whose coefficient is 1 and whose power is coefs.size(), will be added.
-
static inline double getSignAbsThreshold()#
Returns the maximal absolute input value which is supported by the sign polynomial approximation without the need of scaling the input to a smaller value.
-
static CTileTensor multiplyMany(const std::vector<CTileTensor> &cts)#
Returns the product of the given CTileTensors.
The multiplication depth consumed by this function is ceil(log(cst.size()))
- Parameters:
cts – The vector of CTileTensors to multiply
Public Static Attributes
-
static const double sig3Factor#
Coefficients of the polynomials used in the computation of degree 3/7/9 sigmoid approximation.
-
static const double sig7Factor#
-
static const double sig9Factor#
-
static const std::vector<double> sig3Coeffs#
-
static const std::vector<double> sig7Coeffs#
-
static const std::vector<double> sig9Coeffs#
-
static const std::vector<std::vector<double>> sign15Apolys#
Coefficients of the polynomials used in the computation of sign approximations composed of two degree 15 polynomials (A&B) each computed by roots with blocks of size 3, 4, 4, 4.
-
static const std::vector<std::vector<double>> sign15Bpolys#
Friends
- friend class TTFunctionEvaluator
-
FunctionEvaluator(const HeContext &he, Verbosity verbosity = VERBOSITY_NONE)#