From 48a5e19b1a65e5142cb4d92885737dc3a5d0b09c Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Tue, 30 Apr 2019 15:53:31 +0900 Subject: [PATCH] cybozu::RandomGenerator supports CYBOZU_DONT_USE_EXCEPTION --- Makefile | 3 +- include/cybozu/random_generator.hpp | 50 ++++++++++++++++------------- sample/pairing_c.c | 6 +++- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index c124150..c356c42 100644 --- a/Makefile +++ b/Makefile @@ -335,8 +335,7 @@ ecdsa-wasm: bin/emu: $(CXX) -g -o $@ src/fp.cpp src/bn_c256.cpp test/bn_c256_test.cpp -DMCL_DONT_USE_XBYAK -DMCL_DONT_USE_OPENSSL -DMCL_USE_VINT -DMCL_SIZEOF_UNIT=8 -DMCL_VINT_64BIT_PORTABLE -DMCL_VINT_FIXED_BUFFER -DMCL_MAX_BIT_SIZE=256 -I./include bin/pairing_c_min.exe: sample/pairing_c.c include/mcl/vint.hpp src/fp.cpp include/mcl/bn.hpp -# $(CXX) -o $@ sample/pairing_c.c src/fp.cpp src/bn_c256.cpp -O2 -g -I./include -fno-exceptions -fno-rtti -fno-threadsafe-statics -DMCL_DONT_USE_XBYAK -DMCL_DONT_USE_OPENSSL -DMCL_USE_VINT -DMCL_SIZEOF_UNIT=8 -DMCL_VINT_FIXED_BUFFER -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -DMCL_DONT_USE_CSPRNG -DMCL_MAX_BIT_SIZE=256 -DMCL_VINT_64BIT_PORTABLE -DNDEBUG -pg - $(CXX) -o $@ sample/pairing_c.c src/fp.cpp src/bn_c256.cpp -O2 -g -I./include -fno-threadsafe-statics -DMCL_DONT_USE_XBYAK -DMCL_DONT_USE_OPENSSL -DMCL_USE_VINT -DMCL_SIZEOF_UNIT=8 -DMCL_VINT_FIXED_BUFFER -DMCL_DONT_USE_CSPRNG -DMCL_MAX_BIT_SIZE=256 -DMCL_VINT_64BIT_PORTABLE -DNDEBUG + $(CXX) -o $@ sample/pairing_c.c src/fp.cpp src/bn_c256.cpp -Og -g -I./include -fno-threadsafe-statics -DMCL_DONT_USE_XBYAK -DMCL_DONT_USE_OPENSSL -DMCL_USE_VINT -DMCL_SIZEOF_UNIT=8 -DMCL_VINT_FIXED_BUFFER -DMCL_MAX_BIT_SIZE=256 -DMCL_VINT_64BIT_PORTABLE #-DNDEBUG -DMCL_DONT_USE_CSPRNG make_tbl: $(MAKE) ../bls/src/qcoeff-bn254.hpp diff --git a/include/cybozu/random_generator.hpp b/include/cybozu/random_generator.hpp index ff4a78d..4a9f071 100644 --- a/include/cybozu/random_generator.hpp +++ b/include/cybozu/random_generator.hpp @@ -7,7 +7,9 @@ http://opensource.org/licenses/BSD-3-Clause */ +#ifndef CYBOZU_DONT_USE_EXCEPTION #include +#endif #ifdef _WIN32 #include #include @@ -27,22 +29,6 @@ class RandomGenerator { RandomGenerator(const RandomGenerator&); void operator=(const RandomGenerator&); public: - uint32_t operator()() - { - return get32(); - } - uint32_t get32() - { - uint32_t ret; - read(&ret, 1); - return ret; - } - uint64_t get64() - { - uint64_t ret; - read(&ret, 1); - return ret; - } #ifdef _WIN32 RandomGenerator() : prov_(0) @@ -52,10 +38,15 @@ public: for (int i = 0; i < 2; i++) { if (CryptAcquireContext(&prov_, NULL, NULL, PROV_RSA_FULL, flagTbl[i]) != 0) return; } +#ifdef CYBOZU_DONT_USE_EXCEPTION + prov_ = 0; +#else throw cybozu::Exception("randomgenerator"); +#endif } bool read_inner(void *buf, size_t byteSize) { + if (prov_ == 0) return false; return CryptGenRandom(prov_, static_cast(byteSize), static_cast(buf)) != 0; } ~RandomGenerator() @@ -88,13 +79,6 @@ public: } *pb = true; } - template - void read(T *buf, size_t bufNum) - { - bool b; - read(&b, buf, bufNum); - if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum; - } private: HCRYPTPROV prov_; static const size_t bufSize = 1024; @@ -105,7 +89,9 @@ private: RandomGenerator() : fp_(::fopen("/dev/urandom", "rb")) { +#ifndef CYBOZU_DONT_USE_EXCEPTION if (!fp_) throw cybozu::Exception("randomgenerator"); +#endif } ~RandomGenerator() { @@ -121,6 +107,8 @@ private: const size_t byteSize = sizeof(T) * bufNum; *pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize; } +#endif +#ifndef CYBOZU_DONT_USE_EXCEPTION template void read(T *buf, size_t bufNum) { @@ -128,6 +116,22 @@ private: read(&b, buf, bufNum); if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum; } + uint32_t get32() + { + uint32_t ret; + read(&ret, 1); + return ret; + } + uint64_t get64() + { + uint64_t ret; + read(&ret, 1); + return ret; + } + uint32_t operator()() + { + return get32(); + } #endif private: FILE *fp_; diff --git a/sample/pairing_c.c b/sample/pairing_c.c index 5c2cd22..ac55908 100644 --- a/sample/pairing_c.c +++ b/sample/pairing_c.c @@ -11,7 +11,11 @@ int main() char buf[1024]; const char *aStr = "123"; const char *bStr = "456"; - mclBn_init(MCL_BN254, MCLBN_FP_UNIT_SIZE); + int ret = mclBn_init(MCL_BN254, MCLBN_COMPILED_TIME_VAR); + if (ret != 0) { + printf("err ret=%d\n", ret); + return 1; + } mclBnFr a, b, ab; mclBnG1 P, aP; mclBnG2 Q, bQ;