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(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 consumig 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()#
-
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 documnetation 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 documnetation 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 polyCompEval(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 documnetation of “EvalType”.
- Throws:
runtime_error – If for there exists an i s.t. for each j, abs(polys[i][j]) < (1 / he.getDefaultScale()).
-
void polyCompEvalInPlace(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 documnetation 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) const#
Computes src^degree, in place.
- Parameters:
src – To compute its power. The result will be stored here.
degree – The required exponent.
-
void sigmoid3(CTile &res, const CTile &src) const#
An approximation of sigmoid by an order 3 polynomial.
- Parameters:
res – [out] Result of the function.
src – [in] Cipher to calculate its sigmoid.
-
void sigmoid3InPlace(CTile &src) const#
An approximation of sigmoid by an order 3 polynomial, in place.
- Parameters:
src – [inout] Cipher to calculate its sigmoid. This will contain the sigmoid result at the end of the function execution.
-
void sigmoid7(CTile &res, const CTile &src) const#
An approximation of sigmoid by an order 7 polynomial.
- Parameters:
res – [out] Result of the function.
src – [in] Cipher to calculate its sigmoid.
-
void sigmoid7InPlace(CTile &src) const#
An approximation of sigmoid by an order 7 polynomial, in place.
- Parameters:
src – Cipher to calculate its sigmoid. This will contain the sigmoid result at the end of the function execution.
-
void sigmoid9(CTile &res, const CTile &src) const#
An approximation of sigmoid by an order 9 polynomial.
- Parameters:
res – Cipher to calculate its sigmoid. This will contain the sigmoid result at the end of the function execution.
-
void sigmoid9InPlace(CTile &src) const#
An approximation of sigmoid by an order 9 polynomial, in place.
- Parameters:
src – Cipher to calculate its sigmoid. This will contain the sigmoid result at the end of the function execution.
-
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:
res – [out] result of the function.
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.
-
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 contianing 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.
-
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 efficientPowersPolyEvalInPlace(CTile &src, const std::vector<double> &coefs) const#
Calculates a polynomial at a point using Exponentiation by squaring.
- Parameters:
src – [out] polynomial value at the point (encrypted)
coefs – [in] polynomial coeefficents
-
void minDepthPolyEvalInPlace(CTile &src, const std::vector<double> &coefs) const#
Calculates a polynomial at a point using Exponentiation by squaring.
Changes the multiplications order in some cases to reduce multiplication depth. However, this comes at the expense of increasing the total number of multiplications
- Parameters:
src – polynomial value at the point (encrypted)
coefs – polynomial coefficents
-
void numericalStabilityPolyEvalInPlace(CTile &src, const std::vector<double> &coefs) const#
Evaluates a polynomial at a point while focusing on numerical stability.
This polynomial evaluation verison is recommended for use with high degree polynomials, or with polynomials that have very large or very small coefficients.
- Parameters:
src – A CTile encrypting the polynomial inputs. The result will be stored here.
coefs – The polynomial coefficients.
-
void inverse(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 consumig more multiplication depth.
- Throws:
invalid – argument if lowerBound is negative or larger than upperBound, or if upperBound is not positive.
-
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
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 coeffiecients.
type – The polynomial evaluation method.
-
static int getPolyEvalMulDepth(const std::vector<CTile> &coefs, bool normalized)#
Returns the required multiplication depth to evaluate the given polynomial.
- Parameters:
coefs – The polynomial’s coeffiecients.
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 sigmoid3(), sigmoid7() and sigmoid9()
-
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#
-
~FunctionEvaluator()#