diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bls.cpp | 9 | ||||
-rw-r--r-- | src/bls_if.cpp | 220 |
2 files changed, 120 insertions, 109 deletions
diff --git a/src/bls.cpp b/src/bls.cpp index 01a6ae9..70bbfa3 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -258,6 +258,11 @@ void Id::set(const uint64_t *p) getInner().v.setArrayMask(p, keySize); } +void Id::setLittleEndian(const void *buf, size_t bufSize) +{ + getInner().v.setArrayMask((const char *)buf, bufSize); +} + bool Sign::operator==(const Sign& rhs) const { return getInner().sHm == rhs.getInner().sHm; @@ -410,6 +415,10 @@ void SecretKey::set(const uint64_t *p) { getInner().s.setArrayMask(p, keySize); } +void SecretKey::setLittleEndian(const void *buf, size_t bufSize) +{ + getInner().s.setArrayMask((const char *)buf, bufSize); +} void SecretKey::getPublicKey(PublicKey& pub) const { diff --git a/src/bls_if.cpp b/src/bls_if.cpp index 0ed194c..d8b24ea 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -1,19 +1,10 @@ #include "bls/bls.hpp" -#define BLS256_DLL_EXPORT +#define BLS_DLL_EXPORT #include "bls/bls_if.h" #include <iostream> #include <sstream> #include <memory.h> - -template<class Inner, class Outer> -Outer *createT() - try -{ - return (Outer*)new Inner(); -} catch (std::exception& e) { - fprintf(stderr, "err createT %s\n", e.what()); - return NULL; -} +#include <mcl/fp.hpp> template<class Inner, class Outer> int setStrT(Outer *p, const char *buf, size_t bufSize, int ioMode) @@ -42,7 +33,7 @@ size_t getStrT(const Outer *p, char *buf, size_t maxBufSize, int ioMode) std::string s; ((const Inner*)p)->getStr(s, ioMode); size_t terminate = 0; - if (ioMode == 0 || ioMode == blsIoBin || ioMode == blsIoDec || ioMode == blsIoHex) { + if (ioMode == 0 || ioMode == bls::IoBin || ioMode == bls::IoDec || ioMode == bls::IoHex) { terminate = 1; // for '\0' } if (s.size() > maxBufSize + terminate) { @@ -90,103 +81,104 @@ int blsGetFieldOrder(char *buf, size_t maxBufSize) return 0; } -blsId *blsIdCreate() +int blsIdIsSame(const blsId *lhs, const blsId *rhs) { - return createT<bls::Id, blsId>(); + return *(const bls::Id*)lhs == *(const bls::Id*)rhs ? 1 : 0; } - -void blsIdDestroy(blsId *id) +int blsIdSetLittleEndian(blsId *id, const void *buf, size_t bufSize) { - delete (bls::Id*)id; + ((bls::Id*)id)->setLittleEndian(buf, bufSize); + return 0; } -int blsIdIsSame(const blsId *lhs, const blsId *rhs) +int blsIdSetDecStr(blsId *id, const char *buf, size_t bufSize) { - return *(const bls::Id*)lhs == *(const bls::Id*)rhs ? 1 : 0; + return setStrT<bls::Id, blsId>(id, buf, bufSize, 10); } -void blsIdPut(const blsId *id) +int blsIdSetHexStr(blsId *id, const char *buf, size_t bufSize) { - std::cout << *(const bls::Id*)id << std::endl; + return setStrT<bls::Id, blsId>(id, buf, bufSize, 16); } -void blsIdCopy(blsId *dst, const blsId *src) +size_t blsIdGetLittleEndian(void *buf, size_t maxBufSize, const blsId *id) { - *((bls::Id*)dst) = *((const bls::Id*)src); + return getStrT<bls::Id, blsId>(id, (char *)buf, maxBufSize, bls::IoFixedByteSeq); } - -int blsIdSetStr(blsId *id, const char *buf, size_t bufSize, int ioMode) +size_t blsIdGetDecStr(char *buf, size_t maxBufSize, const blsId *id) { - return setStrT<bls::Id, blsId>(id, buf, bufSize, ioMode); + return getStrT<bls::Id, blsId>(id, buf, maxBufSize, 10); } - -size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize, int ioMode) +size_t blsIdGetHexStr(char *buf, size_t maxBufSize, const blsId *id) { - return getStrT<bls::Id, blsId>(id, buf, maxBufSize, ioMode); + return getStrT<bls::Id, blsId>(id, buf, maxBufSize, 16); } - -void blsIdSet(blsId *id, const uint64_t *p) +int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs) { - ((bls::Id*)id)->set(p); + return *(const bls::SecretKey*)lhs == *(const bls::SecretKey*)rhs ? 1 : 0; } - -blsSecretKey* blsSecretKeyCreate() +int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, size_t bufSize) { - return createT<bls::SecretKey, blsSecretKey>(); + ((bls::SecretKey*)sec)->setLittleEndian(buf, bufSize); + return 0; } - -void blsSecretKeyDestroy(blsSecretKey *sec) +int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, size_t bufSize) { - delete (bls::SecretKey*)sec; + return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, 10); } -int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs) +int blsSecretKeySetHexStr(blsSecretKey *sec, const char *buf, size_t bufSize) { - return *(const bls::SecretKey*)lhs == *(const bls::SecretKey*)rhs ? 1 : 0; + return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, 16); } -void blsSecretKeyCopy(blsSecretKey *dst, const blsSecretKey *src) +size_t blsSecretKeyGetLittleEndian(void *buf, size_t maxBufSize, const blsSecretKey *sec) { - *((bls::SecretKey*)dst) = *((const bls::SecretKey*)src); + return getStrT<bls::SecretKey, blsSecretKey>(sec, (char *)buf, maxBufSize, bls::IoFixedByteSeq); } - -void blsSecretKeyPut(const blsSecretKey *sec) +size_t blsSecretKeyGetDecStr(char *buf, size_t maxBufSize, const blsSecretKey *sec) { - std::cout << *(const bls::SecretKey*)sec << std::endl; + return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, 10); } -void blsSecretKeySetArray(blsSecretKey *sec, const uint64_t *p) +size_t blsSecretKeyGetHexStr(char *buf, size_t maxBufSize, const blsSecretKey *sec) { - ((bls::SecretKey*)sec)->set(p); + return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, 16); } -int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize, int ioMode) -{ - return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, ioMode); -} -size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize, int ioMode) +int blsSecretKeySetByHash(blsSecretKey *sec, const void *buf, size_t bufSize) + try { - return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, ioMode); + std::string s = mcl::fp::hash(384, (const char *)buf, bufSize); + return blsSecretKeySetLittleEndian(sec, s.c_str(), s.size()); +} catch (std::exception& e) { + fprintf(stderr, "err blsSecretKeySetByCSPRNG %s\n", e.what()); + return -1; } -void blsSecretKeyInit(blsSecretKey *sec) +int blsSecretKeySetByCSPRNG(blsSecretKey *sec) + try { ((bls::SecretKey*)sec)->init(); + return 0; +} catch (std::exception& e) { + fprintf(stderr, "err blsSecretKeySetByCSPRNG %s\n", e.what()); + return -1; } void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs) { ((bls::SecretKey*)sec)->add(*(const bls::SecretKey*)rhs); } -void blsSecretKeyGetPublicKey(const blsSecretKey *sec, blsPublicKey *pub) +void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec) { ((const bls::SecretKey*)sec)->getPublicKey(*(bls::PublicKey*)pub); } -void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, size_t size) +void blsSign(blsSignature *sig, const blsSecretKey *sec, const char *m, size_t size) { - ((const bls::SecretKey*)sec)->sign(*(bls::Sign*)sign, std::string(m, size)); + ((const bls::SecretKey*)sec)->sign(*(bls::Sign*)sig, std::string(m, size)); } -int blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id) +int blsSecretKeyShare(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id) try { ((bls::SecretKey*)sec)->set((const bls::SecretKey *)msk, k, *(const bls::Id*)id); return 0; } catch (std::exception& e) { - fprintf(stderr, "err blsSecretKeySet %s\n", e.what()); + fprintf(stderr, "err blsSecretKeyShare %s\n", e.what()); return -1; } @@ -200,52 +192,57 @@ int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const bls return -1; } -void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign) -{ - ((const bls::SecretKey*)sec)->getPop(*(bls::Sign*)sign); -} - -blsPublicKey *blsPublicKeyCreate() +void blsGetPop(blsSignature *sig, const blsSecretKey *sec) { - return createT<bls::PublicKey, blsPublicKey>(); + ((const bls::SecretKey*)sec)->getPop(*(bls::Sign*)sig); } -void blsPublicKeyDestroy(blsPublicKey *pub) -{ - delete (bls::PublicKey*)pub; -} int blsPublicKeyIsSame(const blsPublicKey *lhs, const blsPublicKey *rhs) { return *(const bls::PublicKey*)lhs == *(const bls::PublicKey*)rhs ? 1 : 0; } -void blsPublicKeyCopy(blsPublicKey *dst, const blsPublicKey *src) +int blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, size_t bufSize) { - *((bls::PublicKey*)dst) = *((const bls::PublicKey*)src); + return setStrT<bls::PublicKey, blsPublicKey>(pub, (const char*)buf, bufSize, bls::IoFixedByteSeq); } -void blsPublicKeyPut(const blsPublicKey *pub) +size_t blsPublicKeySerialize(void *buf, size_t maxBufSize, const blsPublicKey *pub) { - std::cout << *(const bls::PublicKey*)pub << std::endl; + return getStrT<bls::PublicKey, blsPublicKey>(pub, (char *)buf, maxBufSize, bls::IoFixedByteSeq); } - -int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize, int ioMode) +int blsPublicKeySetHexStr(blsPublicKey *pub, const char *buf, size_t bufSize) + try { - return setStrT<bls::PublicKey, blsPublicKey>(pub, buf, bufSize, ioMode); + std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize); + return blsPublicKeyDeserialize(pub, s.c_str(), s.size()); +} catch (std::exception& e) { + fprintf(stderr, "err blsPublicKeySetHexStr %s\n", e.what()); + return -1; } -size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize, int ioMode) +size_t blsPublicKeyGetHexStr(char *buf, size_t maxBufSize, const blsPublicKey *pub) { - return getStrT<bls::PublicKey, blsPublicKey>(pub, buf, maxBufSize, ioMode); + std::string s; + s.resize(1024); + if (blsPublicKeySerialize(&s[0], s.size(), pub) == 0) { + s = mcl::fp::littleEndianToHexStr(s.c_str(), s.size()); + if (s.size() < maxBufSize) { + memcpy(buf, s.c_str(), s.size()); + buf[s.size()] = '\0'; + return s.size(); + } + } + return 0; } void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs) { ((bls::PublicKey*)pub)->add(*(const bls::PublicKey*)rhs); } -int blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id) +int blsPublicKeyShare(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id) try { ((bls::PublicKey*)pub)->set((const bls::PublicKey*)mpk, k, *(const bls::Id*)id); return 0; } catch (std::exception& e) { - fprintf(stderr, "err blsPublicKeySet %s\n", e.what()); + fprintf(stderr, "err blsPublicKeyShare %s\n", e.what()); return -1; } int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n) @@ -258,57 +255,62 @@ int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const bls return -1; } -blsSign *blsSignCreate() -{ - return createT<bls::Sign, blsSign>(); -} - -void blsSignDestroy(blsSign *sign) -{ - delete (bls::Sign*)sign; -} -int blsSignIsSame(const blsSign *lhs, const blsSign *rhs) +int blsSignatureIsSame(const blsSignature *lhs, const blsSignature *rhs) { return *(const bls::Sign*)lhs == *(const bls::Sign*)rhs ? 1 : 0; } -void blsSignCopy(blsSign *dst, const blsSign *src) +int blsSignatureDeserialize(blsSignature *sig, const void *buf, size_t bufSize) { - *((bls::Sign*)dst) = *((const bls::Sign*)src); + return setStrT<bls::Sign, blsSignature>(sig, (const char *)buf, bufSize, bls::IoFixedByteSeq); } -void blsSignPut(const blsSign *sign) +int blsSignatureSetHexStr(blsSignature *sig, const char *buf, size_t bufSize) + try { - std::cout << *(const bls::Sign*)sign << std::endl; + std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize); + return blsSignatureDeserialize(sig, s.c_str(), s.size()); +} catch (std::exception& e) { + fprintf(stderr, "err blsSignatureSetHexStr %s\n", e.what()); + return -1; } - -int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, int ioMode) +size_t blsSignatureGetHexStr(char *buf, size_t maxBufSize, const blsSignature *sig) { - return setStrT<bls::Sign, blsSign>(sign, buf, bufSize, ioMode); + std::string s; + s.resize(1024); + if (blsSignatureSerialize(&s[0], s.size(), sig) == 0) { + s = mcl::fp::littleEndianToHexStr(s.c_str(), s.size()); + if (s.size() < maxBufSize) { + memcpy(buf, s.c_str(), s.size()); + buf[s.size()] = '\0'; + return s.size(); + } + } + return 0; } -size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize, int ioMode) +size_t blsSignatureSerialize(void *buf, size_t maxBufSize, const blsSignature *sig) { - return getStrT<bls::Sign, blsSign>(sign, buf, maxBufSize, ioMode); + return getStrT<bls::Sign, blsSignature>(sig, (char *)buf, maxBufSize, bls::IoFixedByteSeq); } -void blsSignAdd(blsSign *sign, const blsSign *rhs) +void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs) { - ((bls::Sign*)sign)->add(*(const bls::Sign*)rhs); + ((bls::Sign*)sig)->add(*(const bls::Sign*)rhs); } -int blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n) +int blsSignatureRecover(blsSignature *sig, const blsSignature *signVec, const blsId *idVec, size_t n) try { - ((bls::Sign*)sign)->recover((const bls::Sign*)signVec, (const bls::Id*)idVec, n); + ((bls::Sign*)sig)->recover((const bls::Sign*)signVec, (const bls::Id*)idVec, n); return 0; } catch (std::exception& e) { - fprintf(stderr, "err blsSignRecover %s\n", e.what()); + fprintf(stderr, "err blsSignatureRecover %s\n", e.what()); return -1; } -int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size) +int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const char *m, size_t size) { - return ((const bls::Sign*)sign)->verify(*(const bls::PublicKey*)pub, std::string(m, size)); + return ((const bls::Sign*)sig)->verify(*(const bls::PublicKey*)pub, std::string(m, size)); } -int blsSignVerifyPop(const blsSign *sign, const blsPublicKey *pub) +int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub) { - return ((const bls::Sign*)sign)->verify(*(const bls::PublicKey*)pub); + return ((const bls::Sign*)sig)->verify(*(const bls::PublicKey*)pub); } |