diff options
-rw-r--r-- | go/main.go | 153 | ||||
-rw-r--r-- | include/bls_if.h | 11 | ||||
-rw-r--r-- | src/bls_if.cpp | 116 |
3 files changed, 215 insertions, 65 deletions
@@ -47,9 +47,131 @@ func (id *BlsId) setStr(s string) { } func (id *BlsId) set(v []uint64) { + if len(v) != 4 { + fmt.Println("BlsId:set bad size", len(v)) + return + } C.blsIdSet(id.self, (*C.uint64_t)(unsafe.Pointer(&v[0]))) } +type BlsSecretKey struct { + self *C.blsSecretKey +} + +func destroyBlsSecretKey(p *BlsSecretKey) { + C.blsSecretKeyDestroy(p.self) +} + +func newBlsSecretKey() *BlsSecretKey { + p := new(BlsSecretKey) + p.self = C.blsSecretKeyCreate() + runtime.SetFinalizer(p, destroyBlsSecretKey) + return p +} + +func (sec *BlsSecretKey) String() string { + buf := make([]byte, 1024) + n := C.blsSecretKeyGetStr(sec.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if n == 0 { + return "err" + } + return string(buf[:n]) +} + +func (sec *BlsSecretKey) setStr(s string) { + buf := []byte(s) + err := C.blsSecretKeySetStr(sec.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if err > 0 { + fmt.Println("BlsSecretKey:SetStr", err) + } +} + +func (sec *BlsSecretKey) init() { + C.blsSecretKeyInit(sec.self) +} + +type BlsPublicKey struct { + self *C.blsPublicKey +} + +func destroyBlsPublicKey(p *BlsPublicKey) { + C.blsPublicKeyDestroy(p.self) +} + +func newBlsPublicKey() *BlsPublicKey { + p := new(BlsPublicKey) + p.self = C.blsPublicKeyCreate() + runtime.SetFinalizer(p, destroyBlsPublicKey) + return p +} + +func (pub *BlsPublicKey) String() string { + buf := make([]byte, 1024) + n := C.blsPublicKeyGetStr(pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if n == 0 { + return "err" + } + return string(buf[:n]) +} + +func (pub *BlsPublicKey) setStr(s string) { + buf := []byte(s) + err := C.blsPublicKeySetStr(pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if err > 0 { + fmt.Println("BlsPublicKey:SetStr", err) + } +} + +type BlsSign struct { + self *C.blsSign +} + +func destroyBlsSign(p *BlsSign) { + C.blsSignDestroy(p.self) +} + +func newBlsSign() *BlsSign { + p := new(BlsSign) + p.self = C.blsSignCreate() + runtime.SetFinalizer(p, destroyBlsSign) + return p +} + +func (sign *BlsSign) String() string { + buf := make([]byte, 1024) + n := C.blsSignGetStr(sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if n == 0 { + return "err" + } + return string(buf[:n]) +} + +func (sign *BlsSign) setStr(s string) { + buf := []byte(s) + err := C.blsSignSetStr(sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if err > 0 { + fmt.Println("BlsSign:SetStr", err) + } +} + +func (sec *BlsSecretKey) getPublicKey() (pub *BlsPublicKey) { + pub = newBlsPublicKey() + C.blsSecretKeyGetPublicKey(sec.self, pub.self) + return pub +} + +func (sec *BlsSecretKey) sign(m string) (sign *BlsSign) { + sign = newBlsSign() + buf := []byte(m) + C.blsSecretKeySign(sec.self, sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + return sign +} + +func (sign *BlsSign) verify(pub *BlsPublicKey, m string) bool { + buf := []byte(m) + return C.blsSignVerify(sign.self, pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) == 1 +} + func main() { fmt.Println("init") BlsInit() @@ -63,27 +185,14 @@ func main() { } fmt.Println("create secret key") - sec := C.blsSecretKeyCreate() - defer C.blsSecretKeyDestroy(sec) - C.blsSecretKeyInit(sec) - C.blsSecretKeyPut(sec) - + m := "this is a bls sample for go" + sec := newBlsSecretKey() + sec.init() + fmt.Println("sec:", sec) fmt.Println("create public key") - pub := C.blsPublicKeyCreate() - defer C.blsPublicKeyDestroy(pub) - C.blsSecretKeyGetPublicKey(sec, pub) - - C.blsPublicKeyPut(pub) - - sign := C.blsSignCreate() - defer C.blsSignDestroy(sign) - - msg := []byte("Hello bls") - fmt.Println("sign message") - C.blsSecretKeySign(sec, sign, (*C.char)(unsafe.Pointer(&msg[0])), C.size_t(len(msg))) - - C.blsSignPut(sign) - - fmt.Println("verify:", C.blsSignVerify(sign, pub, (*C.char)(unsafe.Pointer(&msg[0])), C.size_t(len(msg)))) - + pub := sec.getPublicKey() + fmt.Println("pub:", pub) + sign := sec.sign(m) + fmt.Println("sign:", sign) + fmt.Println("verify:", sign.verify(pub, m)) } diff --git a/include/bls_if.h b/include/bls_if.h index ed05106..ac68495 100644 --- a/include/bls_if.h +++ b/include/bls_if.h @@ -23,7 +23,6 @@ void blsInit(void); blsId *blsIdCreate(void); void blsIdDestroy(blsId *id); -void blsIdPut(const blsId *id); // return 0 if success int blsIdSetStr(blsId *id, const char *buf, size_t bufSize); @@ -33,12 +32,16 @@ int blsIdSetStr(blsId *id, const char *buf, size_t bufSize); otherwise 0 */ size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize); - +/* + access p[0], p[1], p[2], p[3] +*/ void blsIdSet(blsId *id, const uint64_t *p); blsSecretKey* blsSecretKeyCreate(void); void blsSecretKeyDestroy(blsSecretKey *sec); void blsSecretKeyPut(const blsSecretKey *sec); +int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize); +size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize); void blsSecretKeyInit(blsSecretKey *sec); void blsSecretKeyGetPublicKey(const blsSecretKey *sec, blsPublicKey *pub); @@ -47,10 +50,14 @@ void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, siz blsPublicKey *blsPublicKeyCreate(void); void blsPublicKeyDestroy(blsPublicKey *pub); void blsPublicKeyPut(const blsPublicKey *pub); +int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize); +size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize); blsSign *blsSignCreate(void); void blsSignDestroy(blsSign *sign); void blsSignPut(const blsSign *sign); +int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize); +size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize); int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size); diff --git a/src/bls_if.cpp b/src/bls_if.cpp index 8dc99f8..8cd4916 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -4,65 +4,78 @@ #include <sstream> #include <memory.h> -void blsInit(void) -{ - bls::init(); -} - -blsId *blsIdCreate(void) +template<class Inner, class Outer> +Outer *createT() try { - return (blsId*)new bls::Id(); + return (Outer*)new Inner(); } catch (std::exception& e) { - fprintf(stderr, "err %s\n", e.what()); + fprintf(stderr, "err createT %s\n", e.what()); return NULL; } -void blsIdDestroy(blsId *id) -{ - delete (bls::Id*)id; -} - -void blsIdPut(const blsId *id) -{ - std::cout << *(const bls::Id*)id << std::endl; -} - -int blsIdSetStr(blsId *id, const char *buf, size_t bufSize) +template<class Inner, class Outer> +int setStrT(Outer *p, const char *buf, size_t bufSize) try { std::istringstream iss(std::string(buf, bufSize)); - iss >> *(bls::Id*)id; + iss >> *(Inner*)p; return 0; } catch (std::exception& e) { + fprintf(stderr, "err setStrT %s\n", e.what()); return 1; } -size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize) +template<class Inner, class Outer> +size_t getStrT(const Outer *p, char *buf, size_t maxBufSize) try { std::ostringstream oss; - oss << *(const bls::Id*)id; + oss << *(const Inner*)p; std::string s = oss.str(); - if (s.size() > maxBufSize) return 0; + 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(); } catch (std::exception& e) { return 0; } +void blsInit() +{ + bls::init(); +} + +blsId *blsIdCreate() +{ + return createT<bls::Id, blsId>(); +} + +void blsIdDestroy(blsId *id) +{ + delete (bls::Id*)id; +} + +int blsIdSetStr(blsId *id, const char *buf, size_t bufSize) +{ + return setStrT<bls::Id, blsId>(id, buf, bufSize); +} + +size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize) +{ + return getStrT<bls::Id, blsId>(id, buf, maxBufSize); +} + void blsIdSet(blsId *id, const uint64_t *p) { ((bls::Id*)id)->set(p); } -blsSecretKey* blsSecretKeyCreate(void) - try +blsSecretKey* blsSecretKeyCreate() { - return (blsSecretKey*)new bls::SecretKey(); -} catch (std::exception& e) { - fprintf(stderr, "err %s\n", e.what()); - return NULL; + return createT<bls::SecretKey, blsSecretKey>(); } void blsSecretKeyDestroy(blsSecretKey *sec) @@ -75,6 +88,15 @@ void blsSecretKeyPut(const blsSecretKey *sec) std::cout << *(const bls::SecretKey*)sec << std::endl; } +int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize) +{ + return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize); +} +size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize) +{ + return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize); +} + void blsSecretKeyInit(blsSecretKey *sec) { ((bls::SecretKey*)sec)->init(); @@ -89,14 +111,11 @@ void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, siz ((const bls::SecretKey*)sec)->sign(*(bls::Sign*)sign, std::string(m, size)); } -blsPublicKey *blsPublicKeyCreate(void) - try +blsPublicKey *blsPublicKeyCreate() { - return (blsPublicKey*)new bls::PublicKey(); -} catch (std::exception& e) { - fprintf(stderr, "err %s\n", e.what()); - return NULL; + return createT<bls::PublicKey, blsPublicKey>(); } + void blsPublicKeyDestroy(blsPublicKey *pub) { delete (bls::PublicKey*)pub; @@ -106,14 +125,20 @@ void blsPublicKeyPut(const blsPublicKey *pub) std::cout << *(const bls::PublicKey*)pub << std::endl; } -blsSign *blsSignCreate(void) - try +int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize) { - return (blsSign*)new bls::Sign(); -} catch (std::exception& e) { - fprintf(stderr, "err %s\n", e.what()); - return NULL; + return setStrT<bls::PublicKey, blsPublicKey>(pub, buf, bufSize); } +size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize) +{ + return getStrT<bls::PublicKey, blsPublicKey>(pub, buf, maxBufSize); +} + +blsSign *blsSignCreate() +{ + return createT<bls::Sign, blsSign>(); +} + void blsSignDestroy(blsSign *sign) { delete (bls::Sign*)sign; @@ -123,6 +148,15 @@ void blsSignPut(const blsSign *sign) std::cout << *(const bls::Sign*)sign << std::endl; } +int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize) +{ + return setStrT<bls::Sign, blsSign>(sign, buf, bufSize); +} +size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize) +{ + return getStrT<bls::Sign, blsSign>(sign, buf, maxBufSize); +} + int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size) { return ((const bls::Sign*)sign)->verify(*(const bls::PublicKey*)pub, std::string(m, size)); |