add initCurve

update-fork
MITSUNARI Shigeo 5 years ago
parent 4c376d551e
commit 85163ee003
  1. 76
      include/mcl/ahe.hpp
  2. 50
      include/mcl/ec.hpp
  3. 27
      include/mcl/ecdsa.hpp
  4. 20
      include/mcl/ecparam.hpp

@ -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

@ -10,6 +10,7 @@
#include <cybozu/exception.hpp> #include <cybozu/exception.hpp>
#include <mcl/op.hpp> #include <mcl/op.hpp>
#include <mcl/util.hpp> #include <mcl/util.hpp>
#include <mcl/ecparam.hpp>
//#define MCL_EC_USE_AFFINE //#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>::B[2][2];
template<class Ec> mpz_class GLV1T<Ec>::r; template<class Ec> mpz_class GLV1T<Ec>::r;
struct EcParam { /*
const char *name; Ec : elliptic curve
const char *p; Zn : cyclic group of the order |Ec|
const char *a; P : set the generator of Ec unless NULL
const char *b; */
const char *gx; template<class Ec, class Zn>
const char *gy; void initCurve(bool *pb, int curveType, Ec *P = 0)
const char *n; {
size_t bitSize; // bit length of p typedef typename Ec::Fp Fp;
int curveType; *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 } // mcl

@ -32,9 +32,9 @@ typedef mcl::EcT<Fp> Ec;
namespace local { namespace local {
struct Param { struct Param {
mcl::EcParam ecParam;
Ec P; Ec P;
mcl::fp::WindowMethod<Ec> Pbase; mcl::fp::WindowMethod<Ec> Pbase;
size_t bitSize;
}; };
inline Param& getParam() inline Param& getParam()
@ -79,28 +79,11 @@ const local::Param& param = local::getParam();
inline void init(bool *pb) 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(); local::Param& p = local::getParam();
p.ecParam = ecParam; mcl::initCurve<Ec, Zn>(pb, MCL_SECP256K1, &p.P);
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);
if (!*pb) return; if (!*pb) return;
p.Pbase.init(pb, p.P, ecParam.bitSize, local::winSize); p.bitSize = 256;
mcl::GLV1T<Ec>::initForSecp256k1(Zn::getOp().mp); p.Pbase.init(pb, p.P, p.bitSize, local::winSize);
Ec::setMulArrayGLV(mcl::GLV1T<Ec>::mulArray);
} }
#ifndef CYBOZU_DONT_USE_EXCEPTION #ifndef CYBOZU_DONT_USE_EXCEPTION
@ -119,7 +102,7 @@ struct PrecomputedPublicKey {
mcl::fp::WindowMethod<Ec> pubBase_; mcl::fp::WindowMethod<Ec> pubBase_;
void init(bool *pb, const PublicKey& pub) 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 #ifndef CYBOZU_DONT_USE_EXCEPTION
void init(const PublicKey& pub) void init(const PublicKey& pub)

@ -6,10 +6,23 @@
@license modified new BSD license @license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause http://opensource.org/licenses/BSD-3-Clause
*/ */
#include <mcl/ec.hpp>
#include <mcl/curve_type.h> #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 = { const struct mcl::EcParam secp160k1 = {
"secp160k1", "secp160k1",
@ -181,6 +194,7 @@ inline const mcl::EcParam* getEcParam(int curve)
case MCL_SECP224K1: return &ecparam::secp224k1; case MCL_SECP224K1: return &ecparam::secp224k1;
case MCL_SECP256K1: return &ecparam::secp256k1; case MCL_SECP256K1: return &ecparam::secp256k1;
case MCL_SECP384R1: return &ecparam::secp384r1; case MCL_SECP384R1: return &ecparam::secp384r1;
case MCL_SECP521R1: return &ecparam::secp521r1;
case MCL_NIST_P192: return &ecparam::NIST_P192; case MCL_NIST_P192: return &ecparam::NIST_P192;
case MCL_NIST_P224: return &ecparam::NIST_P224; case MCL_NIST_P224: return &ecparam::NIST_P224;
case MCL_NIST_P256: return &ecparam::NIST_P256; case MCL_NIST_P256: return &ecparam::NIST_P256;
@ -189,3 +203,5 @@ inline const mcl::EcParam* getEcParam(int curve)
} }
} // mcl } // mcl
#include <mcl/ec.hpp>

Loading…
Cancel
Save