diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-09-25 11:25:57 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-09-25 11:25:57 +0800 |
commit | 850347415c152e78c3d5540fcacced70e2073028 (patch) | |
tree | e1bb397cbc9fba607c824962dab9cc31fff0c664 | |
parent | 06dc69840ae1cd73ae4c9a6b34e97e79ccfb5397 (diff) | |
download | tangerine-mcl-850347415c152e78c3d5540fcacced70e2073028.tar.gz tangerine-mcl-850347415c152e78c3d5540fcacced70e2073028.tar.zst tangerine-mcl-850347415c152e78c3d5540fcacced70e2073028.zip |
[she] change benchmark format
-rw-r--r-- | test/she_test.cpp | 170 |
1 files changed, 106 insertions, 64 deletions
diff --git a/test/she_test.cpp b/test/she_test.cpp index be0d1ed..2673149 100644 --- a/test/she_test.cpp +++ b/test/she_test.cpp @@ -242,14 +242,70 @@ CYBOZU_TEST_AUTO(io) } } -CYBOZU_TEST_AUTO(opBench) +CYBOZU_TEST_AUTO(bench) +{ + const SecretKey& sec = g_sec; + PublicKey pub; + sec.getPublicKey(pub); + CipherText c1, c2, c3; + CYBOZU_BENCH("enc", pub.enc, c1, 5); + pub.enc(c2, 4); + CYBOZU_BENCH("add", c1.add, c2); + CYBOZU_BENCH("mul", CipherText::mul, c3, c1, c2); + pub.enc(c1, 5); + pub.enc(c2, 4); + c1.mul(c2); + CYBOZU_BENCH("dec", sec.dec, c1); + c2 = c1; + CYBOZU_BENCH("add after mul", c1.add, c2); +} + +CYBOZU_TEST_AUTO(saveHash) +{ + mcl::she::local::HashTable<SHE::G1> hashTbl1, hashTbl2; + hashTbl1.init(SHE::P, 1234, 123); + std::stringstream ss; + hashTbl1.save(ss); + hashTbl2.load(ss); + CYBOZU_TEST_ASSERT(hashTbl1 == hashTbl2); +} + +CYBOZU_TEST_AUTO(hashBench) { + SecretKey& sec = g_sec; + sec.setByCSPRNG(); + const size_t hashSize = 1u << 21; + SHE::setRangeForDLP(hashSize, 1024); + PublicKey pub; + sec.getPublicKey(pub); + { + int x = 1 << 20; + CipherText one; + CipherText c1; + pub.enc(c1, x); + pub.enc(one, 1, true); + for (int i = 0; i < 12; i++) { + int y = 1 << i; + CipherText c2; + pub.enc(c2, y); + c2.mul(c1); + c2.sub(one); + int expect = x * y - 1; + CYBOZU_TEST_EQUAL(sec.dec(c2), expect); + printf("i=%2d dec(c2)=%08x ", i, expect); + CYBOZU_BENCH_C("dec", 100, sec.dec, c2); + } + } + G1 P, P2; G2 Q; GT e, e2; - Fr r; - r.setRand(mcl::she::local::g_rg); - mpz_class mr = r.getMpz(); + mpz_class mr; + { + Fr r; + r.setRand(mcl::she::local::g_rg); + mr = r.getMpz(); + } BN::hashAndMapToG1(P, "abc"); BN::hashAndMapToG2(Q, "abc"); BN::pairing(e, P, Q); @@ -274,90 +330,76 @@ CYBOZU_TEST_AUTO(opBench) #endif CYBOZU_BENCH_C("miller ", C, BN::millerLoop, e, P, Q); - const SecretKey& sec = g_sec; - PublicKey pub; - sec.getPublicKey(pub); CipherTextG1 ca1; CipherTextG2 ca2; CipherTextM cm; - int m = 12; + typedef std::vector<double> doubleVec; + doubleVec dv; + + int m = int(mcl::she::local::g_rg() % hashSize); + printf("m = %d\n", m); CYBOZU_BENCH_C("encG1 ", C, pub.enc, ca1, m); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("encG2 ", C, pub.enc, ca2, m); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("encGT ", C, pub.enc, cm, m); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("decG1 ", C, sec.dec, ca1); + dv.push_back(cybozu::bench::g_clk.getClock()); // CYBOZU_BENCH_C("decG2", C, sec.dec, ca2); CYBOZU_BENCH_C("decGT ", C, sec.dec, cm); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("mul ", C, CipherTextM::mul, cm, ca1, ca2); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("addG1 ", C, CipherTextG1::add, ca1, ca1, ca1); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("addG2 ", C, CipherTextG2::add, ca2, ca2, ca2); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("addGT ", C, CipherTextM::add, cm, cm, cm); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("rerandG1", C, pub.rerandomize, ca1); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("rerandG2", C, pub.rerandomize, ca2); + dv.push_back(cybozu::bench::g_clk.getClock()); CYBOZU_BENCH_C("rerandGT", C, pub.rerandomize, cm); + dv.push_back(cybozu::bench::g_clk.getClock()); - int m2 = 12345678; - printf("mul %d\n", m2); - CYBOZU_BENCH_C("mulG1 ", C, CipherTextG1::mul, ca1, ca1, m2); - CYBOZU_BENCH_C("mulG2 ", C, CipherTextG2::mul, ca2, ca2, m2); - CYBOZU_BENCH_C("mulGT ", C, CipherTextM::mul, cm, cm, m2); - - m2 = 12; - printf("mul %d\n", m2); - CYBOZU_BENCH_C("mulG1 ", C, CipherTextG1::mul, ca1, ca1, m2); - CYBOZU_BENCH_C("mulG2 ", C, CipherTextG2::mul, ca2, ca2, m2); - CYBOZU_BENCH_C("mulGT ", C, CipherTextM::mul, cm, cm, m2); -} + CYBOZU_BENCH_C("mulG1 ", C, CipherTextG1::mul, ca1, ca1, m); + dv.push_back(cybozu::bench::g_clk.getClock()); + CYBOZU_BENCH_C("mulG2 ", C, CipherTextG2::mul, ca2, ca2, m); + dv.push_back(cybozu::bench::g_clk.getClock()); + CYBOZU_BENCH_C("mulGT ", C, CipherTextM::mul, cm, cm, m); + dv.push_back(cybozu::bench::g_clk.getClock()); + const char *funcNameTbl[] = { + "encG1", + "encG2", + "encGT", -CYBOZU_TEST_AUTO(bench) -{ - const SecretKey& sec = g_sec; - PublicKey pub; - sec.getPublicKey(pub); - CipherText c1, c2, c3; - CYBOZU_BENCH("enc", pub.enc, c1, 5); - pub.enc(c2, 4); - CYBOZU_BENCH("add", c1.add, c2); - CYBOZU_BENCH("mul", CipherText::mul, c3, c1, c2); - pub.enc(c1, 5); - pub.enc(c2, 4); - c1.mul(c2); - CYBOZU_BENCH("dec", sec.dec, c1); - c2 = c1; - CYBOZU_BENCH("add after mul", c1.add, c2); -} + "decG1", + "degGT", -CYBOZU_TEST_AUTO(saveHash) -{ - mcl::she::local::HashTable<SHE::G1> hashTbl1, hashTbl2; - hashTbl1.init(SHE::P, 1234, 123); - std::stringstream ss; - hashTbl1.save(ss); - hashTbl2.load(ss); - CYBOZU_TEST_ASSERT(hashTbl1 == hashTbl2); -} + "mul", -CYBOZU_TEST_AUTO(hashBench) -{ - SecretKey& sec = g_sec; - sec.setByCSPRNG(); - SHE::setRangeForDLP(1u << 21, 1024); - PublicKey pub; - sec.getPublicKey(pub); - int x = 1 << 21; - CipherText c1; - pub.enc(c1, x); - for (int i = 0; i < 20; i++) { - int y = i * 50; - CipherText c2; - pub.enc(c2, y); - c2.mul(c1); - CYBOZU_TEST_EQUAL(sec.dec(c2), x * y); - printf("i=%2d x * y =%5d ", i, x * y); - CYBOZU_BENCH_C("dec", 100, sec.dec, c2); + "addG1", + "addG2", + "addGT", + + "rerandG1", + "rerandG2", + "rerandGT", + + "mulG1", + "mulG2", + "mulGT", + }; + CYBOZU_TEST_EQUAL(dv.size(), CYBOZU_NUM_OF_ARRAY(funcNameTbl)); + for (size_t i = 0; i < dv.size(); i++) { + printf("%8s %.2e\n", funcNameTbl[i], dv[i] / C); } } + |