From b3c2b12093c9f04970f4acfb0dd825af4e0638ba Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 28 May 2018 05:47:11 +0900 Subject: [PATCH] remove throw in ecdsa-wasm --- Makefile | 2 +- include/mcl/ecdsa.hpp | 16 +++++++++++++++- include/mcl/ecparam.hpp | 2 ++ include/mcl/window_method.hpp | 6 ++++-- src/ecdsa_c.cpp | 5 +++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index c1d0302..63adea4 100644 --- a/Makefile +++ b/Makefile @@ -267,7 +267,7 @@ endif emcc -o $@ src/fp.cpp src/bn_c512.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=512 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -fno-exceptions ../ecdsa-wasm/ecdsa_c.js: src/ecdsa_c.cpp src/fp.cpp include/mcl/ecdsa.hpp include/mcl/ecdsa.h Makefile - emcc -o $@ src/fp.cpp src/ecdsa_c.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 + emcc -o $@ src/fp.cpp src/ecdsa_c.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -fno-exceptions mcl-wasm: $(MAKE) ../mcl-wasm/mcl_c.js diff --git a/include/mcl/ecdsa.hpp b/include/mcl/ecdsa.hpp index f4ee5c4..d39d3f9 100644 --- a/include/mcl/ecdsa.hpp +++ b/include/mcl/ecdsa.hpp @@ -91,16 +91,24 @@ inline void init(bool *pb) Ec::setIoMode(mcl::IoEcAffine); local::Param& p = local::getParam(); p.ecParam = ecParam; - p.P.set(Fp(ecParam.gx), Fp(ecParam.gy)); + Fp x, y; + x.setStr(pb, ecParam.gx); + if (!*pb) return; + y.setStr(pb, ecParam.gy); + if (!*pb) return; + p.P.set(pb, x, y); + if (!*pb) return; p.Pbase.init(pb, p.P, ecParam.bitSize, local::winSize); } +#ifndef CYBOZU_DONT_USE_EXCEPTION inline void init() { bool b; init(&b); if (!b) throw cybozu::Exception("ecdsa:init"); } +#endif typedef Zn SecretKey; typedef Ec PublicKey; @@ -111,12 +119,14 @@ struct PrecomputedPublicKey { { pubBase_.init(pb, pub, param.ecParam.bitSize, local::winSize); } +#ifndef CYBOZU_DONT_USE_EXCEPTION void init(const PublicKey& pub) { bool b; init(&b, pub); if (!b) throw cybozu::Exception("ecdsa:PrecomputedPublicKey:init"); } +#endif }; inline void getPublicKey(PublicKey& pub, const SecretKey& sec) @@ -144,6 +154,7 @@ struct Signature : public mcl::fp::Serializable { } s.save(pb, os, ioMode); } +#ifndef CYBOZU_DONT_USE_EXCEPTION template void load(InputStream& is, int ioMode = IoSerialize) { @@ -158,6 +169,8 @@ struct Signature : public mcl::fp::Serializable { save(&b, os, ioMode); if (!b) throw cybozu::Exception("ecdsa:Signature:save"); } +#endif +#ifndef CYBOZU_DONT_USE_STRING friend std::istream& operator>>(std::istream& is, Signature& self) { self.load(is, fp::detectIoMode(Ec::getIoMode(), is)); @@ -168,6 +181,7 @@ struct Signature : public mcl::fp::Serializable { self.save(os, fp::detectIoMode(Ec::getIoMode(), os)); return os; } +#endif }; inline void sign(Signature& sig, const SecretKey& sec, const void *msg, size_t msgSize) diff --git a/include/mcl/ecparam.hpp b/include/mcl/ecparam.hpp index 8c17025..19b76bf 100644 --- a/include/mcl/ecparam.hpp +++ b/include/mcl/ecparam.hpp @@ -136,6 +136,7 @@ const struct mcl::EcParam NIST_P521 = { } // mcl::ecparam +#ifndef CYBOZU_DONT_USE_STRING static inline const mcl::EcParam* getEcParam(const std::string& name) { static const mcl::EcParam *tbl[] = { @@ -158,5 +159,6 @@ static inline const mcl::EcParam* getEcParam(const std::string& name) } throw cybozu::Exception("mcl::getEcParam:not support name") << name; } +#endif } // mcl diff --git a/include/mcl/window_method.hpp b/include/mcl/window_method.hpp index b2c3228..cb4fad3 100644 --- a/include/mcl/window_method.hpp +++ b/include/mcl/window_method.hpp @@ -109,12 +109,14 @@ public: } } } +#ifndef CYBOZU_DONT_USE_EXCEPTION void init(const Ec& x, size_t bitSize, size_t winSize) { bool b; init(&b, x, bitSize, winSize); if (!b) throw cybozu::Exception("mcl:WindowMethod:init") << bitSize << winSize; } +#endif /* @param z [out] x multiplied by y @param y [in] exponent @@ -129,10 +131,10 @@ public: void mul(Ec& z, int64_t y) const { #if MCL_SIZEOF_UNIT == 8 - Unit u = std::abs(y); + Unit u = fp::abs_(y); powArray(z, &u, 1, y < 0); #else - uint64_t ua = std::abs(y); + uint64_t ua = fp::abs_(y); Unit u[2] = { uint32_t(ua), uint32_t(ua >> 32) }; size_t un = u[1] ? 2 : 1; powArray(z, u, un, y < 0); diff --git a/src/ecdsa_c.cpp b/src/ecdsa_c.cpp index 3b1d53c..f2222a2 100644 --- a/src/ecdsa_c.cpp +++ b/src/ecdsa_c.cpp @@ -1,6 +1,7 @@ #define ECDSA_DLL_EXPORT #include #include +#include using namespace mcl::ecdsa; @@ -18,6 +19,10 @@ static const PrecomputedPublicKey *cast(const ecdsaPrecomputedPublicKey *p) { re #ifdef __EMSCRIPTEN__ // use these functions forcibly +extern "C" ECDSA_DLL_API void *ecdsaMalloc(size_t n) +{ + return malloc(n); +} extern "C" ECDSA_DLL_API void ecdsaFree(void *p) { free(p);