diff options
-rw-r--r-- | include/bls_if.h | 17 | ||||
-rw-r--r-- | src/bls_if.cpp | 75 | ||||
-rw-r--r-- | test/bls_if_test.cpp | 32 |
3 files changed, 124 insertions, 0 deletions
diff --git a/include/bls_if.h b/include/bls_if.h index ce8463e..eb7ba4e 100644 --- a/include/bls_if.h +++ b/include/bls_if.h @@ -48,6 +48,10 @@ size_t blsGetOpUnitSize(void); blsId *blsIdCreate(void); void blsIdDestroy(blsId *id); +size_t blsIdGetData(const blsId *id, char *buf, size_t maxBufSize); +int blsIdSetData(blsId *id, const char *buf, size_t bufSize); +// return 1 if same else 0 +int blsIdIsSame(const blsId *lhs, const blsId *rhs); void blsIdPut(const blsId *id); void blsIdCopy(blsId *dst, const blsId *src); @@ -67,6 +71,11 @@ void blsIdSet(blsId *id, const uint64_t *p); blsSecretKey* blsSecretKeyCreate(void); void blsSecretKeyDestroy(blsSecretKey *sec); +size_t blsSecretKeyGetData(const blsSecretKey *sec, char *buf, size_t maxBufSize); +int blsSecretKeySetData(blsSecretKey *sec, const char *buf, size_t bufSize); +// return 1 if same else 0 +int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs); + void blsSecretKeyPut(const blsSecretKey *sec); void blsSecretKeyCopy(blsSecretKey *dst, const blsSecretKey *src); void blsSecretKeySetArray(blsSecretKey *sec, const uint64_t *p); @@ -83,6 +92,10 @@ void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign); blsPublicKey *blsPublicKeyCreate(void); void blsPublicKeyDestroy(blsPublicKey *pub); +size_t blsPublicKeyGetData(const blsPublicKey *pub, char *buf, size_t maxBufSize); +int blsPublicKeySetData(blsPublicKey *pub, const char *buf, size_t bufSize); +// return 1 if same else 0 +int blsPublicKeyIsSame(const blsPublicKey *lhs, const blsPublicKey *rhs); void blsPublicKeyPut(const blsPublicKey *pub); void blsPublicKeyCopy(blsPublicKey *dst, const blsPublicKey *src); int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize); @@ -93,6 +106,10 @@ void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const bl blsSign *blsSignCreate(void); void blsSignDestroy(blsSign *sign); +size_t blsSignGetData(const blsSign *sign, char *buf, size_t maxBufSize); +int blsSignSetData(blsSign *sign, const char *buf, size_t bufSize); +// return 1 if same else 0 +int blsSignIsSame(const blsSign *lhs, const blsSign *rhs); void blsSignPut(const blsSign *sign); void blsSignCopy(blsSign *dst, const blsSign *src); int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize); diff --git a/src/bls_if.cpp b/src/bls_if.cpp index 56f35b0..3bab145 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -43,6 +43,33 @@ size_t getStrT(const Outer *p, char *buf, size_t maxBufSize) return 0; } +template<class Inner, class Outer> +int setDataT(Outer *p, const char *buf, size_t bufSize) + try +{ + ((Inner*)p)->setData(std::string(buf, bufSize)); + return 0; +} catch (std::exception& e) { + fprintf(stderr, "err setDataT %s\n", e.what()); + return 1; +} + +template<class Inner, class Outer> +size_t getDataT(const Outer *p, char *buf, size_t maxBufSize) + try +{ + std::string s; + ((const Inner*)p)->getData(s); + if (s.size() > maxBufSize) { + fprintf(stderr, "err getDataT size is small %d %d\n", (int)s.size(), (int)maxBufSize); + return 0; + } + memcpy(buf, s.c_str(), s.size()); + return s.size(); +} catch (std::exception&) { + return 0; +} + void blsInit(int curve, int maxUnitSize) { bls::init(curve, maxUnitSize); @@ -61,6 +88,18 @@ void blsIdDestroy(blsId *id) { delete (bls::Id*)id; } +size_t blsIdGetData(const blsId *id, char *buf, size_t maxBufSize) +{ + return getDataT<bls::Id, blsId>(id, buf, maxBufSize); +} +int blsIdSetData(blsId *id, const char *buf, size_t bufSize) +{ + return setDataT<bls::Id, blsId>(id, buf, bufSize); +} +int blsIdIsSame(const blsId *lhs, const blsId *rhs) +{ + return *(const bls::Id*)lhs == *(const bls::Id*)rhs ? 1 : 0; +} void blsIdPut(const blsId *id) { std::cout << *(const bls::Id*)id << std::endl; @@ -94,6 +133,18 @@ void blsSecretKeyDestroy(blsSecretKey *sec) { delete (bls::SecretKey*)sec; } +size_t blsSecretKeyGetData(const blsSecretKey *sec, char *buf, size_t maxBufSize) +{ + return getDataT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize); +} +int blsSecretKeySetData(blsSecretKey *sec, const char *buf, size_t bufSize) +{ + return setDataT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize); +} +int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs) +{ + return *(const bls::SecretKey*)lhs == *(const bls::SecretKey*)rhs ? 1 : 0; +} void blsSecretKeyCopy(blsSecretKey *dst, const blsSecretKey *src) { *((bls::SecretKey*)dst) = *((const bls::SecretKey*)src); @@ -158,6 +209,18 @@ void blsPublicKeyDestroy(blsPublicKey *pub) { delete (bls::PublicKey*)pub; } +size_t blsPublicKeyGetData(const blsPublicKey *pub, char *buf, size_t maxBufSize) +{ + return getDataT<bls::PublicKey, blsPublicKey>(pub, buf, maxBufSize); +} +int blsPublicKeySetData(blsPublicKey *pub, const char *buf, size_t bufSize) +{ + return setDataT<bls::PublicKey, blsPublicKey>(pub, buf, bufSize); +} +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) { *((bls::PublicKey*)dst) = *((const bls::PublicKey*)src); @@ -197,6 +260,18 @@ void blsSignDestroy(blsSign *sign) { delete (bls::Sign*)sign; } +size_t blsSignGetData(const blsSign *sign, char *buf, size_t maxBufSize) +{ + return getDataT<bls::Sign, blsSign>(sign, buf, maxBufSize); +} +int blsSignSetData(blsSign *sign, const char *buf, size_t bufSize) +{ + return setDataT<bls::Sign, blsSign>(sign, buf, bufSize); +} +int blsSignIsSame(const blsSign *lhs, const blsSign *rhs) +{ + return *(const bls::Sign*)lhs == *(const bls::Sign*)rhs ? 1 : 0; +} void blsSignCopy(blsSign *dst, const blsSign *src) { *((bls::Sign*)dst) = *((const bls::Sign*)src); diff --git a/test/bls_if_test.cpp b/test/bls_if_test.cpp index ed59396..2292bfa 100644 --- a/test/bls_if_test.cpp +++ b/test/bls_if_test.cpp @@ -49,6 +49,37 @@ void bls_if_use_stackTest() CYBOZU_TEST_ASSERT(blsSignVerify(&sign, &pub, msg, msgSize)); } +void bls_ifDataTest() +{ + const char *msg = "test test"; + const size_t msgSize = strlen(msg); + const size_t fpSize = blsGetOpUnitSize() * sizeof(uint64_t); + blsSecretKey sec1, sec2; + blsSecretKeyInit(&sec1); + char buf[BLS_MAX_OP_UNIT_SIZE * sizeof(uint64_t) * 2]; + size_t n; + int ret; + n = blsSecretKeyGetData(&sec1, buf, sizeof(buf)); + CYBOZU_TEST_EQUAL(n, fpSize); + ret = blsSecretKeySetData(&sec2, buf, n); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(blsSecretKeyIsSame(&sec1, &sec2)); + blsPublicKey pub1, pub2; + blsSecretKeyGetPublicKey(&sec1, &pub1); + n = blsPublicKeyGetData(&pub1, buf, sizeof(buf)); + CYBOZU_TEST_EQUAL(n, fpSize * 2); + ret = blsPublicKeySetData(&pub2, buf, n); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(blsPublicKeyIsSame(&pub1, &pub2)); + blsSign sign1, sign2; + blsSecretKeySign(&sec1, &sign1, msg, msgSize); + n = blsSignGetData(&sign1, buf, sizeof(buf)); + CYBOZU_TEST_EQUAL(n, fpSize); + ret = blsSignSetData(&sign2, buf, n); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_ASSERT(blsSignIsSame(&sign1, &sign2)); +} + CYBOZU_TEST_AUTO(all) { const int tbl[] = { @@ -63,5 +94,6 @@ CYBOZU_TEST_AUTO(all) blsInit(tbl[i], BLS_MAX_OP_UNIT_SIZE); bls_ifTest(); bls_if_use_stackTest(); + bls_ifDataTest(); } } |