blsInit() is not thread safe

pull/3/head
MITSUNARI Shigeo 6 years ago
parent 6b518d75dc
commit 5600e171f9
  1. 2
      .travis.yml
  2. 2
      CMakeLists.txt
  3. 4
      Makefile
  4. 3
      include/bls/bls.h
  5. 1
      readme.md
  6. 30
      src/bls_c_impl.hpp
  7. 46
      test/bls_c_test.hpp

@ -12,7 +12,7 @@ install:
- git clone --depth 1 https://github.com/herumi/mcl.git $TRAVIS_BUILD_DIR/../mcl
script:
- make -j3
- make test_ci DISABLE_THREAD_TEST=1
- make test_ci
- make test_go
- env LD_LIBRARY_PATH=../mcl/lib bin/bls_c384_test.exe
- make clean && make -C ../mcl clean

@ -23,7 +23,7 @@ install(TARGETS bls_c384 DESTINATION lib)
install(TARGETS bls_c384_256 DESTINATION lib)
install(FILES ${BLS_HEADERS} DESTINATION include/bls)
set(TEST_LIBS pthread gmpxx)
set(TEST_LIBS gmpxx)
add_executable(bls_c256_test test/bls_c256_test.cpp)
target_link_libraries(bls_c256_test bls_c256 ${TEST_LIBS})

@ -10,7 +10,6 @@ LIB_DIR=lib
OBJ_DIR=obj
EXE_DIR=bin
CFLAGS += -std=c++11
LDFLAGS += -lpthread
SRC_SRC=bls_c256.cpp bls_c384.cpp bls_c384_256.cpp bls_c512.cpp
TEST_SRC=bls256_test.cpp bls384_test.cpp bls384_256_test.cpp bls_c256_test.cpp bls_c384_test.cpp bls_c384_256_test.cpp bls_c512_test.cpp
@ -20,9 +19,6 @@ CFLAGS+=-I$(MCL_DIR)/include
ifneq ($(MCL_MAX_BIT_SIZE),)
CFLAGS+=-DMCL_MAX_BIT_SIZE=$(MCL_MAX_BIT_SIZE)
endif
ifeq ($(DISABLE_THREAD_TEST),1)
CFLAGS+=-DDISABLE_THREAD_TEST
endif
ifeq ($(BLS_SWAP_G),1)
CFLAGS+=-DBLS_SWAP_G
endif

@ -80,8 +80,7 @@ typedef struct {
which macro is used to make sure that the values
are the same when the library is built and used
@return 0 if success
@note blsInit() is thread safe and serialized if it is called simultaneously
but don't call it while using other functions.
@note blsInit() is not thread safe
*/
BLS_DLL_API int blsInit(int curve, int compiledTimeVar);

@ -16,6 +16,7 @@ git clone git://github.com/herumi/cybozulib_ext ; for only Windows
```
# News
* blsInit() is not thread safe (pthread is not used)
* -tags option for Go bindings is always necessary
* support Android
* (Break backward compatibility) The suffix `_dy` of library name is removed and bls\*.a requires libmcl.so set LD_LIBRARY_PATH to the directory.

@ -65,7 +65,7 @@ inline const G2& getBasePoint() { return g_Q; }
inline const mcl::FixedArray<Fp6, maxQcoeffN>& getQcoeff() { return g_Qcoeff; }
#endif
int blsInitNotThreadSafe(int curve, int compiledTimeVar)
int blsInit(int curve, int compiledTimeVar)
{
if (compiledTimeVar != MCLBN_COMPILED_TIME_VAR) {
return -(compiledTimeVar | (MCLBN_COMPILED_TIME_VAR * 100));
@ -125,34 +125,6 @@ extern "C" BLS_DLL_API void blsFree(void *p)
}
#endif
#if !defined(__EMSCRIPTEN__) && !defined(__wasm__)
#if defined(CYBOZU_CPP_VERSION) && CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
#include <mutex>
#define USE_STD_MUTEX
#else
#include <cybozu/mutex.hpp>
#define USE_CYBOZU_MUTEX
#endif
#endif
int blsInit(int curve, int compiledTimeVar)
{
int ret = 0;
#ifdef USE_STD_MUTEX
static std::mutex m;
std::lock_guard<std::mutex> lock(m);
#elif defined(USE_CYBOZU_MUTEX)
static cybozu::Mutex m;
cybozu::AutoLock lock(m);
#endif
static int g_curve = -1;
if (g_curve != curve) {
ret = blsInitNotThreadSafe(curve, compiledTimeVar);
g_curve = curve;
}
return ret;
}
static inline const mclBnG1 *cast(const G1* x) { return (const mclBnG1*)x; }
static inline const mclBnG2 *cast(const G2* x) { return (const mclBnG2*)x; }

@ -91,52 +91,6 @@ void blsOrderTest(const char *curveOrder/*Fr*/, const char *fieldOrder/*Fp*/)
CYBOZU_TEST_EQUAL(buf, fieldOrder);
}
#if !defined(DISABLE_THREAD_TEST) || defined(__clang__)
#if defined(CYBOZU_CPP_VERSION) && CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
#include <thread>
#include <vector>
struct Thread {
std::unique_ptr<std::thread> t;
Thread() : t() {}
~Thread()
{
if (t) {
t->join();
}
}
template<class F>
void run(F func, int p1, int p2)
{
t.reset(new std::thread(func, p1, p2));
}
};
CYBOZU_TEST_AUTO(multipleInit)
{
#if MCLBN_FP_UNIT_SIZE == 4
{
const size_t n = 100;
std::vector<Thread> vt(n);
for (size_t i = 0; i < n; i++) {
vt[i].run(blsInit, MCL_BN254, MCLBN_COMPILED_TIME_VAR);
}
}
CYBOZU_TEST_EQUAL(blsGetOpUnitSize(), 4u);
#endif
#if MCLBN_FP_UNIT_SIZE == 6
{
const size_t n = 100;
std::vector<Thread> vt(n);
for (size_t i = 0; i < n; i++) {
vt[i].run(blsInit, MCL_BLS12_381, MCLBN_COMPILED_TIME_VAR);
}
}
CYBOZU_TEST_EQUAL(blsGetOpUnitSize(), 6u);
#endif
}
#endif
#endif
void blsSerializeTest()
{
const size_t FrSize = blsGetFrByteSize();

Loading…
Cancel
Save