aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-05-27 14:41:58 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-05-27 14:41:58 +0800
commit1bf7bbff64d881771b23b407737c1c28d09519c4 (patch)
treebe86f3a78fe284a254e342f65ff393780fe1d979
parent0cbd1e2946aac802fbaaf435c3042da149e028e1 (diff)
downloadtangerine-mcl-1bf7bbff64d881771b23b407737c1c28d09519c4.tar.gz
tangerine-mcl-1bf7bbff64d881771b23b407737c1c28d09519c4.tar.zst
tangerine-mcl-1bf7bbff64d881771b23b407737c1c28d09519c4.zip
split error and throw
-rw-r--r--Makefile2
-rw-r--r--include/mcl/bn.hpp42
-rw-r--r--include/mcl/ec.hpp13
-rw-r--r--include/mcl/ecdsa.hpp28
-rw-r--r--include/mcl/fp.hpp26
-rw-r--r--include/mcl/gmp_util.hpp13
-rw-r--r--include/mcl/op.hpp5
-rw-r--r--include/mcl/operator.hpp14
-rw-r--r--include/mcl/vint.hpp4
-rw-r--r--include/mcl/window_method.hpp18
-rw-r--r--src/bn_c_impl.hpp52
-rw-r--r--src/ecdsa_c.cpp60
-rw-r--r--src/fp.cpp12
-rw-r--r--src/she_c_impl.hpp35
-rw-r--r--test/she_test.cpp78
15 files changed, 210 insertions, 192 deletions
diff --git a/Makefile b/Makefile
index 651dea0..6a4b620 100644
--- a/Makefile
+++ b/Makefile
@@ -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))))
diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp
index 318ec83..a686999 100644
--- a/include/mcl/bn.hpp
+++ b/include/mcl/bn.hpp
@@ -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
diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp
index 27299b0..da80748 100644
--- a/include/mcl/ec.hpp
+++ b/include/mcl/ec.hpp
@@ -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
diff --git a/include/mcl/ecdsa.hpp b/include/mcl/ecdsa.hpp
index 69adfc5..f4ee5c4 100644
--- a/include/mcl/ecdsa.hpp
+++ b/include/mcl/ecdsa.hpp
@@ -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");
}
};
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp
index 4b6d848..b114a1f 100644
--- a/include/mcl/fp.hpp
+++ b/include/mcl/fp.hpp
@@ -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)
{
diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp
index db29d73..399041c 100644
--- a/include/mcl/gmp_util.hpp
+++ b/include/mcl/gmp_util.hpp
@@ -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
diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp
index 8110808..e2f52e9 100644
--- a/include/mcl/op.hpp
+++ b/include/mcl/op.hpp
@@ -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);
diff --git a/include/mcl/operator.hpp b/include/mcl/operator.hpp
index 227d94e..7198929 100644
--- a/include/mcl/operator.hpp
+++ b/include/mcl/operator.hpp
@@ -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);
diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp
index d327268..a7e9f7a 100644
--- a/include/mcl/vint.hpp
+++ b/include/mcl/vint.hpp
@@ -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)
diff --git a/include/mcl/window_method.hpp b/include/mcl/window_method.hpp
index 323b7f4..b2c3228 100644
--- a/include/mcl/window_method.hpp
+++ b/include/mcl/window_method.hpp
@@ -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;
diff --git a/src/bn_c_impl.hpp b/src/bn_c_impl.hpp
index 51c64af..9b54137 100644
--- a/src/bn_c_impl.hpp
+++ b/src/bn_c_impl.hpp
@@ -27,33 +27,12 @@ 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)
{
size_t n = cast(x)->deserialize(buf, bufSize, 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)
diff --git a/src/ecdsa_c.cpp b/src/ecdsa_c.cpp
index d91f1ba..e3e7f40 100644
--- a/src/ecdsa_c.cpp
+++ b/src/ecdsa_c.cpp
@@ -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;
}
diff --git a/src/fp.cpp b/src/fp.cpp
index b71ee69..e45217b 100644
--- a/src/fp.cpp
+++ b/src/fp.cpp
@@ -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)
diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp
index 54d191c..25054b8 100644
--- a/src/she_c_impl.hpp
+++ b/src/she_c_impl.hpp
@@ -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)
diff --git a/test/she_test.cpp b/test/she_test.cpp
index 810a749..79d914e 100644
--- a/test/she_test.cpp
+++ b/test/she_test.cpp
@@ -115,30 +115,33 @@ CYBOZU_TEST_AUTO(bench2)
}
#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>
void HashTableTest(const G& P)
{
- mcl::she::local::HashTable<G> hashTbl;
+ mcl::she::local::HashTable<G> hashTbl, hashTbl2;
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);
- 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);
- }
- std::stringstream ss;
- hashTbl.save(ss);
- mcl::she::local::HashTable<G> hashTbl2;
- hashTbl2.load(ss);
- hashTbl = hashTbl2;
- }
+ GAHashTableTest(maxSize, tryNum, P, hashTbl);
+ std::stringstream ss;
+ hashTbl.save(ss);
+ hashTbl2.load(ss);
+ 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);
- }
- std::stringstream ss;
- hashTbl.save(ss);
- mcl::she::local::HashTable<GT, false> hashTbl2;
- hashTbl2.load(ss);
- hashTbl = hashTbl2;
- }
+ GTHashTableTest(maxSize, tryNum, g, hashTbl);
+ std::stringstream ss;
+ hashTbl.save(ss);
+ hashTbl2.load(ss);
+ GTHashTableTest(maxSize, tryNum, g, hashTbl2);
}
CYBOZU_TEST_AUTO(enc_dec)