constTime version of GLV for G1

dev
MITSUNARI Shigeo 8 years ago
parent 682c294ffa
commit 624259deea
  1. 28
      include/mcl/bn.hpp
  2. 2
      include/mcl/ec.hpp

@ -255,7 +255,7 @@ struct GLV {
a += b; a += b;
b = t - b; b = t - b;
} }
void mul(G1& Q, G1 P, mpz_class x) const void mul(G1& Q, G1 P, mpz_class x, bool constTime = false) const
{ {
x %= r; x %= r;
if (x == 0) { if (x == 0) {
@ -324,14 +324,25 @@ struct GLV {
tbl[2] = P; tbl[2].normalize(); tbl[2] = P; tbl[2].normalize();
tbl[3] = A + P; tbl[3].normalize(); tbl[3] = A + P; tbl[3].normalize();
Q.clear(); Q.clear();
for (int i = (int)n - 1; i >= 0; i--) { if (constTime) {
G1::dbl(Q, Q); tbl[0] = tbl[1];
bool ai = mcl::gmp::testBit(a, i); for (int i = (int)n - 1; i >= 0; i--) {
bool bi = mcl::gmp::testBit(b, i); G1::dbl(Q, Q);
unsigned int c = bi * 2 + ai; bool ai = mcl::gmp::testBit(a, i);
if (c > 0) { bool bi = mcl::gmp::testBit(b, i);
unsigned int c = bi * 2 + ai;
Q += tbl[c]; Q += tbl[c];
} }
} else {
for (int i = (int)n - 1; i >= 0; i--) {
G1::dbl(Q, Q);
bool ai = mcl::gmp::testBit(a, i);
bool bi = mcl::gmp::testBit(b, i);
unsigned int c = bi * 2 + ai;
if (c > 0) {
Q += tbl[c];
}
}
} }
#endif #endif
#else #else
@ -476,11 +487,10 @@ struct BNT {
static Param param; static Param param;
static void mulArrayGLV(G1& z, const G1& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime) static void mulArrayGLV(G1& z, const G1& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime)
{ {
(void)constTime;
mpz_class s; mpz_class s;
mcl::gmp::setArray(s, y, yn); mcl::gmp::setArray(s, y, yn);
if (isNegative) s = -s; if (isNegative) s = -s;
param.glv.mul(z, x, s); param.glv.mul(z, x, s, constTime);
} }
static void init(const mcl::bn::CurveParam& cp = CurveFp254BNb, fp::Mode mode = fp::FP_AUTO) static void init(const mcl::bn::CurveParam& cp = CurveFp254BNb, fp::Mode mode = fp::FP_AUTO)
{ {

@ -770,7 +770,7 @@ public:
bool operator<=(const EcT& rhs) const { return !operator>(rhs); } bool operator<=(const EcT& rhs) const { return !operator>(rhs); }
static inline void mulArray(EcT& z, const EcT& x, const fp::Unit *y, size_t yn, bool isNegative, bool constTime = false) static inline void mulArray(EcT& z, const EcT& x, const fp::Unit *y, size_t yn, bool isNegative, bool constTime = false)
{ {
if (!constTime && mulArrayGLV && yn * 2 > Fp::BaseFp::getOp().N) { if (mulArrayGLV && yn * 2 > Fp::BaseFp::getOp().N) {
mulArrayGLV(z, x, y, yn, isNegative, constTime); mulArrayGLV(z, x, y, yn, isNegative, constTime);
return; return;
} }

Loading…
Cancel
Save