diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-27 14:41:58 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-27 14:41:58 +0800 |
commit | 1bf7bbff64d881771b23b407737c1c28d09519c4 (patch) | |
tree | be86f3a78fe284a254e342f65ff393780fe1d979 /src | |
parent | 0cbd1e2946aac802fbaaf435c3042da149e028e1 (diff) | |
download | tangerine-mcl-1bf7bbff64d881771b23b407737c1c28d09519c4.tar.gz tangerine-mcl-1bf7bbff64d881771b23b407737c1c28d09519c4.tar.zst tangerine-mcl-1bf7bbff64d881771b23b407737c1c28d09519c4.zip |
split error and throw
Diffstat (limited to 'src')
-rw-r--r-- | src/bn_c_impl.hpp | 52 | ||||
-rw-r--r-- | src/ecdsa_c.cpp | 60 | ||||
-rw-r--r-- | src/fp.cpp | 12 | ||||
-rw-r--r-- | src/she_c_impl.hpp | 35 |
4 files changed, 56 insertions, 103 deletions
diff --git a/src/bn_c_impl.hpp b/src/bn_c_impl.hpp index 51c64af..9b54137 100644 --- a/src/bn_c_impl.hpp +++ b/src/bn_c_impl.hpp @@ -27,33 +27,12 @@ static Fp6 *cast(uint64_t *p) { return reinterpret_cast<Fp6*>(p); } static const Fp6 *cast(const uint64_t *p) { return reinterpret_cast<const Fp6*>(p); } template<class T> -mclSize getStr(void *buf, mclSize maxBufSize, const T *x, int ioMode) -{ - size_t n = cast(x)->serialize(buf, maxBufSize, ioMode); - if (n == 0 || n == maxBufSize - 1) return 0; - ((char *)buf)[n] = '\0'; - return n; -} - -template<class T> -mclSize serialize(void *buf, mclSize maxBufSize, const T *x) -{ - return (mclSize)cast(x)->serialize(buf, maxBufSize); -} - -template<class T> int setStr(T *x, const char *buf, mclSize bufSize, int ioMode) { size_t n = cast(x)->deserialize(buf, bufSize, ioMode); return n > 0 ? 0 : -1; } -template<class T> -mclSize deserialize(T *x, const void *buf, mclSize bufSize) -{ - return (mclSize)cast(x)->deserialize(buf, bufSize); -} - #ifdef __EMSCRIPTEN__ // use these functions forcibly extern "C" MCLBN_DLL_API void *mclBnMalloc(size_t n) @@ -72,9 +51,9 @@ int mclBn_init(int curve, int maxUnitSize) return -10; } const mcl::CurveParam& cp = mcl::getCurveParam(curve); - int ret; - initPairing(&ret, cp); - return ret; + bool b; + initPairing(&b, cp); + return b ? 0 : -1; } int mclBn_getOpUnitSize() @@ -129,7 +108,7 @@ int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize) } mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize) { - return deserialize(x, buf, bufSize); + return (mclSize)cast(x)->deserialize(buf, bufSize); } // return 1 if true int mclBnFr_isValid(const mclBnFr *x) @@ -164,11 +143,11 @@ int mclBnFr_setHashOf(mclBnFr *x, const void *buf, mclSize bufSize) mclSize mclBnFr_getStr(char *buf, mclSize maxBufSize, const mclBnFr *x, int ioMode) { - return getStr(buf, maxBufSize, x, ioMode); + return cast(x)->getStr(buf, maxBufSize, ioMode); } mclSize mclBnFr_serialize(void *buf, mclSize maxBufSize, const mclBnFr *x) { - return serialize(buf, maxBufSize, x); + return (mclSize)cast(x)->serialize(buf, maxBufSize); } void mclBnFr_neg(mclBnFr *y, const mclBnFr *x) @@ -213,7 +192,7 @@ int mclBnG1_setStr(mclBnG1 *x, const char *buf, mclSize bufSize, int ioMode) } mclSize mclBnG1_deserialize(mclBnG1 *x, const void *buf, mclSize bufSize) { - return deserialize(x, buf, bufSize); + return (mclSize)cast(x)->deserialize(buf, bufSize); } // return 1 if true @@ -238,12 +217,12 @@ int mclBnG1_hashAndMapTo(mclBnG1 *x, const void *buf, mclSize bufSize) mclSize mclBnG1_getStr(char *buf, mclSize maxBufSize, const mclBnG1 *x, int ioMode) { - return getStr(buf, maxBufSize, x, ioMode); + return cast(x)->getStr(buf, maxBufSize, ioMode); } mclSize mclBnG1_serialize(void *buf, mclSize maxBufSize, const mclBnG1 *x) { - return serialize(buf, maxBufSize, x); + return (mclSize)cast(x)->serialize(buf, maxBufSize); } void mclBnG1_neg(mclBnG1 *y, const mclBnG1 *x) @@ -288,7 +267,7 @@ int mclBnG2_setStr(mclBnG2 *x, const char *buf, mclSize bufSize, int ioMode) } mclSize mclBnG2_deserialize(mclBnG2 *x, const void *buf, mclSize bufSize) { - return deserialize(x, buf, bufSize); + return (mclSize)cast(x)->deserialize(buf, bufSize); } // return 1 if true @@ -313,11 +292,12 @@ int mclBnG2_hashAndMapTo(mclBnG2 *x, const void *buf, mclSize bufSize) mclSize mclBnG2_getStr(char *buf, mclSize maxBufSize, const mclBnG2 *x, int ioMode) { - return getStr(buf, maxBufSize, x, ioMode); + return cast(x)->getStr(buf, maxBufSize, ioMode); } + mclSize mclBnG2_serialize(void *buf, mclSize maxBufSize, const mclBnG2 *x) { - return serialize(buf, maxBufSize, x); + return (mclSize)cast(x)->serialize(buf, maxBufSize); } void mclBnG2_neg(mclBnG2 *y, const mclBnG2 *x) @@ -372,7 +352,7 @@ int mclBnGT_setStr(mclBnGT *x, const char *buf, mclSize bufSize, int ioMode) } mclSize mclBnGT_deserialize(mclBnGT *x, const void *buf, mclSize bufSize) { - return deserialize(x, buf, bufSize); + return (mclSize)cast(x)->deserialize(buf, bufSize); } // return 1 if true @@ -391,12 +371,12 @@ int mclBnGT_isOne(const mclBnGT *x) mclSize mclBnGT_getStr(char *buf, mclSize maxBufSize, const mclBnGT *x, int ioMode) { - return getStr(buf, maxBufSize, x, ioMode); + return cast(x)->getStr(buf, maxBufSize, ioMode); } mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mclBnGT *x) { - return serialize(buf, maxBufSize, x); + return (mclSize)cast(x)->serialize(buf, maxBufSize); } void mclBnGT_neg(mclBnGT *y, const mclBnGT *x) diff --git a/src/ecdsa_c.cpp b/src/ecdsa_c.cpp index d91f1ba..e3e7f40 100644 --- a/src/ecdsa_c.cpp +++ b/src/ecdsa_c.cpp @@ -17,66 +17,43 @@ static PrecomputedPublicKey *cast(ecdsaPrecomputedPublicKey *p) { return reinter static const PrecomputedPublicKey *cast(const ecdsaPrecomputedPublicKey *p) { return reinterpret_cast<const PrecomputedPublicKey*>(p); } int ecdsaInit(void) - try { - init(); - return 0; -} catch (std::exception&) { - return -1; -} - -template<class T> -mclSize serialize(void *buf, mclSize maxBufSize, const T *x) - try -{ - return (mclSize)cast(x)->serialize(buf, maxBufSize); -} catch (std::exception&) { - return 0; + bool b; + init(&b); + return b ? 0 : -1; } mclSize ecdsaSecretKeySerialize(void *buf, mclSize maxBufSize, const ecdsaSecretKey *sec) { - return serialize(buf, maxBufSize, sec); + return (mclSize)cast(sec)->serialize(buf, maxBufSize); } mclSize ecdsaPublicKeySerialize(void *buf, mclSize maxBufSize, const ecdsaPublicKey *pub) { - return serialize(buf, maxBufSize, pub); + return (mclSize)cast(pub)->serialize(buf, maxBufSize); } mclSize ecdsaSignatureSerialize(void *buf, mclSize maxBufSize, const ecdsaSignature *sig) { - return serialize(buf, maxBufSize, sig); -} - -template<class T> -mclSize deserialize(T *x, const void *buf, mclSize bufSize) - try -{ - return (mclSize)cast(x)->deserialize(buf, bufSize); -} catch (std::exception&) { - return 0; + return (mclSize)cast(sig)->serialize(buf, maxBufSize); } mclSize ecdsaSecretKeyDeserialize(ecdsaSecretKey* sec, const void *buf, mclSize bufSize) { - return deserialize(sec, buf, bufSize); + return (mclSize)cast(sec)->deserialize(buf, bufSize); } mclSize ecdsaPublicKeyDeserialize(ecdsaPublicKey* pub, const void *buf, mclSize bufSize) { - return deserialize(pub, buf, bufSize); + return (mclSize)cast(pub)->deserialize(buf, bufSize); } mclSize ecdsaSignatureDeserialize(ecdsaSignature* sig, const void *buf, mclSize bufSize) { - return deserialize(sig, buf, bufSize); + return (mclSize)cast(sig)->deserialize(buf, bufSize); } // return 0 if success int ecdsaSecretKeySetByCSPRNG(ecdsaSecretKey *sec) - try { cast(sec)->setByCSPRNG(); return 0; -} catch (...) { - return -1; } void ecdsaGetPublicKey(ecdsaPublicKey *pub, const ecdsaSecretKey *sec) @@ -99,23 +76,22 @@ int ecdsaVerifyPrecomputed(const ecdsaSignature *sig, const ecdsaPrecomputedPubl } ecdsaPrecomputedPublicKey *ecdsaPrecomputedPublicKeyCreate() - try { - return reinterpret_cast<ecdsaPrecomputedPublicKey*>(new PrecomputedPublicKey()); -} catch (...) { - return 0; + PrecomputedPublicKey *ppub = (PrecomputedPublicKey*)malloc(sizeof(PrecomputedPublicKey)); + if (ppub == 0) return 0; + new(ppub) PrecomputedPublicKey(); + return reinterpret_cast<ecdsaPrecomputedPublicKey*>(ppub); } void ecdsaPrecomputedPublicKeyDestroy(ecdsaPrecomputedPublicKey *ppub) { - delete cast(ppub); + cast(ppub)->~PrecomputedPublicKey(); + free(ppub); } int ecdsaPrecomputedPublicKeyInit(ecdsaPrecomputedPublicKey *ppub, const ecdsaPublicKey *pub) - try { - cast(ppub)->init(*cast(pub)); - return 0; -} catch (...) { - return -1; + bool b; + cast(ppub)->init(&b, *cast(pub)); + return b ? 0 : -1; } @@ -348,21 +348,21 @@ static void initForMont(Op& op, const Unit *p, Mode mode) #endif } -int Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBitSize) +bool Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBitSize) { - if (mclMaxBitSize != MCL_MAX_BIT_SIZE) return -1; + if (mclMaxBitSize != MCL_MAX_BIT_SIZE) return false; #ifdef MCL_USE_VINT assert(sizeof(mcl::vint::Unit) == sizeof(Unit)); #else assert(sizeof(mp_limb_t) == sizeof(Unit)); #endif - if (maxBitSize > MCL_MAX_BIT_SIZE) return -2; - if (_p <= 0) return -3; + if (maxBitSize > MCL_MAX_BIT_SIZE) return false; + if (_p <= 0) return false; clear(); { const size_t maxN = (maxBitSize + fp::UnitBitSize - 1) / fp::UnitBitSize; N = gmp::getUnitSize(_p); - if (N > maxN) return -4; + if (N > maxN) return false; gmp::getArray(p, N, _p); mp = _p; } @@ -484,7 +484,7 @@ int Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBit } else { hash = sha512; } - return 0; + return true; } void copyUnitToByteAsLE(uint8_t *dst, const Unit *src, size_t byteSize) diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index 54d191c..25054b8 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -78,91 +78,88 @@ int sheInit(int curve, int maxUnitSize) mclSize sheSecretKeySerialize(void *buf, mclSize maxBufSize, const sheSecretKey *sec) { - return serialize(buf, maxBufSize, sec); + return (mclSize)cast(sec)->serialize(buf, maxBufSize); } mclSize shePublicKeySerialize(void *buf, mclSize maxBufSize, const shePublicKey *pub) { - return serialize(buf, maxBufSize, pub); + return (mclSize)cast(pub)->serialize(buf, maxBufSize); } mclSize sheCipherTextG1Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG1 *c) { - return serialize(buf, maxBufSize, c); + return (mclSize)cast(c)->serialize(buf, maxBufSize); } mclSize sheCipherTextG2Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG2 *c) { - return serialize(buf, maxBufSize, c); + return (mclSize)cast(c)->serialize(buf, maxBufSize); } mclSize sheCipherTextGTSerialize(void *buf, mclSize maxBufSize, const sheCipherTextGT *c) { - return serialize(buf, maxBufSize, c); + return (mclSize)cast(c)->serialize(buf, maxBufSize); } mclSize sheZkpBinSerialize(void *buf, mclSize maxBufSize, const sheZkpBin *zkp) { - return serialize(buf, maxBufSize, zkp); + return (mclSize)cast(zkp)->serialize(buf, maxBufSize); } mclSize sheZkpEqSerialize(void *buf, mclSize maxBufSize, const sheZkpEq *zkp) { - return serialize(buf, maxBufSize, zkp); + return (mclSize)cast(zkp)->serialize(buf, maxBufSize); } mclSize sheZkpBinEqSerialize(void *buf, mclSize maxBufSize, const sheZkpBinEq *zkp) { - return serialize(buf, maxBufSize, zkp); + return (mclSize)cast(zkp)->serialize(buf, maxBufSize); } mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize) { - return deserialize(sec, buf, bufSize); + return (mclSize)cast(sec)->deserialize(buf, bufSize); } mclSize shePublicKeyDeserialize(shePublicKey* pub, const void *buf, mclSize bufSize) { - return deserialize(pub, buf, bufSize); + return (mclSize)cast(pub)->deserialize(buf, bufSize); } mclSize sheCipherTextG1Deserialize(sheCipherTextG1* c, const void *buf, mclSize bufSize) { - return deserialize(c, buf, bufSize); + return (mclSize)cast(c)->deserialize(buf, bufSize); } mclSize sheCipherTextG2Deserialize(sheCipherTextG2* c, const void *buf, mclSize bufSize) { - return deserialize(c, buf, bufSize); + return (mclSize)cast(c)->deserialize(buf, bufSize); } mclSize sheCipherTextGTDeserialize(sheCipherTextGT* c, const void *buf, mclSize bufSize) { - return deserialize(c, buf, bufSize); + return (mclSize)cast(c)->deserialize(buf, bufSize); } mclSize sheZkpBinDeserialize(sheZkpBin* zkp, const void *buf, mclSize bufSize) { - return deserialize(zkp, buf, bufSize); + return (mclSize)cast(zkp)->deserialize(buf, bufSize); } mclSize sheZkpEqDeserialize(sheZkpEq* zkp, const void *buf, mclSize bufSize) { - return deserialize(zkp, buf, bufSize); + return (mclSize)cast(zkp)->deserialize(buf, bufSize); } mclSize sheZkpBinEqDeserialize(sheZkpBinEq* zkp, const void *buf, mclSize bufSize) { - return deserialize(zkp, buf, bufSize); + return (mclSize)cast(zkp)->deserialize(buf, bufSize); } int sheSecretKeySetByCSPRNG(sheSecretKey *sec) - try { cast(sec)->setByCSPRNG(); return 0; -} catch (std::exception&) { - return -1; } void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec) |