diff options
-rw-r--r-- | include/bls.hpp | 2 | ||||
-rw-r--r-- | include/bls_if.h | 12 | ||||
-rw-r--r-- | src/bls.cpp | 9 | ||||
-rw-r--r-- | src/bls_if.cpp | 37 | ||||
-rw-r--r-- | test/bls_if_test.cpp | 23 |
5 files changed, 73 insertions, 10 deletions
diff --git a/include/bls.hpp b/include/bls.hpp index c4f4769..689d1c9 100644 --- a/include/bls.hpp +++ b/include/bls.hpp @@ -55,6 +55,8 @@ struct Id; */ void init(int curve = CurveFp254BNb, int maxUnitSize = BLS_MAX_OP_UNIT_SIZE); size_t getOpUnitSize(); +void getCurveOrder(std::string& str); +void getFieldOrder(std::string& str); class SecretKey; class PublicKey; diff --git a/include/bls_if.h b/include/bls_if.h index eb7ba4e..37b755f 100644 --- a/include/bls_if.h +++ b/include/bls_if.h @@ -45,6 +45,9 @@ typedef struct { void blsInit(int curve, int maxUnitSize); size_t blsGetOpUnitSize(void); +// return strlen(buf) if success else 0 +int blsGetCurveOrder(char *buf, size_t maxBufSize); +int blsGetFieldOrder(char *buf, size_t maxBufSize); blsId *blsIdCreate(void); void blsIdDestroy(blsId *id); @@ -58,10 +61,7 @@ void blsIdCopy(blsId *dst, const blsId *src); // return 0 if success int blsIdSetStr(blsId *id, const char *buf, size_t bufSize); -/* - return written size - otherwise 0 -*/ +// return strlen(buf) if success else 0 size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize); /* access p[0], ..., p[3] if 256-bit curve @@ -71,6 +71,7 @@ void blsIdSet(blsId *id, const uint64_t *p); blsSecretKey* blsSecretKeyCreate(void); void blsSecretKeyDestroy(blsSecretKey *sec); +// return written byte size if success else 0 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 @@ -80,6 +81,7 @@ void blsSecretKeyPut(const blsSecretKey *sec); void blsSecretKeyCopy(blsSecretKey *dst, const blsSecretKey *src); void blsSecretKeySetArray(blsSecretKey *sec, const uint64_t *p); int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize); +// return strlen(buf) if success else 0 size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize); void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs); @@ -99,6 +101,7 @@ 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); +// return strlen(buf) if success else 0 size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize); void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs); void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id); @@ -113,6 +116,7 @@ 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); +// return strlen(buf) if success else 0 size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize); void blsSignAdd(blsSign *sign, const blsSign *rhs); void blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n); diff --git a/src/bls.cpp b/src/bls.cpp index 312cf23..9f94723 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -211,6 +211,15 @@ size_t getOpUnitSize() return Fp::getUnitSize() * sizeof(mcl::fp::Unit) / sizeof(uint64_t); } +void getCurveOrder(std::string& str) +{ + Fr::getModulo(str); +} +void getFieldOrder(std::string& str) +{ + Fp::getModulo(str); +} + Id::Id(unsigned int id) { getInner().v = id; diff --git a/src/bls_if.cpp b/src/bls_if.cpp index 3bab145..7d2d78a 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -26,6 +26,16 @@ int setStrT(Outer *p, const char *buf, size_t bufSize) return 1; } +size_t checkAndCopy(char *buf, size_t maxBufSize, const std::string& s) +{ + if (s.size() >= maxBufSize) { + return 0; + } + memcpy(buf, s.c_str(), s.size()); + buf[s.size()] = '\0'; + return s.size(); +} + template<class Inner, class Outer> size_t getStrT(const Outer *p, char *buf, size_t maxBufSize) try @@ -33,12 +43,7 @@ size_t getStrT(const Outer *p, char *buf, size_t maxBufSize) std::ostringstream oss; oss << *(const Inner*)p; std::string s = oss.str(); - if (s.size() > maxBufSize) { - fprintf(stderr, "err getStrT size is small %d %d\n", (int)s.size(), (int)maxBufSize); - return 0; - } - memcpy(buf, s.c_str(), s.size()); - return s.size(); + return checkAndCopy(buf, maxBufSize, s); } catch (std::exception&) { return 0; } @@ -79,6 +84,26 @@ size_t blsGetOpUnitSize() return bls::getOpUnitSize(); } +int blsGetCurveOrder(char *buf, size_t maxBufSize) + try +{ + std::string s; + bls::getCurveOrder(s); + return checkAndCopy(buf, maxBufSize, s); +} catch (std::exception&) { + return 0; +} + +int blsGetFieldOrder(char *buf, size_t maxBufSize) + try +{ + std::string s; + bls::getFieldOrder(s); + return checkAndCopy(buf, maxBufSize, s); +} catch (std::exception&) { + return 0; +} + blsId *blsIdCreate() { return createT<bls::Id, blsId>(); diff --git a/test/bls_if_test.cpp b/test/bls_if_test.cpp index 2292bfa..face2fb 100644 --- a/test/bls_if_test.cpp +++ b/test/bls_if_test.cpp @@ -80,6 +80,18 @@ void bls_ifDataTest() CYBOZU_TEST_ASSERT(blsSignIsSame(&sign1, &sign2)); } +void bls_ifOrderTest(const char *curveOrder, const char *fieldOrder) +{ + char buf[1024]; + size_t len; + len = blsGetCurveOrder(buf, sizeof(buf)); + CYBOZU_TEST_ASSERT(len > 0); + CYBOZU_TEST_EQUAL(buf, curveOrder); + len = blsGetFieldOrder(buf, sizeof(buf)); + CYBOZU_TEST_ASSERT(len > 0); + CYBOZU_TEST_EQUAL(buf, fieldOrder); +} + CYBOZU_TEST_AUTO(all) { const int tbl[] = { @@ -89,11 +101,22 @@ CYBOZU_TEST_AUTO(all) BlsCurveFp382_2 #endif }; + const char *curveOrderTbl[] = { + "16798108731015832284940804142231733909759579603404752749028378864165570215949", + "5540996953667913971058039301942914304734176495422447785042938606876043190415948413757785063597439175372845535461389", + "5541245505022739011583672869577435255026888277144126952448297309161979278754528049907713682488818304329661351460877", + }; + const char *fieldOrderTbl[] = { + "16798108731015832284940804142231733909889187121439069848933715426072753864723", + "5540996953667913971058039301942914304734176495422447785045292539108217242186829586959562222833658991069414454984723", + "5541245505022739011583672869577435255026888277144126952450651294188487038640194767986566260919128250811286032482323", + }; for (size_t i = 0; i < sizeof(tbl) / sizeof(tbl[0]); i++) { printf("i=%d\n", (int)i); blsInit(tbl[i], BLS_MAX_OP_UNIT_SIZE); bls_ifTest(); bls_if_use_stackTest(); bls_ifDataTest(); + bls_ifOrderTest(curveOrderTbl[i], fieldOrderTbl[i]); } } |