simplify GLV1T class

update-fork
MITSUNARI Shigeo 5 years ago
parent 626663b8db
commit 2053b495a7
  1. 2
      include/mcl/bn.hpp
  2. 43
      include/mcl/ec.hpp

@ -567,7 +567,7 @@ typedef mcl::FixedArray<int8_t, MCL_MAX_FR_BIT_SIZE / 2 + 2> NafArray;
GLV for G1 on BN/BLS12 GLV for G1 on BN/BLS12
*/ */
struct GLV1 : mcl::GLV1T<Fp, G1> { struct GLV1 : mcl::GLV1T<G1> {
static bool usePrecomputedTable(int curveType) static bool usePrecomputedTable(int curveType)
{ {
if (curveType < 0) return false; if (curveType < 0) return false;

@ -1070,8 +1070,8 @@ template<class Fp> int EcT<Fp>::mode_;
namespace local { namespace local {
template<class G, class Vec> template<class Ec, class Vec>
void addTbl(G& Q, const G *tbl, const Vec& naf, size_t i) void addTbl(Ec& Q, const Ec *tbl, const Vec& naf, size_t i)
{ {
if (i >= naf.size()) return; if (i >= naf.size()) return;
int n = naf[i]; int n = naf[i];
@ -1084,9 +1084,10 @@ void addTbl(G& Q, const G *tbl, const Vec& naf, size_t i)
} // mcl::local } // mcl::local
template<class F, class G> template<class Ec>
struct GLV1T { struct GLV1T {
static F rw; // rw = 1 / w = (-1 - sqrt(-3)) / 2 typedef typename Ec::Fp Fp;
static Fp rw; // rw = 1 / w = (-1 - sqrt(-3)) / 2
static size_t rBitSize; static size_t rBitSize;
static mpz_class v0, v1; static mpz_class v0, v1;
static mpz_class B[2][2]; static mpz_class B[2][2];
@ -1110,9 +1111,9 @@ public:
/* /*
L (x, y) = (rw x, y) L (x, y) = (rw x, y)
*/ */
static void mulLambda(G& Q, const G& P) static void mulLambda(Ec& Q, const Ec& P)
{ {
F::mul(Q.x, P.x, rw); Fp::mul(Q.x, P.x, rw);
Q.y = P.y; Q.y = P.y;
Q.z = P.z; Q.z = P.z;
} }
@ -1127,14 +1128,14 @@ public:
a = x - (t * B[0][0] + b * B[1][0]); a = x - (t * B[0][0] + b * B[1][0]);
b = - (t * B[0][1] + b * B[1][1]); b = - (t * B[0][1] + b * B[1][1]);
} }
static void mul(G& Q, const G& P, mpz_class x, bool constTime = false) static void mul(Ec& Q, const Ec& P, mpz_class x, bool constTime = false)
{ {
const int w = 5; const int w = 5;
const size_t tblSize = 1 << (w - 2); const size_t tblSize = 1 << (w - 2);
typedef mcl::FixedArray<int8_t, sizeof(G) * 8 / 2 + 2> NafArray; typedef mcl::FixedArray<int8_t, sizeof(Fp) * 8 / 2 + 2> NafArray;
NafArray naf[2]; NafArray naf[2];
mpz_class u[2]; mpz_class u[2];
G tbl[2][tblSize]; Ec tbl[2][tblSize];
bool b; bool b;
x %= r; x %= r;
@ -1154,22 +1155,22 @@ public:
tbl[0][0] = P; tbl[0][0] = P;
mulLambda(tbl[1][0], tbl[0][0]); mulLambda(tbl[1][0], tbl[0][0]);
{ {
G P2; Ec P2;
G::dbl(P2, P); Ec::dbl(P2, P);
for (size_t i = 1; i < tblSize; i++) { for (size_t i = 1; i < tblSize; i++) {
G::add(tbl[0][i], tbl[0][i - 1], P2); Ec::add(tbl[0][i], tbl[0][i - 1], P2);
mulLambda(tbl[1][i], tbl[0][i]); mulLambda(tbl[1][i], tbl[0][i]);
} }
} }
const size_t maxBit = fp::max_(naf[0].size(), naf[1].size()); const size_t maxBit = fp::max_(naf[0].size(), naf[1].size());
Q.clear(); Q.clear();
for (size_t i = 0; i < maxBit; i++) { for (size_t i = 0; i < maxBit; i++) {
G::dbl(Q, Q); Ec::dbl(Q, Q);
local::addTbl(Q, tbl[0], naf[0], maxBit - 1 - i); local::addTbl(Q, tbl[0], naf[0], maxBit - 1 - i);
local::addTbl(Q, tbl[1], naf[1], maxBit - 1 - i); local::addTbl(Q, tbl[1], naf[1], maxBit - 1 - i);
} }
} }
static void mulArray(G& z, const G& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime) static void mulArray(Ec& z, const Ec& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime)
{ {
mpz_class s; mpz_class s;
bool b; bool b;
@ -1183,7 +1184,7 @@ public:
*/ */
static void initForSecp256k1(const mpz_class& _r) static void initForSecp256k1(const mpz_class& _r)
{ {
bool b = F::squareRoot(rw, -3); bool b = Fp::squareRoot(rw, -3);
assert(b); assert(b);
(void)b; (void)b;
rw = -(rw + 1) / 2; rw = -(rw + 1) / 2;
@ -1203,12 +1204,12 @@ public:
}; };
// rw = 1 / w = (-1 - sqrt(-3)) / 2 // rw = 1 / w = (-1 - sqrt(-3)) / 2
template<class F, class G> F GLV1T<F, G>::rw; template<class Ec> typename Ec::Fp GLV1T<Ec>::rw;
template<class F, class G> size_t GLV1T<F, G>::rBitSize; template<class Ec> size_t GLV1T<Ec>::rBitSize;
template<class F, class G> mpz_class GLV1T<F, G>::v0; template<class Ec> mpz_class GLV1T<Ec>::v0;
template<class F, class G> mpz_class GLV1T<F, G>::v1; template<class Ec> mpz_class GLV1T<Ec>::v1;
template<class F, class G> mpz_class GLV1T<F, G>::B[2][2]; template<class Ec> mpz_class GLV1T<Ec>::B[2][2];
template<class F, class G> mpz_class GLV1T<F, G>::r; template<class Ec> mpz_class GLV1T<Ec>::r;
struct EcParam { struct EcParam {
const char *name; const char *name;

Loading…
Cancel
Save