From d06ac494def41d2d81fda591e815825a8798b979 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Tue, 23 Oct 2018 09:25:00 +0900 Subject: [PATCH] add mclbn384_256 --- Makefile | 21 ++++++++++++++++++--- src/bn_c384.cpp | 1 + src/bn_c384_256.cpp | 7 +++++++ src/bn_c_impl.hpp | 12 ++++++++---- test/bn_c384_256_test.cpp | 7 +++++++ test/bn_c_test.hpp | 19 ++++++++++++------- 6 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 src/bn_c384_256.cpp create mode 100644 test/bn_c384_256_test.cpp diff --git a/Makefile b/Makefile index ccd052f..fae248a 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ OBJ_DIR=obj EXE_DIR=bin SRC_SRC=fp.cpp bn_c256.cpp bn_c384.cpp bn_c512.cpp she_c256.cpp TEST_SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp elgamal_test.cpp fp_tower_test.cpp gmp_test.cpp bn_test.cpp bn384_test.cpp glv_test.cpp paillier_test.cpp she_test.cpp vint_test.cpp bn512_test.cpp ecdsa_test.cpp conversion_test.cpp -TEST_SRC+=bn_c256_test.cpp bn_c384_test.cpp bn_c512_test.cpp she_c256_test.cpp she_c384_test.cpp +TEST_SRC+=bn_c256_test.cpp bn_c384_test.cpp bn_c384_256_test.cpp bn_c512_test.cpp she_c256_test.cpp she_c384_test.cpp TEST_SRC+=aggregate_sig_test.cpp array_test.cpp TEST_SRC+=bls12_test.cpp TEST_SRC+=ecdsa_c_test.cpp @@ -32,6 +32,7 @@ MCL_LIB=$(LIB_DIR)/libmcl.a MCL_SNAME=mcl$(SHARE_BASENAME_SUF) BN256_SNAME=mclbn256$(SHARE_BASENAME_SUF) BN384_SNAME=mclbn384$(SHARE_BASENAME_SUF) +BN384_256_SNAME=mclbn384_256$(SHARE_BASENAME_SUF) BN512_SNAME=mclbn512$(SHARE_BASENAME_SUF) SHE256_SNAME=mclshe256$(SHARE_BASENAME_SUF) MCL_SLIB=$(LIB_DIR)/lib$(MCL_SNAME).$(LIB_SUF) @@ -39,12 +40,14 @@ BN256_LIB=$(LIB_DIR)/libmclbn256.a BN256_SLIB=$(LIB_DIR)/lib$(BN256_SNAME).$(LIB_SUF) BN384_LIB=$(LIB_DIR)/libmclbn384.a BN384_SLIB=$(LIB_DIR)/lib$(BN384_SNAME).$(LIB_SUF) +BN384_256_LIB=$(LIB_DIR)/libmclbn384_256.a +BN384_256_SLIB=$(LIB_DIR)/lib$(BN384_256_SNAME).$(LIB_SUF) BN512_LIB=$(LIB_DIR)/libmclbn512.a BN512_SLIB=$(LIB_DIR)/lib$(BN512_SNAME).$(LIB_SUF) SHE256_LIB=$(LIB_DIR)/libmclshe256.a SHE384_LIB=$(LIB_DIR)/libmclshe384.a ECDSA_LIB=$(LIB_DIR)/libmclecdsa.a -all: $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(BN512_LIB) $(BN512_SLIB) $(SHE256_LIB) $(SHE384_lib) $(ECDSA_LIB) +all: $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(BN384_256_LIB) $(BN384_256_SLIB) $(BN512_LIB) $(BN512_SLIB) $(SHE256_LIB) $(SHE384_lib) $(ECDSA_LIB) #LLVM_VER=-3.8 LLVM_LLC=llc$(LLVM_VER) @@ -69,6 +72,7 @@ ASM_OBJ=$(OBJ_DIR)/$(CPU).o LIB_OBJ=$(OBJ_DIR)/fp.o BN256_OBJ=$(OBJ_DIR)/bn_c256.o BN384_OBJ=$(OBJ_DIR)/bn_c384.o +BN384_256_OBJ=$(OBJ_DIR)/bn_c384_256.o BN512_OBJ=$(OBJ_DIR)/bn_c512.o SHE256_OBJ=$(OBJ_DIR)/she_c256.o SHE384_OBJ=$(OBJ_DIR)/she_c384.o @@ -115,12 +119,14 @@ endif ifneq ($(findstring $(OS),mac/mingw64),) BN256_SLIB_LDFLAGS+=-l$(MCL_SNAME) -L./lib BN384_SLIB_LDFLAGS+=-l$(MCL_SNAME) -L./lib + BN384_256_SLIB_LDFLAGS+=-l$(MCL_SNAME) -L./lib BN512_SLIB_LDFLAGS+=-l$(MCL_SNAME) -L./lib endif ifeq ($(OS),mingw64) MCL_SLIB_LDFLAGS+=-Wl,--out-implib,$(LIB_DIR)/lib$(MCL_SNAME).a BN256_SLIB_LDFLAGS+=-Wl,--out-implib,$(LIB_DIR)/lib$(BN256_SNAME).a BN384_SLIB_LDFLAGS+=-Wl,--out-implib,$(LIB_DIR)/lib$(BN384_SNAME).a + BN384_256_SLIB_LDFLAGS+=-Wl,--out-implib,$(LIB_DIR)/lib$(BN384_256_SNAME).a BN512_SLIB_LDFLAGS+=-Wl,--out-implib,$(LIB_DIR)/lib$(BN512_SNAME).a endif @@ -148,12 +154,18 @@ $(BN256_SLIB): $(BN256_OBJ) $(MCL_SLIB) $(BN384_LIB): $(BN384_OBJ) $(AR) $@ $(BN384_OBJ) +$(BN384_256_LIB): $(BN384_256_OBJ) + $(AR) $@ $(BN384_256_OBJ) + $(BN512_LIB): $(BN512_OBJ) $(AR) $@ $(BN512_OBJ) $(BN384_SLIB): $(BN384_OBJ) $(MCL_SLIB) $(PRE)$(CXX) -o $@ $(BN384_OBJ) -shared $(LDFLAGS) $(BN384_SLIB_LDFLAGS) +$(BN384_256_SLIB): $(BN384_256_OBJ) $(MCL_SLIB) + $(PRE)$(CXX) -o $@ $(BN384_256_OBJ) -shared $(LDFLAGS) $(BN384_256_SLIB_LDFLAGS) + $(BN512_SLIB): $(BN512_OBJ) $(MCL_SLIB) $(PRE)$(CXX) -o $@ $(BN512_OBJ) -shared $(LDFLAGS) $(BN512_SLIB_LDFLAGS) @@ -235,6 +247,9 @@ $(EXE_DIR)/bn_c256_test.exe: $(OBJ_DIR)/bn_c256_test.o $(BN256_LIB) $(MCL_LIB) $(EXE_DIR)/bn_c384_test.exe: $(OBJ_DIR)/bn_c384_test.o $(BN384_LIB) $(MCL_LIB) $(PRE)$(CXX) $< -o $@ $(BN384_LIB) $(MCL_LIB) $(LDFLAGS) +$(EXE_DIR)/bn_c384_256_test.exe: $(OBJ_DIR)/bn_c384_256_test.o $(BN384_256_LIB) $(MCL_LIB) + $(PRE)$(CXX) $< -o $@ $(BN384_256_LIB) $(MCL_LIB) $(LDFLAGS) + $(EXE_DIR)/bn_c512_test.exe: $(OBJ_DIR)/bn_c512_test.o $(BN512_LIB) $(MCL_LIB) $(PRE)$(CXX) $< -o $@ $(BN512_LIB) $(MCL_LIB) $(LDFLAGS) @@ -315,7 +330,7 @@ update_xbyak: clean: - $(RM) $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(BN512_LIB) $(BN512_SLIB) $(SHE256_LIB) $(OBJ_DIR)/*.o $(OBJ_DIR)/*.d $(EXE_DIR)/*.exe $(GEN_EXE) $(ASM_OBJ) $(LIB_OBJ) $(BN256_OBJ) $(BN384_OBJ) $(BN512_OBJ) $(LLVM_SRC) $(FUNC_LIST) src/*.ll lib/*.a + $(RM) $(LIB_DIR)/*.a $(EXE_DIR)/*.$(LIB_SUF) $(OBJ_DIR)/*.o $(OBJ_DIR)/*.d $(EXE_DIR)/*.exe $(GEN_EXE) $(ASM_OBJ) $(LIB_OBJ) $(BN256_OBJ) $(BN384_OBJ) $(BN512_OBJ) $(LLVM_SRC) $(FUNC_LIST) src/*.ll lib/*.a ALL_SRC=$(SRC_SRC) $(TEST_SRC) $(SAMPLE_SRC) DEPEND_FILE=$(addprefix $(OBJ_DIR)/, $(addsuffix .d,$(basename $(ALL_SRC)))) diff --git a/src/bn_c384.cpp b/src/bn_c384.cpp index bb4a2f7..067e728 100644 --- a/src/bn_c384.cpp +++ b/src/bn_c384.cpp @@ -2,5 +2,6 @@ implementation of mclBn_* apis */ #define MCLBN_FP_UNIT_SIZE 6 +#define MCLBN_FR_UNIT_SIZE 6 #include "bn_c_impl.hpp" diff --git a/src/bn_c384_256.cpp b/src/bn_c384_256.cpp new file mode 100644 index 0000000..5630e83 --- /dev/null +++ b/src/bn_c384_256.cpp @@ -0,0 +1,7 @@ +/* + implementation of mclBn_* apis +*/ +#define MCLBN_FP_UNIT_SIZE 6 +#define MCLBN_FR_UNIT_SIZE 4 +#include "bn_c_impl.hpp" + diff --git a/src/bn_c_impl.hpp b/src/bn_c_impl.hpp index 812ff6c..bb0b4ba 100644 --- a/src/bn_c_impl.hpp +++ b/src/bn_c_impl.hpp @@ -1,12 +1,16 @@ #define MCLBN_DLL_EXPORT #include -#if MCLBN_FP_UNIT_SIZE == 4 +#if MCLBN_FP_UNIT_SIZE == 4 && MCLBN_FR_UNIT_SIZE == 4 #include -#elif MCLBN_FP_UNIT_SIZE == 6 +#elif MCLBN_FP_UNIT_SIZE == 6 && MCLBN_FR_UNIT_SIZE == 6 #include -#elif MCLBN_FP_UNIT_SIZE == 8 +#elif MCLBN_FP_UNIT_SIZE == 6 && MCLBN_FR_UNIT_SIZE == 4 +#include +#elif MCLBN_FP_UNIT_SIZE == 8 && MCLBN_FR_UNIT_SIZE == 8 #include +#else + #error "not supported size" #endif #include using namespace mcl::bn; @@ -48,7 +52,7 @@ extern "C" MCLBN_DLL_API void mclBnFree(void *p) int mclBn_init(int curve, int compiledTimeVar) { if (compiledTimeVar != MCLBN_COMPILED_TIME_VAR) { - return -10; + return -(compiledTimeVar | (MCLBN_COMPILED_TIME_VAR * 100)); } const mcl::CurveParam& cp = mcl::getCurveParam(curve); bool b; diff --git a/test/bn_c384_256_test.cpp b/test/bn_c384_256_test.cpp new file mode 100644 index 0000000..e7bbefd --- /dev/null +++ b/test/bn_c384_256_test.cpp @@ -0,0 +1,7 @@ +#include +using namespace mcl::bls12; +#define MCLBN_DEFINE_STRUCT +#define MCLBN_FP_UNIT_SIZE 6 +#define MCLBN_FR_UNIT_SIZE 4 +#include "bn_c_test.hpp" + diff --git a/test/bn_c_test.hpp b/test/bn_c_test.hpp index 9b5c4bb..d33cdf5 100644 --- a/test/bn_c_test.hpp +++ b/test/bn_c_test.hpp @@ -27,9 +27,12 @@ CYBOZU_TEST_AUTO(init) #if MCLBN_FP_UNIT_SIZE == 4 printf("test BN254 %d\n", MCLBN_FP_UNIT_SIZE); ret = mclBn_init(MCL_BN254, MCLBN_COMPILED_TIME_VAR); -#elif MCLBN_FP_UNIT_SIZE == 6 +#elif MCLBN_FP_UNIT_SIZE == 6 && MCLBN_FR_UNIT_SIZE == 6 printf("test BN381_1 %d\n", MCLBN_FP_UNIT_SIZE); ret = mclBn_init(MCL_BN381_1, MCLBN_COMPILED_TIME_VAR); +#elif MCLBN_FP_UNIT_SIZE == 6 && MCLBN_FR_UNIT_SIZE == 4 + printf("test BLS12_381 %d\n", MCLBN_FP_UNIT_SIZE); + ret = mclBn_init(MCL_BLS12_381, MCLBN_COMPILED_TIME_VAR); #elif MCLBN_FP_UNIT_SIZE == 8 printf("test BN462 %d\n", MCLBN_FP_UNIT_SIZE); ret = mclBn_init(MCL_BN462, MCLBN_COMPILED_TIME_VAR); @@ -358,6 +361,7 @@ CYBOZU_TEST_AUTO(precomputed) CYBOZU_TEST_AUTO(serialize) { + const size_t FrSize = mclBn_getFrByteSize(); const size_t G1Size = mclBn_getG1ByteSize(); mclBnFr x1, x2; mclBnG1 P1, P2; @@ -367,7 +371,7 @@ CYBOZU_TEST_AUTO(serialize) size_t expectSize; size_t ret; // Fr - expectSize = G1Size; + expectSize = FrSize; mclBnFr_setInt(&x1, -1); n = mclBnFr_serialize(buf, sizeof(buf), &x1); CYBOZU_TEST_EQUAL(n, expectSize); @@ -432,7 +436,8 @@ CYBOZU_TEST_AUTO(serialize) CYBOZU_TEST_AUTO(serializeToHexStr) { - const size_t G1Size = mclBn_getG1ByteSize() * 2; + const size_t FrSize = mclBn_getFrByteSize(); + const size_t G1Size = mclBn_getG1ByteSize(); mclBnFr x1, x2; mclBnG1 P1, P2; mclBnG2 Q1, Q2; @@ -441,7 +446,7 @@ CYBOZU_TEST_AUTO(serializeToHexStr) size_t expectSize; size_t ret; // Fr - expectSize = G1Size; + expectSize = FrSize * 2; // hex string mclBnFr_setInt(&x1, -1); n = mclBnFr_getStr(buf, sizeof(buf), &x1, MCLBN_IO_SERIALIZE_HEX_STR); CYBOZU_TEST_EQUAL(n, expectSize); @@ -462,7 +467,7 @@ CYBOZU_TEST_AUTO(serializeToHexStr) CYBOZU_TEST_EQUAL(n, expectSize); // G1 - expectSize = G1Size; + expectSize = G1Size * 2; // hex string mclBnG1_hashAndMapTo(&P1, "1", 1); n = mclBnG1_getStr(buf, sizeof(buf), &P1, MCLBN_IO_SERIALIZE_HEX_STR); CYBOZU_TEST_EQUAL(n, expectSize); @@ -483,7 +488,7 @@ CYBOZU_TEST_AUTO(serializeToHexStr) CYBOZU_TEST_EQUAL(n, expectSize); // G2 - expectSize = G1Size * 2; + expectSize = G1Size * 2 * 2; // hex string mclBnG2_hashAndMapTo(&Q1, "1", 1); n = mclBnG2_getStr(buf, sizeof(buf), &Q1, MCLBN_IO_SERIALIZE_HEX_STR); CYBOZU_TEST_EQUAL(n, expectSize); @@ -504,7 +509,7 @@ CYBOZU_TEST_AUTO(serializeToHexStr) CYBOZU_TEST_EQUAL(n, expectSize); } -#if MCLBN_FP_UNIT_SIZE == 6 +#if MCLBN_FP_UNIT_SIZE == 6 && MCLBN_FR_UNIT_SIZE >= 6 CYBOZU_TEST_AUTO(badG2) { int ret;