diff --git a/sample/bench.cpp b/sample/bench.cpp index 14997f8..4e43020 100644 --- a/sample/bench.cpp +++ b/sample/bench.cpp @@ -173,7 +173,7 @@ void benchFromStr16() Fp x; const size_t N = 64; mcl::fp::Unit buf[N]; - CYBOZU_BENCH("fp:fromStr16", mcl::fp::fromStr16, buf, N, str.c_str(), str.size()); + CYBOZU_BENCH("fp:hexToArray", mcl::fp::hexToArray, buf, N, str.c_str(), str.size()); mpz_class y; CYBOZU_BENCH("gmp:setStr ", mcl::gmp::setStr, y, str, 16); diff --git a/src/conversion.hpp b/src/conversion.hpp index 8681048..3c5a036 100644 --- a/src/conversion.hpp +++ b/src/conversion.hpp @@ -201,32 +201,6 @@ size_t arrayToBin(char *buf, size_t bufSize, const T *x, size_t n, bool withPref return totalSize; } -/* - convert hex string to x[0..xn) - hex string = [0-9a-fA-F]+ -*/ -template -void fromStr16(T *x, size_t xn, const char *str, size_t strLen) -{ - if (strLen == 0) throw cybozu::Exception("fp:fromStr16:strLen is zero"); - const size_t unitLen = sizeof(T) * 2; - const size_t q = strLen / unitLen; - const size_t r = strLen % unitLen; - const size_t requireSize = q + (r ? 1 : 0); - if (xn < requireSize) throw cybozu::Exception("fp:fromStr16:short size") << xn << requireSize; - for (size_t i = 0; i < q; i++) { - bool b; - x[i] = cybozu::hextoi(&b, &str[r + (q - 1 - i) * unitLen], unitLen); - if (!b) throw cybozu::Exception("fp:fromStr16:bad char") << cybozu::exception::makeString(str, strLen); - } - if (r) { - bool b; - x[q] = cybozu::hextoi(&b, str, r); - if (!b) throw cybozu::Exception("fp:fromStr16:bad char") << cybozu::exception::makeString(str, strLen); - } - for (size_t i = requireSize; i < xn; i++) x[i] = 0; -} - /* convert hex string to x[0..xn) hex string = [0-9a-fA-F]+ diff --git a/test/fp_util_test.cpp b/test/fp_util_test.cpp index 72e6c7d..c516274 100644 --- a/test/fp_util_test.cpp +++ b/test/fp_util_test.cpp @@ -53,7 +53,7 @@ CYBOZU_TEST_AUTO(arrayToBin) } // CYBOZU_TEST_AUTO(verifyStr) // QQQ -CYBOZU_TEST_AUTO(fromStr16) +CYBOZU_TEST_AUTO(hexToArray) { const struct { const char *str; @@ -71,10 +71,9 @@ CYBOZU_TEST_AUTO(fromStr16) for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { const size_t xN = 4; uint64_t x[xN]; - mcl::fp::fromStr16(x, xN, tbl[i].str, strlen(tbl[i].str)); - for (size_t j = 0; j < xN; j++) { - CYBOZU_TEST_EQUAL(x[j], tbl[i].x[j]); - } + size_t n = mcl::fp::hexToArray(x, xN, tbl[i].str, strlen(tbl[i].str)); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_EQUAL_ARRAY(x, tbl[i].x, n); } }