add test of power

dev
MITSUNARI Shigeo 9 years ago
parent 0806070fdf
commit 22a875af96
  1. 1
      include/mcl/ec.hpp
  2. 4
      include/mcl/util.hpp
  3. 39
      test/ec_test.cpp

@ -368,6 +368,7 @@ public:
}
static inline void mulArray(EcT& z, const EcT& x, const fp::Unit *y, size_t yn, bool isNegative)
{
x.normalize();
EcT tmp;
const EcT *px = &x;
if (&z == &x) {

@ -139,12 +139,12 @@ void getRandVal(T *out, RG& rg, const T *in, size_t bitSize)
@param x [in]
@param y [in]
@param n [in] size of y[]
@note &out != x and out = the unit element of G
*/
template<class G, class T>
void powerGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, const G&) , void square(G&, const G&)){
#if 1
assert(&out != &x);
G t(x);
while (n > 0) {
if (y[n - 1]) break;
n--;
@ -165,7 +165,7 @@ void powerGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G
for (int j = m - 1; j >= 0; j--) {
square(out, out);
if (v & (T(1) << j)) {
mul(out, out, t);
mul(out, out, x);
}
}
}

@ -31,6 +31,29 @@ struct Test {
Ec::neg(P, O);
CYBOZU_TEST_EQUAL(P, O);
}
void pow2(Ec& Q, const Ec& P, int n) const
{
Q = P;
for (int i = 0; i < n; i++) {
Q += Q;
}
}
void pow2test(const Ec& P, int n) const
{
Ec Q, R;
pow2(Q, P, n);
Q -= P; // Q = (2^n - 1)P
Fp x = 1;
for (int i = 0; i < n; i++) {
x += x;
}
x -= 1; // x = 2^n - 1
Ec::mul(R, P, x);
CYBOZU_TEST_EQUAL(Q, R);
Q = P;
Ec::mul(Q, Q, x);
CYBOZU_TEST_EQUAL(Q, R);
}
void ope() const
{
Fp x(para.gx);
@ -96,6 +119,22 @@ struct Test {
CYBOZU_TEST_EQUAL(R, -P);
R += P; // Ec::mul(R, P, n);
CYBOZU_TEST_ASSERT(R.isZero());
{
const int tbl[] = { 1, 2, 63, 64, 65, 127, 128, 129 };
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
pow2test(P, tbl[i]);
}
}
{
Ec::mul(Q, P, 0);
CYBOZU_TEST_ASSERT(Q.isZero());
Q = P;
CYBOZU_TEST_ASSERT(!Q.isZero());
Ec::mul(Q, Q, 0);
CYBOZU_TEST_ASSERT(Q.isZero());
Ec::mul(Q, P, 1);
CYBOZU_TEST_EQUAL(P, Q);
}
}
void mul() const

Loading…
Cancel
Save