From c0abe65cdf5341cf857dd463738fde2285e14fda Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 23 Oct 2019 21:15:24 +0900 Subject: [PATCH] enable setETHserialization for Fr --- ffi/go/mcl/mcl_test.go | 15 ++++++++++ include/mcl/bn.h | 5 ++++ include/mcl/fp.hpp | 5 +++- include/mcl/impl/bn_c_impl.hpp | 12 ++++++++ test/bn_c_test.hpp | 54 +++++++++++++++++++++++++++++++--- 5 files changed, 86 insertions(+), 5 deletions(-) diff --git a/ffi/go/mcl/mcl_test.go b/ffi/go/mcl/mcl_test.go index 51b147e..e128648 100644 --- a/ffi/go/mcl/mcl_test.go +++ b/ffi/go/mcl/mcl_test.go @@ -247,6 +247,20 @@ func testMcl(t *testing.T, c int) { testSerialize(t) } +func testETHserialize(t *testing.T) { + b := make([]byte, 32) + b[0] = 0x12 + b[1] = 0x34 + var x Fr + SetETHserialization(false) + x.Deserialize(b) + fmt.Printf("AAA x=%s\n", x.GetString(16)) + + SetETHserialization(true) + x.Deserialize(b) + fmt.Printf("AAA x=%s\n", x.GetString(16)) +} + func TestMclMain(t *testing.T) { t.Logf("GetMaxOpUnitSize() = %d\n", GetMaxOpUnitSize()) t.Log("CurveFp254BNb") @@ -258,5 +272,6 @@ func TestMclMain(t *testing.T) { } t.Log("BLS12_381") testMcl(t, BLS12_381) + testETHserialize(t) } } diff --git a/include/mcl/bn.h b/include/mcl/bn.h index 78f8f27..04f88a3 100644 --- a/include/mcl/bn.h +++ b/include/mcl/bn.h @@ -143,6 +143,7 @@ MCLBN_DLL_API int mclBn_getVersion(); */ MCLBN_DLL_API int mclBn_init(int curve, int compiledTimeVar); +MCLBN_DLL_API int mclBn_getCurveType(void); /* pairing : G1 x G2 -> GT @@ -195,6 +196,10 @@ MCLBN_DLL_API mclSize mclBn_getFieldOrder(char *buf, mclSize maxBufSize); @note ignore the flag if curve is not BLS12-381 */ MCLBN_DLL_API void mclBn_setETHserialization(int enable); + +// return 1 if ETH serialization mode else 0 +MCLBN_DLL_API int mclBn_getETHserialization(void); + /* use mapToGi according to https://github.com/ethereum/eth2.0-specs/blob/dev/specs/bls_signature.md#modular_squareroot diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index a1f4b2c..9303ddc 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -549,9 +549,12 @@ public: } static void setETHserialization(bool ETHserialization) { - if (getBitSize() != 381) return; isETHserialization_ = ETHserialization; } + static bool getETHserialization() + { + return isETHserialization_; + } static inline bool isETHserialization() { return isETHserialization_; } static inline int getIoMode() { return ioMode_; } static inline size_t getModBitLen() { return getBitSize(); } diff --git a/include/mcl/impl/bn_c_impl.hpp b/include/mcl/impl/bn_c_impl.hpp index 1d8fe04..58f62f3 100644 --- a/include/mcl/impl/bn_c_impl.hpp +++ b/include/mcl/impl/bn_c_impl.hpp @@ -83,6 +83,11 @@ int mclBn_init(int curve, int compiledTimeVar) return b ? 0 : -1; } +int mclBn_getCurveType() +{ + return mcl::bn::BN::param.cp.curveType; +} + int mclBn_getOpUnitSize() { return (int)Fp::getUnitSize() * sizeof(mcl::fp::Unit) / sizeof(uint64_t); @@ -115,7 +120,14 @@ mclSize mclBn_getFieldOrder(char *buf, mclSize maxBufSize) void mclBn_setETHserialization(int enable) { + if (mclBn_getCurveType() != MCL_BLS12_381) return; Fp::setETHserialization(enable == 1); + Fr::setETHserialization(enable == 1); +} + +int mclBn_getETHserialization() +{ + return Fp::getETHserialization() ? 1 : 0; } void mclBn_setETHmaptTo(int enable) diff --git a/test/bn_c_test.hpp b/test/bn_c_test.hpp index 9c1818b..85a81f9 100644 --- a/test/bn_c_test.hpp +++ b/test/bn_c_test.hpp @@ -25,25 +25,28 @@ CYBOZU_TEST_AUTO(init) CYBOZU_TEST_EQUAL(sizeof(mclBnG1), sizeof(G1)); CYBOZU_TEST_EQUAL(sizeof(mclBnG2), sizeof(G2)); CYBOZU_TEST_EQUAL(sizeof(mclBnGT), sizeof(Fp12)); + int curveType; #if MCLBN_FP_UNIT_SIZE >= 4 printf("test BN254 %d\n", MCLBN_FP_UNIT_SIZE); - ret = mclBn_init(MCL_BN254, MCLBN_COMPILED_TIME_VAR); + curveType = MCL_BN254; #endif #if MCLBN_FP_UNIT_SIZE >= 6 && MCLBN_FR_UNIT_SIZE >= 4 printf("test BLS12_381 %d\n", MCLBN_FP_UNIT_SIZE); - ret = mclBn_init(MCL_BLS12_381, MCLBN_COMPILED_TIME_VAR); + curveType = MCL_BLS12_381; #endif #if MCLBN_FP_UNIT_SIZE >= 6 && MCLBN_FR_UNIT_SIZE >= 6 printf("test BN381_1 %d\n", MCLBN_FP_UNIT_SIZE); - ret = mclBn_init(MCL_BN381_1, MCLBN_COMPILED_TIME_VAR); + curveType = MCL_BN381_1; #endif #if MCLBN_FP_UNIT_SIZE == 8 printf("test BN462 %d\n", MCLBN_FP_UNIT_SIZE); - ret = mclBn_init(MCL_BN462, MCLBN_COMPILED_TIME_VAR); + curveType = MCL_BN462; #endif + ret = mclBn_init(curveType, MCLBN_COMPILED_TIME_VAR); CYBOZU_TEST_EQUAL(ret, 0); if (ret != 0) exit(1); + CYBOZU_TEST_EQUAL(curveType, mclBn_getCurveType()); } CYBOZU_TEST_AUTO(Fr) @@ -612,6 +615,49 @@ CYBOZU_TEST_AUTO(serializeToHexStr) CYBOZU_TEST_EQUAL(n, expectSize); } +CYBOZU_TEST_AUTO(ETHserialization) +{ + int curveType = mclBn_getCurveType(); + if (curveType != MCL_BLS12_381) return; + int keepETH = mclBn_getETHserialization(); + char buf[128] = {}; + char str[128]; + buf[0] = 0x12; + buf[1] = 0x34; + size_t n; + mclBnFr x; + mclBn_setETHserialization(false); + n = mclBnFr_deserialize(&x, buf, 32); + CYBOZU_TEST_EQUAL(n, 32); + n = mclBnFr_getStr(str, sizeof(str), &x, 16); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_EQUAL(strcmp(str, "3412"), 0); + + mclBn_setETHserialization(true); + n = mclBnFr_deserialize(&x, buf, 32); + CYBOZU_TEST_EQUAL(n, 32); + n = mclBnFr_getStr(str, sizeof(str), &x, 16); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_EQUAL(strcmp(str, "1234000000000000000000000000000000000000000000000000000000000000"), 0); + + mclBnFp y; + mclBn_setETHserialization(false); + n = mclBnFp_deserialize(&y, buf, 48); + CYBOZU_TEST_EQUAL(n, 48); + n = mclBnFp_getStr(str, sizeof(str), &y, 16); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_EQUAL(strcmp(str, "3412"), 0); + + mclBn_setETHserialization(true); + n = mclBnFp_deserialize(&y, buf, 48); + CYBOZU_TEST_EQUAL(n, 48); + n = mclBnFp_getStr(str, sizeof(str), &y, 16); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_EQUAL(strcmp(str, "123400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), 0); + + mclBn_setETHserialization(keepETH); +} + #if MCLBN_FP_UNIT_SIZE == 6 && MCLBN_FR_UNIT_SIZE >= 6 CYBOZU_TEST_AUTO(badG2) {