add bn_c512

dev
MITSUNARI Shigeo 7 years ago
parent 4a400fcf78
commit b8b86ed972
  1. 26
      Makefile
  2. 11
      include/mcl/bn.h
  3. 6
      src/bn_c512.cpp
  4. 9
      src/bn_c_impl.hpp
  5. 18
      test/bn_c_test.hpp

@ -2,9 +2,9 @@ include common.mk
LIB_DIR=lib LIB_DIR=lib
OBJ_DIR=obj OBJ_DIR=obj
EXE_DIR=bin 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=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) ifeq ($(CPU),x86-64)
MCL_USE_XBYAK?=1 MCL_USE_XBYAK?=1
TEST_SRC+=mont_fp_test.cpp sq_test.cpp 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) MCL_SNAME=mcl$(SHARE_BASENAME_SUF)
BN256_SNAME=mclbn256$(SHARE_BASENAME_SUF) BN256_SNAME=mclbn256$(SHARE_BASENAME_SUF)
BN384_SNAME=mclbn384$(SHARE_BASENAME_SUF) BN384_SNAME=mclbn384$(SHARE_BASENAME_SUF)
BN512_SNAME=mclbn512$(SHARE_BASENAME_SUF)
SHE256_SNAME=mclshe256$(SHARE_BASENAME_SUF) SHE256_SNAME=mclshe256$(SHARE_BASENAME_SUF)
MCL_SLIB=$(LIB_DIR)/lib$(MCL_SNAME).$(LIB_SUF) MCL_SLIB=$(LIB_DIR)/lib$(MCL_SNAME).$(LIB_SUF)
BN256_LIB=$(LIB_DIR)/libmclbn256.a BN256_LIB=$(LIB_DIR)/libmclbn256.a
BN256_SLIB=$(LIB_DIR)/lib$(BN256_SNAME).$(LIB_SUF) BN256_SLIB=$(LIB_DIR)/lib$(BN256_SNAME).$(LIB_SUF)
BN384_LIB=$(LIB_DIR)/libmclbn384.a BN384_LIB=$(LIB_DIR)/libmclbn384.a
BN384_SLIB=$(LIB_DIR)/lib$(BN384_SNAME).$(LIB_SUF) 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 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_VER=-3.8
LLVM_LLC=llc$(LLVM_VER) LLVM_LLC=llc$(LLVM_VER)
@ -61,6 +64,7 @@ ASM_OBJ=$(OBJ_DIR)/$(CPU).o
LIB_OBJ=$(OBJ_DIR)/fp.o LIB_OBJ=$(OBJ_DIR)/fp.o
BN256_OBJ=$(OBJ_DIR)/bn_c256.o BN256_OBJ=$(OBJ_DIR)/bn_c256.o
BN384_OBJ=$(OBJ_DIR)/bn_c384.o BN384_OBJ=$(OBJ_DIR)/bn_c384.o
BN512_OBJ=$(OBJ_DIR)/bn_c512.o
SHE256_OBJ=$(OBJ_DIR)/she_c256.o SHE256_OBJ=$(OBJ_DIR)/she_c256.o
FUNC_LIST=src/func.list FUNC_LIST=src/func.list
MCL_USE_LLVM?=1 MCL_USE_LLVM?=1
@ -119,9 +123,15 @@ $(BN256_SLIB): $(BN256_OBJ) $(MCL_SLIB)
$(BN384_LIB): $(BN384_OBJ) $(BN384_LIB): $(BN384_OBJ)
$(AR) $@ $(BN384_OBJ) $(AR) $@ $(BN384_OBJ)
$(BN512_LIB): $(BN512_OBJ)
$(AR) $@ $(BN512_OBJ)
$(BN384_SLIB): $(BN384_OBJ) $(MCL_SLIB) $(BN384_SLIB): $(BN384_OBJ) $(MCL_SLIB)
$(PRE)$(CXX) -o $@ $(BN384_OBJ) -shared $(LDFLAGS) $(MAC_LDFLAGS) $(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) $(ASM_OBJ): $(ASM_SRC)
$(PRE)$(CXX) -c $< -o $@ $(CFLAGS) $(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) $(EXE_DIR)/bn_c384_test.exe: $(OBJ_DIR)/bn_c384_test.o $(BN384_LIB) $(MCL_LIB)
$(PRE)$(CXX) $< -o $@ $(BN384_LIB) $(MCL_LIB) $(LDFLAGS) $(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) $(EXE_DIR)/pairing_c.exe: $(OBJ_DIR)/pairing_c.o $(BN256_LIB) $(MCL_LIB)
$(PRE)$(CC) $< -o $@ $(BN256_LIB) $(MCL_LIB) $(LDFLAGS) -lstdc++ $(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 @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=-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 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 JS_DEP=src/fp.cpp src/she_c256.cpp src/she_c_impl.hpp include/mcl/she.hpp Makefile
ifeq ($(MCL_USE_LLVM),2) 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 ../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) 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: 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) ALL_SRC=$(SRC_SRC) $(TEST_SRC) $(SAMPLE_SRC)
DEPEND_FILE=$(addprefix $(OBJ_DIR)/, $(addsuffix .d,$(basename $(ALL_SRC)))) DEPEND_FILE=$(addprefix $(OBJ_DIR)/, $(addsuffix .d,$(basename $(ALL_SRC))))

@ -112,7 +112,7 @@ MCLBN_DLL_API int mclBn_init(int curve, int maxUnitSize);
G1 is a curve defined on Fp G1 is a curve defined on Fp
serialized size of elements 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 |G2| = |G1| * 2
|GT| = |G1| * 12 |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 return the num of Unit(=uint64_t) to store Fr
4 if curve is mclBn_CurveFp254BNb 4 if curve is mclBn_CurveFp254BNb
6 if curve is mclBn_CurveFp382_{1,2} 6 if curve is mclBn_CurveFp382_{1,2}
8 if curve is mclBn_CurveFp462
*/ */
MCLBN_DLL_API int mclBn_getOpUnitSize(); 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 decimal string of the order of the curve(=the characteristic of Fr)
return str(buf) if success return str(buf) if success

@ -0,0 +1,6 @@
/*
implementation of mclBn_* apis
*/
#define MCLBN_FP_UNIT_SIZE 8
#include "bn_c_impl.hpp"

@ -123,14 +123,14 @@ int mclBn_init(int curve, int maxUnitSize)
try try
{ {
if (maxUnitSize != MCLBN_FP_UNIT_SIZE) { 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; return -1;
} }
const mcl::bn::CurveParam& cp = mcl::bn::getCurveParam(curve); const mcl::bn::CurveParam& cp = mcl::bn::getCurveParam(curve);
initPairing(cp); initPairing(cp);
return 0; return 0;
} catch (std::exception& e) { } catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "%s\n", e.what()); fprintf(stderr, "%s\n", e.what());
return -1; return -1;
} }
@ -139,6 +139,11 @@ int mclBn_getOpUnitSize()
return (int)Fp::getUnitSize() * sizeof(mcl::fp::Unit) / sizeof(uint64_t); 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) mclSize copyStrAndReturnSize(char *buf, mclSize maxBufSize, const std::string& str)
{ {
if (str.size() >= maxBufSize) return 0; if (str.size() >= maxBufSize) return 0;

@ -30,11 +30,17 @@ CYBOZU_TEST_AUTO(init)
#if MCLBN_FP_UNIT_SIZE == 4 #if MCLBN_FP_UNIT_SIZE == 4
printf("test MCLBN_curveFp254BNb %d\n", MCLBN_FP_UNIT_SIZE); printf("test MCLBN_curveFp254BNb %d\n", MCLBN_FP_UNIT_SIZE);
ret = mclBn_init(mclBn_CurveFp254BNb, 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); printf("test MCLBN_curveFp382_1 %d\n", MCLBN_FP_UNIT_SIZE);
ret = mclBn_init(mclBn_CurveFp382_1, 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 #endif
CYBOZU_TEST_EQUAL(ret, 0); CYBOZU_TEST_EQUAL(ret, 0);
if (ret != 0) exit(1);
} }
CYBOZU_TEST_AUTO(Fr) CYBOZU_TEST_AUTO(Fr)
@ -42,8 +48,6 @@ CYBOZU_TEST_AUTO(Fr)
mclBnFr x, y; mclBnFr x, y;
memset(&x, 0xff, sizeof(x)); memset(&x, 0xff, sizeof(x));
CYBOZU_TEST_ASSERT(!mclBnFr_isValid(&x)); CYBOZU_TEST_ASSERT(!mclBnFr_isValid(&x));
memset(&x, 1, sizeof(x));
CYBOZU_TEST_ASSERT(mclBnFr_isValid(&x));
CYBOZU_TEST_ASSERT(!mclBnFr_isZero(&x)); CYBOZU_TEST_ASSERT(!mclBnFr_isZero(&x));
mclBnFr_clear(&x); mclBnFr_clear(&x);
@ -333,7 +337,7 @@ CYBOZU_TEST_AUTO(precomputed)
CYBOZU_TEST_AUTO(serialize) CYBOZU_TEST_AUTO(serialize)
{ {
const size_t opUnitSize = mclBn_getOpUnitSize(); const size_t G1Size = mclBn_getG1ByteSize();
mclBnFr x1, x2; mclBnFr x1, x2;
mclBnG1 P1, P2; mclBnG1 P1, P2;
mclBnG2 Q1, Q2; mclBnG2 Q1, Q2;
@ -342,7 +346,7 @@ CYBOZU_TEST_AUTO(serialize)
size_t expectSize; size_t expectSize;
int ret; int ret;
// Fr // Fr
expectSize = opUnitSize * 8; expectSize = G1Size;
mclBnFr_setInt(&x1, -1); mclBnFr_setInt(&x1, -1);
n = mclBnFr_serialize(buf, sizeof(buf), &x1); n = mclBnFr_serialize(buf, sizeof(buf), &x1);
CYBOZU_TEST_EQUAL(n, expectSize); CYBOZU_TEST_EQUAL(n, expectSize);
@ -363,7 +367,7 @@ CYBOZU_TEST_AUTO(serialize)
CYBOZU_TEST_EQUAL(n, expectSize); CYBOZU_TEST_EQUAL(n, expectSize);
// G1 // G1
expectSize = opUnitSize * 8; expectSize = G1Size;
mclBnG1_hashAndMapTo(&P1, "1", 1); mclBnG1_hashAndMapTo(&P1, "1", 1);
n = mclBnG1_serialize(buf, sizeof(buf), &P1); n = mclBnG1_serialize(buf, sizeof(buf), &P1);
CYBOZU_TEST_EQUAL(n, expectSize); CYBOZU_TEST_EQUAL(n, expectSize);
@ -384,7 +388,7 @@ CYBOZU_TEST_AUTO(serialize)
CYBOZU_TEST_EQUAL(n, expectSize); CYBOZU_TEST_EQUAL(n, expectSize);
// G2 // G2
expectSize = opUnitSize * 8 * 2; expectSize = G1Size * 2;
mclBnG2_hashAndMapTo(&Q1, "1", 1); mclBnG2_hashAndMapTo(&Q1, "1", 1);
n = mclBnG2_serialize(buf, sizeof(buf), &Q1); n = mclBnG2_serialize(buf, sizeof(buf), &Q1);
CYBOZU_TEST_EQUAL(n, expectSize); CYBOZU_TEST_EQUAL(n, expectSize);

Loading…
Cancel
Save