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. 56
      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)
$(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)
SAMPLE_EXE=$(addprefix $(EXE_DIR)/,$(addsuffix .exe,$(basename $(SAMPLE_SRC))))

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

@ -199,9 +199,20 @@ public:
{
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)
{
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
bool isValidOrder() const

@ -77,19 +77,29 @@ inline void setHashOf(Zn& x, const void *msg, size_t msgSize)
const local::Param& param = local::getParam();
inline void init()
inline void init(bool *pb)
{
const mcl::EcParam& ecParam = mcl::ecparam::secp256k1;
Zn::init(ecParam.n);
Fp::init(ecParam.p);
Ec::init(ecParam.a, ecParam.b);
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;
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;
@ -97,9 +107,15 @@ typedef Ec PublicKey;
struct PrecomputedPublicKey {
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)
{
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");
}
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);
*pret = op_.init(_p, maxBitSize, mode);
if (*pret < 0) return;
*pb = op_.init(_p, maxBitSize, mode);
if (!*pb) return;
{ // set oneRep
FpT& one = *reinterpret_cast<FpT*>(op_.oneRep);
one.clear();
@ -119,18 +119,26 @@ public:
gmp::getArray(op_.half, op_.N, half);
}
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)
{
int ret;
init(&ret, _p, mode);
if (ret < 0) throw cybozu::Exception("Fp:init") << ret;
bool b;
init(&b, _p, mode);
if (!b) throw cybozu::Exception("Fp:init");
}
static inline void init(const std::string& mstr, fp::Mode mode = fp::FP_AUTO)
{
mpz_class p(mstr);
init(p, mode);
bool b;
init(&b, mstr.c_str(), mode);
if (!b) throw cybozu::Exception("Fp:init");
}
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);
}
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
z.setStr(str, base);
return true;
z.setStr(pb, str, strSize, base);
#else
return z.set_str(str, base) == 0;
*pb = z.set_str(std::string(str, strSize), base) == 0;
#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'
return strlen(buf) if success else 0

@ -312,10 +312,7 @@ struct Op {
*/
fp_mul(y, x, R2, p);
}
/*
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);
bool init(const mpz_class& p, size_t maxBitSize, Mode mode, size_t mclMaxBitSize = MCL_MAX_BIT_SIZE);
void initFp2(int xi_a);
static FpGenerator* createFpGenerator();
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> >
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)
{
cybozu::StringInputStream is(str);

@ -1162,7 +1162,7 @@ public:
"0b..." => base = 2
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);
buf_.alloc(maxN);
@ -1176,7 +1176,7 @@ public:
void setStr(std::string str, int base = 0)
{
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;
}
static int compare(const VintT& x, const VintT& y)

@ -4,7 +4,7 @@
@brief window method
@author MITSUNARI Shigeo(@herumi)
*/
#include <vector>
#include <mcl/array.hpp>
#include <mcl/fp.hpp>
namespace mcl { namespace fp {
@ -71,7 +71,7 @@ class WindowMethod {
public:
size_t bitSize_;
size_t winSize_;
std::vector<Ec> tbl_;
mcl::Array<Ec> tbl_;
WindowMethod(const Ec& x, size_t bitSize, size_t winSize)
{
init(x, bitSize, winSize);
@ -86,13 +86,14 @@ public:
@param bitSize [in] exponent bit length
@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;
winSize_ = winSize;
const size_t tblNum = (bitSize + winSize - 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);
for (size_t i = 0; i < tblNum; i++) {
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 y [in] exponent
@ -143,7 +150,8 @@ public:
n--;
}
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]);
const size_t bitSize = (n - 1) * UnitBitSize + cybozu::bsr<Unit>(y[n - 1]) + 1;
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 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>
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;
}
template<class T>
mclSize deserialize(T *x, const void *buf, mclSize bufSize)
{
return (mclSize)cast(x)->deserialize(buf, bufSize);
}
#ifdef __EMSCRIPTEN__
// use these functions forcibly
extern "C" MCLBN_DLL_API void *mclBnMalloc(size_t n)
@ -72,9 +51,9 @@ int mclBn_init(int curve, int maxUnitSize)
return -10;
}
const mcl::CurveParam& cp = mcl::getCurveParam(curve);
int ret;
initPairing(&ret, cp);
return ret;
bool b;
initPairing(&b, cp);
return b ? 0 : -1;
}
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)
{
return deserialize(x, buf, bufSize);
return (mclSize)cast(x)->deserialize(buf, bufSize);
}
// return 1 if true
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)
{
return getStr(buf, maxBufSize, x, ioMode);
return cast(x)->getStr(buf, maxBufSize, ioMode);
}
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)
@ -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)
{
return deserialize(x, buf, bufSize);
return (mclSize)cast(x)->deserialize(buf, bufSize);
}
// 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)
{
return getStr(buf, maxBufSize, x, ioMode);
return cast(x)->getStr(buf, maxBufSize, ioMode);
}
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)
@ -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)
{
return deserialize(x, buf, bufSize);
return (mclSize)cast(x)->deserialize(buf, bufSize);
}
// 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)
{
return getStr(buf, maxBufSize, x, ioMode);
return cast(x)->getStr(buf, maxBufSize, ioMode);
}
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)
@ -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)
{
return deserialize(x, buf, bufSize);
return (mclSize)cast(x)->deserialize(buf, bufSize);
}
// 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)
{
return getStr(buf, maxBufSize, x, ioMode);
return cast(x)->getStr(buf, maxBufSize, ioMode);
}
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)

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

@ -348,21 +348,21 @@ static void initForMont(Op& op, const Unit *p, Mode mode)
#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
assert(sizeof(mcl::vint::Unit) == sizeof(Unit));
#else
assert(sizeof(mp_limb_t) == sizeof(Unit));
#endif
if (maxBitSize > MCL_MAX_BIT_SIZE) return -2;
if (_p <= 0) return -3;
if (maxBitSize > MCL_MAX_BIT_SIZE) return false;
if (_p <= 0) return false;
clear();
{
const size_t maxN = (maxBitSize + fp::UnitBitSize - 1) / fp::UnitBitSize;
N = gmp::getUnitSize(_p);
if (N > maxN) return -4;
if (N > maxN) return false;
gmp::getArray(p, N, _p);
mp = _p;
}
@ -484,7 +484,7 @@ int Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBit
} else {
hash = sha512;
}
return 0;
return true;
}
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)
{
return serialize(buf, maxBufSize, sec);
return (mclSize)cast(sec)->serialize(buf, maxBufSize);
}
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)
{
return serialize(buf, maxBufSize, c);
return (mclSize)cast(c)->serialize(buf, maxBufSize);
}
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)
{
return serialize(buf, maxBufSize, c);
return (mclSize)cast(c)->serialize(buf, maxBufSize);
}
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)
{
return serialize(buf, maxBufSize, zkp);
return (mclSize)cast(zkp)->serialize(buf, maxBufSize);
}
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)
{
return deserialize(sec, buf, bufSize);
return (mclSize)cast(sec)->deserialize(buf, 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)
{
return deserialize(c, buf, bufSize);
return (mclSize)cast(c)->deserialize(buf, 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)
{
return deserialize(c, buf, bufSize);
return (mclSize)cast(c)->deserialize(buf, 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)
{
return deserialize(zkp, buf, bufSize);
return (mclSize)cast(zkp)->deserialize(buf, 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)
try
{
cast(sec)->setByCSPRNG();
return 0;
} catch (std::exception&) {
return -1;
}
void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec)

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

Loading…
Cancel
Save