cybozu::RandomGenerator supports CYBOZU_DONT_USE_EXCEPTION

pull/2/head
MITSUNARI Shigeo 6 years ago
parent dcc43ea3fe
commit 48a5e19b1a
  1. 3
      Makefile
  2. 50
      include/cybozu/random_generator.hpp
  3. 6
      sample/pairing_c.c

@ -335,8 +335,7 @@ ecdsa-wasm:
bin/emu: 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 $(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 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 -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
$(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
make_tbl: make_tbl:
$(MAKE) ../bls/src/qcoeff-bn254.hpp $(MAKE) ../bls/src/qcoeff-bn254.hpp

@ -7,7 +7,9 @@
http://opensource.org/licenses/BSD-3-Clause http://opensource.org/licenses/BSD-3-Clause
*/ */
#ifndef CYBOZU_DONT_USE_EXCEPTION
#include <cybozu/exception.hpp> #include <cybozu/exception.hpp>
#endif
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
@ -27,22 +29,6 @@ class RandomGenerator {
RandomGenerator(const RandomGenerator&); RandomGenerator(const RandomGenerator&);
void operator=(const RandomGenerator&); void operator=(const RandomGenerator&);
public: 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 #ifdef _WIN32
RandomGenerator() RandomGenerator()
: prov_(0) : prov_(0)
@ -52,10 +38,15 @@ public:
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
if (CryptAcquireContext(&prov_, NULL, NULL, PROV_RSA_FULL, flagTbl[i]) != 0) return; if (CryptAcquireContext(&prov_, NULL, NULL, PROV_RSA_FULL, flagTbl[i]) != 0) return;
} }
#ifdef CYBOZU_DONT_USE_EXCEPTION
prov_ = 0;
#else
throw cybozu::Exception("randomgenerator"); throw cybozu::Exception("randomgenerator");
#endif
} }
bool read_inner(void *buf, size_t byteSize) bool read_inner(void *buf, size_t byteSize)
{ {
if (prov_ == 0) return false;
return CryptGenRandom(prov_, static_cast<DWORD>(byteSize), static_cast<BYTE*>(buf)) != 0; return CryptGenRandom(prov_, static_cast<DWORD>(byteSize), static_cast<BYTE*>(buf)) != 0;
} }
~RandomGenerator() ~RandomGenerator()
@ -88,13 +79,6 @@ public:
} }
*pb = true; *pb = true;
} }
template<class T>
void read(T *buf, size_t bufNum)
{
bool b;
read(&b, buf, bufNum);
if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum;
}
private: private:
HCRYPTPROV prov_; HCRYPTPROV prov_;
static const size_t bufSize = 1024; static const size_t bufSize = 1024;
@ -105,7 +89,9 @@ private:
RandomGenerator() RandomGenerator()
: fp_(::fopen("/dev/urandom", "rb")) : fp_(::fopen("/dev/urandom", "rb"))
{ {
#ifndef CYBOZU_DONT_USE_EXCEPTION
if (!fp_) throw cybozu::Exception("randomgenerator"); if (!fp_) throw cybozu::Exception("randomgenerator");
#endif
} }
~RandomGenerator() ~RandomGenerator()
{ {
@ -121,6 +107,8 @@ private:
const size_t byteSize = sizeof(T) * bufNum; const size_t byteSize = sizeof(T) * bufNum;
*pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize; *pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize;
} }
#endif
#ifndef CYBOZU_DONT_USE_EXCEPTION
template<class T> template<class T>
void read(T *buf, size_t bufNum) void read(T *buf, size_t bufNum)
{ {
@ -128,6 +116,22 @@ private:
read(&b, buf, bufNum); read(&b, buf, bufNum);
if (!b) throw cybozu::Exception("RandomGenerator:read") << 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 #endif
private: private:
FILE *fp_; FILE *fp_;

@ -11,7 +11,11 @@ int main()
char buf[1024]; char buf[1024];
const char *aStr = "123"; const char *aStr = "123";
const char *bStr = "456"; 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; mclBnFr a, b, ab;
mclBnG1 P, aP; mclBnG1 P, aP;
mclBnG2 Q, bQ; mclBnG2 Q, bQ;

Loading…
Cancel
Save