From 58b35f9129e4cb94985c4b8bdd454f03f7d801ad Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 15 Jul 2018 12:43:29 +0900 Subject: add BLS_DONT_EXPORT macro --- include/bls/bls.h | 109 ++++++++++++++++------------- src/bls_c.cpp | 205 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 178 insertions(+), 136 deletions(-) diff --git a/include/bls/bls.h b/include/bls/bls.h index 820fc59..98d05d5 100644 --- a/include/bls/bls.h +++ b/include/bls/bls.h @@ -9,25 +9,29 @@ #include #ifdef _MSC_VER -#ifdef BLS_DLL_EXPORT -#define BLS_DLL_API __declspec(dllexport) -#else -#define BLS_DLL_API __declspec(dllimport) -#ifndef BLS_NO_AUTOLINK - #if MCLBN_FP_UNIT_SIZE == 4 - #pragma comment(lib, "bls256.lib") + #ifdef BLS_DONT_EXPORT + #define BLS_DLL_API + #else + #ifdef BLS_DLL_EXPORT + #define BLS_DLL_API __declspec(dllexport) + #else + #define BLS_DLL_API __declspec(dllimport) + #endif #endif -#endif -#endif -#else -#ifdef __EMSCRIPTEN__ + #ifndef BLS_NO_AUTOLINK + #if MCLBN_FP_UNIT_SIZE == 4 + #pragma comment(lib, "bls256.lib") + #elif MCLBN_FP_UNIT_SIZE == 6 + #pragma comment(lib, "bls384.lib") + #endif + #endif +#elif defined(__EMSCRIPTEN__) && !defined(BLS_DONT_EXPORT) #define BLS_DLL_API __attribute__((used)) -#elif defined(__wasm__) +#elif defined(__wasm__) && !defined(MCLBN_DONT_EXPORT) #define BLS_DLL_API __attribute__((visibility("default"))) #else #define BLS_DLL_API #endif -#endif #ifdef __cplusplus extern "C" { @@ -60,29 +64,18 @@ typedef struct { */ BLS_DLL_API int blsInit(int curve, int maxUnitSize); -// not thread safe version (old blsInit) -BLS_DLL_API int blsInitNotThreadSafe(int curve, int maxUnitSize); - -BLS_DLL_API mclSize blsGetOpUnitSize(void); -// return strlen(buf) if success else 0 -BLS_DLL_API int blsGetCurveOrder(char *buf, mclSize maxBufSize); -BLS_DLL_API int blsGetFieldOrder(char *buf, mclSize maxBufSize); - -// get a generator of G2 -BLS_DLL_API void blsGetGeneratorOfG2(blsPublicKey *pub); - BLS_DLL_API void blsIdSetInt(blsId *id, int x); + // return 0 if success -BLS_DLL_API int blsIdSetDecStr(blsId *id, const char *buf, mclSize bufSize); -BLS_DLL_API int blsIdSetHexStr(blsId *id, const char *buf, mclSize bufSize); +// mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r +BLS_DLL_API int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, mclSize bufSize); -/* - return strlen(buf) if success else 0 - buf is '\0' terminated -*/ -BLS_DLL_API mclSize blsIdGetDecStr(char *buf, mclSize maxBufSize, const blsId *id); -BLS_DLL_API mclSize blsIdGetHexStr(char *buf, mclSize maxBufSize, const blsId *id); +BLS_DLL_API void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec); +BLS_DLL_API void blsSign(blsSignature *sig, const blsSecretKey *sec, const void *m, mclSize size); + +// return 1 if valid +BLS_DLL_API int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const void *m, mclSize size); // return written byte size if success else 0 BLS_DLL_API mclSize blsIdSerialize(void *buf, mclSize maxBufSize, const blsId *id); @@ -102,11 +95,43 @@ BLS_DLL_API int blsSecretKeyIsEqual(const blsSecretKey *lhs, const blsSecretKey BLS_DLL_API int blsPublicKeyIsEqual(const blsPublicKey *lhs, const blsPublicKey *rhs); BLS_DLL_API int blsSignatureIsEqual(const blsSignature *lhs, const blsSignature *rhs); +// return 0 if success +BLS_DLL_API int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, mclSize k, const blsId *id); +BLS_DLL_API int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, mclSize k, const blsId *id); + +BLS_DLL_API int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, mclSize n); +BLS_DLL_API int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, mclSize n); +BLS_DLL_API int blsSignatureRecover(blsSignature *sig, const blsSignature *sigVec, const blsId *idVec, mclSize n); + // add BLS_DLL_API void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs); BLS_DLL_API void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs); BLS_DLL_API void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs); +#ifndef BLS_MINIMUM_API + +// not thread safe version (old blsInit) +BLS_DLL_API int blsInitNotThreadSafe(int curve, int maxUnitSize); + +BLS_DLL_API mclSize blsGetOpUnitSize(void); +// return strlen(buf) if success else 0 +BLS_DLL_API int blsGetCurveOrder(char *buf, mclSize maxBufSize); +BLS_DLL_API int blsGetFieldOrder(char *buf, mclSize maxBufSize); + +// get a generator of G2 +BLS_DLL_API void blsGetGeneratorOfG2(blsPublicKey *pub); + +// return 0 if success +BLS_DLL_API int blsIdSetDecStr(blsId *id, const char *buf, mclSize bufSize); +BLS_DLL_API int blsIdSetHexStr(blsId *id, const char *buf, mclSize bufSize); + +/* + return strlen(buf) if success else 0 + buf is '\0' terminated +*/ +BLS_DLL_API mclSize blsIdGetDecStr(char *buf, mclSize maxBufSize, const blsId *id); +BLS_DLL_API mclSize blsIdGetHexStr(char *buf, mclSize maxBufSize, const blsId *id); + // hash buf and set BLS_DLL_API int blsHashToSecretKey(blsSecretKey *sec, const void *buf, mclSize bufSize); #ifndef MCL_DONT_USE_CSPRNG @@ -117,24 +142,9 @@ BLS_DLL_API int blsHashToSecretKey(blsSecretKey *sec, const void *buf, mclSize b BLS_DLL_API int blsSecretKeySetByCSPRNG(blsSecretKey *sec); #endif -BLS_DLL_API void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec); BLS_DLL_API void blsGetPop(blsSignature *sig, const blsSecretKey *sec); -// return 0 if success -BLS_DLL_API int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, mclSize k, const blsId *id); -BLS_DLL_API int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, mclSize k, const blsId *id); - - -BLS_DLL_API int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, mclSize n); -BLS_DLL_API int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, mclSize n); -BLS_DLL_API int blsSignatureRecover(blsSignature *sig, const blsSignature *sigVec, const blsId *idVec, mclSize n); - -BLS_DLL_API void blsSign(blsSignature *sig, const blsSecretKey *sec, const void *m, mclSize size); - -// return 1 if valid -BLS_DLL_API int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const void *m, mclSize size); BLS_DLL_API int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub); - ////////////////////////////////////////////////////////////////////////// // the following apis will be removed @@ -146,8 +156,6 @@ BLS_DLL_API int blsIdSetLittleEndian(blsId *id, const void *buf, mclSize bufSize BLS_DLL_API mclSize blsIdGetLittleEndian(void *buf, mclSize maxBufSize, const blsId *id); // return 0 if success -// mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r -BLS_DLL_API int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, mclSize bufSize); BLS_DLL_API int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, mclSize bufSize); BLS_DLL_API int blsSecretKeySetHexStr(blsSecretKey *sec, const char *buf, mclSize bufSize); /* @@ -170,6 +178,9 @@ BLS_DLL_API mclSize blsSignatureGetHexStr(char *buf, mclSize maxBufSize, const b out = sec * pub */ BLS_DLL_API void blsDHKeyExchange(blsPublicKey *out, const blsSecretKey *sec, const blsPublicKey *pub); + +#endif // BLS_MINIMUM_API + #ifdef __cplusplus } #endif diff --git a/src/bls_c.cpp b/src/bls_c.cpp index 108d123..d983ff2 100644 --- a/src/bls_c.cpp +++ b/src/bls_c.cpp @@ -76,81 +76,41 @@ int blsInit(int curve, int maxUnitSize) static inline const mclBnG1 *cast(const G1* x) { return (const mclBnG1*)x; } static inline const mclBnG2 *cast(const G2* x) { return (const mclBnG2*)x; } -/* - e(P1, Q1) == e(P2, Q2) - <=> finalExp(ML(P1, Q1)) == finalExp(ML(P2, Q2)) - <=> finalExp(ML(P1, Q1) / ML(P2, Q2)) == 1 - <=> finalExp(ML(P1, Q1) * ML(-P2, Q2)) == 1 - Q1 is precomputed -*/ -bool isEqualTwoPairings(const G1& P1, const Fp6* Q1coeff, const G1& P2, const G2& Q2) -{ - Fp12 e; - precomputedMillerLoop2mixed(e, P2, Q2, -P1, Q1coeff); - finalExp(e, e); - return e.isOne(); -} - -mclSize blsGetOpUnitSize() // FpUint64Size -{ - return Fp::getUnitSize() * sizeof(mcl::fp::Unit) / sizeof(uint64_t); -} - -int blsGetCurveOrder(char *buf, mclSize maxBufSize) -{ - return Fr::getModulo(buf, maxBufSize); -} - -int blsGetFieldOrder(char *buf, mclSize maxBufSize) +void blsIdSetInt(blsId *id, int x) { - return Fp::getModulo(buf, maxBufSize); + mclBnFr_setInt(&id->v, x); } -void blsGetGeneratorOfG2(blsPublicKey *pub) +int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, mclSize bufSize) { - *(G2*)pub = getQ(); + return mclBnFr_setLittleEndian(&sec->v, buf, bufSize); } void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec) { mclBnG2_mul(&pub->v, cast(&getQ()), &sec->v); } + void blsSign(blsSignature *sig, const blsSecretKey *sec, const void *m, mclSize size) { G1 Hm; hashAndMapToG1(Hm, m, size); mclBnG1_mulCT(&sig->v, cast(&Hm), &sec->v); } -int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, mclSize k, const blsId *id) -{ - return mclBn_FrEvaluatePolynomial(&sec->v, &msk->v, k, &id->v); -} -int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, mclSize n) -{ - return mclBn_FrLagrangeInterpolation(&sec->v, &idVec->v, &secVec->v, n); -} - -void blsGetPop(blsSignature *sig, const blsSecretKey *sec) -{ - blsPublicKey pub; - blsGetPublicKey(&pub, sec); - char buf[1024]; - mclSize n = mclBnG2_serialize(buf, sizeof(buf), &pub.v); - assert(n); - blsSign(sig, sec, buf, n); -} -int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, mclSize k, const blsId *id) -{ - return mclBn_G2EvaluatePolynomial(&pub->v, &mpk->v, k, &id->v); -} -int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, mclSize n) -{ - return mclBn_G2LagrangeInterpolation(&pub->v, &idVec->v, &pubVec->v, n); -} -int blsSignatureRecover(blsSignature *sig, const blsSignature *sigVec, const blsId *idVec, mclSize n) +/* + e(P1, Q1) == e(P2, Q2) + <=> finalExp(ML(P1, Q1)) == finalExp(ML(P2, Q2)) + <=> finalExp(ML(P1, Q1) / ML(P2, Q2)) == 1 + <=> finalExp(ML(P1, Q1) * ML(-P2, Q2)) == 1 + Q1 is precomputed +*/ +bool isEqualTwoPairings(const G1& P1, const Fp6* Q1coeff, const G1& P2, const G2& Q2) { - return mclBn_G1LagrangeInterpolation(&sig->v, &idVec->v, &sigVec->v, n); + Fp12 e; + precomputedMillerLoop2mixed(e, P2, Q2, -P1, Q1coeff); + finalExp(e, e); + return e.isOne(); } int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const void *m, mclSize size) @@ -164,82 +124,127 @@ int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const void *m, m return isEqualTwoPairings(*cast(&sig->v), getQcoeff().data(), Hm, *cast(&pub->v)); } -int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub) -{ - char buf[1024]; - mclSize n = mclBnG2_serialize(buf, sizeof(buf), &pub->v); - if (n == 0) return 0; - return blsVerify(sig, pub, buf, n); -} - -void blsIdSetInt(blsId *id, int x) -{ - mclBnFr_setInt(&id->v, x); -} mclSize blsIdSerialize(void *buf, mclSize maxBufSize, const blsId *id) { return mclBnFr_serialize(buf, maxBufSize, &id->v); } + mclSize blsSecretKeySerialize(void *buf, mclSize maxBufSize, const blsSecretKey *sec) { return mclBnFr_serialize(buf, maxBufSize, &sec->v); } + mclSize blsPublicKeySerialize(void *buf, mclSize maxBufSize, const blsPublicKey *pub) { return mclBnG2_serialize(buf, maxBufSize, &pub->v); } + mclSize blsSignatureSerialize(void *buf, mclSize maxBufSize, const blsSignature *sig) { return mclBnG1_serialize(buf, maxBufSize, &sig->v); } + mclSize blsIdDeserialize(blsId *id, const void *buf, mclSize bufSize) { return mclBnFr_deserialize(&id->v, buf, bufSize); } + mclSize blsSecretKeyDeserialize(blsSecretKey *sig, const void *buf, mclSize bufSize) { return mclBnFr_deserialize(&sig->v, buf, bufSize); } + mclSize blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, mclSize bufSize) { return mclBnG2_deserialize(&pub->v, buf, bufSize); } + mclSize blsSignatureDeserialize(blsSignature *sig, const void *buf, mclSize bufSize) { return mclBnG1_deserialize(&sig->v, buf, bufSize); } + int blsIdIsEqual(const blsId *lhs, const blsId *rhs) { return mclBnFr_isEqual(&lhs->v, &rhs->v); } + int blsSecretKeyIsEqual(const blsSecretKey *lhs, const blsSecretKey *rhs) { return mclBnFr_isEqual(&lhs->v, &rhs->v); } + int blsPublicKeyIsEqual(const blsPublicKey *lhs, const blsPublicKey *rhs) { return mclBnG2_isEqual(&lhs->v, &rhs->v); } + int blsSignatureIsEqual(const blsSignature *lhs, const blsSignature *rhs) { return mclBnG1_isEqual(&lhs->v, &rhs->v); } + +int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, mclSize k, const blsId *id) +{ + return mclBn_FrEvaluatePolynomial(&sec->v, &msk->v, k, &id->v); +} + +int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, mclSize k, const blsId *id) +{ + return mclBn_G2EvaluatePolynomial(&pub->v, &mpk->v, k, &id->v); +} + +int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, mclSize n) +{ + return mclBn_FrLagrangeInterpolation(&sec->v, &idVec->v, &secVec->v, n); +} + +int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, mclSize n) +{ + return mclBn_G2LagrangeInterpolation(&pub->v, &idVec->v, &pubVec->v, n); +} + +int blsSignatureRecover(blsSignature *sig, const blsSignature *sigVec, const blsId *idVec, mclSize n) +{ + return mclBn_G1LagrangeInterpolation(&sig->v, &idVec->v, &sigVec->v, n); +} + void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs) { mclBnFr_add(&sec->v, &sec->v, &rhs->v); } + +void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs) +{ + mclBnG2_add(&pub->v, &pub->v, &rhs->v); +} + void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs) { mclBnG1_add(&sig->v, &sig->v, &rhs->v); } -void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs) + +#ifndef BLS_MINIMUM_API +mclSize blsGetOpUnitSize() // FpUint64Size { - mclBnG2_add(&pub->v, &pub->v, &rhs->v); + return Fp::getUnitSize() * sizeof(mcl::fp::Unit) / sizeof(uint64_t); } -int blsIdSetLittleEndian(blsId *id, const void *buf, mclSize bufSize) + +int blsGetCurveOrder(char *buf, mclSize maxBufSize) { - return mclBnFr_setLittleEndian(&id->v, buf, bufSize); + return (int)Fr::getModulo(buf, maxBufSize); } + +int blsGetFieldOrder(char *buf, mclSize maxBufSize) +{ + return (int)Fp::getModulo(buf, maxBufSize); +} + +void blsGetGeneratorOfG2(blsPublicKey *pub) +{ + *(G2*)pub = getQ(); +} + int blsIdSetDecStr(blsId *id, const char *buf, mclSize bufSize) { return mclBnFr_setStr(&id->v, buf, bufSize, 10); @@ -248,21 +253,55 @@ int blsIdSetHexStr(blsId *id, const char *buf, mclSize bufSize) { return mclBnFr_setStr(&id->v, buf, bufSize, 16); } -mclSize blsIdGetLittleEndian(void *buf, mclSize maxBufSize, const blsId *id) + +int blsIdSetLittleEndian(blsId *id, const void *buf, mclSize bufSize) { - return mclBnFr_serialize(buf, maxBufSize, &id->v); + return mclBnFr_setLittleEndian(&id->v, buf, bufSize); } + mclSize blsIdGetDecStr(char *buf, mclSize maxBufSize, const blsId *id) { return mclBnFr_getStr(buf, maxBufSize, &id->v, 10); } + mclSize blsIdGetHexStr(char *buf, mclSize maxBufSize, const blsId *id) { return mclBnFr_getStr(buf, maxBufSize, &id->v, 16); } -int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, mclSize bufSize) + +int blsHashToSecretKey(blsSecretKey *sec, const void *buf, mclSize bufSize) { - return mclBnFr_setLittleEndian(&sec->v, buf, bufSize); + return mclBnFr_setHashOf(&sec->v, buf, bufSize); +} + +#ifndef MCL_DONT_USE_CSPRNG +int blsSecretKeySetByCSPRNG(blsSecretKey *sec) +{ + return mclBnFr_setByCSPRNG(&sec->v); +} +#endif + +void blsGetPop(blsSignature *sig, const blsSecretKey *sec) +{ + blsPublicKey pub; + blsGetPublicKey(&pub, sec); + char buf[1024]; + mclSize n = mclBnG2_serialize(buf, sizeof(buf), &pub.v); + assert(n); + blsSign(sig, sec, buf, n); +} + +int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub) +{ + char buf[1024]; + mclSize n = mclBnG2_serialize(buf, sizeof(buf), &pub->v); + if (n == 0) return 0; + return blsVerify(sig, pub, buf, n); +} + +mclSize blsIdGetLittleEndian(void *buf, mclSize maxBufSize, const blsId *id) +{ + return mclBnFr_serialize(buf, maxBufSize, &id->v); } int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, mclSize bufSize) { @@ -284,16 +323,6 @@ mclSize blsSecretKeyGetHexStr(char *buf, mclSize maxBufSize, const blsSecretKey { return mclBnFr_getStr(buf, maxBufSize, &sec->v, 16); } -int blsHashToSecretKey(blsSecretKey *sec, const void *buf, mclSize bufSize) -{ - return mclBnFr_setHashOf(&sec->v, buf, bufSize); -} -#ifndef MCL_DONT_USE_CSPRNG -int blsSecretKeySetByCSPRNG(blsSecretKey *sec) -{ - return mclBnFr_setByCSPRNG(&sec->v); -} -#endif int blsPublicKeySetHexStr(blsPublicKey *pub, const char *buf, mclSize bufSize) { return mclBnG2_setStr(&pub->v, buf, bufSize, 16); @@ -315,3 +344,5 @@ void blsDHKeyExchange(blsPublicKey *out, const blsSecretKey *sec, const blsPubli mclBnG2_mulCT(&out->v, &pub->v, &sec->v); } +#endif + -- cgit v1.2.3