split error and throw

dev
MITSUNARI Shigeo 7 years ago
parent 0cbd1e2946
commit 1bf7bbff64
  1. 2
      Makefile
  2. 42
      include/mcl/bn.hpp
  3. 13
      include/mcl/ec.hpp
  4. 28
      include/mcl/ecdsa.hpp
  5. 26
      include/mcl/fp.hpp
  6. 13
      include/mcl/gmp_util.hpp
  7. 5
      include/mcl/op.hpp
  8. 14
      include/mcl/operator.hpp
  9. 4
      include/mcl/vint.hpp
  10. 18
      include/mcl/window_method.hpp
  11. 52
      src/bn_c_impl.hpp
  12. 60
      src/ecdsa_c.cpp
  13. 12
      src/fp.cpp
  14. 35
      src/she_c_impl.hpp
  15. 78
      test/she_test.cpp

@ -231,7 +231,7 @@ $(EXE_DIR)/she_c256_test.exe: $(OBJ_DIR)/she_c256_test.o $(SHE256_LIB) $(MCL_LIB
$(EXE_DIR)/she_c384_test.exe: $(OBJ_DIR)/she_c384_test.o $(SHE384_LIB) $(MCL_LIB) $(EXE_DIR)/she_c384_test.exe: $(OBJ_DIR)/she_c384_test.o $(SHE384_LIB) $(MCL_LIB)
$(PRE)$(CXX) $< -o $@ $(SHE384_LIB) $(MCL_LIB) $(LDFLAGS) $(PRE)$(CXX) $< -o $@ $(SHE384_LIB) $(MCL_LIB) $(LDFLAGS)
$(EXE_DIR)/ecdsa_c_test.exe: $(OBJ_DIR)/ecdsa_c_test.o $(ECDSA_LIB) $(MCL_LIB) $(EXE_DIR)/ecdsa_c_test.exe: $(OBJ_DIR)/ecdsa_c_test.o $(ECDSA_LIB) $(MCL_LIB) src/ecdsa_c.cpp include/mcl/ecdsa.hpp include/mcl/ecdsa.h
$(PRE)$(CXX) $< -o $@ $(ECDSA_LIB) $(MCL_LIB) $(LDFLAGS) $(PRE)$(CXX) $< -o $@ $(ECDSA_LIB) $(MCL_LIB) $(LDFLAGS)
SAMPLE_EXE=$(addprefix $(EXE_DIR)/,$(addsuffix .exe,$(basename $(SAMPLE_SRC)))) SAMPLE_EXE=$(addprefix $(EXE_DIR)/,$(addsuffix .exe,$(basename $(SAMPLE_SRC))))

@ -886,7 +886,7 @@ struct Param {
bool useNAF; bool useNAF;
local::SignVec zReplTbl; local::SignVec zReplTbl;
void init(int *pret, const mcl::CurveParam& cp, fp::Mode mode) void init(bool *pb, const mcl::CurveParam& cp, fp::Mode mode)
{ {
this->cp = cp; this->cp = cp;
isBLS12 = cp.curveType == MCL_BLS12_381; isBLS12 = cp.curveType == MCL_BLS12_381;
@ -910,10 +910,10 @@ struct Param {
assert((p % 6) == 1); assert((p % 6) == 1);
r = local::evalPoly(z, rCoff); r = local::evalPoly(z, rCoff);
} }
Fp::init(pret, p, mode); Fp::init(pb, p, mode);
if (*pret < 0) return; if (!*pb) return;
Fr::init(pret, r, mode); Fr::init(pb, r, mode);
if (*pret < 0) return; if (!*pb) return;
Fp2::init(cp.xi_a); Fp2::init(cp.xi_a);
Fp2 xi(cp.xi_a, 1); Fp2 xi(cp.xi_a, 1);
g2 = Fp2::get_gTbl()[0]; g2 = Fp2::get_gTbl()[0];
@ -968,13 +968,13 @@ struct Param {
} }
glv1.init(r, z, isBLS12); glv1.init(r, z, isBLS12);
glv2.init(r, z, isBLS12); glv2.init(r, z, isBLS12);
*pret = 0; *pb = true;
} }
void init(const mcl::CurveParam& cp, fp::Mode mode) void init(const mcl::CurveParam& cp, fp::Mode mode)
{ {
int ret; bool b;
init(&ret, cp, mode); init(&b, cp, mode);
if (ret < 0) throw cybozu::Exception("Param:init") << ret; if (!b) throw cybozu::Exception("Param:init");
} }
}; };
@ -1924,37 +1924,37 @@ namespace BN {
using namespace mcl::bn; // backward compatibility using namespace mcl::bn; // backward compatibility
inline void init(int *pret, const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO) inline void init(bool *pb, const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO)
{ {
local::StaticVar<>::param.init(pret, cp, mode); local::StaticVar<>::param.init(pb, cp, mode);
if (*pret < 0) return; if (*pb) return;
G1::setMulArrayGLV(local::mulArrayGLV1); G1::setMulArrayGLV(local::mulArrayGLV1);
G2::setMulArrayGLV(local::mulArrayGLV2); G2::setMulArrayGLV(local::mulArrayGLV2);
Fp12::setPowArrayGLV(local::powArrayGLV2); Fp12::setPowArrayGLV(local::powArrayGLV2);
G1::setCompressedExpression(); G1::setCompressedExpression();
G2::setCompressedExpression(); G2::setCompressedExpression();
*pret = 0; *pb = true;
} }
inline void init(const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO) inline void init(const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO)
{ {
int ret; bool b;
init(&ret, cp, mode); init(&b, cp, mode);
if (ret < 0) throw cybozu::Exception("BN:init") << ret; if (!b) throw cybozu::Exception("BN:init");
} }
} // mcl::bn::BN } // mcl::bn::BN
inline void initPairing(int *pret, const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO) inline void initPairing(bool *pb, const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO)
{ {
BN::init(pret, cp, mode); BN::init(pb, cp, mode);
} }
inline void initPairing(const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO) inline void initPairing(const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO)
{ {
int ret; bool b;
BN::init(&ret, cp, mode); BN::init(&b, cp, mode);
if (ret < 0) throw cybozu::Exception("bn:initPairing") << ret; if (!b) throw cybozu::Exception("bn:initPairing");
} }
} } // mcl::bn } } // mcl::bn

@ -199,9 +199,20 @@ public:
{ {
init(astr, bstr, mode); init(astr, bstr, mode);
} }
static inline void init(bool *pb, const char *astr, const char *bstr, int mode = ec::Jacobi)
{
Fp a, b;
a.setStr(pb, astr);
if (!*pb) return;
b.setStr(pb, bstr);
if (!*pb) return;
init(a, b, mode);
}
static inline void init(const std::string& astr, const std::string& bstr, int mode = ec::Jacobi) static inline void init(const std::string& astr, const std::string& bstr, int mode = ec::Jacobi)
{ {
init(Fp(astr), Fp(bstr), mode); bool b;
init(&b, astr.c_str(), bstr.c_str(), mode);
if (!b) throw cybozu::Exception("mcl:EcT:init");
} }
// verify the order // verify the order
bool isValidOrder() const bool isValidOrder() const

@ -77,19 +77,29 @@ inline void setHashOf(Zn& x, const void *msg, size_t msgSize)
const local::Param& param = local::getParam(); const local::Param& param = local::getParam();
inline void init() inline void init(bool *pb)
{ {
const mcl::EcParam& ecParam = mcl::ecparam::secp256k1; const mcl::EcParam& ecParam = mcl::ecparam::secp256k1;
Zn::init(ecParam.n); Zn::init(pb, ecParam.n);
Fp::init(ecParam.p); if (!*pb) return;
Ec::init(ecParam.a, ecParam.b); Fp::init(pb, ecParam.p);
if (!*pb) return;
Ec::init(pb, ecParam.a, ecParam.b);
if (!*pb) return;
Zn::setIoMode(16); Zn::setIoMode(16);
Fp::setIoMode(16); Fp::setIoMode(16);
Ec::setIoMode(mcl::IoEcAffine); Ec::setIoMode(mcl::IoEcAffine);
local::Param& p = local::getParam(); local::Param& p = local::getParam();
p.ecParam = ecParam; p.ecParam = ecParam;
p.P.set(Fp(ecParam.gx), Fp(ecParam.gy)); p.P.set(Fp(ecParam.gx), Fp(ecParam.gy));
p.Pbase.init(p.P, ecParam.bitSize, local::winSize); p.Pbase.init(pb, p.P, ecParam.bitSize, local::winSize);
}
inline void init()
{
bool b;
init(&b);
if (!b) throw cybozu::Exception("ecdsa:init");
} }
typedef Zn SecretKey; typedef Zn SecretKey;
@ -97,9 +107,15 @@ typedef Ec PublicKey;
struct PrecomputedPublicKey { struct PrecomputedPublicKey {
mcl::fp::WindowMethod<Ec> pubBase_; mcl::fp::WindowMethod<Ec> pubBase_;
void init(bool *pb, const PublicKey& pub)
{
pubBase_.init(pb, pub, param.ecParam.bitSize, local::winSize);
}
void init(const PublicKey& pub) void init(const PublicKey& pub)
{ {
pubBase_.init(pub, param.ecParam.bitSize, local::winSize); bool b;
init(&b, pub);
if (!b) throw cybozu::Exception("ecdsa:PrecomputedPublicKey:init");
} }
}; };

@ -103,11 +103,11 @@ public:
} }
printf("\n"); printf("\n");
} }
static inline void init(int *pret, const mpz_class& _p, fp::Mode mode = fp::FP_AUTO) static inline void init(bool *pb, const mpz_class& _p, fp::Mode mode = fp::FP_AUTO)
{ {
assert(maxBitSize <= MCL_MAX_BIT_SIZE); assert(maxBitSize <= MCL_MAX_BIT_SIZE);
*pret = op_.init(_p, maxBitSize, mode); *pb = op_.init(_p, maxBitSize, mode);
if (*pret < 0) return; if (!*pb) return;
{ // set oneRep { // set oneRep
FpT& one = *reinterpret_cast<FpT*>(op_.oneRep); FpT& one = *reinterpret_cast<FpT*>(op_.oneRep);
one.clear(); one.clear();
@ -119,18 +119,26 @@ public:
gmp::getArray(op_.half, op_.N, half); gmp::getArray(op_.half, op_.N, half);
} }
inv(inv2_, 2); inv(inv2_, 2);
*pret = 0; *pb = true;
}
static inline void init(bool *pb, const char *mstr, fp::Mode mode = fp::FP_AUTO)
{
mpz_class p;
gmp::setStr(pb, p, mstr, strlen(mstr));
if (!*pb) return;
init(pb, p, mode);
} }
static inline void init(const mpz_class& _p, fp::Mode mode = fp::FP_AUTO) static inline void init(const mpz_class& _p, fp::Mode mode = fp::FP_AUTO)
{ {
int ret; bool b;
init(&ret, _p, mode); init(&b, _p, mode);
if (ret < 0) throw cybozu::Exception("Fp:init") << ret; if (!b) throw cybozu::Exception("Fp:init");
} }
static inline void init(const std::string& mstr, fp::Mode mode = fp::FP_AUTO) static inline void init(const std::string& mstr, fp::Mode mode = fp::FP_AUTO)
{ {
mpz_class p(mstr); bool b;
init(p, mode); init(&b, mstr.c_str(), mode);
if (!b) throw cybozu::Exception("Fp:init");
} }
static inline size_t getModulo(char *buf, size_t bufSize) static inline size_t getModulo(char *buf, size_t bufSize)
{ {

@ -102,15 +102,20 @@ inline void set(mpz_class& z, uint64_t x)
{ {
setArray(z, &x, 1); setArray(z, &x, 1);
} }
inline bool setStr(mpz_class& z, const std::string& str, int base = 0) inline void setStr(bool *pb, mpz_class& z, const char *str, size_t strSize, int base = 0)
{ {
#ifdef MCL_USE_VINT #ifdef MCL_USE_VINT
z.setStr(str, base); z.setStr(pb, str, strSize, base);
return true;
#else #else
return z.set_str(str, base) == 0; *pb = z.set_str(std::string(str, strSize), base) == 0;
#endif #endif
} }
inline void setStr(mpz_class& z, const std::string& str, int base = 0)
{
bool b;
setStr(&b, z, str.c_str(), str.size(), base);
if (!b) throw cybozu::Exception("gmp:setStr");
}
/* /*
set buf with string terminated by '\0' set buf with string terminated by '\0'
return strlen(buf) if success else 0 return strlen(buf) if success else 0

@ -312,10 +312,7 @@ struct Op {
*/ */
fp_mul(y, x, R2, p); fp_mul(y, x, R2, p);
} }
/* bool init(const mpz_class& p, size_t maxBitSize, Mode mode, size_t mclMaxBitSize = MCL_MAX_BIT_SIZE);
return 0 if success else negative value
*/
int init(const mpz_class& p, size_t maxBitSize, Mode mode, size_t mclMaxBitSize = MCL_MAX_BIT_SIZE);
void initFp2(int xi_a); void initFp2(int xi_a);
static FpGenerator* createFpGenerator(); static FpGenerator* createFpGenerator();
static void destroyFpGenerator(FpGenerator *fg); static void destroyFpGenerator(FpGenerator *fg);

@ -122,6 +122,20 @@ void (*Operator<T, E>::powArrayGLV)(T& z, const T& x, const Unit *y, size_t yn,
*/ */
template<class T, class E = Empty<T> > template<class T, class E = Empty<T> >
struct Serializable : public E { struct Serializable : public E {
void setStr(bool *pb, const char *str, int ioMode = 0)
{
size_t len = strlen(str);
size_t n = deserialize(str, len, ioMode);
*pb = n > 0 && n == len;
}
// return strlen(buf) if success else 0
size_t getStr(char *buf, size_t maxBufSize, int ioMode = 0) const
{
size_t n = serialize(buf, maxBufSize, ioMode);
if (n == 0 || n == maxBufSize - 1) return 0;
buf[n] = '\0';
return n;
}
void setStr(const std::string& str, int ioMode = 0) void setStr(const std::string& str, int ioMode = 0)
{ {
cybozu::StringInputStream is(str); cybozu::StringInputStream is(str);

@ -1162,7 +1162,7 @@ public:
"0b..." => base = 2 "0b..." => base = 2
otherwise => base = 10 otherwise => base = 10
*/ */
void setStr(const char *str, size_t strSize, int base, bool *pb) void setStr(bool *pb, const char *str, size_t strSize, int base = 0)
{ {
const size_t maxN = MCL_MAX_BIT_SIZE / (sizeof(MCL_SIZEOF_UNIT) * 8); const size_t maxN = MCL_MAX_BIT_SIZE / (sizeof(MCL_SIZEOF_UNIT) * 8);
buf_.alloc(maxN); buf_.alloc(maxN);
@ -1176,7 +1176,7 @@ public:
void setStr(std::string str, int base = 0) void setStr(std::string str, int base = 0)
{ {
bool b; bool b;
setStr(str.c_str(), str.size(), base, &b); setStr(&b, str.c_str(), str.size(), base);
if (!b) throw cybozu::Exception("Vint:setStr") << str; if (!b) throw cybozu::Exception("Vint:setStr") << str;
} }
static int compare(const VintT& x, const VintT& y) static int compare(const VintT& x, const VintT& y)

@ -4,7 +4,7 @@
@brief window method @brief window method
@author MITSUNARI Shigeo(@herumi) @author MITSUNARI Shigeo(@herumi)
*/ */
#include <vector> #include <mcl/array.hpp>
#include <mcl/fp.hpp> #include <mcl/fp.hpp>
namespace mcl { namespace fp { namespace mcl { namespace fp {
@ -71,7 +71,7 @@ class WindowMethod {
public: public:
size_t bitSize_; size_t bitSize_;
size_t winSize_; size_t winSize_;
std::vector<Ec> tbl_; mcl::Array<Ec> tbl_;
WindowMethod(const Ec& x, size_t bitSize, size_t winSize) WindowMethod(const Ec& x, size_t bitSize, size_t winSize)
{ {
init(x, bitSize, winSize); init(x, bitSize, winSize);
@ -86,13 +86,14 @@ public:
@param bitSize [in] exponent bit length @param bitSize [in] exponent bit length
@param winSize [in] window size @param winSize [in] window size
*/ */
void init(const Ec& x, size_t bitSize, size_t winSize) void init(bool *pb, const Ec& x, size_t bitSize, size_t winSize)
{ {
bitSize_ = bitSize; bitSize_ = bitSize;
winSize_ = winSize; winSize_ = winSize;
const size_t tblNum = (bitSize + winSize - 1) / winSize; const size_t tblNum = (bitSize + winSize - 1) / winSize;
const size_t r = size_t(1) << winSize; const size_t r = size_t(1) << winSize;
tbl_.resize(tblNum * r); *pb = tbl_.resize(tblNum * r);
if (!*pb) return;
Ec t(x); Ec t(x);
for (size_t i = 0; i < tblNum; i++) { for (size_t i = 0; i < tblNum; i++) {
Ec* w = &tbl_[i * r]; Ec* w = &tbl_[i * r];
@ -108,6 +109,12 @@ public:
} }
} }
} }
void init(const Ec& x, size_t bitSize, size_t winSize)
{
bool b;
init(&b, x, bitSize, winSize);
if (!b) throw cybozu::Exception("mcl:WindowMethod:init") << bitSize << winSize;
}
/* /*
@param z [out] x multiplied by y @param z [out] x multiplied by y
@param y [in] exponent @param y [in] exponent
@ -143,7 +150,8 @@ public:
n--; n--;
} }
if (n == 0) return; if (n == 0) return;
if ((n << winSize_) > tbl_.size()) throw cybozu::Exception("mcl:WindowMethod:powArray:bad n") << n << tbl_.size(); assert((n << winSize_) <= tbl_.size());
if ((n << winSize_) > tbl_.size()) return;
assert(y[n - 1]); assert(y[n - 1]);
const size_t bitSize = (n - 1) * UnitBitSize + cybozu::bsr<Unit>(y[n - 1]) + 1; const size_t bitSize = (n - 1) * UnitBitSize + cybozu::bsr<Unit>(y[n - 1]) + 1;
size_t i = 0; size_t i = 0;

@ -26,21 +26,6 @@ static const Fp12 *cast(const mclBnGT *p) { return reinterpret_cast<const Fp12*>
static Fp6 *cast(uint64_t *p) { return reinterpret_cast<Fp6*>(p); } static Fp6 *cast(uint64_t *p) { return reinterpret_cast<Fp6*>(p); }
static const Fp6 *cast(const uint64_t *p) { return reinterpret_cast<const Fp6*>(p); } static const Fp6 *cast(const uint64_t *p) { return reinterpret_cast<const Fp6*>(p); }
template<class T>
mclSize getStr(void *buf, mclSize maxBufSize, const T *x, int ioMode)
{
size_t n = cast(x)->serialize(buf, maxBufSize, ioMode);
if (n == 0 || n == maxBufSize - 1) return 0;
((char *)buf)[n] = '\0';
return n;
}
template<class T>
mclSize serialize(void *buf, mclSize maxBufSize, const T *x)
{
return (mclSize)cast(x)->serialize(buf, maxBufSize);
}
template<class T> template<class T>
int setStr(T *x, const char *buf, mclSize bufSize, int ioMode) int setStr(T *x, const char *buf, mclSize bufSize, int ioMode)
{ {
@ -48,12 +33,6 @@ int setStr(T *x, const char *buf, mclSize bufSize, int ioMode)
return n > 0 ? 0 : -1; return n > 0 ? 0 : -1;
} }
template<class T>
mclSize deserialize(T *x, const void *buf, mclSize bufSize)
{
return (mclSize)cast(x)->deserialize(buf, bufSize);
}
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
// use these functions forcibly // use these functions forcibly
extern "C" MCLBN_DLL_API void *mclBnMalloc(size_t n) extern "C" MCLBN_DLL_API void *mclBnMalloc(size_t n)
@ -72,9 +51,9 @@ int mclBn_init(int curve, int maxUnitSize)
return -10; return -10;
} }
const mcl::CurveParam& cp = mcl::getCurveParam(curve); const mcl::CurveParam& cp = mcl::getCurveParam(curve);
int ret; bool b;
initPairing(&ret, cp); initPairing(&b, cp);
return ret; return b ? 0 : -1;
} }
int mclBn_getOpUnitSize() int mclBn_getOpUnitSize()
@ -129,7 +108,7 @@ int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize)
} }
mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize) mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize)
{ {
return deserialize(x, buf, bufSize); return (mclSize)cast(x)->deserialize(buf, bufSize);
} }
// return 1 if true // return 1 if true
int mclBnFr_isValid(const mclBnFr *x) int mclBnFr_isValid(const mclBnFr *x)
@ -164,11 +143,11 @@ int mclBnFr_setHashOf(mclBnFr *x, const void *buf, mclSize bufSize)
mclSize mclBnFr_getStr(char *buf, mclSize maxBufSize, const mclBnFr *x, int ioMode) mclSize mclBnFr_getStr(char *buf, mclSize maxBufSize, const mclBnFr *x, int ioMode)
{ {
return getStr(buf, maxBufSize, x, ioMode); return cast(x)->getStr(buf, maxBufSize, ioMode);
} }
mclSize mclBnFr_serialize(void *buf, mclSize maxBufSize, const mclBnFr *x) mclSize mclBnFr_serialize(void *buf, mclSize maxBufSize, const mclBnFr *x)
{ {
return serialize(buf, maxBufSize, x); return (mclSize)cast(x)->serialize(buf, maxBufSize);
} }
void mclBnFr_neg(mclBnFr *y, const mclBnFr *x) void mclBnFr_neg(mclBnFr *y, const mclBnFr *x)
@ -213,7 +192,7 @@ int mclBnG1_setStr(mclBnG1 *x, const char *buf, mclSize bufSize, int ioMode)
} }
mclSize mclBnG1_deserialize(mclBnG1 *x, const void *buf, mclSize bufSize) mclSize mclBnG1_deserialize(mclBnG1 *x, const void *buf, mclSize bufSize)
{ {
return deserialize(x, buf, bufSize); return (mclSize)cast(x)->deserialize(buf, bufSize);
} }
// return 1 if true // return 1 if true
@ -238,12 +217,12 @@ int mclBnG1_hashAndMapTo(mclBnG1 *x, const void *buf, mclSize bufSize)
mclSize mclBnG1_getStr(char *buf, mclSize maxBufSize, const mclBnG1 *x, int ioMode) mclSize mclBnG1_getStr(char *buf, mclSize maxBufSize, const mclBnG1 *x, int ioMode)
{ {
return getStr(buf, maxBufSize, x, ioMode); return cast(x)->getStr(buf, maxBufSize, ioMode);
} }
mclSize mclBnG1_serialize(void *buf, mclSize maxBufSize, const mclBnG1 *x) mclSize mclBnG1_serialize(void *buf, mclSize maxBufSize, const mclBnG1 *x)
{ {
return serialize(buf, maxBufSize, x); return (mclSize)cast(x)->serialize(buf, maxBufSize);
} }
void mclBnG1_neg(mclBnG1 *y, const mclBnG1 *x) void mclBnG1_neg(mclBnG1 *y, const mclBnG1 *x)
@ -288,7 +267,7 @@ int mclBnG2_setStr(mclBnG2 *x, const char *buf, mclSize bufSize, int ioMode)
} }
mclSize mclBnG2_deserialize(mclBnG2 *x, const void *buf, mclSize bufSize) mclSize mclBnG2_deserialize(mclBnG2 *x, const void *buf, mclSize bufSize)
{ {
return deserialize(x, buf, bufSize); return (mclSize)cast(x)->deserialize(buf, bufSize);
} }
// return 1 if true // return 1 if true
@ -313,11 +292,12 @@ int mclBnG2_hashAndMapTo(mclBnG2 *x, const void *buf, mclSize bufSize)
mclSize mclBnG2_getStr(char *buf, mclSize maxBufSize, const mclBnG2 *x, int ioMode) mclSize mclBnG2_getStr(char *buf, mclSize maxBufSize, const mclBnG2 *x, int ioMode)
{ {
return getStr(buf, maxBufSize, x, ioMode); return cast(x)->getStr(buf, maxBufSize, ioMode);
} }
mclSize mclBnG2_serialize(void *buf, mclSize maxBufSize, const mclBnG2 *x) mclSize mclBnG2_serialize(void *buf, mclSize maxBufSize, const mclBnG2 *x)
{ {
return serialize(buf, maxBufSize, x); return (mclSize)cast(x)->serialize(buf, maxBufSize);
} }
void mclBnG2_neg(mclBnG2 *y, const mclBnG2 *x) void mclBnG2_neg(mclBnG2 *y, const mclBnG2 *x)
@ -372,7 +352,7 @@ int mclBnGT_setStr(mclBnGT *x, const char *buf, mclSize bufSize, int ioMode)
} }
mclSize mclBnGT_deserialize(mclBnGT *x, const void *buf, mclSize bufSize) mclSize mclBnGT_deserialize(mclBnGT *x, const void *buf, mclSize bufSize)
{ {
return deserialize(x, buf, bufSize); return (mclSize)cast(x)->deserialize(buf, bufSize);
} }
// return 1 if true // return 1 if true
@ -391,12 +371,12 @@ int mclBnGT_isOne(const mclBnGT *x)
mclSize mclBnGT_getStr(char *buf, mclSize maxBufSize, const mclBnGT *x, int ioMode) mclSize mclBnGT_getStr(char *buf, mclSize maxBufSize, const mclBnGT *x, int ioMode)
{ {
return getStr(buf, maxBufSize, x, ioMode); return cast(x)->getStr(buf, maxBufSize, ioMode);
} }
mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mclBnGT *x) mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mclBnGT *x)
{ {
return serialize(buf, maxBufSize, x); return (mclSize)cast(x)->serialize(buf, maxBufSize);
} }
void mclBnGT_neg(mclBnGT *y, const mclBnGT *x) void mclBnGT_neg(mclBnGT *y, const mclBnGT *x)

@ -17,66 +17,43 @@ static PrecomputedPublicKey *cast(ecdsaPrecomputedPublicKey *p) { return reinter
static const PrecomputedPublicKey *cast(const ecdsaPrecomputedPublicKey *p) { return reinterpret_cast<const PrecomputedPublicKey*>(p); } static const PrecomputedPublicKey *cast(const ecdsaPrecomputedPublicKey *p) { return reinterpret_cast<const PrecomputedPublicKey*>(p); }
int ecdsaInit(void) int ecdsaInit(void)
try
{ {
init(); bool b;
return 0; init(&b);
} catch (std::exception&) { return b ? 0 : -1;
return -1;
}
template<class T>
mclSize serialize(void *buf, mclSize maxBufSize, const T *x)
try
{
return (mclSize)cast(x)->serialize(buf, maxBufSize);
} catch (std::exception&) {
return 0;
} }
mclSize ecdsaSecretKeySerialize(void *buf, mclSize maxBufSize, const ecdsaSecretKey *sec) mclSize ecdsaSecretKeySerialize(void *buf, mclSize maxBufSize, const ecdsaSecretKey *sec)
{ {
return serialize(buf, maxBufSize, sec); return (mclSize)cast(sec)->serialize(buf, maxBufSize);
} }
mclSize ecdsaPublicKeySerialize(void *buf, mclSize maxBufSize, const ecdsaPublicKey *pub) mclSize ecdsaPublicKeySerialize(void *buf, mclSize maxBufSize, const ecdsaPublicKey *pub)
{ {
return serialize(buf, maxBufSize, pub); return (mclSize)cast(pub)->serialize(buf, maxBufSize);
} }
mclSize ecdsaSignatureSerialize(void *buf, mclSize maxBufSize, const ecdsaSignature *sig) mclSize ecdsaSignatureSerialize(void *buf, mclSize maxBufSize, const ecdsaSignature *sig)
{ {
return serialize(buf, maxBufSize, sig); return (mclSize)cast(sig)->serialize(buf, maxBufSize);
}
template<class T>
mclSize deserialize(T *x, const void *buf, mclSize bufSize)
try
{
return (mclSize)cast(x)->deserialize(buf, bufSize);
} catch (std::exception&) {
return 0;
} }
mclSize ecdsaSecretKeyDeserialize(ecdsaSecretKey* sec, const void *buf, mclSize bufSize) mclSize ecdsaSecretKeyDeserialize(ecdsaSecretKey* sec, const void *buf, mclSize bufSize)
{ {
return deserialize(sec, buf, bufSize); return (mclSize)cast(sec)->deserialize(buf, bufSize);
} }
mclSize ecdsaPublicKeyDeserialize(ecdsaPublicKey* pub, const void *buf, mclSize bufSize) mclSize ecdsaPublicKeyDeserialize(ecdsaPublicKey* pub, const void *buf, mclSize bufSize)
{ {
return deserialize(pub, buf, bufSize); return (mclSize)cast(pub)->deserialize(buf, bufSize);
} }
mclSize ecdsaSignatureDeserialize(ecdsaSignature* sig, const void *buf, mclSize bufSize) mclSize ecdsaSignatureDeserialize(ecdsaSignature* sig, const void *buf, mclSize bufSize)
{ {
return deserialize(sig, buf, bufSize); return (mclSize)cast(sig)->deserialize(buf, bufSize);
} }
// return 0 if success // return 0 if success
int ecdsaSecretKeySetByCSPRNG(ecdsaSecretKey *sec) int ecdsaSecretKeySetByCSPRNG(ecdsaSecretKey *sec)
try
{ {
cast(sec)->setByCSPRNG(); cast(sec)->setByCSPRNG();
return 0; return 0;
} catch (...) {
return -1;
} }
void ecdsaGetPublicKey(ecdsaPublicKey *pub, const ecdsaSecretKey *sec) void ecdsaGetPublicKey(ecdsaPublicKey *pub, const ecdsaSecretKey *sec)
@ -99,23 +76,22 @@ int ecdsaVerifyPrecomputed(const ecdsaSignature *sig, const ecdsaPrecomputedPubl
} }
ecdsaPrecomputedPublicKey *ecdsaPrecomputedPublicKeyCreate() ecdsaPrecomputedPublicKey *ecdsaPrecomputedPublicKeyCreate()
try
{ {
return reinterpret_cast<ecdsaPrecomputedPublicKey*>(new PrecomputedPublicKey()); PrecomputedPublicKey *ppub = (PrecomputedPublicKey*)malloc(sizeof(PrecomputedPublicKey));
} catch (...) { if (ppub == 0) return 0;
return 0; new(ppub) PrecomputedPublicKey();
return reinterpret_cast<ecdsaPrecomputedPublicKey*>(ppub);
} }
void ecdsaPrecomputedPublicKeyDestroy(ecdsaPrecomputedPublicKey *ppub) void ecdsaPrecomputedPublicKeyDestroy(ecdsaPrecomputedPublicKey *ppub)
{ {
delete cast(ppub); cast(ppub)->~PrecomputedPublicKey();
free(ppub);
} }
int ecdsaPrecomputedPublicKeyInit(ecdsaPrecomputedPublicKey *ppub, const ecdsaPublicKey *pub) int ecdsaPrecomputedPublicKeyInit(ecdsaPrecomputedPublicKey *ppub, const ecdsaPublicKey *pub)
try
{ {
cast(ppub)->init(*cast(pub)); bool b;
return 0; cast(ppub)->init(&b, *cast(pub));
} catch (...) { return b ? 0 : -1;
return -1;
} }

@ -348,21 +348,21 @@ static void initForMont(Op& op, const Unit *p, Mode mode)
#endif #endif
} }
int Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBitSize) bool Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBitSize)
{ {
if (mclMaxBitSize != MCL_MAX_BIT_SIZE) return -1; if (mclMaxBitSize != MCL_MAX_BIT_SIZE) return false;
#ifdef MCL_USE_VINT #ifdef MCL_USE_VINT
assert(sizeof(mcl::vint::Unit) == sizeof(Unit)); assert(sizeof(mcl::vint::Unit) == sizeof(Unit));
#else #else
assert(sizeof(mp_limb_t) == sizeof(Unit)); assert(sizeof(mp_limb_t) == sizeof(Unit));
#endif #endif
if (maxBitSize > MCL_MAX_BIT_SIZE) return -2; if (maxBitSize > MCL_MAX_BIT_SIZE) return false;
if (_p <= 0) return -3; if (_p <= 0) return false;
clear(); clear();
{ {
const size_t maxN = (maxBitSize + fp::UnitBitSize - 1) / fp::UnitBitSize; const size_t maxN = (maxBitSize + fp::UnitBitSize - 1) / fp::UnitBitSize;
N = gmp::getUnitSize(_p); N = gmp::getUnitSize(_p);
if (N > maxN) return -4; if (N > maxN) return false;
gmp::getArray(p, N, _p); gmp::getArray(p, N, _p);
mp = _p; mp = _p;
} }
@ -484,7 +484,7 @@ int Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBit
} else { } else {
hash = sha512; hash = sha512;
} }
return 0; return true;
} }
void copyUnitToByteAsLE(uint8_t *dst, const Unit *src, size_t byteSize) void copyUnitToByteAsLE(uint8_t *dst, const Unit *src, size_t byteSize)

@ -78,91 +78,88 @@ int sheInit(int curve, int maxUnitSize)
mclSize sheSecretKeySerialize(void *buf, mclSize maxBufSize, const sheSecretKey *sec) mclSize sheSecretKeySerialize(void *buf, mclSize maxBufSize, const sheSecretKey *sec)
{ {
return serialize(buf, maxBufSize, sec); return (mclSize)cast(sec)->serialize(buf, maxBufSize);
} }
mclSize shePublicKeySerialize(void *buf, mclSize maxBufSize, const shePublicKey *pub) mclSize shePublicKeySerialize(void *buf, mclSize maxBufSize, const shePublicKey *pub)
{ {
return serialize(buf, maxBufSize, pub); return (mclSize)cast(pub)->serialize(buf, maxBufSize);
} }
mclSize sheCipherTextG1Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG1 *c) mclSize sheCipherTextG1Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG1 *c)
{ {
return serialize(buf, maxBufSize, c); return (mclSize)cast(c)->serialize(buf, maxBufSize);
} }
mclSize sheCipherTextG2Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG2 *c) mclSize sheCipherTextG2Serialize(void *buf, mclSize maxBufSize, const sheCipherTextG2 *c)
{ {
return serialize(buf, maxBufSize, c); return (mclSize)cast(c)->serialize(buf, maxBufSize);
} }
mclSize sheCipherTextGTSerialize(void *buf, mclSize maxBufSize, const sheCipherTextGT *c) mclSize sheCipherTextGTSerialize(void *buf, mclSize maxBufSize, const sheCipherTextGT *c)
{ {
return serialize(buf, maxBufSize, c); return (mclSize)cast(c)->serialize(buf, maxBufSize);
} }
mclSize sheZkpBinSerialize(void *buf, mclSize maxBufSize, const sheZkpBin *zkp) mclSize sheZkpBinSerialize(void *buf, mclSize maxBufSize, const sheZkpBin *zkp)
{ {
return serialize(buf, maxBufSize, zkp); return (mclSize)cast(zkp)->serialize(buf, maxBufSize);
} }
mclSize sheZkpEqSerialize(void *buf, mclSize maxBufSize, const sheZkpEq *zkp) mclSize sheZkpEqSerialize(void *buf, mclSize maxBufSize, const sheZkpEq *zkp)
{ {
return serialize(buf, maxBufSize, zkp); return (mclSize)cast(zkp)->serialize(buf, maxBufSize);
} }
mclSize sheZkpBinEqSerialize(void *buf, mclSize maxBufSize, const sheZkpBinEq *zkp) mclSize sheZkpBinEqSerialize(void *buf, mclSize maxBufSize, const sheZkpBinEq *zkp)
{ {
return serialize(buf, maxBufSize, zkp); return (mclSize)cast(zkp)->serialize(buf, maxBufSize);
} }
mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize) mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize)
{ {
return deserialize(sec, buf, bufSize); return (mclSize)cast(sec)->deserialize(buf, bufSize);
} }
mclSize shePublicKeyDeserialize(shePublicKey* pub, const void *buf, mclSize bufSize) mclSize shePublicKeyDeserialize(shePublicKey* pub, const void *buf, mclSize bufSize)
{ {
return deserialize(pub, buf, bufSize); return (mclSize)cast(pub)->deserialize(buf, bufSize);
} }
mclSize sheCipherTextG1Deserialize(sheCipherTextG1* c, const void *buf, mclSize bufSize) mclSize sheCipherTextG1Deserialize(sheCipherTextG1* c, const void *buf, mclSize bufSize)
{ {
return deserialize(c, buf, bufSize); return (mclSize)cast(c)->deserialize(buf, bufSize);
} }
mclSize sheCipherTextG2Deserialize(sheCipherTextG2* c, const void *buf, mclSize bufSize) mclSize sheCipherTextG2Deserialize(sheCipherTextG2* c, const void *buf, mclSize bufSize)
{ {
return deserialize(c, buf, bufSize); return (mclSize)cast(c)->deserialize(buf, bufSize);
} }
mclSize sheCipherTextGTDeserialize(sheCipherTextGT* c, const void *buf, mclSize bufSize) mclSize sheCipherTextGTDeserialize(sheCipherTextGT* c, const void *buf, mclSize bufSize)
{ {
return deserialize(c, buf, bufSize); return (mclSize)cast(c)->deserialize(buf, bufSize);
} }
mclSize sheZkpBinDeserialize(sheZkpBin* zkp, const void *buf, mclSize bufSize) mclSize sheZkpBinDeserialize(sheZkpBin* zkp, const void *buf, mclSize bufSize)
{ {
return deserialize(zkp, buf, bufSize); return (mclSize)cast(zkp)->deserialize(buf, bufSize);
} }
mclSize sheZkpEqDeserialize(sheZkpEq* zkp, const void *buf, mclSize bufSize) mclSize sheZkpEqDeserialize(sheZkpEq* zkp, const void *buf, mclSize bufSize)
{ {
return deserialize(zkp, buf, bufSize); return (mclSize)cast(zkp)->deserialize(buf, bufSize);
} }
mclSize sheZkpBinEqDeserialize(sheZkpBinEq* zkp, const void *buf, mclSize bufSize) mclSize sheZkpBinEqDeserialize(sheZkpBinEq* zkp, const void *buf, mclSize bufSize)
{ {
return deserialize(zkp, buf, bufSize); return (mclSize)cast(zkp)->deserialize(buf, bufSize);
} }
int sheSecretKeySetByCSPRNG(sheSecretKey *sec) int sheSecretKeySetByCSPRNG(sheSecretKey *sec)
try
{ {
cast(sec)->setByCSPRNG(); cast(sec)->setByCSPRNG();
return 0; return 0;
} catch (std::exception&) {
return -1;
} }
void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec) void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec)

@ -115,30 +115,33 @@ CYBOZU_TEST_AUTO(bench2)
} }
#endif #endif
template<class G, class HashTbl>
void GAHashTableTest(int maxSize, int tryNum, const G& P, const HashTbl& hashTbl)
{
for (int i = -maxSize; i <= maxSize; i++) {
G xP;
G::mul(xP, P, i);
CYBOZU_TEST_EQUAL(hashTbl.basicLog(xP), i);
}
for (int i = -maxSize * tryNum; i <= maxSize * tryNum; i++) {
G xP;
G::mul(xP, P, i);
CYBOZU_TEST_EQUAL(hashTbl.log(xP), i);
}
}
template<class G> template<class G>
void HashTableTest(const G& P) void HashTableTest(const G& P)
{ {
mcl::she::local::HashTable<G> hashTbl; mcl::she::local::HashTable<G> hashTbl, hashTbl2;
const int maxSize = 100; const int maxSize = 100;
const int tryNum = 3; const int tryNum = 3;
hashTbl.init(P, maxSize, tryNum); hashTbl.init(P, maxSize, tryNum);
for (int j = 0; j < 2; j++) { GAHashTableTest(maxSize, tryNum, P, hashTbl);
for (int i = -maxSize; i <= maxSize; i++) { std::stringstream ss;
G xP; hashTbl.save(ss);
G::mul(xP, P, i); hashTbl2.load(ss);
CYBOZU_TEST_EQUAL(hashTbl.basicLog(xP), i); GAHashTableTest(maxSize, tryNum, P, hashTbl2);
}
for (int i = -maxSize * tryNum; i <= maxSize * tryNum; i++) {
G xP;
G::mul(xP, P, i);
CYBOZU_TEST_EQUAL(hashTbl.log(xP), i);
}
std::stringstream ss;
hashTbl.save(ss);
mcl::she::local::HashTable<G> hashTbl2;
hashTbl2.load(ss);
hashTbl = hashTbl2;
}
} }
CYBOZU_TEST_AUTO(HashTable) CYBOZU_TEST_AUTO(HashTable)
@ -151,9 +154,24 @@ CYBOZU_TEST_AUTO(HashTable)
HashTableTest(Q); HashTableTest(Q);
} }
template<class HashTbl>
void GTHashTableTest(int maxSize, int tryNum, const GT& g, const HashTbl& hashTbl)
{
for (int i = -maxSize; i <= maxSize; i++) {
GT gx;
GT::pow(gx, g, i);
CYBOZU_TEST_EQUAL(hashTbl.basicLog(gx), i);
}
for (int i = -maxSize * tryNum; i <= maxSize * tryNum; i++) {
GT gx;
GT::pow(gx, g, i);
CYBOZU_TEST_EQUAL(hashTbl.log(gx), i);
}
}
CYBOZU_TEST_AUTO(GTHashTable) CYBOZU_TEST_AUTO(GTHashTable)
{ {
mcl::she::local::HashTable<GT, false> hashTbl; mcl::she::local::HashTable<GT, false> hashTbl, hashTbl2;
GT g; GT g;
{ {
G1 P; G1 P;
@ -165,23 +183,11 @@ CYBOZU_TEST_AUTO(GTHashTable)
const int maxSize = 100; const int maxSize = 100;
const int tryNum = 3; const int tryNum = 3;
hashTbl.init(g, maxSize, tryNum); hashTbl.init(g, maxSize, tryNum);
for (int j = 0; j < 2; j++) { GTHashTableTest(maxSize, tryNum, g, hashTbl);
for (int i = -maxSize; i <= maxSize; i++) { std::stringstream ss;
GT gx; hashTbl.save(ss);
GT::pow(gx, g, i); hashTbl2.load(ss);
CYBOZU_TEST_EQUAL(hashTbl.basicLog(gx), i); GTHashTableTest(maxSize, tryNum, g, hashTbl2);
}
for (int i = -maxSize * tryNum; i <= maxSize * tryNum; i++) {
GT gx;
GT::pow(gx, g, i);
CYBOZU_TEST_EQUAL(hashTbl.log(gx), i);
}
std::stringstream ss;
hashTbl.save(ss);
mcl::she::local::HashTable<GT, false> hashTbl2;
hashTbl2.load(ss);
hashTbl = hashTbl2;
}
} }
CYBOZU_TEST_AUTO(enc_dec) CYBOZU_TEST_AUTO(enc_dec)

Loading…
Cancel
Save