From d32e456fc8b8479185f3dd722adb96b3bceff26e Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 14 Apr 2021 17:08:17 +0900 Subject: [PATCH] add sample of g1only she --- sample/she_g1only.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 sample/she_g1only.cpp diff --git a/sample/she_g1only.cpp b/sample/she_g1only.cpp new file mode 100644 index 0000000..9dc5461 --- /dev/null +++ b/sample/she_g1only.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + +using namespace mcl::she; + +const mcl::EcParam& g_para = mcl::ecparam::secp256k1; +//const mcl::EcParam& g_para = mcl::ecparam::secp160k1; + +void elgamal() +{ + puts("elgamal"); + struct TagZn; + typedef mcl::FpT<> Fp; + typedef mcl::FpT Zn; + typedef mcl::EcT Ec; + typedef mcl::ElgamalT ElgamalEc; + Ec P; + mcl::initCurve(g_para.curveType, &P); + const size_t bitSize = Zn::getBitSize(); + cybozu::RandomGenerator rg; + + ElgamalEc::PrivateKey sec; + sec.init(P, bitSize, rg); + sec.setCache(0, 60000); + const ElgamalEc::PublicKey& pub = sec.getPublicKey(); + + const int m1 = 123; + const int m2 = 654; + ElgamalEc::CipherText c1, c2; + pub.enc(c1, m1); + pub.enc(c2, m2); + c1.add(c2); + Zn dec; + sec.dec(dec, c1); + std::cout << "dec=" << dec << std::endl; + CYBOZU_BENCH_C("enc", 1000, pub.enc, c1, m1); +} + +int main() + try +{ + // initialize system + initG1only(g_para, 1024); + + SecretKey sec; + sec.setByCSPRNG(); + PublicKey pub; + sec.getPublicKey(pub); + PrecomputedPublicKey ppub; + ppub.init(pub); + + int m1 = 123; + int m2 = 654; + + CipherTextG1 c1, c2; + ppub.enc(c1, m1); + ppub.enc(c2, m2); + CYBOZU_BENCH_C("pub.enc", 1000, pub.enc, c1, m1); + CYBOZU_BENCH_C("ppub.enc", 1000, ppub.enc, c1, m1); + add(c1, c1, c2); + int m = sec.dec(c1); + printf("Dec(Enc(%d) + Enc(%d)) = %d(%s)\n", m1, m2, m, m == m1 + m2 ? "ok" : "ng"); + + elgamal(); + +} catch (std::exception& e) { + printf("ERR %s\n", e.what()); + return 1; +} +