load dlp-table test

dev
MITSUNARI Shigeo 7 years ago
parent 171bd6ebc2
commit df468426c4
  1. 69
      sample/she_make_dlp_table.cpp
  2. 7
      test/she_c_test.hpp
  3. 67
      test/she_test.cpp

@ -0,0 +1,69 @@
/*
make she DLP table
*/
#include <mcl/she.hpp>
#include <cybozu/option.hpp>
#include <fstream>
using namespace mcl::she;
struct Param {
int curveType;
int hashBitSize;
int group;
std::string path;
};
template<class HashTable, class G>
void makeTable(const Param& param, const char *groupStr, HashTable& hashTbl, const G& P)
{
char baseName[32];
CYBOZU_SNPRINTF(baseName, sizeof(baseName), "she-dlp-%d-%d-%s.bin", param.curveType, param.hashBitSize, groupStr);
const std::string fileName = param.path + baseName;
printf("file=%s\n", fileName.c_str());
std::ofstream ofs(fileName.c_str(), std::ios::binary);
const size_t hashSize = 1u << param.hashBitSize;
hashTbl.init(P, hashSize);
hashTbl.save(ofs);
}
void run(const Param& param)
{
SHE::init(mcl::getCurveParam(param.curveType));
switch (param.group) {
case 1:
makeTable(param, "g1", SHE::PhashTbl_, SHE::P_);
break;
case 2:
makeTable(param, "g2", SHE::QhashTbl_, SHE::Q_);
break;
case 3:
makeTable(param, "gt", SHE::ePQhashTbl_, SHE::ePQ_);
break;
default:
throw cybozu::Exception("bad group") << param.group;
}
}
int main(int argc, char *argv[])
try
{
cybozu::Option opt;
Param param;
opt.appendOpt(&param.curveType, 0, "ct", ": curveType(0:BN254, 1:BN381_1, 5:BLS12_381)");
opt.appendOpt(&param.hashBitSize, 20, "hb", ": hash bit size");
opt.appendOpt(&param.group, 3, "g", ": group(1:G1, 2:G2, 3:GT");
opt.appendOpt(&param.path, "./", "path", ": path to table");
opt.appendHelp("h");
if (opt.parse(argc, argv)) {
run(param);
} else {
opt.usage();
return 1;
}
} catch (std::exception& e) {
printf("err %s\n", e.what());
return 1;
}

@ -379,6 +379,7 @@ CYBOZU_TEST_AUTO(saveLoad)
const size_t n1 = sheSaveTableForGTDLP(&buf[0], buf.size());
CYBOZU_TEST_ASSERT(n1 > 0);
if (!g_tableName.empty()) {
printf("use table=%s\n", g_tableName.c_str());
std::ofstream ofs(g_tableName.c_str(), std::ios::binary);
ofs.write(buf.c_str(), n1);
}
@ -389,6 +390,12 @@ CYBOZU_TEST_AUTO(saveLoad)
sheSetTryNum(1);
int64_t dec = 0;
CYBOZU_TEST_ASSERT(sheDecGT(&dec, &sec, &ct) != 0);
if (!g_tableName.empty()) {
std::ifstream ifs(g_tableName.c_str(), std::ios::binary);
buf.clear();
buf.resize(n1);
ifs.read(&buf[0], n1);
}
const size_t n2 = sheLoadTableForGTDLP(&buf[0], n1);
CYBOZU_TEST_ASSERT(n2 > 0);
CYBOZU_TEST_ASSERT(sheDecGT(&dec, &sec, &ct) == 0);

@ -115,26 +115,42 @@ CYBOZU_TEST_AUTO(bench2)
}
#endif
CYBOZU_TEST_AUTO(HashTable)
template<class G>
void HashTableTest(const G& P)
{
mcl::she::local::HashTable<G1> hashTbl;
G1 P;
BN::hashAndMapToG1(P, "abc");
mcl::she::local::HashTable<G> hashTbl;
const int maxSize = 100;
const int tryNum = 3;
hashTbl.init(P, maxSize, tryNum);
for (int i = -maxSize; i <= maxSize; i++) {
G1 xP;
G1::mul(xP, P, i);
CYBOZU_TEST_EQUAL(hashTbl.basicLog(xP), i);
}
for (int i = -maxSize * tryNum; i <= maxSize * tryNum; i++) {
G1 xP;
G1::mul(xP, P, i);
CYBOZU_TEST_EQUAL(hashTbl.log(xP), i);
for (int j = 0; j < 2; j++) {
for (int i = -maxSize; i <= maxSize; i++) {
G xP;
G::mul(xP, P, i);
CYBOZU_TEST_EQUAL(hashTbl.basicLog(xP), i);
}
for (int i = -maxSize * tryNum; i <= maxSize * tryNum; i++) {
G xP;
G::mul(xP, P, i);
CYBOZU_TEST_EQUAL(hashTbl.log(xP), i);
}
std::stringstream ss;
hashTbl.save(ss);
mcl::she::local::HashTable<G> hashTbl2;
hashTbl2.load(ss);
hashTbl = hashTbl2;
}
}
CYBOZU_TEST_AUTO(HashTable)
{
G1 P;
BN::hashAndMapToG1(P, "abc");
G2 Q;
BN::hashAndMapToG2(Q, "abc");
HashTableTest(P);
HashTableTest(Q);
}
CYBOZU_TEST_AUTO(GTHashTable)
{
mcl::she::local::HashTable<GT, false> hashTbl;
@ -149,15 +165,22 @@ CYBOZU_TEST_AUTO(GTHashTable)
const int maxSize = 100;
const int tryNum = 3;
hashTbl.init(g, maxSize, tryNum);
for (int i = -maxSize; i <= maxSize; i++) {
GT gx;
GT::pow(gx, g, i);
CYBOZU_TEST_EQUAL(hashTbl.basicLog(gx), i);
}
for (int i = -maxSize * tryNum; i <= maxSize * tryNum; i++) {
GT gx;
GT::pow(gx, g, i);
CYBOZU_TEST_EQUAL(hashTbl.log(gx), i);
for (int j = 0; j < 2; j++) {
for (int i = -maxSize; i <= maxSize; i++) {
GT gx;
GT::pow(gx, g, i);
CYBOZU_TEST_EQUAL(hashTbl.basicLog(gx), i);
}
for (int i = -maxSize * tryNum; i <= maxSize * tryNum; i++) {
GT gx;
GT::pow(gx, g, i);
CYBOZU_TEST_EQUAL(hashTbl.log(gx), i);
}
std::stringstream ss;
hashTbl.save(ss);
mcl::she::local::HashTable<GT, false> hashTbl2;
hashTbl2.load(ss);
hashTbl = hashTbl2;
}
}

Loading…
Cancel
Save