From 160b6afa742ae7daaacddbc5ffcf597d23d987ba Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sat, 15 Sep 2018 22:48:11 +0900 Subject: test for 256/384 --- Makefile | 44 ++--- sample/bls_smpl.cpp | 3 +- src/bls_c256.cpp | 3 + test/bls256_test.cpp | 3 + test/bls384_test.cpp | 3 + test/bls_c256_test.cpp | 2 + test/bls_c384_test.cpp | 322 +----------------------------------- test/bls_c_test.hpp | 320 +++++++++++++++++++++++++++++++++++ test/bls_test.cpp | 439 ------------------------------------------------- test/bls_test.hpp | 439 +++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 798 insertions(+), 780 deletions(-) create mode 100644 src/bls_c256.cpp create mode 100644 test/bls256_test.cpp create mode 100644 test/bls384_test.cpp create mode 100644 test/bls_c256_test.cpp create mode 100644 test/bls_c_test.hpp delete mode 100644 test/bls_test.cpp create mode 100644 test/bls_test.hpp diff --git a/Makefile b/Makefile index 83e8d83..7b8252b 100644 --- a/Makefile +++ b/Makefile @@ -5,20 +5,11 @@ EXE_DIR=bin CFLAGS += -std=c++11 LDFLAGS += -lpthread -SRC_SRC=bls_c384.cpp -TEST_SRC=bls_test.cpp bls_c384_test.cpp -SAMPLE_SRC=bls_smpl.cpp +SRC_SRC=bls_c256.cpp bls_c384.cpp +TEST_SRC=bls256_test.cpp bls384_test.cpp bls_c256_test.cpp bls_c384_test.cpp +SAMPLE_SRC=bls256_smpl.cpp bls384_smpl.cpp CFLAGS+=-I../mcl/include -I./ -UNIT?=6 -ifeq ($(UNIT),4) - CFLAGS+=-D"MCLBN_FP_UNIT_SIZE=4" - GO_TAG=bn256 -endif -ifeq ($(UNIT),6) - CFLAGS+=-D"MCLBN_FP_UNIT_SIZE=6" - GO_TAG=bn384 -endif ifneq ($(MCL_MAX_BIT_SIZE),) CFLAGS+=-DMCL_MAX_BIT_SIZE=$(MCL_MAX_BIT_SIZE) endif @@ -28,27 +19,36 @@ endif SHARE_BASENAME_SUF?=_dy +BLS256_LIB=$(LIB_DIR)/libbls256.a BLS384_LIB=$(LIB_DIR)/libbls384.a +BLS256_SNAME=bls256$(SHARE_BASENAME_SUF) BLS384_SNAME=bls384$(SHARE_BASENAME_SUF) +BLS256_SLIB=$(LIB_DIR)/lib$(BLS256_SNAME).$(LIB_SUF) BLS384_SLIB=$(LIB_DIR)/lib$(BLS384_SNAME).$(LIB_SUF) -all: $(BLS384_LIB) $(BLS384_SLIB) +all: $(BLS256_LIB) $(BLS256_SLIB) $(BLS384_LIB) $(BLS384_SLIB) MCL_LIB=../mcl/lib/libmcl.a $(MCL_LIB): $(MAKE) -C ../mcl +$(BLS256_LIB): $(OBJ_DIR)/bls_c256.o + $(AR) $@ $< $(BLS384_LIB): $(OBJ_DIR)/bls_c384.o - $(AR) $@ $(OBJ_DIR)/bls_c384.o + $(AR) $@ $< ifneq ($(findstring $(OS),mac/mingw64),) + BLS256_SLIB_LDFLAGS+=-lgmpxx -lgmp -lcrypto -lstdc++ BLS384_SLIB_LDFLAGS+=-lgmpxx -lgmp -lcrypto -lstdc++ endif ifeq ($(OS),mingw64) + BLS256_SLIB_LDFLAGS+=-Wl,--out-implib,$(LIB_DIR)/lib$(BLS256_SNAME).a BLS384_SLIB_LDFLAGS+=-Wl,--out-implib,$(LIB_DIR)/lib$(BLS384_SNAME).a endif +$(BLS256_SLIB): $(OBJ_DIR)/bls_c256.o $(MCL_LIB) + $(PRE)$(CXX) -shared -o $@ $< $(MCL_LIB) $(BLS256_SLIB_LDFLAGS) $(BLS384_SLIB): $(OBJ_DIR)/bls_c384.o $(MCL_LIB) - $(PRE)$(CXX) -shared -o $@ $(OBJ_DIR)/bls_c384.o $(MCL_LIB) $(BLS384_SLIB_LDFLAGS) + $(PRE)$(CXX) -shared -o $@ $< $(MCL_LIB) $(BLS384_SLIB_LDFLAGS) VPATH=test sample src @@ -57,12 +57,16 @@ VPATH=test sample src $(OBJ_DIR)/%.o: %.cpp $(PRE)$(CXX) $(CFLAGS) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) -$(OBJ_DIR)/bls_c384.o: bls_c384.cpp - $(PRE)$(CXX) $(CFLAGS) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) -DMCL_FP_UNIT_SIZE=6 - -$(EXE_DIR)/%.exe: $(OBJ_DIR)/%.o $(BLS384_LIB) $(MCL_LIB) +$(EXE_DIR)/%384_test.exe: $(OBJ_DIR)/%384_test.o $(BLS384_LIB) $(MCL_LIB) $(PRE)$(CXX) $< -o $@ $(BLS384_LIB) -lmcl -L../mcl/lib $(LDFLAGS) +$(EXE_DIR)/%256_test.exe: $(OBJ_DIR)/%256_test.o $(BLS256_LIB) $(MCL_LIB) + $(PRE)$(CXX) $< -o $@ $(BLS256_LIB) -lmcl -L../mcl/lib $(LDFLAGS) + +# sample exe links libbls256.a +$(EXE_DIR)/%.exe: $(OBJ_DIR)/%.o $(BLS256_LIB) $(MCL_LIB) + $(PRE)$(CXX) $< -o $@ $(BLS256_LIB) -lmcl -L../mcl/lib $(LDFLAGS) + SAMPLE_EXE=$(addprefix $(EXE_DIR)/,$(SAMPLE_SRC:.cpp=.exe)) sample: $(SAMPLE_EXE) @@ -98,7 +102,7 @@ bls-wasm: $(MAKE) ../bls-wasm/bls_c.js clean: - $(RM) $(OBJ_DIR)/*.d $(OBJ_DIR)/*.o $(EXE_DIR)/*.exe $(GEN_EXE) $(ASM_SRC) $(ASM_OBJ) $(LLVM_SRC) $(BLS384_LIB) $(BLS384_SLIB) + $(RM) $(OBJ_DIR)/*.d $(OBJ_DIR)/*.o $(EXE_DIR)/*.exe $(GEN_EXE) $(ASM_SRC) $(ASM_OBJ) $(LLVM_SRC) $(BLS256_LIB) $(BLS256_SLIB) $(BLS384_LIB) $(BLS384_SLIB) ALL_SRC=$(SRC_SRC) $(TEST_SRC) $(SAMPLE_SRC) DEPEND_FILE=$(addprefix $(OBJ_DIR)/, $(ALL_SRC:.cpp=.d)) diff --git a/sample/bls_smpl.cpp b/sample/bls_smpl.cpp index 5e0c70c..e812cd5 100644 --- a/sample/bls_smpl.cpp +++ b/sample/bls_smpl.cpp @@ -1,3 +1,4 @@ +#define MCLBN_FP_UNIT_SIZE 4 #include #include #include @@ -118,7 +119,7 @@ int recover(const bls::IdVec& ids) int main(int argc, char *argv[]) try { - bls::init(); + bls::init(); // use BN254 std::string mode; std::string m; diff --git a/src/bls_c256.cpp b/src/bls_c256.cpp new file mode 100644 index 0000000..a9f3412 --- /dev/null +++ b/src/bls_c256.cpp @@ -0,0 +1,3 @@ +#define MCLBN_FP_UNIT_SIZE 4 +#include "bls_c_impl.hpp" + diff --git a/test/bls256_test.cpp b/test/bls256_test.cpp new file mode 100644 index 0000000..e53a870 --- /dev/null +++ b/test/bls256_test.cpp @@ -0,0 +1,3 @@ +#define MCLBN_FP_UNIT_SIZE 4 +#include "bls_test.hpp" + diff --git a/test/bls384_test.cpp b/test/bls384_test.cpp new file mode 100644 index 0000000..2212f8e --- /dev/null +++ b/test/bls384_test.cpp @@ -0,0 +1,3 @@ +#define MCLBN_FP_UNIT_SIZE 6 +#include "bls_test.hpp" + diff --git a/test/bls_c256_test.cpp b/test/bls_c256_test.cpp new file mode 100644 index 0000000..8613720 --- /dev/null +++ b/test/bls_c256_test.cpp @@ -0,0 +1,2 @@ +#define MCLBN_FP_UNIT_SIZE 4 +#include "bls_c_test.hpp" diff --git a/test/bls_c384_test.cpp b/test/bls_c384_test.cpp index ede3596..b6886dd 100644 --- a/test/bls_c384_test.cpp +++ b/test/bls_c384_test.cpp @@ -1,320 +1,2 @@ -#include -#include -#include -#include - -void bls_use_stackTest() -{ - blsSecretKey sec; - blsPublicKey pub; - blsSignature sig; - const char *msg = "this is a pen"; - const size_t msgSize = strlen(msg); - - blsSecretKeySetByCSPRNG(&sec); - - blsGetPublicKey(&pub, &sec); - - blsSign(&sig, &sec, msg, msgSize); - - CYBOZU_TEST_ASSERT(blsVerify(&sig, &pub, msg, msgSize)); -} - -void blsDataTest() -{ - const char *msg = "test test"; - const size_t msgSize = strlen(msg); - const size_t FrSize = blsGetFrByteSize(); - const size_t FpSize = blsGetG1ByteSize(); - blsSecretKey sec1, sec2; - blsSecretKeySetByCSPRNG(&sec1); - char buf[1024]; - size_t n; - size_t ret; - n = blsSecretKeyGetHexStr(buf, sizeof(buf), &sec1); - CYBOZU_TEST_ASSERT(0 < n && n <= FrSize * 2); - ret = blsSecretKeySetHexStr(&sec2, buf, n); - CYBOZU_TEST_EQUAL(ret, 0); - CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec1, &sec2)); - - memset(&sec2, 0, sizeof(sec2)); - n = blsSecretKeySerialize(buf, sizeof(buf), &sec1); - CYBOZU_TEST_EQUAL(n, FrSize); - ret = blsSecretKeyDeserialize(&sec2, buf, n); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec1, &sec2)); - - blsPublicKey pub1, pub2; - blsGetPublicKey(&pub1, &sec1); - n = blsPublicKeySerialize(buf, sizeof(buf), &pub1); - CYBOZU_TEST_EQUAL(n, FpSize * 2); - ret = blsPublicKeyDeserialize(&pub2, buf, n); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub1, &pub2)); - blsSignature sig1, sig2; - blsSign(&sig1, &sec1, msg, msgSize); - n = blsSignatureSerialize(buf, sizeof(buf), &sig1); - CYBOZU_TEST_EQUAL(n, FpSize); - ret = blsSignatureDeserialize(&sig2, buf, n); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig1, &sig2)); -} - -void blsOrderTest(const char *curveOrder, const char *fieldOrder) -{ - char buf[1024]; - size_t len; - len = blsGetCurveOrder(buf, sizeof(buf)); - CYBOZU_TEST_ASSERT(len > 0); - CYBOZU_TEST_EQUAL(buf, curveOrder); - len = blsGetFieldOrder(buf, sizeof(buf)); - CYBOZU_TEST_ASSERT(len > 0); - 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 -#include -struct Thread { - std::unique_ptr t; - Thread() : t() {} - ~Thread() - { - if (t) { - t->join(); - } - } - template - void run(F func, int p1, int p2) - { - t.reset(new std::thread(func, p1, p2)); - } -}; - -CYBOZU_TEST_AUTO(multipleInit) -{ - const size_t n = 100; - { - std::vector vt(n); - for (size_t i = 0; i < n; i++) { - vt[i].run(blsInit, MCL_BN254, MCLBN_FP_UNIT_SIZE); - } - } - CYBOZU_TEST_EQUAL(blsGetOpUnitSize(), 4u); -#if MCLBN_FP_UNIT_SIZE == 6 - { - std::vector vt(n); - for (size_t i = 0; i < n; i++) { - vt[i].run(blsInit, MCL_BLS12_381, MCLBN_FP_UNIT_SIZE); - } - } - CYBOZU_TEST_EQUAL(blsGetOpUnitSize(), 6u); -#endif -} -#endif -#endif - -void blsSerializeTest() -{ - const size_t FrSize = blsGetFrByteSize(); - const size_t FpSize = blsGetG1ByteSize(); - printf("FrSize=%d, FpSize=%d\n", (int)FrSize, (int)FpSize); - blsId id1, id2; - blsSecretKey sec1, sec2; - blsPublicKey pub1, pub2; - blsSignature sig1, sig2; - char buf[1024]; - size_t n; - size_t expectSize; - size_t ret; - const char dummyChar = '1'; - - // Id - expectSize = FrSize; - blsIdSetInt(&id1, -1); - n = blsIdSerialize(buf, sizeof(buf), &id1); - CYBOZU_TEST_EQUAL(n, expectSize); - - ret = blsIdDeserialize(&id2, buf, n); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsIdIsEqual(&id1, &id2)); - - ret = blsIdDeserialize(&id2, buf, n - 1); - CYBOZU_TEST_EQUAL(ret, 0); - - memset(&id2, 0, sizeof(id2)); - buf[n] = dummyChar; - ret = blsIdDeserialize(&id2, buf, n + 1); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsIdIsEqual(&id1, &id2)); - - n = blsIdSerialize(buf, expectSize, &id1); - CYBOZU_TEST_EQUAL(n, expectSize); - - // SecretKey - expectSize = FrSize; - blsSecretKeySetDecStr(&sec1, "-1", 2); - n = blsSecretKeySerialize(buf, sizeof(buf), &sec1); - CYBOZU_TEST_EQUAL(n, expectSize); - - ret = blsSecretKeyDeserialize(&sec2, buf, n); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec1, &sec2)); - - ret = blsSecretKeyDeserialize(&sec2, buf, n - 1); - CYBOZU_TEST_EQUAL(ret, 0); - - memset(&sec2, 0, sizeof(sec2)); - buf[n] = dummyChar; - ret = blsSecretKeyDeserialize(&sec2, buf, n + 1); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec1, &sec2)); - - n = blsSecretKeySerialize(buf, expectSize, &sec1); - CYBOZU_TEST_EQUAL(n, expectSize); - - // PublicKey - expectSize = FpSize * 2; - blsGetPublicKey(&pub1, &sec1); - n = blsPublicKeySerialize(buf, sizeof(buf), &pub1); - CYBOZU_TEST_EQUAL(n, expectSize); - CYBOZU_TEST_ASSERT(blsPublicKeyIsValidOrder(&pub1)); - - ret = blsPublicKeyDeserialize(&pub2, buf, n); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub1, &pub2)); - - ret = blsPublicKeyDeserialize(&pub2, buf, n - 1); - CYBOZU_TEST_EQUAL(ret, 0); - - memset(&pub2, 0, sizeof(pub2)); - buf[n] = dummyChar; - ret = blsPublicKeyDeserialize(&pub2, buf, n + 1); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub1, &pub2)); - - n = blsPublicKeySerialize(buf, expectSize, &pub1); - CYBOZU_TEST_EQUAL(n, expectSize); - - // Signature - expectSize = FpSize; - blsSign(&sig1, &sec1, "abc", 3); - n = blsSignatureSerialize(buf, sizeof(buf), &sig1); - CYBOZU_TEST_EQUAL(n, expectSize); - CYBOZU_TEST_ASSERT(blsSignatureIsValidOrder(&sig1)); - - ret = blsSignatureDeserialize(&sig2, buf, n); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig1, &sig2)); - - ret = blsSignatureDeserialize(&sig2, buf, n - 1); - CYBOZU_TEST_EQUAL(ret, 0); - - memset(&sig2, 0, sizeof(sig2)); - buf[n] = dummyChar; - ret = blsSignatureDeserialize(&sig2, buf, n + 1); - CYBOZU_TEST_EQUAL(ret, n); - CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig1, &sig2)); - - n = blsSignatureSerialize(buf, expectSize, &sig1); - CYBOZU_TEST_EQUAL(n, expectSize); -} - -void blsVerifyOrderTest() -{ - puts("blsVerifyOrderTest"); - const uint8_t Ps[] = { -0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, - }; - const uint8_t Qs[] = { -0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, - }; - size_t n; - blsPublicKey pub; - n = blsPublicKeyDeserialize(&pub, Ps, sizeof(Ps)); - CYBOZU_TEST_EQUAL(n, 0); - blsPublicKeyVerifyOrder(0); - n = blsPublicKeyDeserialize(&pub, Ps, sizeof(Ps)); - CYBOZU_TEST_ASSERT(n > 0); - CYBOZU_TEST_ASSERT(!blsPublicKeyIsValidOrder(&pub)); - blsPublicKeyVerifyOrder(1); - - blsSignature sig; - n = blsSignatureDeserialize(&sig, Qs, sizeof(Ps)); - CYBOZU_TEST_EQUAL(n, 0); - blsSignatureVerifyOrder(0); - n = blsSignatureDeserialize(&sig, Qs, sizeof(Ps)); - CYBOZU_TEST_ASSERT(n > 0); - CYBOZU_TEST_ASSERT(!blsSignatureIsValidOrder(&sig)); - blsSignatureVerifyOrder(1); -} - -void blsAddSubTest() -{ - blsSecretKey sec[3]; - blsPublicKey pub[3]; - blsSignature sig[3]; - const char *msg = "this is a pen"; - const size_t msgSize = strlen(msg); - - const char *secHexStr[8] = { "12", "34" }; - for (int i = 0; i < 2; i++) { - blsSecretKeySetHexStr(&sec[i], secHexStr[i], strlen(secHexStr[i])); - blsGetPublicKey(&pub[i], &sec[i]); - blsSign(&sig[i], &sec[i], msg, msgSize); - } - sec[2] = sec[0]; - blsSecretKeyAdd(&sec[2], &sec[1]); - char buf[1024]; - size_t n = blsSecretKeyGetHexStr(buf, sizeof(buf), &sec[2]); - CYBOZU_TEST_EQUAL(n, 2); - CYBOZU_TEST_EQUAL(buf, "46"); // "12" + "34" - - pub[2] = pub[0]; - blsPublicKeyAdd(&pub[2], &pub[1]); - sig[2] = sig[0]; - blsSignatureAdd(&sig[2], &sig[1]); // sig[2] = sig[0] + sig[1] - blsSignature sig2; - blsSign(&sig2, &sec[2], msg, msgSize); // sig2 = signature by sec[2] - CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig2, &sig[2])); - CYBOZU_TEST_ASSERT(blsVerify(&sig[2], &pub[2], msg, msgSize)); // verify by pub[2] - - blsSecretKeySub(&sec[2], &sec[1]); - CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec[2], &sec[0])); - blsPublicKeySub(&pub[2], &pub[1]); - CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub[2], &pub[0])); - blsSignatureSub(&sig[2], &sig[1]); - CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig[2], &sig[0])); -} - -CYBOZU_TEST_AUTO(all) -{ - const int tbl[] = { - MCL_BN254, -#if MCLBN_FP_UNIT_SIZE == 6 - MCL_BN381_1, - MCL_BLS12_381, -#endif - }; - const char *curveOrderTbl[] = { - "16798108731015832284940804142231733909759579603404752749028378864165570215949", - "5540996953667913971058039301942914304734176495422447785042938606876043190415948413757785063597439175372845535461389", - "52435875175126190479447740508185965837690552500527637822603658699938581184513", - }; - const char *fieldOrderTbl[] = { - "16798108731015832284940804142231733909889187121439069848933715426072753864723", - "5540996953667913971058039301942914304734176495422447785045292539108217242186829586959562222833658991069414454984723", - "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787", - }; - for (size_t i = 0; i < sizeof(tbl) / sizeof(tbl[0]); i++) { - printf("i=%d\n", (int)i); - blsInit(tbl[i], MCLBN_FP_UNIT_SIZE); - bls_use_stackTest(); - blsDataTest(); - blsOrderTest(curveOrderTbl[i], fieldOrderTbl[i]); - blsSerializeTest(); - if (tbl[i] == MCL_BLS12_381) blsVerifyOrderTest(); - blsAddSubTest(); - } -} +#define MCLBN_FP_UNIT_SIZE 6 +#include "bls_c_test.hpp" diff --git a/test/bls_c_test.hpp b/test/bls_c_test.hpp new file mode 100644 index 0000000..ede3596 --- /dev/null +++ b/test/bls_c_test.hpp @@ -0,0 +1,320 @@ +#include +#include +#include +#include + +void bls_use_stackTest() +{ + blsSecretKey sec; + blsPublicKey pub; + blsSignature sig; + const char *msg = "this is a pen"; + const size_t msgSize = strlen(msg); + + blsSecretKeySetByCSPRNG(&sec); + + blsGetPublicKey(&pub, &sec); + + blsSign(&sig, &sec, msg, msgSize); + + CYBOZU_TEST_ASSERT(blsVerify(&sig, &pub, msg, msgSize)); +} + +void blsDataTest() +{ + const char *msg = "test test"; + const size_t msgSize = strlen(msg); + const size_t FrSize = blsGetFrByteSize(); + const size_t FpSize = blsGetG1ByteSize(); + blsSecretKey sec1, sec2; + blsSecretKeySetByCSPRNG(&sec1); + char buf[1024]; + size_t n; + size_t ret; + n = blsSecretKeyGetHexStr(buf, sizeof(buf), &sec1); + CYBOZU_TEST_ASSERT(0 < n && n <= FrSize * 2); + ret = blsSecretKeySetHexStr(&sec2, buf, n); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec1, &sec2)); + + memset(&sec2, 0, sizeof(sec2)); + n = blsSecretKeySerialize(buf, sizeof(buf), &sec1); + CYBOZU_TEST_EQUAL(n, FrSize); + ret = blsSecretKeyDeserialize(&sec2, buf, n); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec1, &sec2)); + + blsPublicKey pub1, pub2; + blsGetPublicKey(&pub1, &sec1); + n = blsPublicKeySerialize(buf, sizeof(buf), &pub1); + CYBOZU_TEST_EQUAL(n, FpSize * 2); + ret = blsPublicKeyDeserialize(&pub2, buf, n); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub1, &pub2)); + blsSignature sig1, sig2; + blsSign(&sig1, &sec1, msg, msgSize); + n = blsSignatureSerialize(buf, sizeof(buf), &sig1); + CYBOZU_TEST_EQUAL(n, FpSize); + ret = blsSignatureDeserialize(&sig2, buf, n); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig1, &sig2)); +} + +void blsOrderTest(const char *curveOrder, const char *fieldOrder) +{ + char buf[1024]; + size_t len; + len = blsGetCurveOrder(buf, sizeof(buf)); + CYBOZU_TEST_ASSERT(len > 0); + CYBOZU_TEST_EQUAL(buf, curveOrder); + len = blsGetFieldOrder(buf, sizeof(buf)); + CYBOZU_TEST_ASSERT(len > 0); + 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 +#include +struct Thread { + std::unique_ptr t; + Thread() : t() {} + ~Thread() + { + if (t) { + t->join(); + } + } + template + void run(F func, int p1, int p2) + { + t.reset(new std::thread(func, p1, p2)); + } +}; + +CYBOZU_TEST_AUTO(multipleInit) +{ + const size_t n = 100; + { + std::vector vt(n); + for (size_t i = 0; i < n; i++) { + vt[i].run(blsInit, MCL_BN254, MCLBN_FP_UNIT_SIZE); + } + } + CYBOZU_TEST_EQUAL(blsGetOpUnitSize(), 4u); +#if MCLBN_FP_UNIT_SIZE == 6 + { + std::vector vt(n); + for (size_t i = 0; i < n; i++) { + vt[i].run(blsInit, MCL_BLS12_381, MCLBN_FP_UNIT_SIZE); + } + } + CYBOZU_TEST_EQUAL(blsGetOpUnitSize(), 6u); +#endif +} +#endif +#endif + +void blsSerializeTest() +{ + const size_t FrSize = blsGetFrByteSize(); + const size_t FpSize = blsGetG1ByteSize(); + printf("FrSize=%d, FpSize=%d\n", (int)FrSize, (int)FpSize); + blsId id1, id2; + blsSecretKey sec1, sec2; + blsPublicKey pub1, pub2; + blsSignature sig1, sig2; + char buf[1024]; + size_t n; + size_t expectSize; + size_t ret; + const char dummyChar = '1'; + + // Id + expectSize = FrSize; + blsIdSetInt(&id1, -1); + n = blsIdSerialize(buf, sizeof(buf), &id1); + CYBOZU_TEST_EQUAL(n, expectSize); + + ret = blsIdDeserialize(&id2, buf, n); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsIdIsEqual(&id1, &id2)); + + ret = blsIdDeserialize(&id2, buf, n - 1); + CYBOZU_TEST_EQUAL(ret, 0); + + memset(&id2, 0, sizeof(id2)); + buf[n] = dummyChar; + ret = blsIdDeserialize(&id2, buf, n + 1); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsIdIsEqual(&id1, &id2)); + + n = blsIdSerialize(buf, expectSize, &id1); + CYBOZU_TEST_EQUAL(n, expectSize); + + // SecretKey + expectSize = FrSize; + blsSecretKeySetDecStr(&sec1, "-1", 2); + n = blsSecretKeySerialize(buf, sizeof(buf), &sec1); + CYBOZU_TEST_EQUAL(n, expectSize); + + ret = blsSecretKeyDeserialize(&sec2, buf, n); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec1, &sec2)); + + ret = blsSecretKeyDeserialize(&sec2, buf, n - 1); + CYBOZU_TEST_EQUAL(ret, 0); + + memset(&sec2, 0, sizeof(sec2)); + buf[n] = dummyChar; + ret = blsSecretKeyDeserialize(&sec2, buf, n + 1); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec1, &sec2)); + + n = blsSecretKeySerialize(buf, expectSize, &sec1); + CYBOZU_TEST_EQUAL(n, expectSize); + + // PublicKey + expectSize = FpSize * 2; + blsGetPublicKey(&pub1, &sec1); + n = blsPublicKeySerialize(buf, sizeof(buf), &pub1); + CYBOZU_TEST_EQUAL(n, expectSize); + CYBOZU_TEST_ASSERT(blsPublicKeyIsValidOrder(&pub1)); + + ret = blsPublicKeyDeserialize(&pub2, buf, n); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub1, &pub2)); + + ret = blsPublicKeyDeserialize(&pub2, buf, n - 1); + CYBOZU_TEST_EQUAL(ret, 0); + + memset(&pub2, 0, sizeof(pub2)); + buf[n] = dummyChar; + ret = blsPublicKeyDeserialize(&pub2, buf, n + 1); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub1, &pub2)); + + n = blsPublicKeySerialize(buf, expectSize, &pub1); + CYBOZU_TEST_EQUAL(n, expectSize); + + // Signature + expectSize = FpSize; + blsSign(&sig1, &sec1, "abc", 3); + n = blsSignatureSerialize(buf, sizeof(buf), &sig1); + CYBOZU_TEST_EQUAL(n, expectSize); + CYBOZU_TEST_ASSERT(blsSignatureIsValidOrder(&sig1)); + + ret = blsSignatureDeserialize(&sig2, buf, n); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig1, &sig2)); + + ret = blsSignatureDeserialize(&sig2, buf, n - 1); + CYBOZU_TEST_EQUAL(ret, 0); + + memset(&sig2, 0, sizeof(sig2)); + buf[n] = dummyChar; + ret = blsSignatureDeserialize(&sig2, buf, n + 1); + CYBOZU_TEST_EQUAL(ret, n); + CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig1, &sig2)); + + n = blsSignatureSerialize(buf, expectSize, &sig1); + CYBOZU_TEST_EQUAL(n, expectSize); +} + +void blsVerifyOrderTest() +{ + puts("blsVerifyOrderTest"); + const uint8_t Ps[] = { +0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, + }; + const uint8_t Qs[] = { +0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, + }; + size_t n; + blsPublicKey pub; + n = blsPublicKeyDeserialize(&pub, Ps, sizeof(Ps)); + CYBOZU_TEST_EQUAL(n, 0); + blsPublicKeyVerifyOrder(0); + n = blsPublicKeyDeserialize(&pub, Ps, sizeof(Ps)); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_ASSERT(!blsPublicKeyIsValidOrder(&pub)); + blsPublicKeyVerifyOrder(1); + + blsSignature sig; + n = blsSignatureDeserialize(&sig, Qs, sizeof(Ps)); + CYBOZU_TEST_EQUAL(n, 0); + blsSignatureVerifyOrder(0); + n = blsSignatureDeserialize(&sig, Qs, sizeof(Ps)); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_ASSERT(!blsSignatureIsValidOrder(&sig)); + blsSignatureVerifyOrder(1); +} + +void blsAddSubTest() +{ + blsSecretKey sec[3]; + blsPublicKey pub[3]; + blsSignature sig[3]; + const char *msg = "this is a pen"; + const size_t msgSize = strlen(msg); + + const char *secHexStr[8] = { "12", "34" }; + for (int i = 0; i < 2; i++) { + blsSecretKeySetHexStr(&sec[i], secHexStr[i], strlen(secHexStr[i])); + blsGetPublicKey(&pub[i], &sec[i]); + blsSign(&sig[i], &sec[i], msg, msgSize); + } + sec[2] = sec[0]; + blsSecretKeyAdd(&sec[2], &sec[1]); + char buf[1024]; + size_t n = blsSecretKeyGetHexStr(buf, sizeof(buf), &sec[2]); + CYBOZU_TEST_EQUAL(n, 2); + CYBOZU_TEST_EQUAL(buf, "46"); // "12" + "34" + + pub[2] = pub[0]; + blsPublicKeyAdd(&pub[2], &pub[1]); + sig[2] = sig[0]; + blsSignatureAdd(&sig[2], &sig[1]); // sig[2] = sig[0] + sig[1] + blsSignature sig2; + blsSign(&sig2, &sec[2], msg, msgSize); // sig2 = signature by sec[2] + CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig2, &sig[2])); + CYBOZU_TEST_ASSERT(blsVerify(&sig[2], &pub[2], msg, msgSize)); // verify by pub[2] + + blsSecretKeySub(&sec[2], &sec[1]); + CYBOZU_TEST_ASSERT(blsSecretKeyIsEqual(&sec[2], &sec[0])); + blsPublicKeySub(&pub[2], &pub[1]); + CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub[2], &pub[0])); + blsSignatureSub(&sig[2], &sig[1]); + CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig[2], &sig[0])); +} + +CYBOZU_TEST_AUTO(all) +{ + const int tbl[] = { + MCL_BN254, +#if MCLBN_FP_UNIT_SIZE == 6 + MCL_BN381_1, + MCL_BLS12_381, +#endif + }; + const char *curveOrderTbl[] = { + "16798108731015832284940804142231733909759579603404752749028378864165570215949", + "5540996953667913971058039301942914304734176495422447785042938606876043190415948413757785063597439175372845535461389", + "52435875175126190479447740508185965837690552500527637822603658699938581184513", + }; + const char *fieldOrderTbl[] = { + "16798108731015832284940804142231733909889187121439069848933715426072753864723", + "5540996953667913971058039301942914304734176495422447785045292539108217242186829586959562222833658991069414454984723", + "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787", + }; + for (size_t i = 0; i < sizeof(tbl) / sizeof(tbl[0]); i++) { + printf("i=%d\n", (int)i); + blsInit(tbl[i], MCLBN_FP_UNIT_SIZE); + bls_use_stackTest(); + blsDataTest(); + blsOrderTest(curveOrderTbl[i], fieldOrderTbl[i]); + blsSerializeTest(); + if (tbl[i] == MCL_BLS12_381) blsVerifyOrderTest(); + blsAddSubTest(); + } +} diff --git a/test/bls_test.cpp b/test/bls_test.cpp deleted file mode 100644 index b2a7860..0000000 --- a/test/bls_test.cpp +++ /dev/null @@ -1,439 +0,0 @@ -#include -#include -#include -#include -#include -#include - -template -void streamTest(const T& t) -{ - std::ostringstream oss; - oss << t; - std::istringstream iss(oss.str()); - T t2; - iss >> t2; - CYBOZU_TEST_EQUAL(t, t2); -} - -template -void testSet() -{ - /* - mask value to be less than r if the value >= (1 << (192 + 62)) - */ - const uint64_t fff = uint64_t(-1); - const uint64_t one = uint64_t(1); - const struct { - uint64_t in; - uint64_t expected; - } tbl[] = { - { fff, (one << 61) - 1 }, // masked with (1 << 61) - 1 - { one << 62, 0 }, // masked - { (one << 62) | (one << 61), (one << 61) }, // masked - { (one << 61) - 1, (one << 61) - 1 }, // same - }; - T t1, t2; - for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { - uint64_t v1[] = { fff, fff, fff, tbl[i].in }; - uint64_t v2[] = { fff, fff, fff, tbl[i].expected }; - t1.set(v1); - t2.set(v2); - CYBOZU_TEST_EQUAL(t1, t2); - } -} - -void IdTestBN256() -{ - bls::Id id; - CYBOZU_TEST_ASSERT(id.isZero()); - id = 5; - CYBOZU_TEST_EQUAL(id, 5); - { - const uint64_t id1[] = { 1, 2, 3, 4 }; - id.set(id1); - std::ostringstream os; - os << id; - CYBOZU_TEST_EQUAL(os.str(), "0x4000000000000000300000000000000020000000000000001"); - } - testSet(); -} - -void SecretKeyTestBN256() -{ - testSet(); -} - -CYBOZU_TEST_AUTO(bn256) -{ - bls::init(MCL_BN254); - IdTestBN256(); - SecretKeyTestBN256(); - CYBOZU_TEST_EQUAL(bls::getOpUnitSize(), 4); -} - -void blsTest() -{ - bls::SecretKey sec; - sec.init(); - streamTest(sec); - bls::PublicKey pub; - sec.getPublicKey(pub); - streamTest(pub); - for (int i = 0; i < 5; i++) { - std::string m = "hello"; - m += char('0' + i); - bls::Signature sig; - sec.sign(sig, m); - CYBOZU_TEST_ASSERT(sig.verify(pub, m)); - CYBOZU_TEST_ASSERT(!sig.verify(pub, m + "a")); - streamTest(sig); - CYBOZU_BENCH_C("sign", 100, sec.sign, sig, m); - CYBOZU_BENCH_C("verify", 100, sig.verify, pub, m); - } -} - -void k_of_nTest() -{ - const std::string m = "abc"; - const int n = 5; - const int k = 3; - bls::SecretKey sec0; - sec0.init(); - bls::Signature sig0; - sec0.sign(sig0, m); - bls::PublicKey pub0; - sec0.getPublicKey(pub0); - CYBOZU_TEST_ASSERT(sig0.verify(pub0, m)); - - bls::SecretKeyVec msk; - sec0.getMasterSecretKey(msk, k); - - bls::SecretKeyVec allPrvVec(n); - bls::IdVec allIdVec(n); - for (int i = 0; i < n; i++) { - int id = i + 1; - allPrvVec[i].set(msk, id); - allIdVec[i] = id; - - bls::SecretKey p; - p.set(msk.data(), k, id); - CYBOZU_TEST_EQUAL(allPrvVec[i], p); - } - - bls::SignatureVec allSigVec(n); - for (int i = 0; i < n; i++) { - CYBOZU_TEST_ASSERT(allPrvVec[i] != sec0); - allPrvVec[i].sign(allSigVec[i], m); - bls::PublicKey pub; - allPrvVec[i].getPublicKey(pub); - CYBOZU_TEST_ASSERT(pub != pub0); - CYBOZU_TEST_ASSERT(allSigVec[i].verify(pub, m)); - } - - /* - 3-out-of-n - can recover - */ - bls::SecretKeyVec secVec(3); - bls::IdVec idVec(3); - for (int a = 0; a < n; a++) { - secVec[0] = allPrvVec[a]; - idVec[0] = allIdVec[a]; - for (int b = a + 1; b < n; b++) { - secVec[1] = allPrvVec[b]; - idVec[1] = allIdVec[b]; - for (int c = b + 1; c < n; c++) { - secVec[2] = allPrvVec[c]; - idVec[2] = allIdVec[c]; - bls::SecretKey sec; - sec.recover(secVec, idVec); - CYBOZU_TEST_EQUAL(sec, sec0); - bls::SecretKey sec2; - sec2.recover(secVec.data(), idVec.data(), secVec.size()); - CYBOZU_TEST_EQUAL(sec, sec2); - } - } - } - { - secVec[0] = allPrvVec[0]; - secVec[1] = allPrvVec[1]; - secVec[2] = allPrvVec[0]; // same of secVec[0] - idVec[0] = allIdVec[0]; - idVec[1] = allIdVec[1]; - idVec[2] = allIdVec[0]; - bls::SecretKey sec; - CYBOZU_TEST_EXCEPTION_MESSAGE(sec.recover(secVec, idVec), std::exception, "same id"); - } - { - /* - n-out-of-n - can recover - */ - bls::SecretKey sec; - sec.recover(allPrvVec, allIdVec); - CYBOZU_TEST_EQUAL(sec, sec0); - } - /* - 2-out-of-n - can't recover - */ - secVec.resize(2); - idVec.resize(2); - for (int a = 0; a < n; a++) { - secVec[0] = allPrvVec[a]; - idVec[0] = allIdVec[a]; - for (int b = a + 1; b < n; b++) { - secVec[1] = allPrvVec[b]; - idVec[1] = allIdVec[b]; - bls::SecretKey sec; - sec.recover(secVec, idVec); - CYBOZU_TEST_ASSERT(sec != sec0); - } - } - /* - 3-out-of-n - can recover - */ - bls::SignatureVec sigVec(3); - idVec.resize(3); - for (int a = 0; a < n; a++) { - sigVec[0] = allSigVec[a]; - idVec[0] = allIdVec[a]; - for (int b = a + 1; b < n; b++) { - sigVec[1] = allSigVec[b]; - idVec[1] = allIdVec[b]; - for (int c = b + 1; c < n; c++) { - sigVec[2] = allSigVec[c]; - idVec[2] = allIdVec[c]; - bls::Signature sig; - sig.recover(sigVec, idVec); - CYBOZU_TEST_EQUAL(sig, sig0); - } - } - } - { - sigVec[0] = allSigVec[1]; idVec[0] = allIdVec[1]; - sigVec[1] = allSigVec[4]; idVec[1] = allIdVec[4]; - sigVec[2] = allSigVec[3]; idVec[2] = allIdVec[3]; - bls::Signature sig; - CYBOZU_BENCH_C("sig.recover", 100, sig.recover, sigVec, idVec); - } - { - /* - n-out-of-n - can recover - */ - bls::Signature sig; - sig.recover(allSigVec, allIdVec); - CYBOZU_TEST_EQUAL(sig, sig0); - } - /* - 2-out-of-n - can't recover - */ - sigVec.resize(2); - idVec.resize(2); - for (int a = 0; a < n; a++) { - sigVec[0] = allSigVec[a]; - idVec[0] = allIdVec[a]; - for (int b = a + 1; b < n; b++) { - sigVec[1] = allSigVec[b]; - idVec[1] = allIdVec[b]; - bls::Signature sig; - sig.recover(sigVec, idVec); - CYBOZU_TEST_ASSERT(sig != sig0); - } - } - // share and recover publicKey - { - bls::PublicKeyVec pubVec(k); - idVec.resize(k); - // select [0, k) publicKey - for (int i = 0; i < k; i++) { - allPrvVec[i].getPublicKey(pubVec[i]); - idVec[i] = allIdVec[i]; - } - bls::PublicKey pub; - pub.recover(pubVec, idVec); - CYBOZU_TEST_EQUAL(pub, pub0); - bls::PublicKey pub2; - pub2.recover(pubVec.data(), idVec.data(), pubVec.size()); - CYBOZU_TEST_EQUAL(pub, pub2); - } -} - -void popTest() -{ - const size_t k = 3; - const size_t n = 6; - const std::string m = "pop test"; - bls::SecretKey sec0; - sec0.init(); - bls::PublicKey pub0; - sec0.getPublicKey(pub0); - bls::Signature sig0; - sec0.sign(sig0, m); - CYBOZU_TEST_ASSERT(sig0.verify(pub0, m)); - - bls::SecretKeyVec msk; - sec0.getMasterSecretKey(msk, k); - - bls::PublicKeyVec mpk; - bls::getMasterPublicKey(mpk, msk); - bls::SignatureVec popVec; - bls::getPopVec(popVec, msk); - - for (size_t i = 0; i < popVec.size(); i++) { - CYBOZU_TEST_ASSERT(popVec[i].verify(mpk[i])); - } - - const int idTbl[n] = { - 3, 5, 193, 22, 15 - }; - bls::SecretKeyVec secVec(n); - bls::PublicKeyVec pubVec(n); - bls::SignatureVec sVec(n); - for (size_t i = 0; i < n; i++) { - int id = idTbl[i]; - secVec[i].set(msk, id); - secVec[i].getPublicKey(pubVec[i]); - bls::PublicKey pub; - pub.set(mpk, id); - CYBOZU_TEST_EQUAL(pubVec[i], pub); - - bls::Signature pop; - secVec[i].getPop(pop); - CYBOZU_TEST_ASSERT(pop.verify(pubVec[i])); - - secVec[i].sign(sVec[i], m); - CYBOZU_TEST_ASSERT(sVec[i].verify(pubVec[i], m)); - } - secVec.resize(k); - sVec.resize(k); - bls::IdVec idVec(k); - for (size_t i = 0; i < k; i++) { - idVec[i] = idTbl[i]; - } - bls::SecretKey sec; - sec.recover(secVec, idVec); - CYBOZU_TEST_EQUAL(sec, sec0); - bls::Signature sig; - sig.recover(sVec, idVec); - CYBOZU_TEST_EQUAL(sig, sig0); - bls::Signature sig2; - sig2.recover(sVec.data(), idVec.data(), sVec.size()); - CYBOZU_TEST_EQUAL(sig, sig2); -} - -void addTest() -{ - bls::SecretKey sec1, sec2; - sec1.init(); - sec2.init(); - CYBOZU_TEST_ASSERT(sec1 != sec2); - - bls::PublicKey pub1, pub2; - sec1.getPublicKey(pub1); - sec2.getPublicKey(pub2); - - const std::string m = "doremi"; - bls::Signature sig1, sig2; - sec1.sign(sig1, m); - sec2.sign(sig2, m); - CYBOZU_TEST_ASSERT((sig1 + sig2).verify(pub1 + pub2, m)); -} - -void aggregateTest() -{ - const size_t n = 10; - bls::SecretKey secs[n]; - bls::PublicKey pubs[n], pub; - bls::Signature sigs[n], sig; - const std::string m = "abc"; - for (size_t i = 0; i < n; i++) { - secs[i].init(); - secs[i].getPublicKey(pubs[i]); - secs[i].sign(sigs[i], m); - } - pub = pubs[0]; - sig = sigs[0]; - for (size_t i = 1; i < n; i++) { - pub.add(pubs[i]); - sig.add(sigs[i]); - } - CYBOZU_TEST_ASSERT(sig.verify(pub, m)); -} - -void dataTest() -{ - const size_t FrSize = bls::getFrByteSize(); - const size_t FpSize = bls::getG1ByteSize(); - bls::SecretKey sec; - sec.init(); - std::string str; - sec.getStr(str, bls::IoFixedByteSeq); - { - CYBOZU_TEST_EQUAL(str.size(), FrSize); - bls::SecretKey sec2; - sec2.setStr(str, bls::IoFixedByteSeq); - CYBOZU_TEST_EQUAL(sec, sec2); - } - bls::PublicKey pub; - sec.getPublicKey(pub); - pub.getStr(str, bls::IoFixedByteSeq); - { - CYBOZU_TEST_EQUAL(str.size(), FpSize * 2); - bls::PublicKey pub2; - pub2.setStr(str, bls::IoFixedByteSeq); - CYBOZU_TEST_EQUAL(pub, pub2); - } - std::string m = "abc"; - bls::Signature sign; - sec.sign(sign, m); - sign.getStr(str, bls::IoFixedByteSeq); - { - CYBOZU_TEST_EQUAL(str.size(), FpSize); - bls::Signature sign2; - sign2.setStr(str, bls::IoFixedByteSeq); - CYBOZU_TEST_EQUAL(sign, sign2); - } - bls::Id id; - const uint64_t v[] = { 1, 2, 3, 4, 5, 6, }; - id.set(v); - id.getStr(str, bls::IoFixedByteSeq); - { - CYBOZU_TEST_EQUAL(str.size(), FrSize); - bls::Id id2; - id2.setStr(str, bls::IoFixedByteSeq); - CYBOZU_TEST_EQUAL(id, id2); - } -} - -void testAll() -{ - blsTest(); - k_of_nTest(); - popTest(); - addTest(); - dataTest(); - aggregateTest(); -} -CYBOZU_TEST_AUTO(all) -{ - const struct { - int type; - const char *name; - } tbl[] = { - { MCL_BN254, "BN254" }, -#if MCLBN_FP_UNIT_SIZE == 6 - { MCL_BN381_1, "BN381_1" }, - { MCL_BLS12_381, "BLS12_381" }, -#endif - }; - for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { - printf("curve=%s\n", tbl[i].name); - bls::init(tbl[i].type); - testAll(); - } -} diff --git a/test/bls_test.hpp b/test/bls_test.hpp new file mode 100644 index 0000000..b2a7860 --- /dev/null +++ b/test/bls_test.hpp @@ -0,0 +1,439 @@ +#include +#include +#include +#include +#include +#include + +template +void streamTest(const T& t) +{ + std::ostringstream oss; + oss << t; + std::istringstream iss(oss.str()); + T t2; + iss >> t2; + CYBOZU_TEST_EQUAL(t, t2); +} + +template +void testSet() +{ + /* + mask value to be less than r if the value >= (1 << (192 + 62)) + */ + const uint64_t fff = uint64_t(-1); + const uint64_t one = uint64_t(1); + const struct { + uint64_t in; + uint64_t expected; + } tbl[] = { + { fff, (one << 61) - 1 }, // masked with (1 << 61) - 1 + { one << 62, 0 }, // masked + { (one << 62) | (one << 61), (one << 61) }, // masked + { (one << 61) - 1, (one << 61) - 1 }, // same + }; + T t1, t2; + for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { + uint64_t v1[] = { fff, fff, fff, tbl[i].in }; + uint64_t v2[] = { fff, fff, fff, tbl[i].expected }; + t1.set(v1); + t2.set(v2); + CYBOZU_TEST_EQUAL(t1, t2); + } +} + +void IdTestBN256() +{ + bls::Id id; + CYBOZU_TEST_ASSERT(id.isZero()); + id = 5; + CYBOZU_TEST_EQUAL(id, 5); + { + const uint64_t id1[] = { 1, 2, 3, 4 }; + id.set(id1); + std::ostringstream os; + os << id; + CYBOZU_TEST_EQUAL(os.str(), "0x4000000000000000300000000000000020000000000000001"); + } + testSet(); +} + +void SecretKeyTestBN256() +{ + testSet(); +} + +CYBOZU_TEST_AUTO(bn256) +{ + bls::init(MCL_BN254); + IdTestBN256(); + SecretKeyTestBN256(); + CYBOZU_TEST_EQUAL(bls::getOpUnitSize(), 4); +} + +void blsTest() +{ + bls::SecretKey sec; + sec.init(); + streamTest(sec); + bls::PublicKey pub; + sec.getPublicKey(pub); + streamTest(pub); + for (int i = 0; i < 5; i++) { + std::string m = "hello"; + m += char('0' + i); + bls::Signature sig; + sec.sign(sig, m); + CYBOZU_TEST_ASSERT(sig.verify(pub, m)); + CYBOZU_TEST_ASSERT(!sig.verify(pub, m + "a")); + streamTest(sig); + CYBOZU_BENCH_C("sign", 100, sec.sign, sig, m); + CYBOZU_BENCH_C("verify", 100, sig.verify, pub, m); + } +} + +void k_of_nTest() +{ + const std::string m = "abc"; + const int n = 5; + const int k = 3; + bls::SecretKey sec0; + sec0.init(); + bls::Signature sig0; + sec0.sign(sig0, m); + bls::PublicKey pub0; + sec0.getPublicKey(pub0); + CYBOZU_TEST_ASSERT(sig0.verify(pub0, m)); + + bls::SecretKeyVec msk; + sec0.getMasterSecretKey(msk, k); + + bls::SecretKeyVec allPrvVec(n); + bls::IdVec allIdVec(n); + for (int i = 0; i < n; i++) { + int id = i + 1; + allPrvVec[i].set(msk, id); + allIdVec[i] = id; + + bls::SecretKey p; + p.set(msk.data(), k, id); + CYBOZU_TEST_EQUAL(allPrvVec[i], p); + } + + bls::SignatureVec allSigVec(n); + for (int i = 0; i < n; i++) { + CYBOZU_TEST_ASSERT(allPrvVec[i] != sec0); + allPrvVec[i].sign(allSigVec[i], m); + bls::PublicKey pub; + allPrvVec[i].getPublicKey(pub); + CYBOZU_TEST_ASSERT(pub != pub0); + CYBOZU_TEST_ASSERT(allSigVec[i].verify(pub, m)); + } + + /* + 3-out-of-n + can recover + */ + bls::SecretKeyVec secVec(3); + bls::IdVec idVec(3); + for (int a = 0; a < n; a++) { + secVec[0] = allPrvVec[a]; + idVec[0] = allIdVec[a]; + for (int b = a + 1; b < n; b++) { + secVec[1] = allPrvVec[b]; + idVec[1] = allIdVec[b]; + for (int c = b + 1; c < n; c++) { + secVec[2] = allPrvVec[c]; + idVec[2] = allIdVec[c]; + bls::SecretKey sec; + sec.recover(secVec, idVec); + CYBOZU_TEST_EQUAL(sec, sec0); + bls::SecretKey sec2; + sec2.recover(secVec.data(), idVec.data(), secVec.size()); + CYBOZU_TEST_EQUAL(sec, sec2); + } + } + } + { + secVec[0] = allPrvVec[0]; + secVec[1] = allPrvVec[1]; + secVec[2] = allPrvVec[0]; // same of secVec[0] + idVec[0] = allIdVec[0]; + idVec[1] = allIdVec[1]; + idVec[2] = allIdVec[0]; + bls::SecretKey sec; + CYBOZU_TEST_EXCEPTION_MESSAGE(sec.recover(secVec, idVec), std::exception, "same id"); + } + { + /* + n-out-of-n + can recover + */ + bls::SecretKey sec; + sec.recover(allPrvVec, allIdVec); + CYBOZU_TEST_EQUAL(sec, sec0); + } + /* + 2-out-of-n + can't recover + */ + secVec.resize(2); + idVec.resize(2); + for (int a = 0; a < n; a++) { + secVec[0] = allPrvVec[a]; + idVec[0] = allIdVec[a]; + for (int b = a + 1; b < n; b++) { + secVec[1] = allPrvVec[b]; + idVec[1] = allIdVec[b]; + bls::SecretKey sec; + sec.recover(secVec, idVec); + CYBOZU_TEST_ASSERT(sec != sec0); + } + } + /* + 3-out-of-n + can recover + */ + bls::SignatureVec sigVec(3); + idVec.resize(3); + for (int a = 0; a < n; a++) { + sigVec[0] = allSigVec[a]; + idVec[0] = allIdVec[a]; + for (int b = a + 1; b < n; b++) { + sigVec[1] = allSigVec[b]; + idVec[1] = allIdVec[b]; + for (int c = b + 1; c < n; c++) { + sigVec[2] = allSigVec[c]; + idVec[2] = allIdVec[c]; + bls::Signature sig; + sig.recover(sigVec, idVec); + CYBOZU_TEST_EQUAL(sig, sig0); + } + } + } + { + sigVec[0] = allSigVec[1]; idVec[0] = allIdVec[1]; + sigVec[1] = allSigVec[4]; idVec[1] = allIdVec[4]; + sigVec[2] = allSigVec[3]; idVec[2] = allIdVec[3]; + bls::Signature sig; + CYBOZU_BENCH_C("sig.recover", 100, sig.recover, sigVec, idVec); + } + { + /* + n-out-of-n + can recover + */ + bls::Signature sig; + sig.recover(allSigVec, allIdVec); + CYBOZU_TEST_EQUAL(sig, sig0); + } + /* + 2-out-of-n + can't recover + */ + sigVec.resize(2); + idVec.resize(2); + for (int a = 0; a < n; a++) { + sigVec[0] = allSigVec[a]; + idVec[0] = allIdVec[a]; + for (int b = a + 1; b < n; b++) { + sigVec[1] = allSigVec[b]; + idVec[1] = allIdVec[b]; + bls::Signature sig; + sig.recover(sigVec, idVec); + CYBOZU_TEST_ASSERT(sig != sig0); + } + } + // share and recover publicKey + { + bls::PublicKeyVec pubVec(k); + idVec.resize(k); + // select [0, k) publicKey + for (int i = 0; i < k; i++) { + allPrvVec[i].getPublicKey(pubVec[i]); + idVec[i] = allIdVec[i]; + } + bls::PublicKey pub; + pub.recover(pubVec, idVec); + CYBOZU_TEST_EQUAL(pub, pub0); + bls::PublicKey pub2; + pub2.recover(pubVec.data(), idVec.data(), pubVec.size()); + CYBOZU_TEST_EQUAL(pub, pub2); + } +} + +void popTest() +{ + const size_t k = 3; + const size_t n = 6; + const std::string m = "pop test"; + bls::SecretKey sec0; + sec0.init(); + bls::PublicKey pub0; + sec0.getPublicKey(pub0); + bls::Signature sig0; + sec0.sign(sig0, m); + CYBOZU_TEST_ASSERT(sig0.verify(pub0, m)); + + bls::SecretKeyVec msk; + sec0.getMasterSecretKey(msk, k); + + bls::PublicKeyVec mpk; + bls::getMasterPublicKey(mpk, msk); + bls::SignatureVec popVec; + bls::getPopVec(popVec, msk); + + for (size_t i = 0; i < popVec.size(); i++) { + CYBOZU_TEST_ASSERT(popVec[i].verify(mpk[i])); + } + + const int idTbl[n] = { + 3, 5, 193, 22, 15 + }; + bls::SecretKeyVec secVec(n); + bls::PublicKeyVec pubVec(n); + bls::SignatureVec sVec(n); + for (size_t i = 0; i < n; i++) { + int id = idTbl[i]; + secVec[i].set(msk, id); + secVec[i].getPublicKey(pubVec[i]); + bls::PublicKey pub; + pub.set(mpk, id); + CYBOZU_TEST_EQUAL(pubVec[i], pub); + + bls::Signature pop; + secVec[i].getPop(pop); + CYBOZU_TEST_ASSERT(pop.verify(pubVec[i])); + + secVec[i].sign(sVec[i], m); + CYBOZU_TEST_ASSERT(sVec[i].verify(pubVec[i], m)); + } + secVec.resize(k); + sVec.resize(k); + bls::IdVec idVec(k); + for (size_t i = 0; i < k; i++) { + idVec[i] = idTbl[i]; + } + bls::SecretKey sec; + sec.recover(secVec, idVec); + CYBOZU_TEST_EQUAL(sec, sec0); + bls::Signature sig; + sig.recover(sVec, idVec); + CYBOZU_TEST_EQUAL(sig, sig0); + bls::Signature sig2; + sig2.recover(sVec.data(), idVec.data(), sVec.size()); + CYBOZU_TEST_EQUAL(sig, sig2); +} + +void addTest() +{ + bls::SecretKey sec1, sec2; + sec1.init(); + sec2.init(); + CYBOZU_TEST_ASSERT(sec1 != sec2); + + bls::PublicKey pub1, pub2; + sec1.getPublicKey(pub1); + sec2.getPublicKey(pub2); + + const std::string m = "doremi"; + bls::Signature sig1, sig2; + sec1.sign(sig1, m); + sec2.sign(sig2, m); + CYBOZU_TEST_ASSERT((sig1 + sig2).verify(pub1 + pub2, m)); +} + +void aggregateTest() +{ + const size_t n = 10; + bls::SecretKey secs[n]; + bls::PublicKey pubs[n], pub; + bls::Signature sigs[n], sig; + const std::string m = "abc"; + for (size_t i = 0; i < n; i++) { + secs[i].init(); + secs[i].getPublicKey(pubs[i]); + secs[i].sign(sigs[i], m); + } + pub = pubs[0]; + sig = sigs[0]; + for (size_t i = 1; i < n; i++) { + pub.add(pubs[i]); + sig.add(sigs[i]); + } + CYBOZU_TEST_ASSERT(sig.verify(pub, m)); +} + +void dataTest() +{ + const size_t FrSize = bls::getFrByteSize(); + const size_t FpSize = bls::getG1ByteSize(); + bls::SecretKey sec; + sec.init(); + std::string str; + sec.getStr(str, bls::IoFixedByteSeq); + { + CYBOZU_TEST_EQUAL(str.size(), FrSize); + bls::SecretKey sec2; + sec2.setStr(str, bls::IoFixedByteSeq); + CYBOZU_TEST_EQUAL(sec, sec2); + } + bls::PublicKey pub; + sec.getPublicKey(pub); + pub.getStr(str, bls::IoFixedByteSeq); + { + CYBOZU_TEST_EQUAL(str.size(), FpSize * 2); + bls::PublicKey pub2; + pub2.setStr(str, bls::IoFixedByteSeq); + CYBOZU_TEST_EQUAL(pub, pub2); + } + std::string m = "abc"; + bls::Signature sign; + sec.sign(sign, m); + sign.getStr(str, bls::IoFixedByteSeq); + { + CYBOZU_TEST_EQUAL(str.size(), FpSize); + bls::Signature sign2; + sign2.setStr(str, bls::IoFixedByteSeq); + CYBOZU_TEST_EQUAL(sign, sign2); + } + bls::Id id; + const uint64_t v[] = { 1, 2, 3, 4, 5, 6, }; + id.set(v); + id.getStr(str, bls::IoFixedByteSeq); + { + CYBOZU_TEST_EQUAL(str.size(), FrSize); + bls::Id id2; + id2.setStr(str, bls::IoFixedByteSeq); + CYBOZU_TEST_EQUAL(id, id2); + } +} + +void testAll() +{ + blsTest(); + k_of_nTest(); + popTest(); + addTest(); + dataTest(); + aggregateTest(); +} +CYBOZU_TEST_AUTO(all) +{ + const struct { + int type; + const char *name; + } tbl[] = { + { MCL_BN254, "BN254" }, +#if MCLBN_FP_UNIT_SIZE == 6 + { MCL_BN381_1, "BN381_1" }, + { MCL_BLS12_381, "BLS12_381" }, +#endif + }; + for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { + printf("curve=%s\n", tbl[i].name); + bls::init(tbl[i].type); + testAll(); + } +} -- cgit v1.2.3