From b57e25f0cf0cca7738c3716b92b247f14184cff4 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 29 Jul 2019 13:01:57 +0900 Subject: [PATCH] old codes use initCurve --- ffi/java/Makefile | 1 + ffi/java/elgamal_impl.hpp | 10 +++++----- include/mcl/curve_type.h | 6 ++++-- include/mcl/ec.hpp | 23 +++++++++++++++-------- include/mcl/ecparam.hpp | 8 +++++--- sample/bench.cpp | 8 ++------ sample/ecdh.cpp | 16 +++++----------- sample/vote.cpp | 9 +++------ test/ec_test.cpp | 4 +--- test/elgamal_test.cpp | 8 ++------ 10 files changed, 43 insertions(+), 50 deletions(-) diff --git a/ffi/java/Makefile b/ffi/java/Makefile index f8172c3..bb581db 100644 --- a/ffi/java/Makefile +++ b/ffi/java/Makefile @@ -49,6 +49,7 @@ test_elgamal: ElgamalTest.class $(ELGAMAL_LIB) $(JAVA_EXE) ElgamalTest $(JAVA_EXE) ElgamalTest -e NIST_P192 $(JAVA_EXE) ElgamalTest -e NIST_P256 -h sha256 + $(JAVA_EXE) ElgamalTest -e secp256k1 -h sha256 $(JAVA_EXE) ElgamalTest -e NIST_P384 -h sha384 $(JAVA_EXE) ElgamalTest -e NIST_P521 -h sha512 diff --git a/ffi/java/elgamal_impl.hpp b/ffi/java/elgamal_impl.hpp index d7130c2..c54f58f 100644 --- a/ffi/java/elgamal_impl.hpp +++ b/ffi/java/elgamal_impl.hpp @@ -34,11 +34,11 @@ void SystemInit(const std::string& param) throw(std::exception) if (iss >> ecParamStr >> hashNameStr) { Param& p = Param::getParam(); p.ecParam = mcl::getEcParam(ecParamStr); - Zn::init(p.ecParam->n); - Fp::init(p.ecParam->p); - Ec::init(p.ecParam->a, p.ecParam->b); - p.hashName = cybozu::crypto::Hash::getName(hashNameStr); - return; + if (p.ecParam) { + mcl::initCurve(p.ecParam->curveType); + p.hashName = cybozu::crypto::Hash::getName(hashNameStr); + return; + } } throw cybozu::Exception("SystemInit:bad param") << param; } diff --git a/include/mcl/curve_type.h b/include/mcl/curve_type.h index 9e4a941..42ba6a6 100644 --- a/include/mcl/curve_type.h +++ b/include/mcl/curve_type.h @@ -18,7 +18,7 @@ enum { /* for only G1 - the size of curve must be less or equal to MCLBN_FP_UNIT_SIZE + the size of curve must be <= MCLBN_FP_UNIT_SIZE */ MCL_EC_BEGIN = 100, MCL_SECP192K1 = MCL_EC_BEGIN, @@ -29,7 +29,9 @@ enum { MCL_NIST_P192 = 105, MCL_NIST_P224 = 106, MCL_NIST_P256 = 107, - MCL_EC_END = MCL_NIST_P256 + 1, + MCL_SECP160K1 = 108, + MCL_P160_1 = 109, + MCL_EC_END = MCL_P160_1 + 1, MCL_NIST_P384 = MCL_SECP384R1, MCL_NIST_P521 = MCL_SECP521R1 }; diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 1ab4096..3686239 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -1215,25 +1215,22 @@ template mpz_class GLV1T::r; /* Ec : elliptic curve Zn : cyclic group of the order |Ec| - P : set the generator of Ec unless NULL + set P the generator of Ec if P != 0 */ template -void initCurve(bool *pb, int curveType, Ec *P = 0) +void initCurve(bool *pb, int curveType, Ec *P = 0, mcl::fp::Mode mode = fp::FP_AUTO, mcl::ec::Mode ecMode = ec::Jacobi) { typedef typename Ec::Fp Fp; *pb = false; const EcParam *ecParam = getEcParam(curveType); if (ecParam == 0) return; - Zn::init(pb, ecParam->n); + Zn::init(pb, ecParam->n, mode); if (!*pb) return; - Fp::init(pb, ecParam->p); + Fp::init(pb, ecParam->p, mode); if (!*pb) return; - Ec::init(pb, ecParam->a, ecParam->b); + Ec::init(pb, ecParam->a, ecParam->b, ecMode); if (!*pb) return; - Zn::setIoMode(16); - Fp::setIoMode(16); -// Ec::setIoMode(IoEcAffine); if (P) { Fp x, y; x.setStr(pb, ecParam->gx); @@ -1251,6 +1248,16 @@ void initCurve(bool *pb, int curveType, Ec *P = 0) } } +#ifndef CYBOZU_DONT_USE_EXCEPTION +template +void initCurve(int curveType, Ec *P = 0, mcl::fp::Mode mode = fp::FP_AUTO, mcl::ec::Mode ecMode = ec::Jacobi) +{ + bool b; + initCurve(&b, curveType, P, mode, ecMode); + if (!b) throw cybozu::Exception("mcl:initCurve") << curveType << mode << ecMode; +} +#endif + } // mcl #ifdef CYBOZU_USE_BOOST diff --git a/include/mcl/ecparam.hpp b/include/mcl/ecparam.hpp index 9fa4e04..dba8063 100644 --- a/include/mcl/ecparam.hpp +++ b/include/mcl/ecparam.hpp @@ -33,9 +33,9 @@ const struct mcl::EcParam secp160k1 = { "0x938cf935318fdced6bc28286531733c3f03c4fee", "0x100000000000000000001b8fa16dfab9aca16b6b3", 160, - -1 + MCL_SECP160K1 }; -// p=2^160 + 7 +// p=2^160 + 7 (for test) const struct mcl::EcParam p160_1 = { "p160_1", "0x10000000000000000000000000000000000000007", @@ -45,7 +45,7 @@ const struct mcl::EcParam p160_1 = { "1236612389951462151661156731535316138439983579284", "1461501637330902918203683518218126812711137002561", 161, - -1 + MCL_P160_1 }; const struct mcl::EcParam secp192k1 = { "secp192k1", @@ -198,6 +198,8 @@ inline const mcl::EcParam* getEcParam(int curve) case MCL_NIST_P192: return &ecparam::NIST_P192; case MCL_NIST_P224: return &ecparam::NIST_P224; case MCL_NIST_P256: return &ecparam::NIST_P256; + case MCL_SECP160K1: return &ecparam::secp160k1; + case MCL_P160_1: return &ecparam::p160_1; default: return 0; } } diff --git a/sample/bench.cpp b/sample/bench.cpp index 0f865b1..de81f25 100644 --- a/sample/bench.cpp +++ b/sample/bench.cpp @@ -76,12 +76,8 @@ void benchFp(size_t bitSize, int mode) void benchEcSub(const mcl::EcParam& para, mcl::fp::Mode mode, mcl::ec::Mode ecMode) { - Fp::init(para.p, mode); - Zn::init(para.n); - Ec::init(para.a, para.b, ecMode); - Fp x(para.gx); - Fp y(para.gy); - Ec P(x, y); + Ec P; + mcl::initCurve(para.curveType, &P, mode, ecMode); Ec P2; Ec::add(P2, P, P); Ec Q = P + P + P; double addT, add2T, subT, dblT, mulT, mulCTT, mulRandT, mulCTRandT, normT; diff --git a/sample/ecdh.cpp b/sample/ecdh.cpp index d5c4a31..4fca3c0 100644 --- a/sample/ecdh.cpp +++ b/sample/ecdh.cpp @@ -7,31 +7,25 @@ #include #include -typedef mcl::FpT<> Fp; +typedef mcl::FpT Fp; typedef mcl::FpT Zn; typedef mcl::EcT Ec; int main() { - cybozu::RandomGenerator rg; /* - system setup with a parameter secp192k1 recommended by SECG Ec is an elliptic curve over Fp the cyclic group of

is isomorphic to Zn */ - const mcl::EcParam& para = mcl::ecparam::secp192k1; - Zn::init(para.n); - Fp::init(para.p); - Ec::init(para.a, para.b); - const Ec P(Fp(para.gx), Fp(para.gy)); - + Ec P; + mcl::initCurve(MCL_SECP192K1, &P); /* Alice setups a private key a and public key aP */ Zn a; Ec aP; - a.setRand(rg); + a.setByCSPRNG(); Ec::mul(aP, P, a); // aP = a * P; std::cout << "aP=" << aP << std::endl; @@ -42,7 +36,7 @@ int main() Zn b; Ec bP; - b.setRand(rg); + b.setByCSPRNG(); Ec::mul(bP, P, b); // bP = b * P; std::cout << "bP=" << bP << std::endl; diff --git a/sample/vote.cpp b/sample/vote.cpp index 8813718..36b08e2 100644 --- a/sample/vote.cpp +++ b/sample/vote.cpp @@ -16,8 +16,8 @@ #include #include -typedef mcl::FpT<> Fp; -typedef mcl::FpT Zn; // use ZnTag because Zn is different class with Fp +typedef mcl::FpT Fp; +typedef mcl::FpT Zn; typedef mcl::EcT Ec; typedef mcl::ElgamalT Elgamal; @@ -59,10 +59,7 @@ struct Param { void SysInit() { - const mcl::EcParam& para = mcl::ecparam::secp192k1; - Zn::init(para.n); - Fp::init(para.p); - Ec::init(para.a, para.b); + mcl::initCurve(MCL_SECP192K1); } template diff --git a/test/ec_test.cpp b/test/ec_test.cpp index ec49adb..7999443 100644 --- a/test/ec_test.cpp +++ b/test/ec_test.cpp @@ -32,9 +32,7 @@ struct Test { : para(para) { printf("fpMode=%s\n", mcl::fp::ModeToStr(fpMode)); - Fp::init(para.p, fpMode); - Zn::init(para.n, fpMode); - Ec::init(para.a, para.b, ecMode); + mcl::initCurve(para.curveType, 0, fpMode, ecMode); } void cstr() const { diff --git a/test/elgamal_test.cpp b/test/elgamal_test.cpp index 8f27f90..f021cff 100644 --- a/test/elgamal_test.cpp +++ b/test/elgamal_test.cpp @@ -15,13 +15,9 @@ cybozu::RandomGenerator g_rg; CYBOZU_TEST_AUTO(testEc) { - Fp::init(para.p); - Zn::init(para.n); - Ec::init(para.a, para.b); - const Fp x0(para.gx); - const Fp y0(para.gy); + Ec P; + mcl::initCurve(para.curveType, &P); const size_t bitSize = Zn::getBitSize(); - const Ec P(x0, y0); /* Zn =

*/