Skip to content

Commit

Permalink
Added error handling for EvalPoly and EvalChebyshev/Degree (#561)
Browse files Browse the repository at this point in the history
* Added fixes and error handling to EvalChebyshevCoefficients(), Degree() and EvalPoly()

* Changed a comment and replaced ssize_t with int32_t as ssize_t is not a part of standard C/C++

---------

Co-authored-by: Dmitriy Suponitskiy <dsuponitskiy@dualitytech.com>
  • Loading branch information
dsuponitskiy and dsuponitskiy-duality authored Oct 3, 2023
1 parent 67b5c52 commit 0a7f39c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/core/lib/math/chebyshev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

#include "math/chebyshev.h"
#include "utils/exception.h"

#include <cmath>
#include <cstdint>
Expand All @@ -44,6 +45,9 @@
namespace lbcrypto {

std::vector<double> EvalChebyshevCoefficients(std::function<double(double)> func, double a, double b, uint32_t degree) {
if (!degree) {
OPENFHE_THROW(config_error, "The degree of approximation can not be zero");
}
// the number of coefficients to be generated should be degree+1 as zero is also included
size_t coeffTotal{degree+1};
double bMinusA = 0.5 * (b - a);
Expand Down
6 changes: 3 additions & 3 deletions src/pke/include/scheme/ckksrns/ckksrns-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ struct longDiv {
};

/**
* Gets the degree of a polynomial specified by its coefficients.
*
* @param &coefficients vector of coefficients of a polynomial.
* @brief Gets the degree of a polynomial specified by its coefficients, which is the index of
* the last non-zero element in the coefficients. If all the coefficients are zero, it returns 0.
* @param coefficients vector of coefficients of a polynomial (can not be empty)
* @return the integer degree of the polynomial.
*/
uint32_t Degree(const std::vector<double>& coefficients);
Expand Down
11 changes: 10 additions & 1 deletion src/pke/lib/scheme/ckksrns/ckksrns-advancedshe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,16 @@ Ciphertext<DCRTPoly> AdvancedSHECKKSRNS::EvalPoly(ConstCiphertext<DCRTPoly> x,

Ciphertext<DCRTPoly> AdvancedSHECKKSRNS::EvalPolyLinear(ConstCiphertext<DCRTPoly> x,
const std::vector<double>& coefficients) const {
uint32_t k = coefficients.size() - 1;
const size_t coefficientsSize = coefficients.size();
if (coefficientsSize == 0) {
OPENFHE_THROW(math_error, "The coefficients vector can not be empty");
}

uint32_t k = coefficientsSize - 1;
if (k == 0) {
OPENFHE_THROW(math_error, "The coefficients vector should have, at least, 2 elements");
}

if (coefficients[k] == 0)
OPENFHE_THROW(math_error, "EvalPolyLinear: The highest-order coefficient cannot be set to 0.");

Expand Down
21 changes: 11 additions & 10 deletions src/pke/lib/scheme/ckksrns/ckksrns-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,20 @@ inline bool IsNotEqualOne(double val) {
return false;
}

/*Return the degree of the polynomial described by coefficients,
which is the index of the last non-zero element in the coefficients - 1.
Don't throw an error if all the coefficients are zero, but return 0. */
uint32_t Degree(const std::vector<double>& coefficients) {
uint32_t deg = 1;
for (int i = coefficients.size() - 1; i > 0; i--) {
if (coefficients[i] == 0) {
deg += 1;
}
else
const size_t coefficientsSize = coefficients.size();
if (!coefficientsSize) {
OPENFHE_THROW(math_error, "The coefficients vector can not be empty");
}

int32_t indx = coefficientsSize;
while (--indx >= 0) {
if (coefficients[indx])
break;
}
return coefficients.size() - deg;

// indx becomes negative (-1) only when all coefficients are zeroes. in this case we return 0
return static_cast<uint32_t>((indx < 0) ? 0 : indx);
}

/* f and g are vectors of coefficients of the two polynomials. We assume their dominant
Expand Down

0 comments on commit 0a7f39c

Please sign in to comment.