reduce toMont inf invMontOp

dev
MITSUNARI Shigeo 10 years ago
parent 4f61d3360d
commit 0a326d8a9c
  1. 15
      include/mcl/op.hpp
  2. 21
      src/fp.cpp

@ -54,10 +54,12 @@ struct Op {
for Montgomery
one = 1
R = (1 << (N * sizeof(Unit) * 8)) % p
RR = (R * R) % p
R2 = (R * R) % p
R3 = RR^3
*/
Unit one[maxOpUnitSize];
Unit RR[maxOpUnitSize];
Unit R2[maxOpUnitSize];
Unit R3[maxOpUnitSize];
std::vector<Unit> invTbl;
size_t N;
size_t bitSize;
@ -100,11 +102,18 @@ struct Op {
}
void fromMont(Unit* y, const Unit *x) const
{
/*
M(x, y) = xyR^-1
y = M(x, 1) = xR^-1
*/
mul(y, x, one);
}
void toMont(Unit* y, const Unit *x) const
{
mul(y, x, RR);
/*
y = M(x, R2) = xR^2 R^-1 = xR
*/
mul(y, x, R2);
}
void init(const std::string& mstr, int base, size_t maxBitSize, Mode mode);
static FpGenerator* createFpGenerator();

@ -151,13 +151,14 @@ struct OpeFunc {
Gmp::getArray(y, N, my);
}
/*
inv(x/R) = (1/x)R -toMont-> 1/x -toMont-> (1/x)R^-1
inv(xR) = (1/x)R^-1 -toMont-> 1/x -toMont-> (1/x)R
*/
static void invMontOp(Unit *y, const Unit *x, const Op& op)
{
invOp(y, x, op);
op.toMont(y, y);
op.toMont(y, y);
op.mul(y, y, op.R3);
// op.toMont(y, y);
// op.toMont(y, y);
}
static inline bool isZeroC(const Unit *x)
{
@ -233,11 +234,15 @@ static void initForMont(Op& op, const Unit *p)
{
const size_t N = op.N;
assert(N >= 2);
mpz_class t = 1;
Gmp::getArray(op.one, N, t);
t = (t << (N * 64)) % op.mp;
t = (t * t) % op.mp;
Gmp::getArray(op.RR, N, t);
{
mpz_class t = 1, R;
Gmp::getArray(op.one, N, t);
R = (t << (N * 64)) % op.mp;
t = (R * R) % op.mp;
Gmp::getArray(op.R2, N, t);
t = (R * R * R) % op.mp;
Gmp::getArray(op.R3, N, t);
}
op.rp = getMontgomeryCoeff(p[0]);
#ifdef USE_MONT_FP
FpGenerator *fg = op.fg;

Loading…
Cancel
Save