diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index 31a54a4..f2faebe 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -379,11 +379,16 @@ public: } static uint32_t get_xi_a() { return Fp::getOp().xi_a; } - static void init() + static void init(bool *pb) { // assert(Fp::maxSize <= 256); mcl::fp::Op& op = Fp::op_; assert(op.xi_a); + // assume p < W/4 where W = 1 << (N * sizeof(Unit) * 8) + if ((op.p[op.N - 1] >> (sizeof(fp::Unit) * 8 - 2)) != 0) { + *pb = false; + return; + } mul_xi = 0; #ifdef MCL_XBYAK_DIRECT_CALL add = fp::func_ptr_cast(op.fp2_addA_); @@ -440,6 +445,12 @@ public: } } #ifndef CYBOZU_DONT_USE_EXCEPTION + static void init() + { + bool b; + init(&b); + if (!b) throw cybozu::Exception("Fp2::init"); + } template void load(InputStream& is, int ioMode = IoSerialize) { diff --git a/test/fp_tower_test.cpp b/test/fp_tower_test.cpp index b5e77db..5dd3a70 100644 --- a/test/fp_tower_test.cpp +++ b/test/fp_tower_test.cpp @@ -400,9 +400,11 @@ void test(const char *p, mcl::fp::Mode mode) { const int xi_a = 1; Fp::init(xi_a, p, mode); - printf("mode=%s\n", mcl::fp::ModeToStr(mode)); if (Fp::getOp().isFullBit) return; - Fp2::init(); + bool b; + Fp2::init(&b); + if (!b) return; + printf("mode=%s\n", mcl::fp::ModeToStr(mode)); printf("bitSize=%d\n", (int)Fp::getBitSize()); #if 0 if (Fp::getBitSize() > 256) {