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

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

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

Loading…
Cancel
Save