aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-05-05 07:07:47 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-05-05 07:07:47 +0800
commitb85df3bdcea93108a0c1c12e9471ba3e700329f6 (patch)
treec2c70171360cfd345e982494361edfe589bd18ab
parente75cbb14166d3b418d84cd5924d8445c96e849a8 (diff)
downloaddexon-bls-b85df3bdcea93108a0c1c12e9471ba3e700329f6.tar
dexon-bls-b85df3bdcea93108a0c1c12e9471ba3e700329f6.tar.gz
dexon-bls-b85df3bdcea93108a0c1c12e9471ba3e700329f6.tar.bz2
dexon-bls-b85df3bdcea93108a0c1c12e9471ba3e700329f6.tar.lz
dexon-bls-b85df3bdcea93108a0c1c12e9471ba3e700329f6.tar.xz
dexon-bls-b85df3bdcea93108a0c1c12e9471ba3e700329f6.tar.zst
dexon-bls-b85df3bdcea93108a0c1c12e9471ba3e700329f6.zip
change api of GetStr/SetStr
-rw-r--r--go/bls/bls.go52
-rw-r--r--go/bls/bls_test.go30
-rw-r--r--include/bls.hpp36
-rw-r--r--include/bls_if.h57
-rw-r--r--src/bls.cpp32
-rw-r--r--src/bls_if.cpp109
-rw-r--r--test/bls_if_test.cpp12
-rw-r--r--test/bls_test.cpp16
8 files changed, 150 insertions, 194 deletions
diff --git a/go/bls/bls.go b/go/bls/bls.go
index f4faf92..a62cddd 100644
--- a/go/bls/bls.go
+++ b/go/bls/bls.go
@@ -70,11 +70,11 @@ func (id *ID) getPointer() (p *C.blsId) {
return (*C.blsId)(unsafe.Pointer(&id.v[0]))
}
-// String --
-func (id *ID) String() string {
+// GetString --
+func (id *ID) GetString(ioMode int) string {
buf := make([]byte, 1024)
// #nosec
- n := C.blsIdGetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ n := C.blsIdGetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
if n == 0 {
panic("implementation err. size of buf is small")
}
@@ -82,10 +82,10 @@ func (id *ID) String() string {
}
// SetStr --
-func (id *ID) SetStr(s string) error {
+func (id *ID) SetStr(s string, ioMode int) error {
buf := []byte(s)
// #nosec
- err := C.blsIdSetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ err := C.blsIdSetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
if err > 0 {
return fmt.Errorf("bad string:%s", s)
}
@@ -113,11 +113,11 @@ func (sec *SecretKey) getPointer() (p *C.blsSecretKey) {
return (*C.blsSecretKey)(unsafe.Pointer(&sec.v[0]))
}
-// String --
-func (sec *SecretKey) String() string {
+// GetString --
+func (sec *SecretKey) GetString(ioMode int) string {
buf := make([]byte, 1024)
// #nosec
- n := C.blsSecretKeyGetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ n := C.blsSecretKeyGetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
if n == 0 {
panic("implementation err. size of buf is small")
}
@@ -125,10 +125,10 @@ func (sec *SecretKey) String() string {
}
// SetStr -- The string passed in is a number and can be either hex or decimal
-func (sec *SecretKey) SetStr(s string) error {
+func (sec *SecretKey) SetStr(s string, ioMode int) error {
buf := []byte(s)
// #nosec
- err := C.blsSecretKeySetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ err := C.blsSecretKeySetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
if err > 0 {
return fmt.Errorf("bad string:%s", s)
}
@@ -138,7 +138,7 @@ func (sec *SecretKey) SetStr(s string) error {
// SetData --
func (sec *SecretKey) SetData(buf []byte) error {
// #nosec
- err := C.blsSecretKeySetData(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ err := C.blsSecretKeySetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.BlsIoEcComp)
if err > 0 {
return fmt.Errorf("bad buf:%s", buf)
}
@@ -150,7 +150,7 @@ func (sec *SecretKey) GetData() []byte {
fpSize := GetOpUnitSize() * 8
buf := make([]byte, fpSize)
// #nosec
- n := C.blsSecretKeyGetData(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ n := C.blsSecretKeyGetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.BlsIoEcComp)
if n != C.size_t(fpSize) {
panic("implementation err. size of buf is small")
}
@@ -230,11 +230,11 @@ func (pub *PublicKey) getPointer() (p *C.blsPublicKey) {
return (*C.blsPublicKey)(unsafe.Pointer(&pub.v[0]))
}
-// String --
-func (pub *PublicKey) String() string {
+// GetString --
+func (pub *PublicKey) GetString(ioMode int) string {
buf := make([]byte, 1024)
// #nosec
- n := C.blsPublicKeyGetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ n := C.blsPublicKeyGetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
if n == 0 {
panic("implementation err. size of buf is small")
}
@@ -242,10 +242,10 @@ func (pub *PublicKey) String() string {
}
// SetStr --
-func (pub *PublicKey) SetStr(s string) error {
+func (pub *PublicKey) SetStr(s string, ioMode int) error {
buf := []byte(s)
// #nosec
- err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
if err > 0 {
return fmt.Errorf("bad string:%s", s)
}
@@ -255,7 +255,7 @@ func (pub *PublicKey) SetStr(s string) error {
// SetData --
func (pub *PublicKey) SetData(buf []byte) error {
// #nosec
- err := C.blsPublicKeySetData(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.BlsIoEcComp)
if err > 0 {
return fmt.Errorf("bad buf:%s", buf)
}
@@ -267,7 +267,7 @@ func (pub *PublicKey) GetData() []byte {
fpSize := GetOpUnitSize() * 8
buf := make([]byte, fpSize*2)
// #nosec
- n := C.blsPublicKeyGetData(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ n := C.blsPublicKeyGetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.BlsIoEcComp)
if n != C.size_t(fpSize*2) {
panic("implementation err. size of buf is small")
}
@@ -305,11 +305,11 @@ func (sign *Sign) getPointer() (p *C.blsSign) {
return (*C.blsSign)(unsafe.Pointer(&sign.v[0]))
}
-// String --
-func (sign *Sign) String() string {
+// GetString --
+func (sign *Sign) GetString(ioMode int) string {
buf := make([]byte, 1024)
// #nosec
- n := C.blsSignGetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ n := C.blsSignGetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
if n == 0 {
panic("implementation err. size of buf is small")
}
@@ -317,10 +317,10 @@ func (sign *Sign) String() string {
}
// SetStr --
-func (sign *Sign) SetStr(s string) error {
+func (sign *Sign) SetStr(s string, ioMode int) error {
buf := []byte(s)
// #nosec
- err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
if err > 0 {
return fmt.Errorf("bad string:%s", s)
}
@@ -330,7 +330,7 @@ func (sign *Sign) SetStr(s string) error {
// SetData --
func (sign *Sign) SetData(buf []byte) error {
// #nosec
- err := C.blsSignSetData(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.BlsIoEcComp)
if err > 0 {
return fmt.Errorf("bad buf:%s", buf)
}
@@ -342,7 +342,7 @@ func (sign *Sign) GetData() []byte {
fpSize := GetOpUnitSize() * 8
buf := make([]byte, fpSize)
// #nosec
- n := C.blsSignGetData(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ n := C.blsSignGetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.BlsIoEcComp)
if n != C.size_t(fpSize) {
panic("implementation err. size of buf is small")
}
diff --git a/go/bls/bls_test.go b/go/bls/bls_test.go
index 3a62e93..1a325d1 100644
--- a/go/bls/bls_test.go
+++ b/go/bls/bls_test.go
@@ -15,7 +15,7 @@ func testPre(t *testing.T) {
t.Log("id :", id)
var id2 ID
- err := id2.SetStr(id.String())
+ err := id2.SetStr(id.GetString(10), 10)
if err != nil {
t.Fatal(err)
}
@@ -46,7 +46,7 @@ func testPre(t *testing.T) {
sec := make([]SecretKey, 3)
for i := 0; i < len(sec); i++ {
sec[i].Init()
- t.Log("sec=", sec[i].String())
+ t.Log("sec=", sec[i].GetString(16))
}
}
}
@@ -60,17 +60,13 @@ func testStringConversion(t *testing.T) {
} else {
s = "40804142231733909759579603404752749028378864165570215949"
}
- err := sec.SetStr(s)
+ err := sec.SetStr(s, 10)
if err != nil {
t.Fatal(err)
}
- t.Log("String: ", s, sec.String())
- s = "401035055535747319451436327113007154621327258807739504261475863403006987855"
- err = sec.SetStr(s)
- if err != nil {
- t.Fatal(err)
+ if s != sec.GetString(10) {
+ t.Error("not equal")
}
- t.Log("String: ", s, sec.String())
}
func testRecoverSecretKey(t *testing.T) {
@@ -92,8 +88,8 @@ func testRecoverSecretKey(t *testing.T) {
// recover sec2 from secVec and idVec
var sec2 SecretKey
sec2.Recover(secVec, idVec)
- if sec.String() != sec2.String() {
- t.Errorf("Mismatch in recovered secret key:\n %s\n %s.", sec.String(), sec2.String())
+ if sec.GetString(16) != sec2.GetString(16) {
+ t.Errorf("Mismatch in recovered secret key:\n %s\n %s.", sec.GetString(16), sec2.GetString(16))
}
}
@@ -123,14 +119,14 @@ func testSign(t *testing.T) {
for i := 0; i < n; i++ {
idVec[i].Set([]uint64{idTbl[i], 0, 0, 0, 0, 0}[0:unitN])
- t.Logf("idVec[%d]=%s\n", i, idVec[i].String())
+ t.Logf("idVec[%d]=%s\n", i, idVec[i].GetString(16))
secVec[i].Set(msk, &idVec[i])
pubVec[i].Set(mpk, &idVec[i])
- t.Logf("pubVec[%d]=%s\n", i, pubVec[i].String())
+ t.Logf("pubVec[%d]=%s\n", i, pubVec[i].GetString(16))
- if pubVec[i].String() != secVec[i].GetPublicKey().String() {
+ if pubVec[i].GetString(16) != secVec[i].GetPublicKey().GetString(16) {
t.Error("Pubkey derivation does not match")
}
@@ -141,17 +137,17 @@ func testSign(t *testing.T) {
}
var sec1 SecretKey
sec1.Recover(secVec, idVec)
- if sec0.String() != sec1.String() {
+ if sec0.GetString(16) != sec1.GetString(16) {
t.Error("Mismatch in recovered seckey.")
}
var pub1 PublicKey
pub1.Recover(pubVec, idVec)
- if pub0.String() != pub1.String() {
+ if pub0.GetString(16) != pub1.GetString(16) {
t.Error("Mismatch in recovered pubkey.")
}
var s1 Sign
s1.Recover(signVec, idVec)
- if s0.String() != s1.String() {
+ if s0.GetString(16) != s1.GetString(16) {
t.Error("Mismatch in recovered signature.")
}
}
diff --git a/include/bls.hpp b/include/bls.hpp
index 5cb4471..915e8bb 100644
--- a/include/bls.hpp
+++ b/include/bls.hpp
@@ -27,6 +27,14 @@ enum {
CurveFp382_2 = 2
};
+// same value with IoMode of mcl/op.hpp
+enum {
+ IoBin = 2, // binary number
+ IoDec = 10, // decimal number
+ IoHex = 16, // hexadecimal number
+ IoEcComp = 512 // fixed byte representation
+};
+
namespace impl {
struct SecretKey;
@@ -89,11 +97,8 @@ public:
bool operator!=(const Id& rhs) const { return !(*this == rhs); }
friend std::ostream& operator<<(std::ostream& os, const Id& id);
friend std::istream& operator>>(std::istream& is, Id& id);
- /*
- get tight repl
- */
- void getData(std::string& str) const;
- void setData(const std::string& str);
+ void getStr(std::string& str, int ioMode = 0) const;
+ void setStr(const std::string& str, int ioMode = 0);
bool isZero() const;
/*
set p[0, .., keySize)
@@ -117,11 +122,8 @@ public:
bool operator!=(const SecretKey& rhs) const { return !(*this == rhs); }
friend std::ostream& operator<<(std::ostream& os, const SecretKey& sec);
friend std::istream& operator>>(std::istream& is, SecretKey& sec);
- /*
- get tight repl
- */
- void getData(std::string& str) const;
- void setData(const std::string& str);
+ void getStr(std::string& str, int ioMode = 0) const;
+ void setStr(const std::string& str, int ioMode = 0);
/*
initialize secretKey with random number and set id = 0
*/
@@ -183,11 +185,8 @@ public:
bool operator!=(const PublicKey& rhs) const { return !(*this == rhs); }
friend std::ostream& operator<<(std::ostream& os, const PublicKey& pub);
friend std::istream& operator>>(std::istream& is, PublicKey& pub);
- /*
- get tight repl
- */
- void getData(std::string& str) const;
- void setData(const std::string& str);
+ void getStr(std::string& str, int ioMode = 0) const;
+ void setStr(const std::string& str, int ioMode = 0);
/*
set public for id from mpk
*/
@@ -224,11 +223,8 @@ public:
bool operator!=(const Sign& rhs) const { return !(*this == rhs); }
friend std::ostream& operator<<(std::ostream& os, const Sign& s);
friend std::istream& operator>>(std::istream& is, Sign& s);
- /*
- get tight repl
- */
- void getData(std::string& str) const;
- void setData(const std::string& str);
+ void getStr(std::string& str, int ioMode = 0) const;
+ void setStr(const std::string& str, int ioMode = 0);
bool verify(const PublicKey& pub, const std::string& m) const;
/*
verify self(pop) with pub
diff --git a/include/bls_if.h b/include/bls_if.h
index 2eb8f1b..313e78a 100644
--- a/include/bls_if.h
+++ b/include/bls_if.h
@@ -27,6 +27,14 @@ enum {
BlsCurveFp382_2 = 2
};
+// same value with bls.hpp
+enum {
+ BlsIoBin = 2, // binary number
+ BlsIoDec = 10, // decimal number
+ BlsIoHex = 16, // hexadecimal number
+ BlsIoEcComp = 512 // fixed byte representation
+};
+
typedef struct {
uint64_t buf[BLS_MAX_OP_UNIT_SIZE];
} blsId;
@@ -56,18 +64,20 @@ int blsGetFieldOrder(char *buf, size_t maxBufSize);
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);
// return 0 if success
-int blsIdSetStr(blsId *id, const char *buf, size_t bufSize);
+int blsIdSetStr(blsId *id, const char *buf, size_t bufSize, int ioMode);
-// return strlen(buf) if success else 0
-size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize);
+/*
+ return written byte size if ioMode = BlsIoComp
+ return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
+ return 0 otherwise
+*/
+size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize, int ioMode);
/*
access p[0], ..., p[3] if 256-bit curve
access p[0], ..., p[5] if 384-bit curve
@@ -76,18 +86,19 @@ 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
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);
-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);
+int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize, int ioMode);
+/*
+ return written byte size if ioMode = BlsIoComp
+ return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
+ return 0 otherwise
+*/
+size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize, int ioMode);
void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs);
void blsSecretKeyInit(blsSecretKey *sec);
@@ -99,30 +110,34 @@ 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);
-// return strlen(buf) if success else 0
-size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize);
+int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize, int ioMode);
+/*
+ return written byte size if ioMode = BlsIoComp
+ return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
+ return 0 otherwise
+*/
+size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize, int ioMode);
void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs);
void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id);
void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n);
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);
-// return strlen(buf) if success else 0
-size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize);
+int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, int ioMode);
+/*
+ return written byte size if ioMode = BlsIoComp
+ return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
+ return 0 otherwise
+*/
+size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize, int ioMode);
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 51c774b..dd8a1b2 100644
--- a/src/bls.cpp
+++ b/src/bls.cpp
@@ -239,13 +239,13 @@ std::istream& operator>>(std::istream& is, Id& id)
{
return is >> id.getInner().v;
}
-void Id::getData(std::string& str) const
+void Id::getStr(std::string& str, int ioMode) const
{
- getInner().v.getStr(str, mcl::IoEcComp);
+ getInner().v.getStr(str, ioMode);
}
-void Id::setData(const std::string& str)
+void Id::setStr(const std::string& str, int ioMode)
{
- getInner().v.setStr(str, mcl::IoEcComp);
+ getInner().v.setStr(str, ioMode);
}
bool Id::isZero() const
@@ -272,13 +272,13 @@ std::istream& operator>>(std::istream& os, Sign& s)
{
return os >> s.getInner().sHm;
}
-void Sign::getData(std::string& str) const
+void Sign::getStr(std::string& str, int ioMode) const
{
- getInner().sHm.getStr(str, mcl::IoEcComp);
+ getInner().sHm.getStr(str, ioMode);
}
-void Sign::setData(const std::string& str)
+void Sign::setStr(const std::string& str, int ioMode)
{
- getInner().sHm.setStr(str, mcl::IoEcComp);
+ getInner().sHm.setStr(str, ioMode);
}
bool Sign::verify(const PublicKey& pub, const std::string& m) const
@@ -347,13 +347,13 @@ std::istream& operator>>(std::istream& is, PublicKey& pub)
return is >> pub.getInner().sQ;
}
-void PublicKey::getData(std::string& str) const
+void PublicKey::getStr(std::string& str, int ioMode) const
{
- getInner().sQ.getStr(str, mcl::IoEcComp);
+ getInner().sQ.getStr(str, ioMode);
}
-void PublicKey::setData(const std::string& str)
+void PublicKey::setStr(const std::string& str, int ioMode)
{
- getInner().sQ.setStr(str, mcl::IoEcComp);
+ getInner().sQ.setStr(str, ioMode);
}
void PublicKey::set(const PublicKey *mpk, size_t k, const Id& id)
{
@@ -392,13 +392,13 @@ std::istream& operator>>(std::istream& is, SecretKey& sec)
{
return is >> sec.getInner().s;
}
-void SecretKey::getData(std::string& str) const
+void SecretKey::getStr(std::string& str, int ioMode) const
{
- getInner().s.getStr(str, mcl::IoEcComp);
+ getInner().s.getStr(str, ioMode);
}
-void SecretKey::setData(const std::string& str)
+void SecretKey::setStr(const std::string& str, int ioMode)
{
- getInner().s.setStr(str, mcl::IoEcComp);
+ getInner().s.setStr(str, ioMode);
}
void SecretKey::init()
diff --git a/src/bls_if.cpp b/src/bls_if.cpp
index 7d2d78a..c495818 100644
--- a/src/bls_if.cpp
+++ b/src/bls_if.cpp
@@ -15,11 +15,10 @@ Outer *createT()
}
template<class Inner, class Outer>
-int setStrT(Outer *p, const char *buf, size_t bufSize)
+int setStrT(Outer *p, const char *buf, size_t bufSize, int ioMode)
try
{
- std::istringstream iss(std::string(buf, bufSize));
- iss >> *(Inner*)p;
+ ((Inner*)p)->setStr(std::string(buf, bufSize), ioMode);
return 0;
} catch (std::exception& e) {
fprintf(stderr, "err setStrT %s\n", e.what());
@@ -28,48 +27,30 @@ int setStrT(Outer *p, const char *buf, size_t bufSize)
size_t checkAndCopy(char *buf, size_t maxBufSize, const std::string& s)
{
- if (s.size() >= maxBufSize) {
+ if (s.size() > maxBufSize + 1) {
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
-{
- std::ostringstream oss;
- oss << *(const Inner*)p;
- std::string s = oss.str();
- return checkAndCopy(buf, maxBufSize, s);
-} catch (std::exception&) {
- 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)
+size_t getStrT(const Outer *p, char *buf, size_t maxBufSize, int ioMode)
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);
+ ((const Inner*)p)->getStr(s, ioMode);
+ size_t terminate = 0;
+ if (ioMode == 0 || ioMode == BlsIoBin || ioMode == BlsIoDec || ioMode == BlsIoHex) {
+ terminate = 1; // for '\0'
+ }
+ if (s.size() > maxBufSize + terminate) {
return 0;
}
memcpy(buf, s.c_str(), s.size());
+ if (terminate) {
+ buf[s.size()] = '\0';
+ }
return s.size();
} catch (std::exception&) {
return 0;
@@ -113,14 +94,6 @@ 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;
@@ -134,14 +107,14 @@ void blsIdCopy(blsId *dst, const blsId *src)
*((bls::Id*)dst) = *((const bls::Id*)src);
}
-int blsIdSetStr(blsId *id, const char *buf, size_t bufSize)
+int blsIdSetStr(blsId *id, const char *buf, size_t bufSize, int ioMode)
{
- return setStrT<bls::Id, blsId>(id, buf, bufSize);
+ return setStrT<bls::Id, blsId>(id, buf, bufSize, ioMode);
}
-size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize)
+size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize, int ioMode)
{
- return getStrT<bls::Id, blsId>(id, buf, maxBufSize);
+ return getStrT<bls::Id, blsId>(id, buf, maxBufSize, ioMode);
}
void blsIdSet(blsId *id, const uint64_t *p)
@@ -158,14 +131,6 @@ 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;
@@ -184,13 +149,13 @@ void blsSecretKeySetArray(blsSecretKey *sec, const uint64_t *p)
((bls::SecretKey*)sec)->set(p);
}
-int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize)
+int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize, int ioMode)
{
- return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize);
+ return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, ioMode);
}
-size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize)
+size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize, int ioMode)
{
- return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize);
+ return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, ioMode);
}
void blsSecretKeyInit(blsSecretKey *sec)
@@ -234,14 +199,6 @@ 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;
@@ -255,13 +212,13 @@ void blsPublicKeyPut(const blsPublicKey *pub)
std::cout << *(const bls::PublicKey*)pub << std::endl;
}
-int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize)
+int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize, int ioMode)
{
- return setStrT<bls::PublicKey, blsPublicKey>(pub, buf, bufSize);
+ return setStrT<bls::PublicKey, blsPublicKey>(pub, buf, bufSize, ioMode);
}
-size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize)
+size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize, int ioMode)
{
- return getStrT<bls::PublicKey, blsPublicKey>(pub, buf, maxBufSize);
+ return getStrT<bls::PublicKey, blsPublicKey>(pub, buf, maxBufSize, ioMode);
}
void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs)
{
@@ -285,14 +242,6 @@ 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;
@@ -306,13 +255,13 @@ void blsSignPut(const blsSign *sign)
std::cout << *(const bls::Sign*)sign << std::endl;
}
-int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize)
+int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, int ioMode)
{
- return setStrT<bls::Sign, blsSign>(sign, buf, bufSize);
+ return setStrT<bls::Sign, blsSign>(sign, buf, bufSize, ioMode);
}
-size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize)
+size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize, int ioMode)
{
- return getStrT<bls::Sign, blsSign>(sign, buf, maxBufSize);
+ return getStrT<bls::Sign, blsSign>(sign, buf, maxBufSize, ioMode);
}
void blsSignAdd(blsSign *sign, const blsSign *rhs)
{
diff --git a/test/bls_if_test.cpp b/test/bls_if_test.cpp
index face2fb..32a9f4e 100644
--- a/test/bls_if_test.cpp
+++ b/test/bls_if_test.cpp
@@ -59,23 +59,23 @@ void bls_ifDataTest()
char buf[BLS_MAX_OP_UNIT_SIZE * sizeof(uint64_t) * 2];
size_t n;
int ret;
- n = blsSecretKeyGetData(&sec1, buf, sizeof(buf));
+ n = blsSecretKeyGetStr(&sec1, buf, sizeof(buf), BlsIoEcComp);
CYBOZU_TEST_EQUAL(n, fpSize);
- ret = blsSecretKeySetData(&sec2, buf, n);
+ ret = blsSecretKeySetStr(&sec2, buf, n, BlsIoEcComp);
CYBOZU_TEST_EQUAL(ret, 0);
CYBOZU_TEST_ASSERT(blsSecretKeyIsSame(&sec1, &sec2));
blsPublicKey pub1, pub2;
blsSecretKeyGetPublicKey(&sec1, &pub1);
- n = blsPublicKeyGetData(&pub1, buf, sizeof(buf));
+ n = blsPublicKeyGetStr(&pub1, buf, sizeof(buf), BlsIoEcComp);
CYBOZU_TEST_EQUAL(n, fpSize * 2);
- ret = blsPublicKeySetData(&pub2, buf, n);
+ ret = blsPublicKeySetStr(&pub2, buf, n, BlsIoEcComp);
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));
+ n = blsSignGetStr(&sign1, buf, sizeof(buf), BlsIoEcComp);
CYBOZU_TEST_EQUAL(n, fpSize);
- ret = blsSignSetData(&sign2, buf, n);
+ ret = blsSignSetStr(&sign2, buf, n, BlsIoEcComp);
CYBOZU_TEST_EQUAL(ret, 0);
CYBOZU_TEST_ASSERT(blsSignIsSame(&sign1, &sign2));
}
diff --git a/test/bls_test.cpp b/test/bls_test.cpp
index 69d246f..bc11611 100644
--- a/test/bls_test.cpp
+++ b/test/bls_test.cpp
@@ -350,40 +350,40 @@ void dataTest()
bls::SecretKey sec;
sec.init();
std::string str;
- sec.getData(str);
+ sec.getStr(str, bls::IoEcComp);
{
CYBOZU_TEST_EQUAL(str.size(), size);
bls::SecretKey sec2;
- sec2.setData(str);
+ sec2.setStr(str, bls::IoEcComp);
CYBOZU_TEST_EQUAL(sec, sec2);
}
bls::PublicKey pub;
sec.getPublicKey(pub);
- pub.getData(str);
+ pub.getStr(str, bls::IoEcComp);
{
CYBOZU_TEST_EQUAL(str.size(), size * 2);
bls::PublicKey pub2;
- pub2.setData(str);
+ pub2.setStr(str, bls::IoEcComp);
CYBOZU_TEST_EQUAL(pub, pub2);
}
std::string m = "abc";
bls::Sign sign;
sec.sign(sign, m);
- sign.getData(str);
+ sign.getStr(str, bls::IoEcComp);
{
CYBOZU_TEST_EQUAL(str.size(), size);
bls::Sign sign2;
- sign2.setData(str);
+ sign2.setStr(str, bls::IoEcComp);
CYBOZU_TEST_EQUAL(sign, sign2);
}
bls::Id id;
const uint64_t v[] = { 1, 2, 3, 4, 5, 6, };
id.set(v);
- id.getData(str);
+ id.getStr(str, bls::IoEcComp);
{
CYBOZU_TEST_EQUAL(str.size(), size);
bls::Id id2;
- id2.setData(str);
+ id2.setStr(str, bls::IoEcComp);
CYBOZU_TEST_EQUAL(id, id2);
}
}