From 4e1a4f29ceb809ce89853f1d39f1a0fcb3bced5f Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 3 Jun 2015 15:37:55 +0900 Subject: [PATCH] move members in MontFp to Op --- include/mcl/fp.hpp | 6 +-- include/mcl/fp_base.hpp | 87 ++++++++++++----------------------------- 2 files changed, 29 insertions(+), 64 deletions(-) diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 873c824..298178e 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -168,11 +168,11 @@ public: } void toMont(FpT& y, const FpT& x) { - if (op_.toMont) op_.toMont(y.v_, x.v_); + if (op_.useMont) op_.toMont(y.v_, x.v_); } void fromMont(FpT& y, const FpT& x) { - if (op_.fromMont) op_.fromMont(y.v_, x.v_); + if (op_.useMont) op_.fromMont(y.v_, x.v_); } void fromStr(const std::string& str, int base = 0) { @@ -218,7 +218,7 @@ public: { assert(maxUnitN <= fp::Block::maxUnitN); b.n = op_.N; - if (op_.fromMont) { + if (op_.useMont) { op_.fromMont(b.v_, v_); b.p = &b.v_[0]; } else { diff --git a/include/mcl/fp_base.hpp b/include/mcl/fp_base.hpp index 3f472c7..536b441 100644 --- a/include/mcl/fp_base.hpp +++ b/include/mcl/fp_base.hpp @@ -152,6 +152,8 @@ struct Op { mpz_class mp; mcl::SquareRoot sq; Unit p[maxUnitN]; + Unit one[maxUnitN]; // 1 + Unit RR[maxUnitN]; // (R * R) % p size_t N; size_t bitLen; // independent from p @@ -165,8 +167,7 @@ struct Op { void3op sub; void3op mul; // for Montgomery - void2op toMont; - void2op fromMont; + bool useMont; // require p void3op negG; void2opOp invG; @@ -179,7 +180,7 @@ struct Op { : p(), N(0), bitLen(0) , isZero(0), clear(0), copy(0) , neg(0), inv(0), add(0), sub(0), mul(0) - , toMont(0), fromMont(0) + , useMont(false)//, toMont(0), fromMont(0) , negG(0), invG(0), addG(0), subG(0), mulPreG(0), modG(0) , fg(createFpGenerator()) { @@ -188,6 +189,14 @@ struct Op { { destroyFpGenerator(fg); } + void fromMont(Unit* y, const Unit *x) const + { + mul(y, x, one); + } + void toMont(Unit* y, const Unit *x) const + { + mul(y, x, RR); + } }; @@ -197,42 +206,28 @@ struct MontFp { typedef fp::Unit Unit; static const size_t N = (bitN + sizeof(Unit) * 8 - 1) / (sizeof(Unit) * 8); static const size_t invTblN = N * sizeof(Unit) * 8 * 2; - static mpz_class mp_; - static Unit p_[N]; - static Unit one_[N]; - static Unit RR_[N]; // (R * R) % p static Unit invTbl_[invTblN][N]; static FpGenerator fg_; - static void3op add_; - static void3op mul_; static inline void fromRawGmp(Unit *y, size_t n, const mpz_class& x) { local::toArray(y, n, x.get_mpz_t()); } - static void initInvTbl(Unit invTbl[invTblN][N]) + static void initInvTbl(Unit invTbl[invTblN][N], const Op& op) { Unit t[N]; - clear(t); + memset(t, 0, sizeof(t)); t[0] = 2; - toMont(t, t); + op.toMont(t, t); for (int i = 0; i < invTblN; i++) { - copy(invTbl[invTblN - 1 - i], t); - add_(t, t, t); + op.copy(invTbl[invTblN - 1 - i], t); + op.add(t, t, t); } } - static inline void clear(Unit *x) - { - local::clearArray(x, 0, N); - } static inline void copy(Unit *y, const Unit *x) { local::copyArray(y, x, N); } - static inline bool isZero(const Unit *x) - { - return local::isZeroArray(x, N); - } static inline void invC(Unit *y, const Unit *x, const Op& op) { const int2op preInv = Xbyak::CastTo(op.fg->preInv_); @@ -245,58 +240,28 @@ struct MontFp { */ op.mul(y, r, invTbl_[k]); } - static inline void toMont(Unit *y, const Unit *x) - { - mul_(y, x, RR_); - } - static inline void fromMont(Unit *y, const Unit *x) - { - mul_(y, x, one_); - } static inline void init(Op& op, const Unit *p) { - copy(p_, p); - Gmp::setRaw(mp_, p, N); - mpz_class t = 1; - fromRawGmp(one_, N, t); - t = (t << (N * 64)) % mp_; - t = (t * t) % mp_; - fromRawGmp(RR_, N, t); - fg_.init(p_, N); + fromRawGmp(op.one, N, t); + t = (t << (N * 64)) % op.mp; + t = (t * t) % op.mp; + fromRawGmp(op.RR, N, t); + fg_.init(p, N); - add_ = Xbyak::CastTo(fg_.add_); - mul_ = Xbyak::CastTo(fg_.mul_); - op.N = N; - op.isZero = &isZero; - op.clear = &clear; op.neg = Xbyak::CastTo(fg_.neg_); op.invG = &invC; -// { -// void2op square = Xbyak::CastTo(fg_.sqr_); -// if (square) op.square = square; -// } - op.copy = © - op.add = add_; + op.add = Xbyak::CastTo(fg_.add_); op.sub = Xbyak::CastTo(fg_.sub_); - op.mul = mul_; - op.mp = mp_; - copy(op.p, p_); - op.toMont = &toMont; - op.fromMont = &fromMont; + op.mul = Xbyak::CastTo(fg_.mul_); + op.useMont = true; - initInvTbl(invTbl_); + initInvTbl(invTbl_, op); op.fg = &fg_; } }; -template mpz_class MontFp::mp_; -template fp::Unit MontFp::p_[MontFp::N]; -template fp::Unit MontFp::one_[MontFp::N]; -template fp::Unit MontFp::RR_[MontFp::N]; template fp::Unit MontFp::invTbl_[MontFp::invTblN][MontFp::N]; template FpGenerator MontFp::fg_; -template void3op MontFp::add_; -template void3op MontFp::mul_; #endif } } // mcl::fp