a portable and fast pairing-based cryptography library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mcl/test/window_method_test.cpp

71 lines
1.6 KiB

10 years ago
#include <cybozu/test.hpp>
#include <mcl/window_method.hpp>
10 years ago
#include <mcl/ec.hpp>
10 years ago
#include <mcl/fp.hpp>
10 years ago
#include <mcl/ecparam.hpp>
10 years ago
10 years ago
CYBOZU_TEST_AUTO(ArrayIterator)
{
const uint32_t in[2] = { 0x12345678, 0xabcdef89 };
const size_t bitSize = 64;
10 years ago
for (size_t w = 1; w <= 32; w++) {
const uint32_t mask = uint32_t((uint64_t(1) << w) - 1);
mpz_class x;
mcl::gmp::setArray(x, in, 2);
mcl::fp::ArrayIterator<uint32_t> ai(in, bitSize, w);
size_t n = (bitSize + w - 1) / w;
10 years ago
for (size_t j = 0; j < n; j++) {
CYBOZU_TEST_ASSERT(ai.hasNext());
uint32_t v = ai.getNext();
CYBOZU_TEST_EQUAL(v, x & mask);
x >>= w;
}
CYBOZU_TEST_ASSERT(!ai.hasNext());
}
}
10 years ago
CYBOZU_TEST_AUTO(int)
{
typedef mcl::FpT<> Fp;
10 years ago
typedef mcl::EcT<Fp> Ec;
const struct mcl::EcParam& para = mcl::ecparam::secp192k1;
Fp::setModulo(para.p);
Ec::setParam(para.a, para.b);
const Fp x(para.gx);
const Fp y(para.gy);
const Ec P(x, y);
10 years ago
typedef mcl::fp::WindowMethod<Ec> PW;
10 years ago
const size_t bitSize = 13;
10 years ago
Ec Q, R;
10 years ago
for (size_t winSize = 10; winSize <= bitSize; winSize++) {
PW pw(P, bitSize, winSize);
for (int i = 0; i < (1 << bitSize); i++) {
pw.mul(Q, i);
Ec::mul(R, P, i);
10 years ago
CYBOZU_TEST_EQUAL(Q, R);
10 years ago
}
}
PW pw(P, para.bitSize, 10);
pw.mul(Q, -12345);
Ec::mul(R, P, -12345);
10 years ago
CYBOZU_TEST_EQUAL(Q, R);
mpz_class t(para.gx);
pw.mul(Q, t);
Ec::mul(R, P, t);
10 years ago
CYBOZU_TEST_EQUAL(Q, R);
t = -t;
pw.mul(Q, t);
Ec::mul(R, P, t);
10 years ago
CYBOZU_TEST_EQUAL(Q, R);
pw.mul(Q, x);
Ec::mul(R, P, x);
10 years ago
CYBOZU_TEST_EQUAL(Q, R);
pw.mul(Q, y);
Ec::mul(R, P, y);
10 years ago
CYBOZU_TEST_EQUAL(Q, R);
10 years ago
}