add EcT::normalize(EcT&, const EcT&)

dev
MITSUNARI Shigeo 8 years ago
parent 46393928fd
commit dd69b60975
  1. 7
      include/mcl/ec.hpp
  2. 1
      include/mcl/fp.hpp
  3. 3
      include/mcl/fp_tower.hpp
  4. 2
      include/mcl/operator.hpp
  5. 15
      include/mcl/util.hpp

@ -155,6 +155,11 @@ public:
} }
#endif #endif
} }
static void normalize(EcT& y, const EcT& x)
{
y = x;
y.normalize();
}
static inline void init(const Fp& a, const Fp& b, int mode = ec::Jacobi) static inline void init(const Fp& a, const Fp& b, int mode = ec::Jacobi)
{ {
a_ = a; a_ = a;
@ -835,7 +840,7 @@ public:
px = &tmp; px = &tmp;
} }
z.clear(); z.clear();
fp::powGeneric(z, *px, y, yn, EcT::add, EcT::dbl, constTime); fp::powGeneric(z, *px, y, yn, EcT::add, EcT::dbl, EcT::normalize, constTime);
if (isNegative) { if (isNegative) {
neg(z, z); neg(z, z);
} }

@ -455,7 +455,6 @@ public:
{ {
return fp::isLessArray(v_, rhs.v_, op_.N); return fp::isLessArray(v_, rhs.v_, op_.N);
} }
void normalize() const {} // dummy method
/* /*
set IoMode for operator<<(), or operator>>() set IoMode for operator<<(), or operator>>()
*/ */

@ -180,7 +180,6 @@ public:
bool isOne() const { return a.isOne() && b.isZero(); } bool isOne() const { return a.isOne() && b.isZero(); }
bool operator==(const Fp2T& rhs) const { return a == rhs.a && b == rhs.b; } bool operator==(const Fp2T& rhs) const { return a == rhs.a && b == rhs.b; }
bool operator!=(const Fp2T& rhs) const { return !operator==(rhs); } bool operator!=(const Fp2T& rhs) const { return !operator==(rhs); }
void normalize() const {} // dummy method
/* /*
return true is a is odd (do not consider b) return true is a is odd (do not consider b)
this function is for only compressed reprezentation of EC this function is for only compressed reprezentation of EC
@ -782,7 +781,6 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > {
Fp2::mul(y.b, p.b, q); Fp2::mul(y.b, p.b, q);
Fp2::mul(y.c, p.c, q); Fp2::mul(y.c, p.c, q);
} }
void normalize() const {} // dummy
}; };
/* /*
@ -926,7 +924,6 @@ struct Fp12T : public fp::Operator<Fp12T<Fp> > {
{ {
return a.getStr(ioMode) + fp::getIoSeparator(ioMode) + b.getStr(ioMode); return a.getStr(ioMode) + fp::getIoSeparator(ioMode) + b.getStr(ioMode);
} }
void normalize() const {} // dummy
}; };
} // mcl } // mcl

@ -66,7 +66,7 @@ private:
px = &tmp; px = &tmp;
} }
z = 1; z = 1;
fp::powGeneric(z, *px, y, yn, T::mul, T::sqr); fp::powGeneric(z, *px, y, yn, T::mul, T::sqr, (void (*)(T&, const T&))0);
if (isNegative) { if (isNegative) {
T::inv(z, z); T::inv(z, z);
} }

@ -196,7 +196,7 @@ void getRandVal(T *out, RG& rg, const T *in, size_t bitSize)
@note &out != x and out = the unit element of G @note &out != x and out = the unit element of G
*/ */
template<class G, class T> template<class G, class T>
void powGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, const G&) , void sqr(G&, const G&), bool constTime = false) void powGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, const G&) , void sqr(G&, const G&), void normalize(G&, const G&), bool constTime = false)
{ {
assert(&out != &x); assert(&out != &x);
while (n > 0) { while (n > 0) {
@ -223,11 +223,16 @@ void powGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&,
} }
} }
G tbl[4]; // tbl = { discard, x, x^2, x^3 } G tbl[4]; // tbl = { discard, x, x^2, x^3 }
x.normalize(); if (normalize) {
normalize(tbl[0], x);
} else {
tbl[0] = x; tbl[0] = x;
tbl[1] = x; }
sqr(tbl[2], tbl[1]); tbl[2].normalize(); tbl[1] = tbl[0];
mul(tbl[3], tbl[2], x); tbl[3].normalize(); sqr(tbl[2], tbl[1]);
if (normalize) { normalize(tbl[2], tbl[2]); }
mul(tbl[3], tbl[2], x);
if (normalize) { normalize(tbl[3], tbl[3]); }
T v = y[n - 1]; T v = y[n - 1];
int m = cybozu::bsr<T>(v); int m = cybozu::bsr<T>(v);
if (m & 1) { if (m & 1) {

Loading…
Cancel
Save