aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-05-29 05:31:17 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-05-29 05:31:17 +0800
commit14c6246a6ca198a89498d766641b07230e9c647d (patch)
tree4b8ebda7c87269b300429f91991d1d4119cb8ecd
parentd6de132a2d40e79eaa944999882c484015c3261a (diff)
downloaddexon-bls-14c6246a6ca198a89498d766641b07230e9c647d.tar
dexon-bls-14c6246a6ca198a89498d766641b07230e9c647d.tar.gz
dexon-bls-14c6246a6ca198a89498d766641b07230e9c647d.tar.bz2
dexon-bls-14c6246a6ca198a89498d766641b07230e9c647d.tar.lz
dexon-bls-14c6246a6ca198a89498d766641b07230e9c647d.tar.xz
dexon-bls-14c6246a6ca198a89498d766641b07230e9c647d.tar.zst
dexon-bls-14c6246a6ca198a89498d766641b07230e9c647d.zip
update api
-rw-r--r--go/bls/bls.go216
-rw-r--r--go/bls/bls_test.go76
-rw-r--r--include/bls/bls.hpp11
-rw-r--r--include/bls/bls_if.h141
-rw-r--r--sample/bls_smpl.cpp2
-rw-r--r--src/bls.cpp9
-rw-r--r--src/bls_if.cpp220
-rw-r--r--test/bls_if_test.cpp65
-rw-r--r--test/bls_test.cpp16
9 files changed, 370 insertions, 386 deletions
diff --git a/go/bls/bls.go b/go/bls/bls.go
index 087b386..f342615 100644
--- a/go/bls/bls.go
+++ b/go/bls/bls.go
@@ -75,55 +75,69 @@ func (id *ID) getPointer() (p *C.blsId) {
return (*C.blsId)(unsafe.Pointer(&id.v[0]))
}
-// GetByte --
-func (id *ID) GetByte(ioMode int) []byte {
+// GetLittleEndian --
+func (id *ID) GetLittleEndian() []byte {
buf := make([]byte, 1024)
// #nosec
- n := C.blsIdGetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
+ n := C.blsIdGetLittleEndian(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), id.getPointer())
if n == 0 {
- panic("implementation err. size of buf is small")
+ panic("err blsIdGetLittleEndian")
}
return buf[:n]
}
-// SetByte --
-func (id *ID) SetByte(buf []byte, ioMode int) error {
+// SetLittleEndian --
+func (id *ID) SetLittleEndian(buf []byte) error {
// #nosec
- err := C.blsIdSetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
+ err := C.blsIdSetLittleEndian(id.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
if err != 0 {
- return fmt.Errorf("bad byte:%x", buf)
+ return fmt.Errorf("err blsIdSetLittleEndian %x", err)
}
return nil
}
-// Serialize --
-func (id *ID) Serialize() []byte {
- return id.GetByte(C.blsIoEcComp)
-}
-
-// Deserialize --
-func (id *ID) Deserialize(b []byte) error {
- return id.SetByte(b, C.blsIoEcComp)
-}
-
// GetHexString --
func (id *ID) GetHexString() string {
- return string(id.GetByte(16))
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.blsIdGetHexStr((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), id.getPointer())
+ if n == 0 {
+ panic("err blsIdGetHexStr")
+ }
+ return string(buf[:n])
}
// GetDecString --
func (id *ID) GetDecString() string {
- return string(id.GetByte(10))
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.blsIdGetDecStr((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), id.getPointer())
+ if n == 0 {
+ panic("err blsIdGetDecStr")
+ }
+ return string(buf[:n])
}
// SetHexString --
func (id *ID) SetHexString(s string) error {
- return id.SetByte([]byte(s), 16)
+ buf := []byte(s)
+ // #nosec
+ err := C.blsIdSetHexStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err blsIdSetHexStr %x", err)
+ }
+ return nil
}
// SetDecString --
func (id *ID) SetDecString(s string) error {
- return id.SetByte([]byte(s), 10)
+ buf := []byte(s)
+ // #nosec
+ err := C.blsIdSetDecStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("err blsIdSetDecStr %x", buf)
+ }
+ return nil
}
// IsSame --
@@ -131,16 +145,6 @@ func (id *ID) IsSame(rhs *ID) bool {
return C.blsIdIsSame(id.getPointer(), rhs.getPointer()) == 1
}
-// Set --
-func (id *ID) Set(v []uint64) {
- expect := GetOpUnitSize()
- if len(v) != expect {
- panic(fmt.Errorf("bad size (%d), expected size %d", len(v), expect))
- }
- // #nosec
- C.blsIdSet(id.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0])))
-}
-
// SecretKey --
type SecretKey struct {
v [C.BLS_MAX_OP_UNIT_SIZE]C.uint64_t
@@ -152,55 +156,69 @@ func (sec *SecretKey) getPointer() (p *C.blsSecretKey) {
return (*C.blsSecretKey)(unsafe.Pointer(&sec.v[0]))
}
-// GetByte --
-func (sec *SecretKey) GetByte(ioMode int) []byte {
+// GetLittleEndian --
+func (sec *SecretKey) GetLittleEndian() []byte {
buf := make([]byte, 1024)
// #nosec
- n := C.blsSecretKeyGetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
+ n := C.blsSecretKeyGetLittleEndian(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), sec.getPointer())
if n == 0 {
- panic("implementation err. size of buf is small")
+ panic("err blsSecretKeyGetLittleEndian")
}
return buf[:n]
}
-// SetByte --
-func (sec *SecretKey) SetByte(buf []byte, ioMode int) error {
+// SetLittleEndian --
+func (sec *SecretKey) SetLittleEndian(buf []byte) error {
// #nosec
- err := C.blsSecretKeySetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
+ err := C.blsSecretKeySetLittleEndian(sec.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
if err != 0 {
- return fmt.Errorf("bad byte:%x", buf)
+ return fmt.Errorf("err blsSecretKeySetLittleEndian %x", buf)
}
return nil
}
-// Serialize --
-func (sec *SecretKey) Serialize() []byte {
- return sec.GetByte(C.blsIoEcComp)
-}
-
-// Deserialize --
-func (sec *SecretKey) Deserialize(b []byte) error {
- return sec.SetByte(b, C.blsIoEcComp)
-}
-
// GetHexString --
func (sec *SecretKey) GetHexString() string {
- return string(sec.GetByte(16))
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.blsSecretKeyGetHexStr((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), sec.getPointer())
+ if n == 0 {
+ panic("err blsSecretKeyGetHexStr")
+ }
+ return string(buf[:n])
}
// GetDecString --
func (sec *SecretKey) GetDecString() string {
- return string(sec.GetByte(10))
+ buf := make([]byte, 1024)
+ // #nosec
+ n := C.blsSecretKeyGetDecStr((*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), sec.getPointer())
+ if n == 0 {
+ panic("err blsSecretKeyGetDecStr")
+ }
+ return string(buf[:n])
}
// SetHexString --
func (sec *SecretKey) SetHexString(s string) error {
- return sec.SetByte([]byte(s), 16)
+ buf := []byte(s)
+ // #nosec
+ err := C.blsSecretKeySetHexStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("erre blsSecretKeySetHexStr %s", s)
+ }
+ return nil
}
// SetDecString --
func (sec *SecretKey) SetDecString(s string) error {
- return sec.SetByte([]byte(s), 10)
+ buf := []byte(s)
+ // #nosec
+ err := C.blsSecretKeySetDecStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ if err != 0 {
+ return fmt.Errorf("erre blsSecretKeySetDecStr %s", s)
+ }
+ return nil
}
// IsSame --
@@ -208,19 +226,9 @@ func (sec *SecretKey) IsSame(rhs *SecretKey) bool {
return C.blsSecretKeyIsSame(sec.getPointer(), rhs.getPointer()) == 1
}
-// SetArray --
-func (sec *SecretKey) SetArray(v []uint64) {
- expect := GetOpUnitSize()
- if len(v) != expect {
- panic(fmt.Errorf("bad size (%d), expected size %d", len(v), expect))
- }
- // #nosec
- C.blsSecretKeySetArray(sec.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0])))
-}
-
// Init --
func (sec *SecretKey) Init() {
- C.blsSecretKeyInit(sec.getPointer())
+ C.blsSecretKeySetByCSPRNG(sec.getPointer())
}
// Add --
@@ -250,9 +258,9 @@ func GetMasterPublicKey(msk []SecretKey) (mpk []PublicKey) {
// Set --
func (sec *SecretKey) Set(msk []SecretKey, id *ID) error {
- err := C.blsSecretKeySet(sec.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer())
+ err := C.blsSecretKeyShare(sec.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer())
if err != 0 {
- return fmt.Errorf("SecretKey.Set")
+ return fmt.Errorf("err blsSecretKeyShare id %s", id.GetHexString())
}
return nil
}
@@ -269,7 +277,7 @@ func (sec *SecretKey) Recover(secVec []SecretKey, idVec []ID) error {
// GetPop --
func (sec *SecretKey) GetPop() (sign *Sign) {
sign = new(Sign)
- C.blsSecretKeyGetPop(sec.getPointer(), sign.getPointer())
+ C.blsGetPop(sign.getPointer(), sec.getPointer())
return sign
}
@@ -284,37 +292,27 @@ func (pub *PublicKey) getPointer() (p *C.blsPublicKey) {
return (*C.blsPublicKey)(unsafe.Pointer(&pub.v[0]))
}
-// GetByte --
-func (pub *PublicKey) GetByte(ioMode int) []byte {
+// Serialize --
+func (pub *PublicKey) Serialize() []byte {
buf := make([]byte, 1024)
// #nosec
- n := C.blsPublicKeyGetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
+ n := C.blsPublicKeySerialize(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), pub.getPointer())
if n == 0 {
- panic("implementation err. size of buf is small")
+ panic("err blsPublicKeySerialize")
}
return buf[:n]
}
-// SetByte --
-func (pub *PublicKey) SetByte(buf []byte, ioMode int) error {
+// Deserialize --
+func (pub *PublicKey) Deserialize(buf []byte) error {
// #nosec
- err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
+ err := C.blsPublicKeyDeserialize(pub.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
if err != 0 {
- return fmt.Errorf("bad byte:%x", buf)
+ return fmt.Errorf("err blsPublicKeyDeserialize %x", buf)
}
return nil
}
-// Serialize --
-func (pub *PublicKey) Serialize() []byte {
- return pub.GetByte(C.blsIoEcComp)
-}
-
-// Deserialize --
-func (pub *PublicKey) Deserialize(b []byte) error {
- return pub.SetByte(b, C.blsIoEcComp)
-}
-
// GetHexString --
func (pub *PublicKey) GetHexString() string {
return fmt.Sprintf("%x", pub.Serialize())
@@ -341,7 +339,7 @@ func (pub *PublicKey) Add(rhs *PublicKey) {
// Set --
func (pub *PublicKey) Set(mpk []PublicKey, id *ID) error {
- err := C.blsPublicKeySet(pub.getPointer(), mpk[0].getPointer(), C.size_t(len(mpk)), id.getPointer())
+ err := C.blsPublicKeyShare(pub.getPointer(), mpk[0].getPointer(), C.size_t(len(mpk)), id.getPointer())
if err != 0 {
return fmt.Errorf("PublicKey.set")
}
@@ -363,42 +361,32 @@ type Sign struct {
}
// getPointer --
-func (sign *Sign) getPointer() (p *C.blsSign) {
+func (sign *Sign) getPointer() (p *C.blsSignature) {
// #nosec
- return (*C.blsSign)(unsafe.Pointer(&sign.v[0]))
+ return (*C.blsSignature)(unsafe.Pointer(&sign.v[0]))
}
-// GetByte --
-func (sign *Sign) GetByte(ioMode int) []byte {
+// Serialize --
+func (sign *Sign) Serialize() []byte {
buf := make([]byte, 1024)
// #nosec
- n := C.blsSignGetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
+ n := C.blsSignatureSerialize(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), sign.getPointer())
if n == 0 {
- panic("implementation err. size of buf is small")
+ panic("err blsSignatureSerialize")
}
return buf[:n]
}
-// SetByte --
-func (sign *Sign) SetByte(buf []byte, ioMode int) error {
+// Deserialize --
+func (sign *Sign) Deserialize(buf []byte) error {
// #nosec
- err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)), C.int(ioMode))
+ err := C.blsSignatureDeserialize(sign.getPointer(), unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
if err != 0 {
- return fmt.Errorf("bad byte:%x", buf)
+ return fmt.Errorf("err blsSignatureDeserialize %x", buf)
}
return nil
}
-// Serialize --
-func (sign *Sign) Serialize() []byte {
- return sign.GetByte(C.blsIoEcComp)
-}
-
-// Deserialize --
-func (sign *Sign) Deserialize(b []byte) error {
- return sign.SetByte(b, C.blsIoEcComp)
-}
-
// GetHexString --
func (sign *Sign) GetHexString() string {
return fmt.Sprintf("%x", sign.Serialize())
@@ -415,13 +403,13 @@ func (sign *Sign) SetHexString(s string) error {
// IsSame --
func (sign *Sign) IsSame(rhs *Sign) bool {
- return C.blsSignIsSame(sign.getPointer(), rhs.getPointer()) == 1
+ return C.blsSignatureIsSame(sign.getPointer(), rhs.getPointer()) == 1
}
// GetPublicKey --
func (sec *SecretKey) GetPublicKey() (pub *PublicKey) {
pub = new(PublicKey)
- C.blsSecretKeyGetPublicKey(sec.getPointer(), pub.getPointer())
+ C.blsGetPublicKey(pub.getPointer(), sec.getPointer())
return pub
}
@@ -430,18 +418,18 @@ func (sec *SecretKey) Sign(m string) (sign *Sign) {
sign = new(Sign)
buf := []byte(m)
// #nosec
- C.blsSecretKeySign(sec.getPointer(), sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
+ C.blsSign(sign.getPointer(), sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
return sign
}
// Add --
func (sign *Sign) Add(rhs *Sign) {
- C.blsSignAdd(sign.getPointer(), rhs.getPointer())
+ C.blsSignatureAdd(sign.getPointer(), rhs.getPointer())
}
// Recover --
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)))
+ err := C.blsSignatureRecover(sign.getPointer(), signVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(signVec)))
if err != 0 {
return fmt.Errorf("Sign.Recover")
}
@@ -452,10 +440,10 @@ func (sign *Sign) Recover(signVec []Sign, idVec []ID) error {
func (sign *Sign) Verify(pub *PublicKey, m string) bool {
buf := []byte(m)
// #nosec
- return C.blsSignVerify(sign.getPointer(), pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) == 1
+ return C.blsVerify(sign.getPointer(), pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) == 1
}
// VerifyPop --
func (sign *Sign) VerifyPop(pub *PublicKey) bool {
- return C.blsSignVerifyPop(sign.getPointer(), pub.getPointer()) == 1
+ return C.blsVerifyPop(sign.getPointer(), pub.getPointer()) == 1
}
diff --git a/go/bls/bls_test.go b/go/bls/bls_test.go
index 361b662..2561e83 100644
--- a/go/bls/bls_test.go
+++ b/go/bls/bls_test.go
@@ -11,11 +11,13 @@ func testPre(t *testing.T) {
t.Log("init")
{
var id ID
- id.Set([]uint64{6, 5, 4, 3, 2, 1}[0:unitN])
-
- t.Log("id :", id)
+ err := id.SetLittleEndian([]byte{6, 5, 4, 3, 2, 1})
+ if err != nil {
+ t.Error(err)
+ }
+ t.Log("id :", id.GetHexString())
var id2 ID
- err := id2.SetHexString(id.GetHexString())
+ err = id2.SetHexString(id.GetHexString())
if err != nil {
t.Fatal(err)
}
@@ -32,20 +34,23 @@ func testPre(t *testing.T) {
}
{
var sec SecretKey
- sec.SetArray([]uint64{1, 2, 3, 4, 5, 6}[0:unitN])
- t.Log("sec=", sec)
+ err := sec.SetLittleEndian([]byte{1, 2, 3, 4, 5, 6})
+ if err != nil {
+ t.Error(err)
+ }
+ t.Log("sec=", sec.GetHexString())
}
t.Log("create secret key")
m := "this is a bls sample for go"
var sec SecretKey
sec.Init()
- t.Log("sec:", sec)
+ t.Log("sec:", sec.GetHexString())
t.Log("create public key")
pub := sec.GetPublicKey()
- t.Log("pub:", pub)
+ t.Log("pub:", pub.GetHexString())
sign := sec.Sign(m)
- t.Log("sign:", sign)
+ t.Log("sign:", sign.GetHexString())
if !sign.Verify(pub, m) {
t.Error("Signature does not verify")
}
@@ -92,6 +97,7 @@ func testRecoverSecretKey(t *testing.T) {
k := 3000
var sec SecretKey
sec.Init()
+ t.Logf("sec=%s\n", sec.GetHexString())
// make master secret key
msk := sec.GetMasterSecretKey(k)
@@ -100,17 +106,21 @@ func testRecoverSecretKey(t *testing.T) {
secVec := make([]SecretKey, n)
idVec := make([]ID, n)
for i := 0; i < n; i++ {
- idVec[i].Set([]uint64{uint64(i), 1, 2, 3, 4, 5}[0:unitN])
- err := secVec[i].Set(msk, &idVec[i])
+ err := idVec[i].SetLittleEndian([]byte{byte(i & 255), byte(i >> 8), 2, 3, 4, 5})
if err != nil {
- t.Errorf("%s\n", err)
+ t.Error(err)
+ }
+ err = secVec[i].Set(msk, &idVec[i])
+ if err != nil {
+ t.Error(err)
}
+ // t.Logf("idVec[%d]=%s\n", i, idVec[i].GetHexString())
}
// recover sec2 from secVec and idVec
var sec2 SecretKey
err := sec2.Recover(secVec, idVec)
if err != nil {
- t.Errorf("err%s\n", err)
+ t.Error(err)
}
if !sec.IsSame(&sec2) {
t.Errorf("Mismatch in recovered secret key:\n %s\n %s.", sec.GetHexString(), sec2.GetHexString())
@@ -118,7 +128,7 @@ func testRecoverSecretKey(t *testing.T) {
}
func testEachSign(t *testing.T, m string, msk []SecretKey, mpk []PublicKey) ([]ID, []SecretKey, []PublicKey, []Sign) {
- idTbl := []uint64{3, 5, 193, 22, 15}
+ idTbl := []byte{3, 5, 193, 22, 15}
n := len(idTbl)
secVec := make([]SecretKey, n)
@@ -127,10 +137,13 @@ func testEachSign(t *testing.T, m string, msk []SecretKey, mpk []PublicKey) ([]I
idVec := make([]ID, n)
for i := 0; i < n; i++ {
- idVec[i].Set([]uint64{idTbl[i], 0, 0, 0, 0, 0}[0:unitN])
+ err := idVec[i].SetLittleEndian([]byte{idTbl[i], 0, 0, 0, 0, 0})
+ if err != nil {
+ t.Error(err)
+ }
t.Logf("idVec[%d]=%s\n", i, idVec[i].GetHexString())
- err := secVec[i].Set(msk, &idVec[i])
+ err = secVec[i].Set(msk, &idVec[i])
if err != nil {
t.Error(err)
}
@@ -236,8 +249,8 @@ func testData(t *testing.T) {
t.Log("testData")
var sec1, sec2 SecretKey
sec1.Init()
- b := sec1.Serialize()
- err := sec2.Deserialize(b)
+ b := sec1.GetLittleEndian()
+ err := sec2.SetLittleEndian(b)
if err != nil {
t.Fatal(err)
}
@@ -386,12 +399,15 @@ func benchmarkDeriveSeckeyShare(k int, b *testing.B) {
msk := sec.GetMasterSecretKey(k)
var id ID
for n := 0; n < b.N; n++ {
- id.Set([]uint64{1, 2, 3, 4, 5, uint64(n)})
+ err = id.SetLittleEndian([]byte{1, 2, 3, 4, 5, byte(n)})
+ if err != nil {
+ b.Error(err)
+ }
b.StartTimer()
err := sec.Set(msk, &id)
b.StopTimer()
if err != nil {
- b.Errorf("%s\n", err)
+ b.Error(err)
}
}
}
@@ -417,10 +433,13 @@ func benchmarkRecoverSeckey(k int, b *testing.B) {
secVec := make([]SecretKey, n)
idVec := make([]ID, n)
for i := 0; i < n; i++ {
- idVec[i].Set([]uint64{1, 2, 3, 4, 5, uint64(i)})
- err := secVec[i].Set(msk, &idVec[i])
+ err := idVec[i].SetLittleEndian([]byte{1, 2, 3, 4, 5, byte(i)})
if err != nil {
- b.Errorf("%s\n", err)
+ b.Error(err)
+ }
+ err = secVec[i].Set(msk, &idVec[i])
+ if err != nil {
+ b.Error(err)
}
}
@@ -456,10 +475,13 @@ func benchmarkRecoverSignature(k int, b *testing.B) {
secVec := make([]SecretKey, n)
signVec := make([]Sign, n)
for i := 0; i < n; i++ {
- idVec[i].Set([]uint64{1, 2, 3, 4, 5, uint64(i)})
- err := secVec[i].Set(msk, &idVec[i])
+ err := idVec[i].SetLittleEndian([]byte{1, 2, 3, 4, 5, byte(i)})
if err != nil {
- b.Errorf("%s\n", err)
+ b.Error(err)
+ }
+ err = secVec[i].Set(msk, &idVec[i])
+ if err != nil {
+ b.Error(err)
}
signVec[i] = *secVec[i].Sign("test message")
}
@@ -470,7 +492,7 @@ func benchmarkRecoverSignature(k int, b *testing.B) {
for n := 0; n < b.N; n++ {
err := sig.Recover(signVec, idVec)
if err != nil {
- b.Errorf("%s\n", err)
+ b.Error(err)
}
}
}
diff --git a/include/bls/bls.hpp b/include/bls/bls.hpp
index 7e4eaad..bd74f39 100644
--- a/include/bls/bls.hpp
+++ b/include/bls/bls.hpp
@@ -32,7 +32,7 @@ enum {
IoBin = 2, // binary number
IoDec = 10, // decimal number
IoHex = 16, // hexadecimal number
- IoEcComp = 512 // fixed byte representation
+ IoFixedByteSeq = 512 // fixed byte representation
};
namespace impl {
@@ -105,7 +105,8 @@ public:
@note the value must be less than r
*/
void set(const uint64_t *p);
-
+ // bufSize is truncted/zero extended to keySize
+ void setLittleEndian(const void *buf, size_t bufSize);
};
/*
@@ -125,14 +126,16 @@ public:
void getStr(std::string& str, int ioMode = 0) const;
void setStr(const std::string& str, int ioMode = 0);
/*
- initialize secretKey with random number
+ initialize secretKey with random number and set id = 0
*/
void init();
/*
- set secretKey with p[0, .., keySize)
+ set secretKey with p[0, .., keySize) and set id = 0
@note the value must be less than r
*/
void set(const uint64_t *p);
+ // bufSize is truncted/zero extended to keySize
+ void setLittleEndian(const void *buf, size_t bufSize);
void getPublicKey(PublicKey& pub) const;
// constant time sign
void sign(Sign& sign, const std::string& m) const;
diff --git a/include/bls/bls_if.h b/include/bls/bls_if.h
index 7d617d9..9293069 100644
--- a/include/bls/bls_if.h
+++ b/include/bls/bls_if.h
@@ -14,10 +14,10 @@
#include <stdlib.h> // for size_t
#ifdef _MSC_VER
-#ifdef BLS256_DLL_EXPORT
-#define BLS256_DLL_API __declspec(dllexport)
+#ifdef BLS_DLL_EXPORT
+#define BLS_DLL_API __declspec(dllexport)
#else
-#define BLS256_DLL_API __declspec(dllimport)
+#define BLS_DLL_API __declspec(dllimport)
#ifndef BLS_NO_AUTOLINK
#if BLS_MAX_OP_UNIT_SIZE == 4
#pragma comment(lib, "bls_if256.lib")
@@ -25,7 +25,7 @@
#endif
#endif
#else
-#define BLS256_DLL_API
+#define BLS_DLL_API
#endif
#ifdef __cplusplus
@@ -38,14 +38,6 @@ 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;
@@ -60,7 +52,7 @@ typedef struct {
typedef struct {
uint64_t buf[BLS_MAX_OP_UNIT_SIZE * 3];
-} blsSign;
+} blsSignature;
/*
initialize this library
@@ -68,101 +60,100 @@ typedef struct {
return 0 if success
@note init() is not thread safe
*/
-BLS256_DLL_API int blsInit(int curve, int maxUnitSize);
-BLS256_DLL_API size_t blsGetOpUnitSize(void);
+BLS_DLL_API int blsInit(int curve, int maxUnitSize);
+BLS_DLL_API size_t blsGetOpUnitSize(void);
// return strlen(buf) if success else 0
-BLS256_DLL_API int blsGetCurveOrder(char *buf, size_t maxBufSize);
-BLS256_DLL_API int blsGetFieldOrder(char *buf, size_t maxBufSize);
+BLS_DLL_API int blsGetCurveOrder(char *buf, size_t maxBufSize);
+BLS_DLL_API int blsGetFieldOrder(char *buf, size_t maxBufSize);
-BLS256_DLL_API blsId *blsIdCreate(void);
-BLS256_DLL_API void blsIdDestroy(blsId *id);
// return 1 if same else 0
-BLS256_DLL_API int blsIdIsSame(const blsId *lhs, const blsId *rhs);
-BLS256_DLL_API void blsIdPut(const blsId *id);
-BLS256_DLL_API void blsIdCopy(blsId *dst, const blsId *src);
+BLS_DLL_API int blsIdIsSame(const blsId *lhs, const blsId *rhs);
// return 0 if success
-BLS256_DLL_API int blsIdSetStr(blsId *id, const char *buf, size_t bufSize, int ioMode);
+BLS_DLL_API int blsIdSetLittleEndian(blsId *id, const void *buf, size_t bufSize);
+BLS_DLL_API int blsIdSetDecStr(blsId *id, const char *buf, size_t bufSize);
+BLS_DLL_API int blsIdSetHexStr(blsId *id, const char *buf, size_t bufSize);
/*
- return written byte size if ioMode = BlsIoComp
- return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
- return 0 otherwise
+ return written byte size if success else 0
*/
-BLS256_DLL_API size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize, int ioMode);
+BLS_DLL_API size_t blsIdGetLittleEndian(void *buf, size_t maxBufSize, const blsId *id);
/*
- access p[0], ..., p[3] if 256-bit curve
- access p[0], ..., p[5] if 384-bit curve
+ return strlen(buf) if success else 0
+ buf is '\0' terminated
*/
-BLS256_DLL_API void blsIdSet(blsId *id, const uint64_t *p);
+BLS_DLL_API size_t blsIdGetDecStr(char *buf, size_t maxBufSize, const blsId *id);
+BLS_DLL_API size_t blsIdGetHexStr(char *buf, size_t maxBufSize, const blsId *id);
-BLS256_DLL_API blsSecretKey* blsSecretKeyCreate(void);
-BLS256_DLL_API void blsSecretKeyDestroy(blsSecretKey *sec);
// return 1 if same else 0
-BLS256_DLL_API int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs);
+BLS_DLL_API int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs);
-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);
+BLS_DLL_API int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, size_t bufSize);
+BLS_DLL_API int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, size_t bufSize);
+BLS_DLL_API int blsSecretKeySetHexStr(blsSecretKey *sec, const char *buf, size_t bufSize);
+/*
+ return written byte size if success else 0
+*/
+BLS_DLL_API size_t blsSecretKeyGetLittleEndian(void *buf, size_t maxBufSize, const blsSecretKey *sec);
/*
- return written byte size if ioMode = BlsIoComp
- return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
- return 0 otherwise
+ hash buf and set
*/
-BLS256_DLL_API size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize, int ioMode);
-BLS256_DLL_API void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs);
+BLS_DLL_API int blsSecretKeySetByHash(blsSecretKey *sec, const void *buf, size_t bufSize);
+/*
+ set secretKey if system has /dev/urandom or CryptGenRandom
+ return 0 if success else -1
+*/
+BLS_DLL_API int blsSecretKeySetByCSPRNG(blsSecretKey *sec);
+/*
+ return strlen(buf) if success else 0
+ buf is '\0' terminated
+*/
+BLS_DLL_API size_t blsSecretKeyGetDecStr(char *buf, size_t maxBufSize, const blsSecretKey *sec);
+BLS_DLL_API size_t blsSecretKeyGetHexStr(char *buf, size_t maxBufSize, const blsSecretKey *sec);
+BLS_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);
+BLS_DLL_API void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec);
+BLS_DLL_API void blsSign(blsSignature *sig, const blsSecretKey *sec, const char *m, size_t size);
// return 0 if success
-BLS256_DLL_API int blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id);
+BLS_DLL_API int blsSecretKeyShare(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);
+BLS_DLL_API int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n);
+BLS_DLL_API void blsGetPop(blsSignature *sig, const blsSecretKey *sec);
-BLS256_DLL_API blsPublicKey *blsPublicKeyCreate(void);
-BLS256_DLL_API void blsPublicKeyDestroy(blsPublicKey *pub);
// return 1 if same else 0
-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);
+BLS_DLL_API int blsPublicKeyIsSame(const blsPublicKey *lhs, const blsPublicKey *rhs);
// return 0 if success
-BLS256_DLL_API int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize, int ioMode);
+BLS_DLL_API int blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, size_t bufSize);
/*
- return written byte size if ioMode = BlsIoComp
- return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
- return 0 otherwise
+ return written byte size if success else 0
*/
-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);
+BLS_DLL_API size_t blsPublicKeySerialize(void *buf, size_t maxBufSize, const blsPublicKey *pub);
+BLS_DLL_API int blsPublicKeySetHexStr(blsPublicKey *pub, const char *buf, size_t bufSize);
+BLS_DLL_API size_t blsPublicKeyGetHexStr(char *buf, size_t maxBufSize, const blsPublicKey *pub);
+BLS_DLL_API void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs);
// return 0 if success
-BLS256_DLL_API int blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id);
+BLS_DLL_API int blsPublicKeyShare(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);
+BLS_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);
// return 1 if same else 0
-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);
+BLS_DLL_API int blsSignatureIsSame(const blsSignature *lhs, const blsSignature *rhs);
+
// return 0 if success
-BLS256_DLL_API int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, int ioMode);
+BLS_DLL_API int blsSignatureDeserialize(blsSignature *sig, const void *buf, size_t bufSize);
/*
- return written byte size if ioMode = BlsIoComp
- return strlen(buf) if ioMode = 2, 10, 16 ; written byte size = strlen(buf) + 1
- return 0 otherwise
+ return written byte size if success else 0
*/
-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);
+BLS_DLL_API size_t blsSignatureSerialize(void *buf, size_t maxBufSize, const blsSignature *sig);
+BLS_DLL_API int blsSignatureSetHexStr(blsSignature *sig, const char *buf, size_t bufSize);
+BLS_DLL_API size_t blsSignatureGetHexStr(char *buf, size_t maxBufSize, const blsSignature *sig);
+BLS_DLL_API void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs);
// 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);
+BLS_DLL_API int blsSignatureRecover(blsSignature *sig, const blsSignature *signVec, const blsId *idVec, size_t n);
+BLS_DLL_API int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const char *m, size_t size);
-BLS256_DLL_API int blsSignVerifyPop(const blsSign *sign, const blsPublicKey *pub);
+BLS_DLL_API int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub);
#ifdef __cplusplus
}
diff --git a/sample/bls_smpl.cpp b/sample/bls_smpl.cpp
index f87294b..a91616c 100644
--- a/sample/bls_smpl.cpp
+++ b/sample/bls_smpl.cpp
@@ -1,4 +1,4 @@
-#include <bls.hpp>
+#include <bls/bls.hpp>
#include <cybozu/option.hpp>
#include <cybozu/itoa.hpp>
#include <fstream>
diff --git a/src/bls.cpp b/src/bls.cpp
index 01a6ae9..70bbfa3 100644
--- a/src/bls.cpp
+++ b/src/bls.cpp
@@ -258,6 +258,11 @@ void Id::set(const uint64_t *p)
getInner().v.setArrayMask(p, keySize);
}
+void Id::setLittleEndian(const void *buf, size_t bufSize)
+{
+ getInner().v.setArrayMask((const char *)buf, bufSize);
+}
+
bool Sign::operator==(const Sign& rhs) const
{
return getInner().sHm == rhs.getInner().sHm;
@@ -410,6 +415,10 @@ void SecretKey::set(const uint64_t *p)
{
getInner().s.setArrayMask(p, keySize);
}
+void SecretKey::setLittleEndian(const void *buf, size_t bufSize)
+{
+ getInner().s.setArrayMask((const char *)buf, bufSize);
+}
void SecretKey::getPublicKey(PublicKey& pub) const
{
diff --git a/src/bls_if.cpp b/src/bls_if.cpp
index 0ed194c..d8b24ea 100644
--- a/src/bls_if.cpp
+++ b/src/bls_if.cpp
@@ -1,19 +1,10 @@
#include "bls/bls.hpp"
-#define BLS256_DLL_EXPORT
+#define BLS_DLL_EXPORT
#include "bls/bls_if.h"
#include <iostream>
#include <sstream>
#include <memory.h>
-
-template<class Inner, class Outer>
-Outer *createT()
- try
-{
- return (Outer*)new Inner();
-} catch (std::exception& e) {
- fprintf(stderr, "err createT %s\n", e.what());
- return NULL;
-}
+#include <mcl/fp.hpp>
template<class Inner, class Outer>
int setStrT(Outer *p, const char *buf, size_t bufSize, int ioMode)
@@ -42,7 +33,7 @@ size_t getStrT(const Outer *p, char *buf, size_t maxBufSize, int ioMode)
std::string s;
((const Inner*)p)->getStr(s, ioMode);
size_t terminate = 0;
- if (ioMode == 0 || ioMode == blsIoBin || ioMode == blsIoDec || ioMode == blsIoHex) {
+ if (ioMode == 0 || ioMode == bls::IoBin || ioMode == bls::IoDec || ioMode == bls::IoHex) {
terminate = 1; // for '\0'
}
if (s.size() > maxBufSize + terminate) {
@@ -90,103 +81,104 @@ int blsGetFieldOrder(char *buf, size_t maxBufSize)
return 0;
}
-blsId *blsIdCreate()
+int blsIdIsSame(const blsId *lhs, const blsId *rhs)
{
- return createT<bls::Id, blsId>();
+ return *(const bls::Id*)lhs == *(const bls::Id*)rhs ? 1 : 0;
}
-
-void blsIdDestroy(blsId *id)
+int blsIdSetLittleEndian(blsId *id, const void *buf, size_t bufSize)
{
- delete (bls::Id*)id;
+ ((bls::Id*)id)->setLittleEndian(buf, bufSize);
+ return 0;
}
-int blsIdIsSame(const blsId *lhs, const blsId *rhs)
+int blsIdSetDecStr(blsId *id, const char *buf, size_t bufSize)
{
- return *(const bls::Id*)lhs == *(const bls::Id*)rhs ? 1 : 0;
+ return setStrT<bls::Id, blsId>(id, buf, bufSize, 10);
}
-void blsIdPut(const blsId *id)
+int blsIdSetHexStr(blsId *id, const char *buf, size_t bufSize)
{
- std::cout << *(const bls::Id*)id << std::endl;
+ return setStrT<bls::Id, blsId>(id, buf, bufSize, 16);
}
-void blsIdCopy(blsId *dst, const blsId *src)
+size_t blsIdGetLittleEndian(void *buf, size_t maxBufSize, const blsId *id)
{
- *((bls::Id*)dst) = *((const bls::Id*)src);
+ return getStrT<bls::Id, blsId>(id, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
}
-
-int blsIdSetStr(blsId *id, const char *buf, size_t bufSize, int ioMode)
+size_t blsIdGetDecStr(char *buf, size_t maxBufSize, const blsId *id)
{
- return setStrT<bls::Id, blsId>(id, buf, bufSize, ioMode);
+ return getStrT<bls::Id, blsId>(id, buf, maxBufSize, 10);
}
-
-size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize, int ioMode)
+size_t blsIdGetHexStr(char *buf, size_t maxBufSize, const blsId *id)
{
- return getStrT<bls::Id, blsId>(id, buf, maxBufSize, ioMode);
+ return getStrT<bls::Id, blsId>(id, buf, maxBufSize, 16);
}
-
-void blsIdSet(blsId *id, const uint64_t *p)
+int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs)
{
- ((bls::Id*)id)->set(p);
+ return *(const bls::SecretKey*)lhs == *(const bls::SecretKey*)rhs ? 1 : 0;
}
-
-blsSecretKey* blsSecretKeyCreate()
+int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, size_t bufSize)
{
- return createT<bls::SecretKey, blsSecretKey>();
+ ((bls::SecretKey*)sec)->setLittleEndian(buf, bufSize);
+ return 0;
}
-
-void blsSecretKeyDestroy(blsSecretKey *sec)
+int blsSecretKeySetDecStr(blsSecretKey *sec, const char *buf, size_t bufSize)
{
- delete (bls::SecretKey*)sec;
+ return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, 10);
}
-int blsSecretKeyIsSame(const blsSecretKey *lhs, const blsSecretKey *rhs)
+int blsSecretKeySetHexStr(blsSecretKey *sec, const char *buf, size_t bufSize)
{
- return *(const bls::SecretKey*)lhs == *(const bls::SecretKey*)rhs ? 1 : 0;
+ return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, 16);
}
-void blsSecretKeyCopy(blsSecretKey *dst, const blsSecretKey *src)
+size_t blsSecretKeyGetLittleEndian(void *buf, size_t maxBufSize, const blsSecretKey *sec)
{
- *((bls::SecretKey*)dst) = *((const bls::SecretKey*)src);
+ return getStrT<bls::SecretKey, blsSecretKey>(sec, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
}
-
-void blsSecretKeyPut(const blsSecretKey *sec)
+size_t blsSecretKeyGetDecStr(char *buf, size_t maxBufSize, const blsSecretKey *sec)
{
- std::cout << *(const bls::SecretKey*)sec << std::endl;
+ return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, 10);
}
-void blsSecretKeySetArray(blsSecretKey *sec, const uint64_t *p)
+size_t blsSecretKeyGetHexStr(char *buf, size_t maxBufSize, const blsSecretKey *sec)
{
- ((bls::SecretKey*)sec)->set(p);
+ return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, 16);
}
-int blsSecretKeySetStr(blsSecretKey *sec, const char *buf, size_t bufSize, int ioMode)
-{
- return setStrT<bls::SecretKey, blsSecretKey>(sec, buf, bufSize, ioMode);
-}
-size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize, int ioMode)
+int blsSecretKeySetByHash(blsSecretKey *sec, const void *buf, size_t bufSize)
+ try
{
- return getStrT<bls::SecretKey, blsSecretKey>(sec, buf, maxBufSize, ioMode);
+ std::string s = mcl::fp::hash(384, (const char *)buf, bufSize);
+ return blsSecretKeySetLittleEndian(sec, s.c_str(), s.size());
+} catch (std::exception& e) {
+ fprintf(stderr, "err blsSecretKeySetByCSPRNG %s\n", e.what());
+ return -1;
}
-void blsSecretKeyInit(blsSecretKey *sec)
+int blsSecretKeySetByCSPRNG(blsSecretKey *sec)
+ try
{
((bls::SecretKey*)sec)->init();
+ return 0;
+} catch (std::exception& e) {
+ fprintf(stderr, "err blsSecretKeySetByCSPRNG %s\n", e.what());
+ return -1;
}
void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs)
{
((bls::SecretKey*)sec)->add(*(const bls::SecretKey*)rhs);
}
-void blsSecretKeyGetPublicKey(const blsSecretKey *sec, blsPublicKey *pub)
+void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec)
{
((const bls::SecretKey*)sec)->getPublicKey(*(bls::PublicKey*)pub);
}
-void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, size_t size)
+void blsSign(blsSignature *sig, const blsSecretKey *sec, const char *m, size_t size)
{
- ((const bls::SecretKey*)sec)->sign(*(bls::Sign*)sign, std::string(m, size));
+ ((const bls::SecretKey*)sec)->sign(*(bls::Sign*)sig, std::string(m, size));
}
-int blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id)
+int blsSecretKeyShare(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());
+ fprintf(stderr, "err blsSecretKeyShare %s\n", e.what());
return -1;
}
@@ -200,52 +192,57 @@ int blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const bls
return -1;
}
-void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign)
-{
- ((const bls::SecretKey*)sec)->getPop(*(bls::Sign*)sign);
-}
-
-blsPublicKey *blsPublicKeyCreate()
+void blsGetPop(blsSignature *sig, const blsSecretKey *sec)
{
- return createT<bls::PublicKey, blsPublicKey>();
+ ((const bls::SecretKey*)sec)->getPop(*(bls::Sign*)sig);
}
-void blsPublicKeyDestroy(blsPublicKey *pub)
-{
- delete (bls::PublicKey*)pub;
-}
int blsPublicKeyIsSame(const blsPublicKey *lhs, const blsPublicKey *rhs)
{
return *(const bls::PublicKey*)lhs == *(const bls::PublicKey*)rhs ? 1 : 0;
}
-void blsPublicKeyCopy(blsPublicKey *dst, const blsPublicKey *src)
+int blsPublicKeyDeserialize(blsPublicKey *pub, const void *buf, size_t bufSize)
{
- *((bls::PublicKey*)dst) = *((const bls::PublicKey*)src);
+ return setStrT<bls::PublicKey, blsPublicKey>(pub, (const char*)buf, bufSize, bls::IoFixedByteSeq);
}
-void blsPublicKeyPut(const blsPublicKey *pub)
+size_t blsPublicKeySerialize(void *buf, size_t maxBufSize, const blsPublicKey *pub)
{
- std::cout << *(const bls::PublicKey*)pub << std::endl;
+ return getStrT<bls::PublicKey, blsPublicKey>(pub, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
}
-
-int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize, int ioMode)
+int blsPublicKeySetHexStr(blsPublicKey *pub, const char *buf, size_t bufSize)
+ try
{
- return setStrT<bls::PublicKey, blsPublicKey>(pub, buf, bufSize, ioMode);
+ std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize);
+ return blsPublicKeyDeserialize(pub, s.c_str(), s.size());
+} catch (std::exception& e) {
+ fprintf(stderr, "err blsPublicKeySetHexStr %s\n", e.what());
+ return -1;
}
-size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize, int ioMode)
+size_t blsPublicKeyGetHexStr(char *buf, size_t maxBufSize, const blsPublicKey *pub)
{
- return getStrT<bls::PublicKey, blsPublicKey>(pub, buf, maxBufSize, ioMode);
+ std::string s;
+ s.resize(1024);
+ if (blsPublicKeySerialize(&s[0], s.size(), pub) == 0) {
+ s = mcl::fp::littleEndianToHexStr(s.c_str(), s.size());
+ if (s.size() < maxBufSize) {
+ memcpy(buf, s.c_str(), s.size());
+ buf[s.size()] = '\0';
+ return s.size();
+ }
+ }
+ return 0;
}
void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs)
{
((bls::PublicKey*)pub)->add(*(const bls::PublicKey*)rhs);
}
-int blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id)
+int blsPublicKeyShare(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());
+ fprintf(stderr, "err blsPublicKeyShare %s\n", e.what());
return -1;
}
int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n)
@@ -258,57 +255,62 @@ int blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const bls
return -1;
}
-blsSign *blsSignCreate()
-{
- return createT<bls::Sign, blsSign>();
-}
-
-void blsSignDestroy(blsSign *sign)
-{
- delete (bls::Sign*)sign;
-}
-int blsSignIsSame(const blsSign *lhs, const blsSign *rhs)
+int blsSignatureIsSame(const blsSignature *lhs, const blsSignature *rhs)
{
return *(const bls::Sign*)lhs == *(const bls::Sign*)rhs ? 1 : 0;
}
-void blsSignCopy(blsSign *dst, const blsSign *src)
+int blsSignatureDeserialize(blsSignature *sig, const void *buf, size_t bufSize)
{
- *((bls::Sign*)dst) = *((const bls::Sign*)src);
+ return setStrT<bls::Sign, blsSignature>(sig, (const char *)buf, bufSize, bls::IoFixedByteSeq);
}
-void blsSignPut(const blsSign *sign)
+int blsSignatureSetHexStr(blsSignature *sig, const char *buf, size_t bufSize)
+ try
{
- std::cout << *(const bls::Sign*)sign << std::endl;
+ std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize);
+ return blsSignatureDeserialize(sig, s.c_str(), s.size());
+} catch (std::exception& e) {
+ fprintf(stderr, "err blsSignatureSetHexStr %s\n", e.what());
+ return -1;
}
-
-int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize, int ioMode)
+size_t blsSignatureGetHexStr(char *buf, size_t maxBufSize, const blsSignature *sig)
{
- return setStrT<bls::Sign, blsSign>(sign, buf, bufSize, ioMode);
+ std::string s;
+ s.resize(1024);
+ if (blsSignatureSerialize(&s[0], s.size(), sig) == 0) {
+ s = mcl::fp::littleEndianToHexStr(s.c_str(), s.size());
+ if (s.size() < maxBufSize) {
+ memcpy(buf, s.c_str(), s.size());
+ buf[s.size()] = '\0';
+ return s.size();
+ }
+ }
+ return 0;
}
-size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize, int ioMode)
+size_t blsSignatureSerialize(void *buf, size_t maxBufSize, const blsSignature *sig)
{
- return getStrT<bls::Sign, blsSign>(sign, buf, maxBufSize, ioMode);
+ return getStrT<bls::Sign, blsSignature>(sig, (char *)buf, maxBufSize, bls::IoFixedByteSeq);
}
-void blsSignAdd(blsSign *sign, const blsSign *rhs)
+void blsSignatureAdd(blsSignature *sig, const blsSignature *rhs)
{
- ((bls::Sign*)sign)->add(*(const bls::Sign*)rhs);
+ ((bls::Sign*)sig)->add(*(const bls::Sign*)rhs);
}
-int blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n)
+int blsSignatureRecover(blsSignature *sig, const blsSignature *signVec, const blsId *idVec, size_t n)
try
{
- ((bls::Sign*)sign)->recover((const bls::Sign*)signVec, (const bls::Id*)idVec, n);
+ ((bls::Sign*)sig)->recover((const bls::Sign*)signVec, (const bls::Id*)idVec, n);
return 0;
} catch (std::exception& e) {
- fprintf(stderr, "err blsSignRecover %s\n", e.what());
+ fprintf(stderr, "err blsSignatureRecover %s\n", e.what());
return -1;
}
-int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size)
+int blsVerify(const blsSignature *sig, const blsPublicKey *pub, const char *m, size_t size)
{
- return ((const bls::Sign*)sign)->verify(*(const bls::PublicKey*)pub, std::string(m, size));
+ return ((const bls::Sign*)sig)->verify(*(const bls::PublicKey*)pub, std::string(m, size));
}
-int blsSignVerifyPop(const blsSign *sign, const blsPublicKey *pub)
+int blsVerifyPop(const blsSignature *sig, const blsPublicKey *pub)
{
- return ((const bls::Sign*)sign)->verify(*(const bls::PublicKey*)pub);
+ return ((const bls::Sign*)sig)->verify(*(const bls::PublicKey*)pub);
}
diff --git a/test/bls_if_test.cpp b/test/bls_if_test.cpp
index 48ed33f..317afee 100644
--- a/test/bls_if_test.cpp
+++ b/test/bls_if_test.cpp
@@ -2,51 +2,21 @@
#include <bls/bls_if.h>
#include <string.h>
-void bls_ifTest()
-{
- blsSecretKey *sec;
- blsPublicKey *pub;
- blsSign *sign;
- const char *msg = "this is a pen";
- const size_t msgSize = strlen(msg);
-
- sec = blsSecretKeyCreate();
- blsSecretKeyInit(sec);
- blsSecretKeyPut(sec);
-
- pub = blsPublicKeyCreate();
- blsSecretKeyGetPublicKey(sec, pub);
- blsPublicKeyPut(pub);
-
- sign = blsSignCreate();
- blsSecretKeySign(sec, sign, msg, msgSize);
- blsSignPut(sign);
-
- CYBOZU_TEST_ASSERT(blsSignVerify(sign, pub, msg, msgSize));
-
- blsSignDestroy(sign);
- blsPublicKeyDestroy(pub);
- blsSecretKeyDestroy(sec);
-}
-
void bls_if_use_stackTest()
{
blsSecretKey sec;
blsPublicKey pub;
- blsSign sign;
+ blsSignature sig;
const char *msg = "this is a pen";
const size_t msgSize = strlen(msg);
- blsSecretKeyInit(&sec);
- blsSecretKeyPut(&sec);
+ blsSecretKeySetByCSPRNG(&sec);
- blsSecretKeyGetPublicKey(&sec, &pub);
- blsPublicKeyPut(&pub);
+ blsGetPublicKey(&pub, &sec);
- blsSecretKeySign(&sec, &sign, msg, msgSize);
- blsSignPut(&sign);
+ blsSign(&sig, &sec, msg, msgSize);
- CYBOZU_TEST_ASSERT(blsSignVerify(&sign, &pub, msg, msgSize));
+ CYBOZU_TEST_ASSERT(blsVerify(&sig, &pub, msg, msgSize));
}
void bls_ifDataTest()
@@ -55,29 +25,29 @@ void bls_ifDataTest()
const size_t msgSize = strlen(msg);
const size_t fpSize = blsGetOpUnitSize() * sizeof(uint64_t);
blsSecretKey sec1, sec2;
- blsSecretKeyInit(&sec1);
+ blsSecretKeySetByCSPRNG(&sec1);
char buf[BLS_MAX_OP_UNIT_SIZE * sizeof(uint64_t) * 2];
size_t n;
int ret;
- n = blsSecretKeyGetStr(&sec1, buf, sizeof(buf), blsIoEcComp);
- CYBOZU_TEST_EQUAL(n, fpSize);
- ret = blsSecretKeySetStr(&sec2, buf, n, blsIoEcComp);
+ n = blsSecretKeyGetHexStr(buf, sizeof(buf), &sec1);
+ CYBOZU_TEST_EQUAL(n, fpSize * 2);
+ ret = blsSecretKeySetHexStr(&sec2, buf, n);
CYBOZU_TEST_EQUAL(ret, 0);
CYBOZU_TEST_ASSERT(blsSecretKeyIsSame(&sec1, &sec2));
blsPublicKey pub1, pub2;
- blsSecretKeyGetPublicKey(&sec1, &pub1);
- n = blsPublicKeyGetStr(&pub1, buf, sizeof(buf), blsIoEcComp);
+ blsGetPublicKey(&pub1, &sec1);
+ n = blsPublicKeySerialize(buf, sizeof(buf), &pub1);
CYBOZU_TEST_EQUAL(n, fpSize * 2);
- ret = blsPublicKeySetStr(&pub2, buf, n, blsIoEcComp);
+ ret = blsPublicKeyDeserialize(&pub2, buf, n);
CYBOZU_TEST_EQUAL(ret, 0);
CYBOZU_TEST_ASSERT(blsPublicKeyIsSame(&pub1, &pub2));
- blsSign sign1, sign2;
- blsSecretKeySign(&sec1, &sign1, msg, msgSize);
- n = blsSignGetStr(&sign1, buf, sizeof(buf), blsIoEcComp);
+ blsSignature sig1, sig2;
+ blsSign(&sig1, &sec1, msg, msgSize);
+ n = blsSignatureSerialize(buf, sizeof(buf), &sig1);
CYBOZU_TEST_EQUAL(n, fpSize);
- ret = blsSignSetStr(&sign2, buf, n, blsIoEcComp);
+ ret = blsSignatureDeserialize(&sig2, buf, n);
CYBOZU_TEST_EQUAL(ret, 0);
- CYBOZU_TEST_ASSERT(blsSignIsSame(&sign1, &sign2));
+ CYBOZU_TEST_ASSERT(blsSignatureIsSame(&sig1, &sig2));
}
void bls_ifOrderTest(const char *curveOrder, const char *fieldOrder)
@@ -114,7 +84,6 @@ CYBOZU_TEST_AUTO(all)
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]);
diff --git a/test/bls_test.cpp b/test/bls_test.cpp
index 22ce1ac..fc49f6e 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.getStr(str, bls::IoEcComp);
+ sec.getStr(str, bls::IoFixedByteSeq);
{
CYBOZU_TEST_EQUAL(str.size(), size);
bls::SecretKey sec2;
- sec2.setStr(str, bls::IoEcComp);
+ sec2.setStr(str, bls::IoFixedByteSeq);
CYBOZU_TEST_EQUAL(sec, sec2);
}
bls::PublicKey pub;
sec.getPublicKey(pub);
- pub.getStr(str, bls::IoEcComp);
+ pub.getStr(str, bls::IoFixedByteSeq);
{
CYBOZU_TEST_EQUAL(str.size(), size * 2);
bls::PublicKey pub2;
- pub2.setStr(str, bls::IoEcComp);
+ pub2.setStr(str, bls::IoFixedByteSeq);
CYBOZU_TEST_EQUAL(pub, pub2);
}
std::string m = "abc";
bls::Sign sign;
sec.sign(sign, m);
- sign.getStr(str, bls::IoEcComp);
+ sign.getStr(str, bls::IoFixedByteSeq);
{
CYBOZU_TEST_EQUAL(str.size(), size);
bls::Sign sign2;
- sign2.setStr(str, bls::IoEcComp);
+ sign2.setStr(str, bls::IoFixedByteSeq);
CYBOZU_TEST_EQUAL(sign, sign2);
}
bls::Id id;
const uint64_t v[] = { 1, 2, 3, 4, 5, 6, };
id.set(v);
- id.getStr(str, bls::IoEcComp);
+ id.getStr(str, bls::IoFixedByteSeq);
{
CYBOZU_TEST_EQUAL(str.size(), size);
bls::Id id2;
- id2.setStr(str, bls::IoEcComp);
+ id2.setStr(str, bls::IoFixedByteSeq);
CYBOZU_TEST_EQUAL(id, id2);
}
}