[bug] fix Ec::add(A, B, A) for no-normalized A, B

update-fork
MITSUNARI Shigeo 5 years ago
parent 13d9500f3f
commit 6f82259d9e
  1. 18
      include/mcl/ec.hpp
  2. 42
      test/ec_test.cpp

@ -504,12 +504,18 @@ public:
return;
}
if (isPzOne) {
R.z = H;
if (isQzOne) {
R.z = H;
} else {
Fp::mul(R.z, H, Q.z);
}
} else {
Fp::mul(R.z, P.z, H);
}
if (!isQzOne) {
R.z *= Q.z;
if (isQzOne) {
Fp::mul(R.z, P.z, H);
} else {
Fp::mul(R.z, P.z, Q.z);
R.z *= H;
}
}
Fp::sqr(H3, H); // H^2
Fp::sqr(R.y, r); // r^2
@ -994,7 +1000,7 @@ public:
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)
{
if (!constTime && yn == 1 && *y <= 16 && !isNegative) {
if (!constTime && yn == 1) {
if (mulSmallInt(z, x, static_cast<int>(*y), isNegative)) return;
}
if (mulArrayGLV && (constTime || yn > 1)) {

@ -205,11 +205,43 @@ struct Test {
Ec R;
R.clear();
for (int i = 0; i < 100; i++) {
Ec::mul(Q, P, i);
Q = P;
Ec::mul(Q, Q, i);
CYBOZU_TEST_EQUAL(Q, R);
Q = P;
if (Ec::mulSmallInt(Q, Q, i, false)) {
CYBOZU_TEST_EQUAL(Q, R);
}
R += P;
}
}
void add() const
{
Fp x(para.gx);
Fp y(para.gy);
Ec P1(x, y);
Ec P2, Q1, Q2;
Ec::dbl(P1, P1);
Ec::normalize(P2, P1);
Q1 = P1 + P1;
Ec::normalize(Q2, Q1);
Ec Ptbl[] = { P1, P2 };
Ec Qtbl[] = { Q1, Q2 };
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Ec R1, R2, R3, R4;
R1 = Ptbl[i];
R2 = Qtbl[i];
Ec::add(R3, R1, R2);
Ec::add(R1, R1, R2);
CYBOZU_TEST_EQUAL(R1, R3);
R1 = Ptbl[i];
R2 = Qtbl[i];
Ec::add(R2, R1, R2);
CYBOZU_TEST_EQUAL(R2, R3);
}
}
}
void neg_mul() const
{
@ -220,8 +252,13 @@ struct Test {
Ec R;
R.clear();
for (int i = 0; i < 100; i++) {
Ec::mul(Q, P, -i);
Q = P;
Ec::mul(Q, Q, -i);
CYBOZU_TEST_EQUAL(Q, R);
Q = P;
if (Ec::mulSmallInt(Q, Q, -i, true)) {
CYBOZU_TEST_EQUAL(Q, R);
}
R -= P;
}
}
@ -476,6 +513,7 @@ mul 499.00usec
cstr();
ope();
mul();
add();
neg_mul();
mul_fp();
squareRoot();

Loading…
Cancel
Save