add MCLBN_IO_SERIALIZE_HEX_STR

dev
MITSUNARI Shigeo 6 years ago
parent db844ba830
commit 16d5fab948
  1. 3
      include/mcl/bn.h
  2. 2
      include/mcl/op.hpp
  3. 74
      test/bn_c_test.hpp

@ -83,6 +83,8 @@ typedef struct {
#endif #endif
#include <mcl/curve_type.h> #include <mcl/curve_type.h>
#define MCLBN_IO_SERIALIZE_HEX_STR 2048
// for backword compatibility // for backword compatibility
enum { enum {
mclBn_CurveFp254BNb = 0, mclBn_CurveFp254BNb = 0,
@ -172,6 +174,7 @@ MCLBN_DLL_API mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mcl
ioMode ioMode
10 : decimal number 10 : decimal number
16 : hexadecimal number 16 : hexadecimal number
MCLBN_IO_SERIALIZE_HEX_STR : hex string of serialized data
return 0 if success else -1 return 0 if success else -1
*/ */
MCLBN_DLL_API int mclBnFr_setStr(mclBnFr *x, const char *buf, mclSize bufSize, int ioMode); MCLBN_DLL_API int mclBnFr_setStr(mclBnFr *x, const char *buf, mclSize bufSize, int ioMode);

@ -358,7 +358,7 @@ private:
inline const char* getIoSeparator(int ioMode) 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) inline void dump(const char *s, size_t n)

@ -430,6 +430,80 @@ CYBOZU_TEST_AUTO(serialize)
CYBOZU_TEST_EQUAL(n, expectSize); 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 #if MCLBN_FP_UNIT_SIZE == 6
CYBOZU_TEST_AUTO(badG2) CYBOZU_TEST_AUTO(badG2)
{ {

Loading…
Cancel
Save