diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2019-07-29 03:59:03 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2019-07-29 03:59:03 +0800 |
commit | 85163ee0037b87452080c13a31ff1a8ea0318855 (patch) | |
tree | 65f583121850c5cfab0b6a44947d9c734303b2b6 | |
parent | 4c376d551ea3c66de37265fb3699b79247248961 (diff) | |
download | tangerine-mcl-85163ee0037b87452080c13a31ff1a8ea0318855.tar.gz tangerine-mcl-85163ee0037b87452080c13a31ff1a8ea0318855.tar.zst tangerine-mcl-85163ee0037b87452080c13a31ff1a8ea0318855.zip |
add initCurve
-rw-r--r-- | include/mcl/ahe.hpp | 76 | ||||
-rw-r--r-- | include/mcl/ec.hpp | 50 | ||||
-rw-r--r-- | include/mcl/ecdsa.hpp | 27 | ||||
-rw-r--r-- | include/mcl/ecparam.hpp | 20 |
4 files changed, 62 insertions, 111 deletions
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 <mcl/elgamal.hpp> -#include <mcl/ecparam.hpp> - -namespace mcl { - -#ifdef MCL_USE_AHE192 -namespace ahe192 { - -const mcl::EcParam& para = mcl::ecparam::NIST_P192; - -typedef mcl::FpT<mcl::FpTag, 192> Fp; -typedef mcl::FpT<mcl::ZnTag, 192> Zn; -typedef mcl::EcT<Fp> Ec; -typedef mcl::ElgamalT<Ec, Zn> 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<mcl::FpTag, 256> Fp; -typedef mcl::FpT<mcl::ZnTag, 256> Zn; -typedef mcl::EcT<Fp> Ec; -typedef mcl::ElgamalT<Ec, Zn> 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 <cybozu/exception.hpp> #include <mcl/op.hpp> #include <mcl/util.hpp> +#include <mcl/ecparam.hpp> //#define MCL_EC_USE_AFFINE @@ -1211,17 +1212,44 @@ template<class Ec> mpz_class GLV1T<Ec>::v1; template<class Ec> mpz_class GLV1T<Ec>::B[2][2]; template<class Ec> mpz_class GLV1T<Ec>::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<class Ec, class Zn> +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<Ec>::initForSecp256k1(Zn::getOp().mp); + Ec::setMulArrayGLV(GLV1T<Ec>::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<Fp> Ec; namespace local { struct Param { - mcl::EcParam ecParam; Ec P; mcl::fp::WindowMethod<Ec> 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<Ec, Zn>(pb, MCL_SECP256K1, &p.P); if (!*pb) return; - p.Pbase.init(pb, p.P, ecParam.bitSize, local::winSize); - mcl::GLV1T<Ec>::initForSecp256k1(Zn::getOp().mp); - Ec::setMulArrayGLV(mcl::GLV1T<Ec>::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<Ec> 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 <mcl/ec.hpp> #include <mcl/curve_type.h> -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 <mcl/ec.hpp> |