From 16d5fab948a7af47561b6bc4c9588995e737d094 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Tue, 25 Sep 2018 06:45:53 +0900 Subject: [PATCH] add MCLBN_IO_SERIALIZE_HEX_STR --- include/mcl/bn.h | 3 ++ include/mcl/op.hpp | 2 +- test/bn_c_test.hpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/include/mcl/bn.h b/include/mcl/bn.h index fe32da6..8da9cfe 100644 --- a/include/mcl/bn.h +++ b/include/mcl/bn.h @@ -83,6 +83,8 @@ typedef struct { #endif #include + +#define MCLBN_IO_SERIALIZE_HEX_STR 2048 // for backword compatibility enum { mclBn_CurveFp254BNb = 0, @@ -172,6 +174,7 @@ MCLBN_DLL_API mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mcl ioMode 10 : decimal number 16 : hexadecimal number + MCLBN_IO_SERIALIZE_HEX_STR : hex string of serialized data return 0 if success else -1 */ MCLBN_DLL_API int mclBnFr_setStr(mclBnFr *x, const char *buf, mclSize bufSize, int ioMode); diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp index 56c1389..d108d1a 100644 --- a/include/mcl/op.hpp +++ b/include/mcl/op.hpp @@ -358,7 +358,7 @@ private: inline const char* getIoSeparator(int ioMode) { - return (ioMode & (IoArray | IoArrayRaw | IoSerialize)) ? "" : " "; + return (ioMode & (IoArray | IoArrayRaw | IoSerialize | IoSerializeHexStr)) ? "" : " "; } inline void dump(const char *s, size_t n) diff --git a/test/bn_c_test.hpp b/test/bn_c_test.hpp index d0d4141..90bda27 100644 --- a/test/bn_c_test.hpp +++ b/test/bn_c_test.hpp @@ -430,6 +430,80 @@ CYBOZU_TEST_AUTO(serialize) CYBOZU_TEST_EQUAL(n, expectSize); } +CYBOZU_TEST_AUTO(serializeToHexStr) +{ + const size_t G1Size = mclBn_getG1ByteSize() * 2; + mclBnFr x1, x2; + mclBnG1 P1, P2; + mclBnG2 Q1, Q2; + char buf[1024]; + size_t n; + size_t expectSize; + size_t ret; + // Fr + expectSize = G1Size; + mclBnFr_setInt(&x1, -1); + n = mclBnFr_getStr(buf, sizeof(buf), &x1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(n, expectSize); + + ret = mclBnFr_setStr(&x2, buf, n, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(mclBnFr_isEqual(&x1, &x2)); + + ret = mclBnFr_setStr(&x2, buf, n - 1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_ASSERT(ret != 0); + + memset(&x2, 0, sizeof(x2)); + ret = mclBnFr_setStr(&x2, buf, n + 1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(mclBnFr_isEqual(&x1, &x2)); + + n = mclBnFr_getStr(buf, expectSize, &x1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(n, expectSize); + + // G1 + expectSize = G1Size; + mclBnG1_hashAndMapTo(&P1, "1", 1); + n = mclBnG1_getStr(buf, sizeof(buf), &P1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(n, expectSize); + + ret = mclBnG1_setStr(&P2, buf, n, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(mclBnG1_isEqual(&P1, &P2)); + + ret = mclBnG1_setStr(&P2, buf, n - 1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_ASSERT(ret != 0); + + memset(&P2, 0, sizeof(P2)); + ret = mclBnG1_setStr(&P2, buf, n + 1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(mclBnG1_isEqual(&P1, &P2)); + + n = mclBnG1_getStr(buf, expectSize, &P1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(n, expectSize); + + // G2 + expectSize = G1Size * 2; + mclBnG2_hashAndMapTo(&Q1, "1", 1); + n = mclBnG2_getStr(buf, sizeof(buf), &Q1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(n, expectSize); + + ret = mclBnG2_setStr(&Q2, buf, n, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(mclBnG2_isEqual(&Q1, &Q2)); + + ret = mclBnG2_setStr(&Q2, buf, n - 1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_ASSERT(ret != 0); + + memset(&Q2, 0, sizeof(Q2)); + ret = mclBnG2_setStr(&Q2, buf, n + 1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(mclBnG2_isEqual(&Q1, &Q2)); + + n = mclBnG2_getStr(buf, expectSize, &Q1, MCLBN_IO_SERIALIZE_HEX_STR); + CYBOZU_TEST_EQUAL(n, expectSize); +} + #if MCLBN_FP_UNIT_SIZE == 6 CYBOZU_TEST_AUTO(badG2) {