From 549b0fa11d45130f6a74ad3aeb82228264fa0cae Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 31 Oct 2016 16:27:59 +0900 Subject: [PATCH] fix check for MCL_MAX_BIT_SIZE > 521 --- src/fp.cpp | 17 +++++++++-------- test/fp_test.cpp | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/fp.cpp b/src/fp.cpp index 1cf12b4..f85f841 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -272,13 +272,21 @@ void Op::init(const std::string& mstr, size_t maxBitSize, Mode mode) { assert(sizeof(mp_limb_t) == sizeof(Unit)); clear(); + if (maxBitSize > MCL_MAX_BIT_SIZE) { + throw cybozu::Exception("Op:init:too large maxBitSize") << maxBitSize << MCL_MAX_BIT_SIZE; + } + { + bool isMinus = fp::strToMpzArray(&bitSize, p, maxBitSize, mp, mstr, 0); + if (isMinus) throw cybozu::Exception("Op:init:mstr is minus") << mstr; + } + if (mp == 0) throw cybozu::Exception("Op:init:mstr is zero") << mstr; /* priority : MCL_USE_XBYAK > MCL_USE_LLVM > none Xbyak > llvm_mont > llvm > gmp_mont > gmp */ #ifdef MCL_USE_XBYAK if (mode == fp::FP_AUTO) mode = fp::FP_XBYAK; - if (mode == fp::FP_XBYAK && maxBitSize > 521) { + if (mode == fp::FP_XBYAK && bitSize > 521) { mode = fp::FP_AUTO; } #else @@ -300,15 +308,8 @@ void Op::init(const std::string& mstr, size_t maxBitSize, Mode mode) #endif "\n", ModeToStr(mode), isMont, (int)maxBitSize); #endif - if (maxBitSize > MCL_MAX_BIT_SIZE) { - throw cybozu::Exception("Op:init:too large maxBitSize") << maxBitSize << MCL_MAX_BIT_SIZE; - } - bool isMinus = fp::strToMpzArray(&bitSize, p, maxBitSize, mp, mstr, 0); - if (isMinus) throw cybozu::Exception("Op:init:mstr is minus") << mstr; - if (mp == 0) throw cybozu::Exception("Op:init:mstr is zero") << mstr; isFullBit = (bitSize % UnitBitSize) == 0; - primeMode = PM_GENERIC; #if defined(MCL_USE_LLVM) || defined(MCL_USE_XBYAK) if ((mode == FP_AUTO || mode == FP_LLVM || mode == FP_XBYAK) && mp == mpz_class("0xfffffffffffffffffffffffffffffffeffffffffffffffff")) { diff --git a/test/fp_test.cpp b/test/fp_test.cpp index 05c4907..e9b9e7b 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -753,12 +753,12 @@ CYBOZU_TEST_AUTO(mod_NIST_P521) mcl::gmp::getArray(ok, N + 1, my); #ifdef MCL_USE_LLVM mcl_fpDbl_mod_NIST_P521L(ex, in, Fp::getOp().p); - CYBOZU_TEST_ASSERT(memcmp(ex, ok, sizeof(ex)) == 0); + CYBOZU_TEST_EQUAL_ARRAY(ex, ok, N + 1); #endif #ifdef MCL_USE_XBYAK const mcl::fp::Op& op = Fp::getOp(); op.fpDbl_mod(ex, in, op.p); - CYBOZU_TEST_ASSERT(memcmp(ex, ok, sizeof(ex)) == 0); + CYBOZU_TEST_EQUAL_ARRAY(ex, ok, N + 1); #endif } }