From 6f82259d9ea9ee4e81a397b0722b121a777e60dd Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 8 Sep 2019 15:01:25 +0900 Subject: [PATCH] [bug] fix Ec::add(A, B, A) for no-normalized A, B --- include/mcl/ec.hpp | 18 ++++++++++++------ test/ec_test.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 708b854..9a802e7 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -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(*y), isNegative)) return; } if (mulArrayGLV && (constTime || yn > 1)) { diff --git a/test/ec_test.cpp b/test/ec_test.cpp index 7999443..3f1e8d4 100644 --- a/test/ec_test.cpp +++ b/test/ec_test.cpp @@ -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();