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/low_test.cpp

76 lines
2.0 KiB

9 years ago
#define MCL_USE_LLVM
#include <cybozu/test.hpp>
#include <cybozu/xorshift.hpp>
#include <cybozu/itoa.hpp>
9 years ago
#include "../src/fp_proto.hpp"
#include "../src/low_gmp.hpp"
#include <cybozu/benchmark.hpp>
9 years ago
cybozu::XorShift rg;
extern "C" void mcl_fp_addNC64(uint32_t *z, const uint32_t *x, const uint32_t *y);
extern "C" void mcl_fp_addNC96(uint32_t *z, const uint32_t *x, const uint32_t *y);
extern "C" void mcl_fp_addNC128(uint32_t *z, const uint32_t *x, const uint32_t *y);
extern "C" void mcl_fp_addNC256(uint32_t *z, const uint32_t *x, const uint32_t *y);
extern "C" void add_test(uint32_t *z, const uint32_t *x, const uint32_t *y);
template<size_t N>
void addNC(uint32_t *z, const uint32_t *x, const uint32_t *y);
#define DEF_ADD(BIT) template<> void addNC<BIT>(uint32_t *z, const uint32_t *x, const uint32_t *y) { mcl_fp_addNC ## BIT(z, x, y); }
DEF_ADD(64)
DEF_ADD(96)
DEF_ADD(128)
DEF_ADD(256)
#define CAT(S, BIT) "S##BIT"
template<size_t bit>
void benchAdd()
9 years ago
{
using namespace mcl::fp;
const size_t N = bit / UnitBitSize;
9 years ago
Unit x[N], y[N];
for (int i = 0; i < 10; i++) {
Unit z[N];
Unit w[N];
rg.read(x, N);
rg.read(y, N);
low_add<N>(z, x, y);
addNC<bit>(w, x, y);
CYBOZU_TEST_EQUAL_ARRAY(z, w, N);
9 years ago
}
std::string name = "add" + cybozu::itoa(bit);
CYBOZU_BENCH(name.c_str(), addNC<bit>, x, x, y);
9 years ago
}
CYBOZU_TEST_AUTO(addNC64) { benchAdd<64>(); }
CYBOZU_TEST_AUTO(addNC96) { benchAdd<96>(); }
CYBOZU_TEST_AUTO(addNC128) { benchAdd<128>(); }
CYBOZU_TEST_AUTO(addNC256) { benchAdd<256>(); }
#if 1
CYBOZU_TEST_AUTO(addNC)
9 years ago
{
using namespace mcl::fp;
const size_t bit = 128;
const size_t N = bit / UnitBitSize;
Unit x[N], y[N];
for (int i = 0; i < 10; i++) {
Unit z[N];
Unit w[N];
rg.read(x, N);
rg.read(y, N);
low_add<N>(z, x, y);
addNC<bit>(w, x, y);
CYBOZU_TEST_EQUAL_ARRAY(z, w, N);
add_test(w, x, y);
CYBOZU_TEST_EQUAL_ARRAY(z, w, N);
}
std::string name = "add" + cybozu::itoa(bit);
CYBOZU_BENCH(name.c_str(), addNC<bit>, x, x, y);
CYBOZU_BENCH("add", add_test, x, x, y);
9 years ago
}
#endif