From b1584bf9e612698e2958542e2f2a3831a48f5a9e Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 10 May 2017 06:20:07 +0900 Subject: return error if an exception is thrown --- go/bls/bls.go | 56 ++++++++++++++++++++++++++++++++++++++++---------------- include/bls_if.h | 22 +++++++++++++++------- src/bls_if.cpp | 43 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 91 insertions(+), 30 deletions(-) diff --git a/go/bls/bls.go b/go/bls/bls.go index f931bbe..45408ac 100644 --- a/go/bls/bls.go +++ b/go/bls/bls.go @@ -24,8 +24,12 @@ const CurveFp382_2 = 2 // Init -- // call this function before calling all the other operations // this function is not thread safe -func Init(curve int) { - C.blsInit(C.int(curve), C.BLS_MAX_OP_UNIT_SIZE) +func Init(curve int) error { + err := C.blsInit(C.int(curve), C.BLS_MAX_OP_UNIT_SIZE) + if err != 0 { + return fmt.Errorf("ERR Init curve=%d\n", curve) + } + return nil } // GetMaxOpUnitSize -- @@ -86,7 +90,7 @@ func (id *ID) GetByte(ioMode int) []byte { func (id *ID) SetByte(buf []byte, ioMode int) error { // #nosec err := C.blsIdSetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode)) - if err > 0 { + if err != 0 { return fmt.Errorf("bad byte:%x", buf) } return nil @@ -163,7 +167,7 @@ func (sec *SecretKey) GetByte(ioMode int) []byte { func (sec *SecretKey) SetByte(buf []byte, ioMode int) error { // #nosec err := C.blsSecretKeySetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode)) - if err > 0 { + if err != 0 { return fmt.Errorf("bad byte:%x", buf) } return nil @@ -245,13 +249,21 @@ func GetMasterPublicKey(msk []SecretKey) (mpk []PublicKey) { } // Set -- -func (sec *SecretKey) Set(msk []SecretKey, id *ID) { - C.blsSecretKeySet(sec.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer()) +func (sec *SecretKey) Set(msk []SecretKey, id *ID) error { + err := C.blsSecretKeySet(sec.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer()) + if err != 0 { + return fmt.Errorf("SecretKey.Set") + } + return nil } // Recover -- -func (sec *SecretKey) Recover(secVec []SecretKey, idVec []ID) { - C.blsSecretKeyRecover(sec.getPointer(), secVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(secVec))) +func (sec *SecretKey) Recover(secVec []SecretKey, idVec []ID) error { + err := C.blsSecretKeyRecover(sec.getPointer(), secVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(secVec))) + if err != 0 { + return fmt.Errorf("SecretKey.Recover") + } + return nil } // GetPop -- @@ -287,7 +299,7 @@ func (pub *PublicKey) GetByte(ioMode int) []byte { func (pub *PublicKey) SetByte(buf []byte, ioMode int) error { // #nopub err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode)) - if err > 0 { + if err != 0 { return fmt.Errorf("bad byte:%x", buf) } return nil @@ -328,13 +340,21 @@ func (pub *PublicKey) Add(rhs *PublicKey) { } // Set -- -func (pub *PublicKey) Set(mpk []PublicKey, id *ID) { - C.blsPublicKeySet(pub.getPointer(), mpk[0].getPointer(), C.size_t(len(mpk)), id.getPointer()) +func (pub *PublicKey) Set(mpk []PublicKey, id *ID) error { + err := C.blsPublicKeySet(pub.getPointer(), mpk[0].getPointer(), C.size_t(len(mpk)), id.getPointer()) + if err != 0 { + return fmt.Errorf("PublicKey.set") + } + return nil } // Recover -- -func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []ID) { - C.blsPublicKeyRecover(pub.getPointer(), pubVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(pubVec))) +func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []ID) error { + err := C.blsPublicKeyRecover(pub.getPointer(), pubVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(pubVec))) + if err != 0 { + return fmt.Errorf("PublicKey.Recover") + } + return nil } // Sign -- @@ -363,7 +383,7 @@ func (sign *Sign) GetByte(ioMode int) []byte { func (sign *Sign) SetByte(buf []byte, ioMode int) error { // #nosign err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode)) - if err > 0 { + if err != 0 { return fmt.Errorf("bad byte:%x", buf) } return nil @@ -420,8 +440,12 @@ func (sign *Sign) Add(rhs *Sign) { } // Recover -- -func (sign *Sign) Recover(signVec []Sign, idVec []ID) { - C.blsSignRecover(sign.getPointer(), signVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(signVec))) +func (sign *Sign) Recover(signVec []Sign, idVec []ID) error { + err := C.blsSignRecover(sign.getPointer(), signVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(signVec))) + if err != 0 { + return fmt.Errorf("Sign.Recover") + } + return nil } // Verify -- diff --git a/include/bls_if.h b/include/bls_if.h index f8d174d..7d617d9 100644 --- a/include/bls_if.h +++ b/include/bls_if.h @@ -65,9 +65,10 @@ typedef struct { /* initialize this library call this once before using the other method + return 0 if success @note init() is not thread safe */ -BLS256_DLL_API void blsInit(int curve, int maxUnitSize); +BLS256_DLL_API int blsInit(int curve, int maxUnitSize); BLS256_DLL_API size_t blsGetOpUnitSize(void); // return strlen(buf) if success else 0 BLS256_DLL_API int blsGetCurveOrder(char *buf, size_t maxBufSize); @@ -103,6 +104,7 @@ BLS256_DLL_API int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKe BLS256_DLL_API void blsSecretKeyPut(const blsSecretKey *sec); BLS256_DLL_API void blsSecretKeyCopy(blsSecretKey *dst, const blsSecretKey *src); BLS256_DLL_API void blsSecretKeySetArray(blsSecretKey *sec, const uint64_t *p); +// return 0 if success BLS256_DLL_API int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize, int ioMode); /* return written byte size if ioMode = BlsIoComp @@ -115,8 +117,10 @@ BLS256_DLL_API void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs); BLS256_DLL_API void blsSecretKeyInit(blsSecretKey *sec); BLS256_DLL_API void blsSecretKeyGetPublicKey(const blsSecretKey *sec, blsPublicKey *pub); BLS256_DLL_API void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, size_t size); -BLS256_DLL_API void blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id); -BLS256_DLL_API void blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n); +// return 0 if success +BLS256_DLL_API int blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id); +// return 0 if success +BLS256_DLL_API int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n); BLS256_DLL_API void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign); BLS256_DLL_API blsPublicKey *blsPublicKeyCreate(void); @@ -125,6 +129,7 @@ BLS256_DLL_API void blsPublicKeyDestroy(blsPublicKey *pub); BLS256_DLL_API int blsPublicKeyIsSame(const blsPublicKey *lhs, const blsPublicKey *rhs); BLS256_DLL_API void blsPublicKeyPut(const blsPublicKey *pub); BLS256_DLL_API void blsPublicKeyCopy(blsPublicKey *dst, const blsPublicKey *src); +// return 0 if success BLS256_DLL_API int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize, int ioMode); /* return written byte size if ioMode = BlsIoComp @@ -133,8 +138,10 @@ BLS256_DLL_API int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t */ BLS256_DLL_API size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize, int ioMode); BLS256_DLL_API void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs); -BLS256_DLL_API void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id); -BLS256_DLL_API void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n); +// return 0 if success +BLS256_DLL_API int blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id); +// return 0 if success +BLS256_DLL_API int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n); BLS256_DLL_API blsSign *blsSignCreate(void); BLS256_DLL_API void blsSignDestroy(blsSign *sign); @@ -142,6 +149,7 @@ BLS256_DLL_API void blsSignDestroy(blsSign *sign); BLS256_DLL_API int blsSignIsSame(const blsSign *lhs, const blsSign *rhs); BLS256_DLL_API void blsSignPut(const blsSign *sign); BLS256_DLL_API void blsSignCopy(blsSign *dst, const blsSign *src); +// return 0 if success BLS256_DLL_API int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, int ioMode); /* return written byte size if ioMode = BlsIoComp @@ -150,8 +158,8 @@ BLS256_DLL_API int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, */ BLS256_DLL_API size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize, int ioMode); BLS256_DLL_API void blsSignAdd(blsSign *sign, const blsSign *rhs); -BLS256_DLL_API void blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n); - +// return 0 if success +BLS256_DLL_API int blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n); BLS256_DLL_API int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size); BLS256_DLL_API int blsSignVerifyPop(const blsSign *sign, const blsPublicKey *pub); diff --git a/src/bls_if.cpp b/src/bls_if.cpp index a9aec44..dca8289 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -23,7 +23,7 @@ int setStrT(Outer *p, const char *buf, size_t bufSize, int ioMode) return 0; } catch (std::exception& e) { fprintf(stderr, "err setStrT %s\n", e.what()); - return 1; + return -1; } size_t checkAndCopy(char *buf, size_t maxBufSize, const std::string& s) @@ -57,9 +57,13 @@ size_t getStrT(const Outer *p, char *buf, size_t maxBufSize, int ioMode) return 0; } -void blsInit(int curve, int maxUnitSize) +int blsInit(int curve, int maxUnitSize) + try { bls::init(curve, maxUnitSize); + return 0; +} catch (std::exception&) { + return -1; } size_t blsGetOpUnitSize() { @@ -176,14 +180,24 @@ void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, siz { ((const bls::SecretKey*)sec)->sign(*(bls::Sign*)sign, std::string(m, size)); } -void blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id) +int blsSecretKeySet(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()); + return -1; } -void blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n) +int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n) + try { ((bls::SecretKey*)sec)->recover((const bls::SecretKey *)secVec, (const bls::Id *)idVec, n); + return 0; +} catch (std::exception& e) { + fprintf(stderr, "err blsSecretKeyRecover %s\n", e.what()); + return -1; } void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign) @@ -225,13 +239,23 @@ void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs) { ((bls::PublicKey*)pub)->add(*(const bls::PublicKey*)rhs); } -void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id) +int blsPublicKeySet(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()); + return -1; } -void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n) +int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n) + try { ((bls::PublicKey*)pub)->recover((const bls::PublicKey*)pubVec, (const bls::Id*)idVec, n); + return 0; +} catch (std::exception& e) { + fprintf(stderr, "err blsPublicKeyRecover %s\n", e.what()); + return -1; } blsSign *blsSignCreate() @@ -268,9 +292,14 @@ void blsSignAdd(blsSign *sign, const blsSign *rhs) { ((bls::Sign*)sign)->add(*(const bls::Sign*)rhs); } -void blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n) +int blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n) + try { ((bls::Sign*)sign)->recover((const bls::Sign*)signVec, (const bls::Id*)idVec, n); + return 0; +} catch (std::exception& e) { + fprintf(stderr, "err blsSignRecover %s\n", e.what()); + return -1; } int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size) -- cgit v1.2.3