old codes use initCurve

update-fork
MITSUNARI Shigeo 5 years ago
parent 85163ee003
commit b57e25f0cf
  1. 1
      ffi/java/Makefile
  2. 10
      ffi/java/elgamal_impl.hpp
  3. 6
      include/mcl/curve_type.h
  4. 23
      include/mcl/ec.hpp
  5. 8
      include/mcl/ecparam.hpp
  6. 8
      sample/bench.cpp
  7. 16
      sample/ecdh.cpp
  8. 9
      sample/vote.cpp
  9. 4
      test/ec_test.cpp
  10. 8
      test/elgamal_test.cpp

@ -49,6 +49,7 @@ test_elgamal: ElgamalTest.class $(ELGAMAL_LIB)
$(JAVA_EXE) ElgamalTest $(JAVA_EXE) ElgamalTest
$(JAVA_EXE) ElgamalTest -e NIST_P192 $(JAVA_EXE) ElgamalTest -e NIST_P192
$(JAVA_EXE) ElgamalTest -e NIST_P256 -h sha256 $(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_P384 -h sha384
$(JAVA_EXE) ElgamalTest -e NIST_P521 -h sha512 $(JAVA_EXE) ElgamalTest -e NIST_P521 -h sha512

@ -34,11 +34,11 @@ void SystemInit(const std::string& param) throw(std::exception)
if (iss >> ecParamStr >> hashNameStr) { if (iss >> ecParamStr >> hashNameStr) {
Param& p = Param::getParam(); Param& p = Param::getParam();
p.ecParam = mcl::getEcParam(ecParamStr); p.ecParam = mcl::getEcParam(ecParamStr);
Zn::init(p.ecParam->n); if (p.ecParam) {
Fp::init(p.ecParam->p); mcl::initCurve<Ec, Zn>(p.ecParam->curveType);
Ec::init(p.ecParam->a, p.ecParam->b); p.hashName = cybozu::crypto::Hash::getName(hashNameStr);
p.hashName = cybozu::crypto::Hash::getName(hashNameStr); return;
return; }
} }
throw cybozu::Exception("SystemInit:bad param") << param; throw cybozu::Exception("SystemInit:bad param") << param;
} }

@ -18,7 +18,7 @@ enum {
/* /*
for only G1 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_EC_BEGIN = 100,
MCL_SECP192K1 = MCL_EC_BEGIN, MCL_SECP192K1 = MCL_EC_BEGIN,
@ -29,7 +29,9 @@ enum {
MCL_NIST_P192 = 105, MCL_NIST_P192 = 105,
MCL_NIST_P224 = 106, MCL_NIST_P224 = 106,
MCL_NIST_P256 = 107, 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_P384 = MCL_SECP384R1,
MCL_NIST_P521 = MCL_SECP521R1 MCL_NIST_P521 = MCL_SECP521R1
}; };

@ -1215,25 +1215,22 @@ template<class Ec> mpz_class GLV1T<Ec>::r;
/* /*
Ec : elliptic curve Ec : elliptic curve
Zn : cyclic group of the order |Ec| 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<class Ec, class Zn> template<class Ec, class Zn>
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; typedef typename Ec::Fp Fp;
*pb = false; *pb = false;
const EcParam *ecParam = getEcParam(curveType); const EcParam *ecParam = getEcParam(curveType);
if (ecParam == 0) return; if (ecParam == 0) return;
Zn::init(pb, ecParam->n); Zn::init(pb, ecParam->n, mode);
if (!*pb) return; if (!*pb) return;
Fp::init(pb, ecParam->p); Fp::init(pb, ecParam->p, mode);
if (!*pb) return; if (!*pb) return;
Ec::init(pb, ecParam->a, ecParam->b); Ec::init(pb, ecParam->a, ecParam->b, ecMode);
if (!*pb) return; if (!*pb) return;
Zn::setIoMode(16);
Fp::setIoMode(16);
// Ec::setIoMode(IoEcAffine);
if (P) { if (P) {
Fp x, y; Fp x, y;
x.setStr(pb, ecParam->gx); x.setStr(pb, ecParam->gx);
@ -1251,6 +1248,16 @@ void initCurve(bool *pb, int curveType, Ec *P = 0)
} }
} }
#ifndef CYBOZU_DONT_USE_EXCEPTION
template<class Ec, class Zn>
void initCurve(int curveType, Ec *P = 0, mcl::fp::Mode mode = fp::FP_AUTO, mcl::ec::Mode ecMode = ec::Jacobi)
{
bool b;
initCurve<Ec, Zn>(&b, curveType, P, mode, ecMode);
if (!b) throw cybozu::Exception("mcl:initCurve") << curveType << mode << ecMode;
}
#endif
} // mcl } // mcl
#ifdef CYBOZU_USE_BOOST #ifdef CYBOZU_USE_BOOST

@ -33,9 +33,9 @@ const struct mcl::EcParam secp160k1 = {
"0x938cf935318fdced6bc28286531733c3f03c4fee", "0x938cf935318fdced6bc28286531733c3f03c4fee",
"0x100000000000000000001b8fa16dfab9aca16b6b3", "0x100000000000000000001b8fa16dfab9aca16b6b3",
160, 160,
-1 MCL_SECP160K1
}; };
// p=2^160 + 7 // p=2^160 + 7 (for test)
const struct mcl::EcParam p160_1 = { const struct mcl::EcParam p160_1 = {
"p160_1", "p160_1",
"0x10000000000000000000000000000000000000007", "0x10000000000000000000000000000000000000007",
@ -45,7 +45,7 @@ const struct mcl::EcParam p160_1 = {
"1236612389951462151661156731535316138439983579284", "1236612389951462151661156731535316138439983579284",
"1461501637330902918203683518218126812711137002561", "1461501637330902918203683518218126812711137002561",
161, 161,
-1 MCL_P160_1
}; };
const struct mcl::EcParam secp192k1 = { const struct mcl::EcParam secp192k1 = {
"secp192k1", "secp192k1",
@ -198,6 +198,8 @@ inline const mcl::EcParam* getEcParam(int curve)
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;
case MCL_SECP160K1: return &ecparam::secp160k1;
case MCL_P160_1: return &ecparam::p160_1;
default: return 0; default: return 0;
} }
} }

@ -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) void benchEcSub(const mcl::EcParam& para, mcl::fp::Mode mode, mcl::ec::Mode ecMode)
{ {
Fp::init(para.p, mode); Ec P;
Zn::init(para.n); mcl::initCurve<Ec, Zn>(para.curveType, &P, mode, ecMode);
Ec::init(para.a, para.b, ecMode);
Fp x(para.gx);
Fp y(para.gy);
Ec P(x, y);
Ec P2; Ec::add(P2, P, P); Ec P2; Ec::add(P2, P, P);
Ec Q = P + P + P; Ec Q = P + P + P;
double addT, add2T, subT, dblT, mulT, mulCTT, mulRandT, mulCTRandT, normT; double addT, add2T, subT, dblT, mulT, mulCTT, mulRandT, mulCTRandT, normT;

@ -7,31 +7,25 @@
#include <mcl/fp.hpp> #include <mcl/fp.hpp>
#include <mcl/ecparam.hpp> #include <mcl/ecparam.hpp>
typedef mcl::FpT<> Fp; typedef mcl::FpT<mcl::FpTag> Fp;
typedef mcl::FpT<mcl::ZnTag> Zn; typedef mcl::FpT<mcl::ZnTag> Zn;
typedef mcl::EcT<Fp> Ec; typedef mcl::EcT<Fp> Ec;
int main() int main()
{ {
cybozu::RandomGenerator rg;
/* /*
system setup with a parameter secp192k1 recommended by SECG
Ec is an elliptic curve over Fp Ec is an elliptic curve over Fp
the cyclic group of <P> is isomorphic to Zn the cyclic group of <P> is isomorphic to Zn
*/ */
const mcl::EcParam& para = mcl::ecparam::secp192k1; Ec P;
Zn::init(para.n); mcl::initCurve<Ec, Zn>(MCL_SECP192K1, &P);
Fp::init(para.p);
Ec::init(para.a, para.b);
const Ec P(Fp(para.gx), Fp(para.gy));
/* /*
Alice setups a private key a and public key aP Alice setups a private key a and public key aP
*/ */
Zn a; Zn a;
Ec aP; Ec aP;
a.setRand(rg); a.setByCSPRNG();
Ec::mul(aP, P, a); // aP = a * P; Ec::mul(aP, P, a); // aP = a * P;
std::cout << "aP=" << aP << std::endl; std::cout << "aP=" << aP << std::endl;
@ -42,7 +36,7 @@ int main()
Zn b; Zn b;
Ec bP; Ec bP;
b.setRand(rg); b.setByCSPRNG();
Ec::mul(bP, P, b); // bP = b * P; Ec::mul(bP, P, b); // bP = b * P;
std::cout << "bP=" << bP << std::endl; std::cout << "bP=" << bP << std::endl;

@ -16,8 +16,8 @@
#include <mcl/elgamal.hpp> #include <mcl/elgamal.hpp>
#include <mcl/ecparam.hpp> #include <mcl/ecparam.hpp>
typedef mcl::FpT<> Fp; typedef mcl::FpT<mcl::FpTag> Fp;
typedef mcl::FpT<mcl::ZnTag> Zn; // use ZnTag because Zn is different class with Fp typedef mcl::FpT<mcl::ZnTag> Zn;
typedef mcl::EcT<Fp> Ec; typedef mcl::EcT<Fp> Ec;
typedef mcl::ElgamalT<Ec, Zn> Elgamal; typedef mcl::ElgamalT<Ec, Zn> Elgamal;
@ -59,10 +59,7 @@ struct Param {
void SysInit() void SysInit()
{ {
const mcl::EcParam& para = mcl::ecparam::secp192k1; mcl::initCurve<Ec, Zn>(MCL_SECP192K1);
Zn::init(para.n);
Fp::init(para.p);
Ec::init(para.a, para.b);
} }
template<class T> template<class T>

@ -32,9 +32,7 @@ struct Test {
: para(para) : para(para)
{ {
printf("fpMode=%s\n", mcl::fp::ModeToStr(fpMode)); printf("fpMode=%s\n", mcl::fp::ModeToStr(fpMode));
Fp::init(para.p, fpMode); mcl::initCurve<Ec, Zn>(para.curveType, 0, fpMode, ecMode);
Zn::init(para.n, fpMode);
Ec::init(para.a, para.b, ecMode);
} }
void cstr() const void cstr() const
{ {

@ -15,13 +15,9 @@ cybozu::RandomGenerator g_rg;
CYBOZU_TEST_AUTO(testEc) CYBOZU_TEST_AUTO(testEc)
{ {
Fp::init(para.p); Ec P;
Zn::init(para.n); mcl::initCurve<Ec, Zn>(para.curveType, &P);
Ec::init(para.a, para.b);
const Fp x0(para.gx);
const Fp y0(para.gy);
const size_t bitSize = Zn::getBitSize(); const size_t bitSize = Zn::getBitSize();
const Ec P(x0, y0);
/* /*
Zn = <P> Zn = <P>
*/ */

Loading…
Cancel
Save