aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-04-06 17:21:04 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-04-06 17:21:04 +0800
commitdf468426c4651464788ed3c4a67318b02ed06cf4 (patch)
tree869fc1cfbfb9bf7ffa96af99fb929fbf196bfbb5
parent171bd6ebc2fb06f44c3b5a0d68f15975f86a5557 (diff)
downloaddexon-mcl-df468426c4651464788ed3c4a67318b02ed06cf4.tar.gz
dexon-mcl-df468426c4651464788ed3c4a67318b02ed06cf4.tar.zst
dexon-mcl-df468426c4651464788ed3c4a67318b02ed06cf4.zip
load dlp-table test
-rw-r--r--sample/she_make_dlp_table.cpp69
-rw-r--r--test/she_c_test.hpp7
-rw-r--r--test/she_test.cpp67
3 files changed, 121 insertions, 22 deletions
diff --git a/sample/she_make_dlp_table.cpp b/sample/she_make_dlp_table.cpp
new file mode 100644
index 0000000..6b57999
--- /dev/null
+++ b/sample/she_make_dlp_table.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;
+}
diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp
index 6e30e29..e9dd71a 100644
--- a/test/she_c_test.hpp
+++ b/test/she_c_test.hpp
@@ -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);
diff --git a/test/she_test.cpp b/test/she_test.cpp
index d07eac5..e1851a8 100644
--- a/test/she_test.cpp
+++ b/test/she_test.cpp
@@ -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;
}
}