aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2016-09-06 14:15:11 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2016-09-06 14:15:11 +0800
commit329b7e21ea915f4579581570ee6ae821eaeaef69 (patch)
treecccbba673d9529599754a544f78fa869edab01db
parent5d6ecea7ad17251f90fe4fc0a1f1937d0eac47b5 (diff)
downloaddexon-bls-329b7e21ea915f4579581570ee6ae821eaeaef69.tar
dexon-bls-329b7e21ea915f4579581570ee6ae821eaeaef69.tar.gz
dexon-bls-329b7e21ea915f4579581570ee6ae821eaeaef69.tar.bz2
dexon-bls-329b7e21ea915f4579581570ee6ae821eaeaef69.tar.lz
dexon-bls-329b7e21ea915f4579581570ee6ae821eaeaef69.tar.xz
dexon-bls-329b7e21ea915f4579581570ee6ae821eaeaef69.tar.zst
dexon-bls-329b7e21ea915f4579581570ee6ae821eaeaef69.zip
use defer
-rw-r--r--go/main.go11
-rw-r--r--include/bls_if.h11
-rw-r--r--src/bls_if.cpp25
3 files changed, 44 insertions, 3 deletions
diff --git a/go/main.go b/go/main.go
index 242aa62..f4ee030 100644
--- a/go/main.go
+++ b/go/main.go
@@ -13,18 +13,27 @@ func main() {
fmt.Println("init")
C.blsInit()
+ id := C.blsIdCreate()
+ defer C.blsIdDestroy(id)
+
+ C.blsIdSet(id, (*C.uint64_t)(unsafe.Pointer(&[]uint64{1, 2, 3, 4}[0])))
+ C.blsIdPut(id)
+
fmt.Println("create secret key")
sec := C.blsSecretKeyCreate()
+ defer C.blsSecretKeyDestroy(sec)
C.blsSecretKeyInit(sec)
C.blsSecretKeyPut(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")
@@ -34,6 +43,4 @@ func main() {
fmt.Println("verify:", C.blsSignVerify(sign, pub, (*C.char)(unsafe.Pointer(&msg[0])), C.size_t(len(msg))))
- C.blsPublicKeyDestroy(pub)
- C.blsSecretKeyDestroy(sec)
}
diff --git a/include/bls_if.h b/include/bls_if.h
index 479e535..ed05106 100644
--- a/include/bls_if.h
+++ b/include/bls_if.h
@@ -7,7 +7,7 @@
http://opensource.org/licenses/BSD-3-Clause
*/
-#include <stdint.h> // for uint64_t
+#include <stdint.h> // for uint64_t, uint8_t
#include <stdlib.h> // for size_t
#ifdef __cplusplus
@@ -25,6 +25,15 @@ 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);
+
+/*
+ return written size
+ otherwise 0
+*/
+size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize);
+
void blsIdSet(blsId *id, const uint64_t *p);
blsSecretKey* blsSecretKeyCreate(void);
diff --git a/src/bls_if.cpp b/src/bls_if.cpp
index 163996c..8dc99f8 100644
--- a/src/bls_if.cpp
+++ b/src/bls_if.cpp
@@ -1,6 +1,8 @@
#include "bls.hpp"
#include "bls_if.h"
#include <iostream>
+#include <sstream>
+#include <memory.h>
void blsInit(void)
{
@@ -26,6 +28,29 @@ void blsIdPut(const blsId *id)
std::cout << *(const bls::Id*)id << std::endl;
}
+int blsIdSetStr(blsId *id, const char *buf, size_t bufSize)
+ try
+{
+ std::istringstream iss(std::string(buf, bufSize));
+ iss >> *(bls::Id*)id;
+ return 0;
+} catch (std::exception& e) {
+ return 1;
+}
+
+size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize)
+ try
+{
+ std::ostringstream oss;
+ oss << *(const bls::Id*)id;
+ std::string s = oss.str();
+ if (s.size() > maxBufSize) return 0;
+ memcpy(buf, s.c_str(), s.size());
+ return s.size();
+} catch (std::exception& e) {
+ return 0;
+}
+
void blsIdSet(blsId *id, const uint64_t *p)
{
((bls::Id*)id)->set(p);