From 85163ee0037b87452080c13a31ff1a8ea0318855 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 29 Jul 2019 04:59:03 +0900 Subject: [PATCH] add initCurve --- include/mcl/ahe.hpp | 76 ----------------------------------------- include/mcl/ec.hpp | 50 +++++++++++++++++++++------ include/mcl/ecdsa.hpp | 27 +++------------ include/mcl/ecparam.hpp | 20 +++++++++-- 4 files changed, 62 insertions(+), 111 deletions(-) delete mode 100644 include/mcl/ahe.hpp diff --git a/include/mcl/ahe.hpp b/include/mcl/ahe.hpp deleted file mode 100644 index 239319d..0000000 --- a/include/mcl/ahe.hpp +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once -/** - @file - @brief 192/256-bit additive homomorphic encryption by lifted-ElGamal - @author MITSUNARI Shigeo(@herumi) - @license modified new BSD license - http://opensource.org/licenses/BSD-3-Clause -*/ -#include -#include - -namespace mcl { - -#ifdef MCL_USE_AHE192 -namespace ahe192 { - -const mcl::EcParam& para = mcl::ecparam::NIST_P192; - -typedef mcl::FpT Fp; -typedef mcl::FpT Zn; -typedef mcl::EcT Ec; -typedef mcl::ElgamalT ElgamalEc; -typedef ElgamalEc::PrivateKey SecretKey; -typedef ElgamalEc::PublicKey PublicKey; -typedef ElgamalEc::CipherText CipherText; - -static inline void initAhe() -{ - Fp::init(para.p); - Zn::init(para.n); - Ec::init(para.a, para.b); - Ec::setIoMode(16); - Zn::setIoMode(16); -} - -static inline void initSecretKey(SecretKey& sec) -{ - const Ec P(Fp(para.gx), Fp(para.gy)); - sec.init(P, Zn::getBitSize()); -} - -} //mcl::ahe192 -#endif - -#ifdef MCL_USE_AHE256 -namespace ahe256 { - -const mcl::EcParam& para = mcl::ecparam::NIST_P256; - -typedef mcl::FpT Fp; -typedef mcl::FpT Zn; -typedef mcl::EcT Ec; -typedef mcl::ElgamalT ElgamalEc; -typedef ElgamalEc::PrivateKey SecretKey; -typedef ElgamalEc::PublicKey PublicKey; -typedef ElgamalEc::CipherText CipherText; - -static inline void initAhe() -{ - Fp::init(para.p); - Zn::init(para.n); - Ec::init(para.a, para.b); - Ec::setIoMode(16); - Zn::setIoMode(16); -} - -static inline void initSecretKey(SecretKey& sec) -{ - const Ec P(Fp(para.gx), Fp(para.gy)); - sec.init(P, Zn::getBitSize()); -} - -} //mcl::ahe256 -#endif - -} // mcl diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 1d0ad49..1ab4096 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -10,6 +10,7 @@ #include #include #include +#include //#define MCL_EC_USE_AFFINE @@ -1211,17 +1212,44 @@ template mpz_class GLV1T::v1; template mpz_class GLV1T::B[2][2]; template mpz_class GLV1T::r; -struct EcParam { - const char *name; - const char *p; - const char *a; - const char *b; - const char *gx; - const char *gy; - const char *n; - size_t bitSize; // bit length of p - int curveType; -}; +/* + Ec : elliptic curve + Zn : cyclic group of the order |Ec| + P : set the generator of Ec unless NULL +*/ +template +void initCurve(bool *pb, int curveType, Ec *P = 0) +{ + typedef typename Ec::Fp Fp; + *pb = false; + const EcParam *ecParam = getEcParam(curveType); + if (ecParam == 0) return; + + Zn::init(pb, ecParam->n); + if (!*pb) return; + Fp::init(pb, ecParam->p); + if (!*pb) return; + Ec::init(pb, ecParam->a, ecParam->b); + if (!*pb) return; + Zn::setIoMode(16); + Fp::setIoMode(16); +// Ec::setIoMode(IoEcAffine); + if (P) { + Fp x, y; + x.setStr(pb, ecParam->gx); + if (!*pb) return; + y.setStr(pb, ecParam->gy); + if (!*pb) return; + P->set(pb, x, y); + if (!*pb) return; + } + if (curveType == MCL_SECP256K1) { + GLV1T::initForSecp256k1(Zn::getOp().mp); + Ec::setMulArrayGLV(GLV1T::mulArray); + } else { + Ec::setMulArrayGLV(0); + } +} } // mcl diff --git a/include/mcl/ecdsa.hpp b/include/mcl/ecdsa.hpp index 6540c19..c92000a 100644 --- a/include/mcl/ecdsa.hpp +++ b/include/mcl/ecdsa.hpp @@ -32,9 +32,9 @@ typedef mcl::EcT Ec; namespace local { struct Param { - mcl::EcParam ecParam; Ec P; mcl::fp::WindowMethod Pbase; + size_t bitSize; }; inline Param& getParam() @@ -79,28 +79,11 @@ const local::Param& param = local::getParam(); inline void init(bool *pb) { - const mcl::EcParam& ecParam = mcl::ecparam::secp256k1; - Zn::init(pb, ecParam.n); - if (!*pb) return; - Fp::init(pb, ecParam.p); - if (!*pb) return; - Ec::init(pb, ecParam.a, ecParam.b); - if (!*pb) return; - Zn::setIoMode(16); - Fp::setIoMode(16); - Ec::setIoMode(mcl::IoEcAffine); local::Param& p = local::getParam(); - p.ecParam = ecParam; - Fp x, y; - x.setStr(pb, ecParam.gx); - if (!*pb) return; - y.setStr(pb, ecParam.gy); - if (!*pb) return; - p.P.set(pb, x, y); + mcl::initCurve(pb, MCL_SECP256K1, &p.P); if (!*pb) return; - p.Pbase.init(pb, p.P, ecParam.bitSize, local::winSize); - mcl::GLV1T::initForSecp256k1(Zn::getOp().mp); - Ec::setMulArrayGLV(mcl::GLV1T::mulArray); + p.bitSize = 256; + p.Pbase.init(pb, p.P, p.bitSize, local::winSize); } #ifndef CYBOZU_DONT_USE_EXCEPTION @@ -119,7 +102,7 @@ struct PrecomputedPublicKey { mcl::fp::WindowMethod pubBase_; void init(bool *pb, const PublicKey& pub) { - pubBase_.init(pb, pub, param.ecParam.bitSize, local::winSize); + pubBase_.init(pb, pub, param.bitSize, local::winSize); } #ifndef CYBOZU_DONT_USE_EXCEPTION void init(const PublicKey& pub) diff --git a/include/mcl/ecparam.hpp b/include/mcl/ecparam.hpp index 087bf8b..9fa4e04 100644 --- a/include/mcl/ecparam.hpp +++ b/include/mcl/ecparam.hpp @@ -6,10 +6,23 @@ @license modified new BSD license http://opensource.org/licenses/BSD-3-Clause */ -#include #include -namespace mcl { namespace ecparam { +namespace mcl { + +struct EcParam { + const char *name; + const char *p; + const char *a; + const char *b; + const char *gx; + const char *gy; + const char *n; + size_t bitSize; // bit length of p + int curveType; +}; + +namespace ecparam { const struct mcl::EcParam secp160k1 = { "secp160k1", @@ -181,6 +194,7 @@ inline const mcl::EcParam* getEcParam(int curve) case MCL_SECP224K1: return &ecparam::secp224k1; case MCL_SECP256K1: return &ecparam::secp256k1; case MCL_SECP384R1: return &ecparam::secp384r1; + case MCL_SECP521R1: return &ecparam::secp521r1; case MCL_NIST_P192: return &ecparam::NIST_P192; case MCL_NIST_P224: return &ecparam::NIST_P224; case MCL_NIST_P256: return &ecparam::NIST_P256; @@ -189,3 +203,5 @@ inline const mcl::EcParam* getEcParam(int curve) } } // mcl + +#include