diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index ef93acc..2d7d58a 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -66,6 +66,7 @@ public: const Unit *getUnit() const { return v_; } static inline size_t getUnitSize() { return op_.N; } static inline size_t getBitSize() { return op_.bitSize; } + static inline const fp::Op& getOp() { return op_; } void dump() const { const size_t N = op_.N; diff --git a/include/mcl/fp_generator.hpp b/include/mcl/fp_generator.hpp index 559e253..a896278 100644 --- a/include/mcl/fp_generator.hpp +++ b/include/mcl/fp_generator.hpp @@ -161,14 +161,11 @@ struct FpGenerator : Xbyak::CodeGenerator { typedef int (*int2op)(uint64_t*, const uint64_t*); bool3op addNC_; bool3op subNC_; - void3op add_; - void3op sub_; - void3op mul_; + void3u mul_; uint3opI mulI_; void *montRedRaw_; void2op montRed_; void2op sqr_; - void2op neg_; void2op shr1_; int2op preInv_; FpGenerator() @@ -179,28 +176,24 @@ struct FpGenerator : Xbyak::CodeGenerator { , isFullBit_(0) , addNC_(0) , subNC_(0) - , add_(0) - , sub_(0) , mul_(0) , mulI_(0) , montRedRaw_(0) , montRed_(0) - , neg_(0) , shr1_(0) , preInv_(0) { useMulx_ = cpu_.has(Xbyak::util::Cpu::tBMI2); } /* - @param p [in] pointer to prime - @param pn [in] length of prime + @param op [in] ; use op.p and op.N */ - void init(Op& op, const uint64_t *p, int pn) + void init(Op& op) { - if (pn < 2) throw cybozu::Exception("mcl:FpGenerator:small pn") << pn; - p_ = p; - rp_ = fp::getMontgomeryCoeff(p[0]); - pn_ = pn; + if (op.N < 2) throw cybozu::Exception("mcl:FpGenerator:small pn") << op.N; + p_ = op.p; + rp_ = fp::getMontgomeryCoeff(p_[0]); + pn_ = (int)op.N; isFullBit_ = (p_[pn_ - 1] >> 63) != 0; // printf("p=%p, pn_=%d, isFullBit_=%d\n", p_, pn_, isFullBit_); @@ -212,19 +205,20 @@ struct FpGenerator : Xbyak::CodeGenerator { subNC_ = getCurr(); gen_addSubNC(false); align(16); - add_ = getCurr(); + op.fp_add = getCurr(); gen_addMod(); align(16); - sub_ = getCurr(); + op.fp_sub = getCurr(); gen_sub(); align(16); - neg_ = getCurr(); + op.fp_neg = getCurr(); gen_neg(); align(16); mulI_ = getCurr(); gen_mulI(); align(16); - mul_ = getCurr(); + mul_ = getCurr(); + op.fp_mul = mul_; gen_mul(); align(16); montRed_ = getCurr(); @@ -240,15 +234,11 @@ struct FpGenerator : Xbyak::CodeGenerator { preInv_ = getCurr(); gen_preInv(); - op.fp_neg = Xbyak::CastTo(neg_); - op.fp_add = Xbyak::CastTo(add_); - op.fp_sub = Xbyak::CastTo(sub_); op.fp_addNC = Xbyak::CastTo(addNC_); op.fp_subNC = Xbyak::CastTo(subNC_); - op.fp_mul = Xbyak::CastTo(mul_); op.fp_sqr = Xbyak::CastTo(sqr_); - if (pn <= 4) { + if (op.N <= 4) { if (montRed_) { op.fp_mod = Xbyak::CastTo(montRed_); } diff --git a/src/fp.cpp b/src/fp.cpp index f3b1ef6..0142ec7 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -363,7 +363,7 @@ static void initForMont(Op& op, const Unit *p, Mode mode) #ifdef MCL_USE_XBYAK FpGenerator *fg = op.fg; if (fg == 0) return; - fg->init(op, p, (int)N); + fg->init(op); if (N <= 4) { op.fp_invOp = &invOpForMontC; diff --git a/test/fp_generator_test.cpp b/test/fp_generator_test.cpp index 5930727..1143881 100644 --- a/test/fp_generator_test.cpp +++ b/test/fp_generator_test.cpp @@ -93,10 +93,10 @@ static inline std::ostream& operator<<(std::ostream& os, const Int& x) return os << x.getStr(); } -void testAddSub(const mcl::fp::FpGenerator& fg, int pn) +void testAddSub(const mcl::fp::Op& op) { Fp x, y; - Int mx(pn), my(pn); + Int mx(op.N), my(op.N); x.setStr("0x8811aabb23427cc"); y.setStr("0x8811aabb23427cc11"); mx.set(x); @@ -104,24 +104,24 @@ void testAddSub(const mcl::fp::FpGenerator& fg, int pn) for (int i = 0; i < 30; i++) { CYBOZU_TEST_EQUAL(mx, x); x += x; - fg.add_(mx.v, mx.v, mx.v); + op.fp_add(mx.v, mx.v, mx.v); } for (int i = 0; i < 30; i++) { CYBOZU_TEST_EQUAL(mx, x); x += y; - fg.add_(mx.v, mx.v, my.v); + op.fp_add(mx.v, mx.v, my.v); } for (int i = 0; i < 30; i++) { CYBOZU_TEST_EQUAL(my, y); y -= x; - fg.sub_(my.v, my.v, mx.v); + op.fp_sub(my.v, my.v, mx.v); } } -void testNeg(const mcl::fp::FpGenerator& fg, int pn) +void testNeg(const mcl::fp::Op& op) { Fp x; - Int mx(pn), my(pn); + Int mx(op.N), my(op.N); const char *tbl[] = { "0", "0x12346", @@ -132,7 +132,7 @@ void testNeg(const mcl::fp::FpGenerator& fg, int pn) x.setStr(tbl[i]); mx.set(x); x = -x; - fg.neg_(mx.v, mx.v); + op.fp_neg(mx.v, mx.v); CYBOZU_TEST_EQUAL(mx, x); } } @@ -183,18 +183,13 @@ void testShr1(const mcl::fp::FpGenerator& fg, int pn) void test(const char *pStr) { - Fp::setModulo(pStr, 16); - uint64_t p[MAX_N]; - strToArray(p, MAX_N, pStr); - const int pn = mcl::fp::getNonZeroArraySize(p, MAX_N); - printf("pn=%d\n", pn); - mcl::fp::FpGenerator fg; - mcl::fp::Op op; - fg.init(op, p, pn); - testAddSub(fg, pn); - testNeg(fg, pn); - testMulI(fg, pn); - testShr1(fg, pn); + Fp::setModulo(pStr, 16, mcl::fp::FP_XBYAK); + const mcl::fp::Op& op = Fp::getOp(); + const int pn = (int)op.N; + testAddSub(op); + testNeg(op); + testMulI(*op.fg, pn); + testShr1(*op.fg, pn); } CYBOZU_TEST_AUTO(all)