move some methods to global

update-fork
MITSUNARI Shigeo 5 years ago
parent 16fe1740de
commit 8234909744
  1. 167
      include/mcl/ec.hpp

@ -21,6 +21,22 @@ namespace mcl {
template<class _Fp> class Fp2T; template<class _Fp> class Fp2T;
namespace local {
template<class Ec, class Vec>
void addTbl(Ec& Q, const Ec *tbl, const Vec& naf, size_t i)
{
if (i >= naf.size()) return;
int n = naf[i];
if (n > 0) {
Q += tbl[(n - 1) >> 1];
} else if (n < 0) {
Q -= tbl[(-n - 1) >> 1];
}
}
} // mcl::local
namespace ec { namespace ec {
enum Mode { enum Mode {
@ -47,23 +63,78 @@ bool get_a_flag(const mcl::Fp2T<Fp>& x)
} // mcl::ec::local } // mcl::ec::local
} // mcl::ec template<class T>
void normalizeJacobi(T& x, T& y, T& z)
{
assert(!z.isZero());
T rz2;
T::inv(z, z);
T::sqr(rz2, z);
x *= rz2;
y *= rz2;
y *= z;
z = 1;
}
namespace local { // Y^2 == X(X^2 + aZ^4) + bZ^6
template<class T>
bool isValidJacobi(const T& a, const T& b, const T& x, const T& y, const T& z)
{
T y2, x2, z2, z4, t;
T::sqr(x2, x);
T::sqr(y2, y);
T::sqr(z2, z);
T::sqr(z4, z2);
T::mul(t, z4, a);
t += x2;
t *= x;
z4 *= z2;
z4 *= b;
t += z4;
return y2 == t;
}
template<class Ec, class Vec> template<class T>
void addTbl(Ec& Q, const Ec *tbl, const Vec& naf, size_t i) void normalizeProj(T& x, T& y, T& z)
{ {
if (i >= naf.size()) return; assert(!z.isZero());
int n = naf[i]; T::inv(z, z);
if (n > 0) { x *= z;
Q += tbl[(n - 1) >> 1]; y *= z;
} else if (n < 0) { z = 1;
Q -= tbl[(-n - 1) >> 1];
}
} }
} // mcl::local // (Y^2 - bZ^2)Z = X(X^2 + aZ^2)
template<class T>
bool isValidProj(const T& a, const T& b, const T& x, const T& y, const T& z)
{
T y2, x2, z2, t;
T::sqr(x2, x);
T::sqr(y2, y);
T::sqr(z2, z);
T::mul(t, a, z2);
t += x2;
t *= x;
z2 *= b;
y2 -= z2;
y2 *= z;
return y2 == t;
}
// y^2 == (x^2 + a)x + b
template<class T>
bool isValidAffine(const T& a, const T& b, const T& x, const T& y)
{
T y2, t;
T::sqr(y2, y);
T::sqr(t, x);
t += a;
t *= x;
t += b;
return y2 == t;
}
} // mcl::ec
/* /*
elliptic curve elliptic curve
@ -117,65 +188,24 @@ public:
private: private:
void normalizeJacobi() void normalizeJacobi()
{ {
assert(!z.isZero()); ec::normalizeJacobi(x, y, z);
Fp rz2;
Fp::inv(z, z);
Fp::sqr(rz2, z);
x *= rz2;
y *= rz2;
y *= z;
z = 1;
} }
void normalizeProj() void normalizeProj()
{ {
assert(!z.isZero()); ec::normalizeProj(x, y, z);
Fp::inv(z, z);
x *= z;
y *= z;
z = 1;
} }
// Y^2 == X(X^2 + aZ^4) + bZ^6
bool isValidJacobi() const bool isValidJacobi() const
{ {
Fp y2, x2, z2, z4, t; return ec::isValidJacobi(a_, b_, x, y, z);
Fp::sqr(x2, x);
Fp::sqr(y2, y);
Fp::sqr(z2, z);
Fp::sqr(z4, z2);
Fp::mul(t, z4, a_);
t += x2;
t *= x;
z4 *= z2;
z4 *= b_;
t += z4;
return y2 == t;
} }
// (Y^2 - bZ^2)Z = X(X^2 + aZ^2)
bool isValidProj() const bool isValidProj() const
{ {
Fp y2, x2, z2, t; return ec::isValidProj(a_, b_, x, y, z);
Fp::sqr(x2, x);
Fp::sqr(y2, y);
Fp::sqr(z2, z);
Fp::mul(t, a_, z2);
t += x2;
t *= x;
z2 *= b_;
y2 -= z2;
y2 *= z;
return y2 == t;
} }
#endif #endif
// y^2 == (x^2 + a)x + b bool isValidAffine() const
static inline bool isValid(const Fp& _x, const Fp& _y)
{ {
Fp y2, t; return ec::isValidAffine(a_, b_, x, y);
Fp::sqr(y2, _y);
Fp::sqr(t, _x);
t += a_;
t *= _x;
t += b_;
return y2 == t;
} }
public: public:
void normalize() void normalize()
@ -272,29 +302,26 @@ public:
} else } else
#endif #endif
{ {
isOK = isValid(x, y); isOK = isValidAffine();
} }
if (!isOK) return false; if (!isOK) return false;
if (verifyOrder_) return isValidOrder(); if (verifyOrder_) return isValidOrder();
return true; return true;
} }
void set(bool *pb, const Fp& _x, const Fp& _y, bool verify = true) void set(bool *pb, const Fp& x, const Fp& y, bool verify = true)
{ {
if (verify && !isValid(_x, _y)) { this->x = x; this->y = y;
*pb = false;
return;
}
x = _x; y = _y;
#ifdef MCL_EC_USE_AFFINE #ifdef MCL_EC_USE_AFFINE
inf_ = false; inf_ = false;
#else #else
z = 1; z = 1;
#endif #endif
if (verify && verifyOrder_ && !isValidOrder()) { if (!verify || (isValidAffine() && (!verifyOrder_ || isValidOrder()))) {
*pb = false;
} else {
*pb = true; *pb = true;
return;
} }
*pb = false;
clear();
} }
void clear() void clear()
{ {
@ -972,7 +999,7 @@ public:
} }
return; return;
verifyValidness: verifyValidness:
if (!isValid(x, y)) { if (!isValidAffine()) {
*pb = false; *pb = false;
return; return;
} }

Loading…
Cancel
Save