diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-14 14:57:42 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-14 14:57:42 +0800 |
commit | 241e38efe91d06e54a180a85e21d2e1cba22ad18 (patch) | |
tree | 3bc7dce550dd809b7098abebe06c75ed2eb5e0f5 | |
parent | 299a7053e9024e113adf59cbeb6b16e89984786c (diff) | |
download | tangerine-mcl-241e38efe91d06e54a180a85e21d2e1cba22ad18.tar.gz tangerine-mcl-241e38efe91d06e54a180a85e21d2e1cba22ad18.tar.zst tangerine-mcl-241e38efe91d06e54a180a85e21d2e1cba22ad18.zip |
reduce try..catch
-rw-r--r-- | include/mcl/lagrange.hpp | 41 | ||||
-rw-r--r-- | src/bn_c_impl.hpp | 68 |
2 files changed, 63 insertions, 46 deletions
diff --git a/include/mcl/lagrange.hpp b/include/mcl/lagrange.hpp index 25b29a6..f06582f 100644 --- a/include/mcl/lagrange.hpp +++ b/include/mcl/lagrange.hpp @@ -15,25 +15,34 @@ namespace mcl { @retval 0 if succeed else -1 */ template<class G, class F> -void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k) +void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k, bool *pb) { /* delta_{i,S}(0) = prod_{j != i} S[j] / (S[j] - S[i]) = a / b where a = prod S[j], b = S[i] * prod_{j != i} (S[j] - S[i]) */ - if (k < 2) throw cybozu::Exception("LagrangeInterpolation:smalll k") << k; + if (k < 2) { + *pb = false; + return; + } std::vector<F> delta(k); F a = S[0]; for (size_t i = 1; i < k; i++) { a *= S[i]; } - if (a.isZero()) throw cybozu::Exception("LagrangeInterpolation:S has zero"); + if (a.isZero()) { + *pb = false; + return; + } for (size_t i = 0; i < k; i++) { F b = S[i]; for (size_t j = 0; j < k; j++) { if (j != i) { F v = S[j] - S[i]; - if (v.isZero()) throw cybozu::Exception("LagrangeInterpolation:same S") << i << j; + if (v.isZero()) { + *pb = false; + return; + } b *= v; } } @@ -50,22 +59,42 @@ void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k) r += t; } out = r; + *pb = true; } +template<class G, class F> +void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k) +{ + bool b; + LagrangeInterpolation(out, S, vec, k, &b); + if (!b) throw cybozu::Exception("LagrangeInterpolation"); +} /* out = f(x) = c[0] + c[1] * x + c[2] * x^2 + ... + c[cSize - 1] * x^(cSize - 1) @retval 0 if succeed else -1 */ template<class G, class T> -void evaluatePolynomial(G& out, const G *c, size_t cSize, const T& x) +void evaluatePolynomial(G& out, const G *c, size_t cSize, const T& x, bool *pb) { - if (cSize < 2) throw cybozu::Exception("evaluatePolynomial:small cSize") << cSize; + if (cSize < 2) { + *pb = false; + return; + } G y = c[cSize - 1]; for (int i = (int)cSize - 2; i >= 0; i--) { G::mul(y, y, x); G::add(y, y, c[i]); } out = y; + *pb = true; +} + +template<class G, class T> +void evaluatePolynomial(G& out, const G *c, size_t cSize, const T& x) +{ + bool b; + evaluatePolynomial(out, c, cSize, x, &b); + if (!b) throw cybozu::Exception("evaluatePolynomial"); } } // mcl diff --git a/src/bn_c_impl.hpp b/src/bn_c_impl.hpp index c6af9fa..6e17870 100644 --- a/src/bn_c_impl.hpp +++ b/src/bn_c_impl.hpp @@ -43,7 +43,7 @@ mclSize getStr(void *buf, mclSize maxBufSize, const T *x, int ioMode) ((char *)buf)[str.size()] = '\0'; } return str.size(); -} catch (std::exception& e) { +} catch (std::exception&) { return 0; } @@ -52,7 +52,7 @@ mclSize serialize(void *buf, mclSize maxBufSize, const T *x) try { return (mclSize)cast(x)->serialize(buf, maxBufSize); -} catch (std::exception& e) { +} catch (std::exception&) { return 0; } @@ -62,7 +62,7 @@ int setStr(T *x, const char *buf, mclSize bufSize, int ioMode) { cast(x)->setStr(std::string(buf, bufSize), ioMode); return 0; -} catch (std::exception& e) { +} catch (std::exception&) { return -1; } @@ -72,7 +72,7 @@ mclSize deserialize(T *x, const void *buf, mclSize bufSize) { const size_t n = cast(x)->deserialize(buf, bufSize); return (mclSize)n; -} catch (std::exception& e) { +} catch (std::exception&) { return 0; } @@ -85,7 +85,7 @@ int mclBn_init(int curve, int maxUnitSize) const mcl::CurveParam& cp = mcl::getCurveParam(curve); initPairing(cp); return 0; -} catch (std::exception& e) { +} catch (std::exception&) { return -1; } @@ -151,7 +151,7 @@ int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize) { cast(x)->setArrayMask((const char *)buf, bufSize); return 0; -} catch (std::exception& e) { +} catch (std::exception&) { return -1; } mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize) @@ -181,7 +181,7 @@ int mclBnFr_setByCSPRNG(mclBnFr *x) { cast(x)->setByCSPRNG(); return 0; -} catch (std::exception& e) { +} catch (std::exception&) { return -1; } @@ -191,7 +191,7 @@ int mclBnFr_setHashOf(mclBnFr *x, const void *buf, mclSize bufSize) { cast(x)->setHashOf(buf, bufSize); return 0; -} catch (std::exception& e) { +} catch (std::exception&) { return -1; } @@ -268,7 +268,7 @@ int mclBnG1_hashAndMapTo(mclBnG1 *x, const void *buf, mclSize bufSize) { hashAndMapToG1(*cast(x), buf, bufSize); return 0; -} catch (std::exception& e) { +} catch (std::exception&) { return 1; } @@ -346,7 +346,7 @@ int mclBnG2_hashAndMapTo(mclBnG2 *x, const void *buf, mclSize bufSize) { hashAndMapToG2(*cast(x), buf, bufSize); return 0; -} catch (std::exception& e) { +} catch (std::exception&) { return 1; } @@ -509,52 +509,40 @@ void mclBn_precomputedMillerLoop2(mclBnGT *f, const mclBnG1 *P1, const uint64_t } int mclBn_FrLagrangeInterpolation(mclBnFr *out, const mclBnFr *xVec, const mclBnFr *yVec, mclSize k) - try { - mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k); - return 0; -} catch (std::exception& e) { - return -1; + bool b; + mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k, &b); + return b ? 0 : -1; } int mclBn_G1LagrangeInterpolation(mclBnG1 *out, const mclBnFr *xVec, const mclBnG1 *yVec, mclSize k) - try { - mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k); - return 0; -} catch (std::exception& e) { - return -1; + bool b; + mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k, &b); + return b ? 0 : -1; } int mclBn_G2LagrangeInterpolation(mclBnG2 *out, const mclBnFr *xVec, const mclBnG2 *yVec, mclSize k) - try { - mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k); - return 0; -} catch (std::exception& e) { - return -1; + bool b; + mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k, &b); + return b ? 0 : -1; } int mclBn_FrEvaluatePolynomial(mclBnFr *out, const mclBnFr *cVec, mclSize cSize, const mclBnFr *x) - try { - mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x)); - return 0; -} catch (std::exception& e) { - return -1; + bool b; + mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x), &b); + return b ? 0 : -1; } int mclBn_G1EvaluatePolynomial(mclBnG1 *out, const mclBnG1 *cVec, mclSize cSize, const mclBnFr *x) - try { - mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x)); - return 0; -} catch (std::exception& e) { - return -1; + bool b; + mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x), &b); + return b ? 0 : -1; } int mclBn_G2EvaluatePolynomial(mclBnG2 *out, const mclBnG2 *cVec, mclSize cSize, const mclBnFr *x) - try { - mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x)); - return 0; -} catch (std::exception& e) { - return -1; + bool b; + mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x), &b); + return b ? 0 : -1; } void mclBn_verifyOrderG1(int doVerify) |