Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error handling for EvalPoly and EvalChebyshev/Degree #561

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading