From b8b86ed9721bcd7bc17c24209693eab233372648 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Fri, 26 Jan 2018 10:24:28 +0900 Subject: [PATCH] add bn_c512 --- Makefile | 26 +++++++++++++++++++++----- include/mcl/bn.h | 11 ++++++++++- src/bn_c512.cpp | 6 ++++++ src/bn_c_impl.hpp | 9 +++++++-- test/bn_c_test.hpp | 18 +++++++++++------- 5 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 src/bn_c512.cpp diff --git a/Makefile b/Makefile index 7292b21..76f45b9 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,9 @@ include common.mk LIB_DIR=lib OBJ_DIR=obj EXE_DIR=bin -SRC_SRC=fp.cpp bn_c256.cpp bn_c384.cpp she_c256.cpp +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 -TEST_SRC+=bn_c256_test.cpp bn_c384_test.cpp she_c256_test.cpp +TEST_SRC+=bn_c256_test.cpp bn_c384_test.cpp bn_c512_test.cpp she_c256_test.cpp ifeq ($(CPU),x86-64) MCL_USE_XBYAK?=1 TEST_SRC+=mont_fp_test.cpp sq_test.cpp @@ -29,14 +29,17 @@ MCL_LIB=$(LIB_DIR)/libmcl.a MCL_SNAME=mcl$(SHARE_BASENAME_SUF) BN256_SNAME=mclbn256$(SHARE_BASENAME_SUF) BN384_SNAME=mclbn384$(SHARE_BASENAME_SUF) +BN512_SNAME=mclbn512$(SHARE_BASENAME_SUF) SHE256_SNAME=mclshe256$(SHARE_BASENAME_SUF) MCL_SLIB=$(LIB_DIR)/lib$(MCL_SNAME).$(LIB_SUF) 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) +BN512_LIB=$(LIB_DIR)/libmclbn512.a +BN512_SLIB=$(LIB_DIR)/lib$(BN512_SNAME).$(LIB_SUF) SHE256_LIB=$(LIB_DIR)/libmclshe256.a -all: $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(SHE256_LIB) +all: $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(BN512_LIB) $(BN512_SLIB) $(SHE256_LIB) #LLVM_VER=-3.8 LLVM_LLC=llc$(LLVM_VER) @@ -61,6 +64,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 +BN512_OBJ=$(OBJ_DIR)/bn_c512.o SHE256_OBJ=$(OBJ_DIR)/she_c256.o FUNC_LIST=src/func.list MCL_USE_LLVM?=1 @@ -119,9 +123,15 @@ $(BN256_SLIB): $(BN256_OBJ) $(MCL_SLIB) $(BN384_LIB): $(BN384_OBJ) $(AR) $@ $(BN384_OBJ) +$(BN512_LIB): $(BN512_OBJ) + $(AR) $@ $(BN512_OBJ) + $(BN384_SLIB): $(BN384_OBJ) $(MCL_SLIB) $(PRE)$(CXX) -o $@ $(BN384_OBJ) -shared $(LDFLAGS) $(MAC_LDFLAGS) +$(BN512_SLIB): $(BN512_OBJ) $(MCL_SLIB) + $(PRE)$(CXX) -o $@ $(BN512_OBJ) -shared $(LDFLAGS) $(MAC_LDFLAGS) + $(ASM_OBJ): $(ASM_SRC) $(PRE)$(CXX) -c $< -o $@ $(CFLAGS) @@ -194,6 +204,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_c512_test.exe: $(OBJ_DIR)/bn_c512_test.o $(BN512_LIB) $(MCL_LIB) + $(PRE)$(CXX) $< -o $@ $(BN512_LIB) $(MCL_LIB) $(LDFLAGS) + $(EXE_DIR)/pairing_c.exe: $(OBJ_DIR)/pairing_c.o $(BN256_LIB) $(MCL_LIB) $(PRE)$(CC) $< -o $@ $(BN256_LIB) $(MCL_LIB) $(LDFLAGS) -lstdc++ @@ -210,7 +223,7 @@ test: $(TEST_EXE) @grep -v "ng=0, exception=0" result.txt; if [ $$? -eq 1 ]; then echo "all unit tests succeed"; else exit 1; fi EMCC_OPT=-I./include -I./src -I../cybozulib/include -Wall -Wextra -EMCC_OPT+=-O3 -DNDEBUG -DMCLBN_FP_UNIT_SIZE=4 -DMCL_MAX_BIT_SIZE=256 -DMCLSHE_WIN_SIZE=8 +EMCC_OPT+=-O3 -DNDEBUG -DMCLSHE_WIN_SIZE=8 EMCC_OPT+=-s WASM=1 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s MODULARIZE=1 JS_DEP=src/fp.cpp src/she_c256.cpp src/she_c_impl.hpp include/mcl/she.hpp Makefile ifeq ($(MCL_USE_LLVM),2) @@ -223,8 +236,11 @@ endif ../mcl-wasm/mcl_c.js: src/fp.cpp src/bn_c256.cpp include/mcl/bn.h Makefile emcc -o $@ src/fp.cpp src/bn_c256.cpp $(EMCC_OPT) +../mcl-wasm/mcl_c512.js: src/fp.cpp src/bn_c512.cpp include/mcl/bn.h Makefile + emcc -o $@ src/fp.cpp src/bn_c512.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=512 + clean: - $(RM) $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(OBJ_DIR)/*.o $(OBJ_DIR)/*.d $(EXE_DIR)/*.exe $(GEN_EXE) $(ASM_OBJ) $(LIB_OBJ) $(BN256_OBJ) $(BN384_OBJ) $(LLVM_SRC) $(FUNC_LIST) src/*.ll + $(RM) $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) $(BN384_LIB) $(BN384_SLIB) $(BN512_LIB) $(BN512_SLIB) $(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 ALL_SRC=$(SRC_SRC) $(TEST_SRC) $(SAMPLE_SRC) DEPEND_FILE=$(addprefix $(OBJ_DIR)/, $(addsuffix .d,$(basename $(ALL_SRC)))) diff --git a/include/mcl/bn.h b/include/mcl/bn.h index f6978dc..747254d 100644 --- a/include/mcl/bn.h +++ b/include/mcl/bn.h @@ -112,7 +112,7 @@ MCLBN_DLL_API int mclBn_init(int curve, int maxUnitSize); G1 is a curve defined on Fp serialized size of elements - |Fr| = |G1| = 32 bytes (if CurveFp254BNb), 48 bytes (if CurevFp382_{1,2}) + |Fr| = |G1| = 32 bytes (if CurveFp254BNb), 48 bytes (if CurevFp382_{1,2}), 58 bytes (if CurveFp462) |G2| = |G1| * 2 |GT| = |G1| * 12 */ @@ -120,9 +120,18 @@ MCLBN_DLL_API int mclBn_init(int curve, int maxUnitSize); return the num of Unit(=uint64_t) to store Fr 4 if curve is mclBn_CurveFp254BNb 6 if curve is mclBn_CurveFp382_{1,2} + 8 if curve is mclBn_CurveFp462 */ MCLBN_DLL_API int mclBn_getOpUnitSize(); +/* + return bytes for serialized G1 + 32 if mclBn_CurveFp254BNb + 48 if mclBn_CurveFp382_{1,2} + 58 if mclBn_CurveFpA462 +*/ +MCLBN_DLL_API int mclBn_getG1ByteSize(); + /* return decimal string of the order of the curve(=the characteristic of Fr) return str(buf) if success diff --git a/src/bn_c512.cpp b/src/bn_c512.cpp new file mode 100644 index 0000000..90be2d9 --- /dev/null +++ b/src/bn_c512.cpp @@ -0,0 +1,6 @@ +/* + implementation of mclBn_* apis +*/ +#define MCLBN_FP_UNIT_SIZE 8 +#include "bn_c_impl.hpp" + diff --git a/src/bn_c_impl.hpp b/src/bn_c_impl.hpp index 0ec0f34..e845bc3 100644 --- a/src/bn_c_impl.hpp +++ b/src/bn_c_impl.hpp @@ -123,14 +123,14 @@ int mclBn_init(int curve, int maxUnitSize) try { if (maxUnitSize != MCLBN_FP_UNIT_SIZE) { - if (g_fp) fprintf(g_fp, "mclBn_init:maxUnitSize is mismatch %d %d\n", maxUnitSize, MCLBN_FP_UNIT_SIZE); + fprintf(stderr, "mclBn_init:maxUnitSize is mismatch %d %d\n", maxUnitSize, MCLBN_FP_UNIT_SIZE); return -1; } const mcl::bn::CurveParam& cp = mcl::bn::getCurveParam(curve); initPairing(cp); return 0; } catch (std::exception& e) { - if (g_fp) fprintf(g_fp, "%s\n", e.what()); + fprintf(stderr, "%s\n", e.what()); return -1; } @@ -139,6 +139,11 @@ int mclBn_getOpUnitSize() return (int)Fp::getUnitSize() * sizeof(mcl::fp::Unit) / sizeof(uint64_t); } +int mclBn_getG1ByteSize() +{ + return (int)Fp::getByteSize(); +} + mclSize copyStrAndReturnSize(char *buf, mclSize maxBufSize, const std::string& str) { if (str.size() >= maxBufSize) return 0; diff --git a/test/bn_c_test.hpp b/test/bn_c_test.hpp index e3049ac..d395378 100644 --- a/test/bn_c_test.hpp +++ b/test/bn_c_test.hpp @@ -30,11 +30,17 @@ CYBOZU_TEST_AUTO(init) #if MCLBN_FP_UNIT_SIZE == 4 printf("test MCLBN_curveFp254BNb %d\n", MCLBN_FP_UNIT_SIZE); ret = mclBn_init(mclBn_CurveFp254BNb, MCLBN_FP_UNIT_SIZE); -#else +#elif MCLBN_FP_UNIT_SIZE == 6 printf("test MCLBN_curveFp382_1 %d\n", MCLBN_FP_UNIT_SIZE); ret = mclBn_init(mclBn_CurveFp382_1, MCLBN_FP_UNIT_SIZE); +#elif MCLBN_FP_UNIT_SIZE == 8 + printf("test MCLBN_curveFp462 %d\n", MCLBN_FP_UNIT_SIZE); + ret = mclBn_init(mclBn_CurveFp462, MCLBN_FP_UNIT_SIZE); +#else + #error "bad MCLBN_FP_UNIT_SIZE" #endif CYBOZU_TEST_EQUAL(ret, 0); + if (ret != 0) exit(1); } CYBOZU_TEST_AUTO(Fr) @@ -42,8 +48,6 @@ CYBOZU_TEST_AUTO(Fr) mclBnFr x, y; memset(&x, 0xff, sizeof(x)); CYBOZU_TEST_ASSERT(!mclBnFr_isValid(&x)); - memset(&x, 1, sizeof(x)); - CYBOZU_TEST_ASSERT(mclBnFr_isValid(&x)); CYBOZU_TEST_ASSERT(!mclBnFr_isZero(&x)); mclBnFr_clear(&x); @@ -333,7 +337,7 @@ CYBOZU_TEST_AUTO(precomputed) CYBOZU_TEST_AUTO(serialize) { - const size_t opUnitSize = mclBn_getOpUnitSize(); + const size_t G1Size = mclBn_getG1ByteSize(); mclBnFr x1, x2; mclBnG1 P1, P2; mclBnG2 Q1, Q2; @@ -342,7 +346,7 @@ CYBOZU_TEST_AUTO(serialize) size_t expectSize; int ret; // Fr - expectSize = opUnitSize * 8; + expectSize = G1Size; mclBnFr_setInt(&x1, -1); n = mclBnFr_serialize(buf, sizeof(buf), &x1); CYBOZU_TEST_EQUAL(n, expectSize); @@ -363,7 +367,7 @@ CYBOZU_TEST_AUTO(serialize) CYBOZU_TEST_EQUAL(n, expectSize); // G1 - expectSize = opUnitSize * 8; + expectSize = G1Size; mclBnG1_hashAndMapTo(&P1, "1", 1); n = mclBnG1_serialize(buf, sizeof(buf), &P1); CYBOZU_TEST_EQUAL(n, expectSize); @@ -384,7 +388,7 @@ CYBOZU_TEST_AUTO(serialize) CYBOZU_TEST_EQUAL(n, expectSize); // G2 - expectSize = opUnitSize * 8 * 2; + expectSize = G1Size * 2; mclBnG2_hashAndMapTo(&Q1, "1", 1); n = mclBnG2_serialize(buf, sizeof(buf), &Q1); CYBOZU_TEST_EQUAL(n, expectSize);