defaut constructor of Ec does not clear the memory

dev
MITSUNARI Shigeo 8 years ago
parent ba9de011e7
commit faa01c6e11
  1. 12
      include/mcl/ec.hpp
  2. 1
      include/mcl/window_method.hpp
  3. 4
      readme.md
  4. 1
      test/bn_test.cpp
  5. 7
      test/ec_test.cpp

@ -61,12 +61,8 @@ public:
*/ */
static bool verifyOrder_; static bool verifyOrder_;
static mpz_class order_; static mpz_class order_;
#ifdef MCL_EC_USE_AFFINE /* default constructor is undefined value */
EcT() : inf_(true) {} EcT() {}
#else
/* can't call z.clear() beforing Fp::init() */
EcT() { memset(&z, 0, sizeof(z)); }
#endif
EcT(const Fp& _x, const Fp& _y) EcT(const Fp& _x, const Fp& _y)
{ {
set(_x, _y); set(_x, _y);
@ -737,7 +733,9 @@ public:
static inline void getYfromX(Fp& y, const Fp& x, bool isYodd) static inline void getYfromX(Fp& y, const Fp& x, bool isYodd)
{ {
getWeierstrass(y, x); getWeierstrass(y, x);
if (!Fp::squareRoot(y, y)) throw cybozu::Exception("EcT:getYfromX") << x << isYodd; if (!Fp::squareRoot(y, y)) {
throw cybozu::Exception("EcT:getYfromX") << x << isYodd;
}
if (y.isOdd() ^ isYodd) { if (y.isOdd() ^ isYodd) {
Fp::neg(y, y); Fp::neg(y, y);
} }

@ -98,6 +98,7 @@ public:
for (size_t i = 0; i < tblNum; i++) { for (size_t i = 0; i < tblNum; i++) {
tbl_[i].resize(r); tbl_[i].resize(r);
EcV& w = tbl_[i]; EcV& w = tbl_[i];
w[0].clear();
for (size_t d = 1; d < r; d *= 2) { for (size_t d = 1; d < r; d *= 2) {
for (size_t j = 0; j < d; j++) { for (size_t j = 0; j < d; j++) {
Ec::add(w[j + d], w[j], t); Ec::add(w[j + d], w[j], t);

@ -116,6 +116,10 @@ mcl::bn256::BN::pairing(e, P, Q);
See [test/bn_test.cpp](https://github.com/herumi/mcl/blob/master/test/bn_test.cpp). See [test/bn_test.cpp](https://github.com/herumi/mcl/blob/master/test/bn_test.cpp).
## Default constructor of Fp, Ec, etc.
A default constructor does not initialize the instance.
Set a valid value before reffering it.
## String format of G1 and G2 ## String format of G1 and G2
G1 and G2 have three elements of Fp (x, y, z) for Jacobi coordinate. G1 and G2 have three elements of Fp (x, y, z) for Jacobi coordinate.
normalize() method normalizes it to affine coordinate (x, y, 1) or (0, 0, 0). normalize() method normalizes it to affine coordinate (x, y, 1) or (0, 0, 0).

@ -89,6 +89,7 @@ void testSetStr(const G2& Q0)
{ {
G2::setCompressedExpression(); G2::setCompressedExpression();
G2 Q; G2 Q;
Q.clear();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
G2 R; G2 R;
R.setStr(Q.getStr()); R.setStr(Q.getStr());

@ -28,9 +28,11 @@ struct Test {
void cstr() const void cstr() const
{ {
Ec O; Ec O;
O.clear();
CYBOZU_TEST_ASSERT(O.isZero()); CYBOZU_TEST_ASSERT(O.isZero());
CYBOZU_TEST_ASSERT(O.isValid()); CYBOZU_TEST_ASSERT(O.isValid());
Ec P; Ec P;
P.clear();
Ec::neg(P, O); Ec::neg(P, O);
CYBOZU_TEST_EQUAL(P, O); CYBOZU_TEST_EQUAL(P, O);
} }
@ -65,6 +67,7 @@ struct Test {
CYBOZU_TEST_NO_EXCEPTION(Ec(x, y)); CYBOZU_TEST_NO_EXCEPTION(Ec(x, y));
CYBOZU_TEST_EXCEPTION(Ec(x, y + 1), cybozu::Exception); CYBOZU_TEST_EXCEPTION(Ec(x, y + 1), cybozu::Exception);
Ec P(x, y), Q, R, O; Ec P(x, y), Q, R, O;
O.clear();
CYBOZU_TEST_ASSERT(P.isNormalized()); CYBOZU_TEST_ASSERT(P.isNormalized());
{ {
Ec::neg(Q, P); Ec::neg(Q, P);
@ -153,6 +156,7 @@ struct Test {
Ec P(x, y); Ec P(x, y);
Ec Q; Ec Q;
Ec R; Ec R;
R.clear();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Ec::mul(Q, P, i); Ec::mul(Q, P, i);
CYBOZU_TEST_EQUAL(Q, R); CYBOZU_TEST_EQUAL(Q, R);
@ -167,6 +171,7 @@ struct Test {
Ec P(x, y); Ec P(x, y);
Ec Q; Ec Q;
Ec R; Ec R;
R.clear();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Ec::mul(Q, P, -i); Ec::mul(Q, P, -i);
CYBOZU_TEST_EQUAL(Q, R); CYBOZU_TEST_EQUAL(Q, R);
@ -194,6 +199,7 @@ struct Test {
Ec P(x, y); Ec P(x, y);
Ec Q; Ec Q;
Ec R; Ec R;
R.clear();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Ec::mul(Q, P, Zn(i)); Ec::mul(Q, P, Zn(i));
CYBOZU_TEST_EQUAL(Q, R); CYBOZU_TEST_EQUAL(Q, R);
@ -299,6 +305,7 @@ struct Test {
{ {
std::stringstream ss; std::stringstream ss;
Ec Q; Ec Q;
Q.clear();
ss << Q; ss << Q;
Ec R; Ec R;
ss >> R; ss >> R;

Loading…
Cancel
Save