From 172d4cb1b40f105d953bda7b9162a8bfc6227c9b Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Tue, 14 Mar 2017 19:49:02 +0900 Subject: minimize difference between bls.go and https://raw.githubusercontent.com/dfinity/random-beacon/master/blscgo/bls.go --- go/bls/bls.go | 246 -------------------------------------------------- go/blscgo/bls.go | 267 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ go/main.go | 48 +++++----- 3 files changed, 291 insertions(+), 270 deletions(-) delete mode 100644 go/bls/bls.go create mode 100644 go/blscgo/bls.go diff --git a/go/bls/bls.go b/go/bls/bls.go deleted file mode 100644 index d9fbdd6..0000000 --- a/go/bls/bls.go +++ /dev/null @@ -1,246 +0,0 @@ -package bls - -/* -#cgo CFLAGS:-I../../include -#cgo LDFLAGS:-lbls -lbls_if -lmcl -lgmp -lgmpxx -L../lib -L../../lib -L../../../mcl/lib -L../../mcl/lib -lstdc++ -lcrypto -#include "bls_if.h" -*/ -import "C" -import "fmt" -import "unsafe" - -func Init() { - C.blsInit() -} - -type Id struct { - v [4]C.uint64_t -} - -func (id *Id) getPointer() (p *C.blsId) { - return (*C.blsId)(unsafe.Pointer(&id.v[0])) -} - -func (id *Id) String() string { - buf := make([]byte, 1024) - n := C.blsIdGetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if n == 0 { - panic("implementation err. size of buf is small") - } - return string(buf[:n]) -} - -func (id *Id) SetStr(s string) error { - buf := []byte(s) - err := C.blsIdSetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if err > 0 { - return fmt.Errorf("bad string:%s", s) - } - return nil -} - -func (id *Id) Set(v []uint64) error { - if len(v) != 4 { - return fmt.Errorf("bad size", len(v)) - } - C.blsIdSet(id.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0]))) - return nil -} - -type SecretKey struct { - v [4]C.uint64_t -} - -func (sec *SecretKey) getPointer() (p *C.blsSecretKey) { - return (*C.blsSecretKey)(unsafe.Pointer(&sec.v[0])) -} - -func (sec *SecretKey) String() string { - buf := make([]byte, 1024) - n := C.blsSecretKeyGetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if n == 0 { - panic("implementation err. size of buf is small") - } - return string(buf[:n]) -} - -func (sec *SecretKey) SetStr(s string) error { - buf := []byte(s) - err := C.blsSecretKeySetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if err > 0 { - return fmt.Errorf("bad string:%s", s) - } - return nil -} - -func (sec *SecretKey) SetArray(v []uint64) error { - if len(v) != 4 { - return fmt.Errorf("bad size", len(v)) - } - C.blsSecretKeySetArray(sec.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0]))) - return nil -} - -func (sec *SecretKey) Init() { - C.blsSecretKeyInit(sec.getPointer()) -} - -func (sec *SecretKey) Add(rhs *SecretKey) { - C.blsSecretKeyAdd(sec.getPointer(), rhs.getPointer()) -} - -func (sec *SecretKey) GetMasterSecretKey(k int) (msk []SecretKey) { - msk = make([]SecretKey, k) - msk[0] = *sec - for i := 1; i < k; i++ { - msk[i].Init() - } - return msk -} - -func GetMasterPublicKey(msk []SecretKey) (mpk []PublicKey) { - n := len(msk) - mpk = make([]PublicKey, n) - for i := 0; i < n; i++ { - mpk[i] = *msk[i].GetPublicKey() - } - return mpk -} - -func makeSecretKeyPointerArray(v []SecretKey) (pv []*C.blsSecretKey) { - n := len(v) - pv = make([]*C.blsSecretKey, n) - for i := 0; i < n; i++ { - pv[i] = v[i].getPointer() - } - return pv -} -func makePublicKeyPointerArray(v []PublicKey) (pv []*C.blsPublicKey) { - n := len(v) - pv = make([]*C.blsPublicKey, n) - for i := 0; i < n; i++ { - pv[i] = v[i].getPointer() - } - return pv -} -func makeSignPointerArray(v []Sign) (pv []*C.blsSign) { - n := len(v) - pv = make([]*C.blsSign, n) - for i := 0; i < n; i++ { - pv[i] = v[i].getPointer() - } - return pv -} -func makeIdPointerArray(v []Id) (pv []*C.blsId) { - n := len(v) - pv = make([]*C.blsId, n) - for i := 0; i < n; i++ { - pv[i] = v[i].getPointer() - } - return pv -} -func (sec *SecretKey) Set(msk []SecretKey, id *Id) { - C.blsSecretKeySet(sec.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer()) -} - -func (sec *SecretKey) Recover(secVec []SecretKey, idVec []Id) { - C.blsSecretKeyRecover(sec.getPointer(), secVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(secVec))) -} - -func (sec *SecretKey) GetPop() (sign *Sign) { - sign = new(Sign) - C.blsSecretKeyGetPop(sec.getPointer(), sign.getPointer()) - return sign -} - -type PublicKey struct { - v [4 * 2 * 3]C.uint64_t -} - -func (pub *PublicKey) getPointer() (p *C.blsPublicKey) { - return (*C.blsPublicKey)(unsafe.Pointer(&pub.v[0])) -} - -func (pub *PublicKey) String() string { - buf := make([]byte, 1024) - n := C.blsPublicKeyGetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if n == 0 { - panic("implementation err. size of buf is small") - } - return string(buf[:n]) -} - -func (pub *PublicKey) SetStr(s string) error { - buf := []byte(s) - err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if err > 0 { - return fmt.Errorf("bad string:%s", s) - } - return nil -} - -func (pub *PublicKey) Add(rhs *PublicKey) { - C.blsPublicKeyAdd(pub.getPointer(), rhs.getPointer()) -} -func (pub *PublicKey) Set(msk []PublicKey, id *Id) { - C.blsPublicKeySet(pub.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer()) -} - -func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []Id) { - C.blsPublicKeyRecover(pub.getPointer(), pubVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(pubVec))) -} - -type Sign struct { - v [4 * 3]C.uint64_t -} - -func (sign *Sign) getPointer() (p *C.blsSign) { - return (*C.blsSign)(unsafe.Pointer(&sign.v[0])) -} - -func (sign *Sign) String() string { - buf := make([]byte, 1024) - n := C.blsSignGetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if n == 0 { - panic("implementation err. size of buf is small") - } - return string(buf[:n]) -} - -func (sign *Sign) SetStr(s string) error { - buf := []byte(s) - err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if err > 0 { - return fmt.Errorf("bad string:%s", s) - } - return nil -} - -func (sec *SecretKey) GetPublicKey() (pub *PublicKey) { - pub = new(PublicKey) - C.blsSecretKeyGetPublicKey(sec.getPointer(), pub.getPointer()) - return pub -} - -func (sec *SecretKey) Sign(m string) (sign *Sign) { - sign = new(Sign) - buf := []byte(m) - C.blsSecretKeySign(sec.getPointer(), sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - return sign -} - -func (sign *Sign) Add(rhs *Sign) { - C.blsSignAdd(sign.getPointer(), rhs.getPointer()) -} -func (sign *Sign) Recover(signVec []Sign, idVec []Id) { - C.blsSignRecover(sign.getPointer(), signVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(signVec))) -} - -func (sign *Sign) Verify(pub *PublicKey, m string) bool { - buf := []byte(m) - return C.blsSignVerify(sign.getPointer(), pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) == 1 -} - -func (sign *Sign) VerifyPop(pub *PublicKey) bool { - return C.blsSignVerifyPop(sign.getPointer(), pub.getPointer()) == 1 -} diff --git a/go/blscgo/bls.go b/go/blscgo/bls.go new file mode 100644 index 0000000..bd0cd2d --- /dev/null +++ b/go/blscgo/bls.go @@ -0,0 +1,267 @@ +package blscgo + +/* +#cgo CFLAGS:-I../../include +#cgo LDFLAGS:-lbls -lbls_if -lmcl -lgmp -lgmpxx -L../lib -L../../lib -L../../../mcl/lib -L../../mcl/lib -lstdc++ -lcrypto +#include "bls_if.h" +*/ +import "C" +import "fmt" +import "unsafe" + +// Init -- +func Init() { + C.blsInit() +} + +// ID -- +type ID struct { + v [4]C.uint64_t +} + +// getPointer -- +func (id *ID) getPointer() (p *C.blsId) { + // #nosec + return (*C.blsId)(unsafe.Pointer(&id.v[0])) +} + +// String -- +func (id *ID) String() string { + buf := make([]byte, 1024) + // #nosec + n := C.blsIdGetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if n == 0 { + panic("implementation err. size of buf is small") + } + return string(buf[:n]) +} + +// SetStr -- +func (id *ID) SetStr(s string) error { + buf := []byte(s) + // #nosec + err := C.blsIdSetStr(id.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if err > 0 { + return fmt.Errorf("bad string:%s", s) + } + return nil +} + +// Set -- +func (id *ID) Set(v []uint64) error { + if len(v) != 4 { + return fmt.Errorf("bad size (%d), expected size 4", len(v)) + } + // #nosec + C.blsIdSet(id.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0]))) + return nil +} + +// SecretKey -- +type SecretKey struct { + v [4]C.uint64_t +} + +// getPointer -- +func (sec *SecretKey) getPointer() (p *C.blsSecretKey) { + // #nosec + return (*C.blsSecretKey)(unsafe.Pointer(&sec.v[0])) +} + +// String -- +func (sec *SecretKey) String() string { + buf := make([]byte, 1024) + // #nosec + n := C.blsSecretKeyGetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if n == 0 { + panic("implementation err. size of buf is small") + } + return string(buf[:n]) +} + +// SetStr -- The string passed in is a number and can be either hex or decimal +func (sec *SecretKey) SetStr(s string) error { + buf := []byte(s) + // #nosec + err := C.blsSecretKeySetStr(sec.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if err > 0 { + return fmt.Errorf("bad string:%s", s) + } + return nil +} + +// SetArray -- +func (sec *SecretKey) SetArray(v []uint64) error { + if len(v) != 4 { + return fmt.Errorf("bad size (%d), expected size 4", len(v)) + } + // #nosec + C.blsSecretKeySetArray(sec.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0]))) + return nil +} + +// Init -- +func (sec *SecretKey) Init() { + C.blsSecretKeyInit(sec.getPointer()) +} + +// Add -- +func (sec *SecretKey) Add(rhs *SecretKey) { + C.blsSecretKeyAdd(sec.getPointer(), rhs.getPointer()) +} + +// GetMasterSecretKey -- +func (sec *SecretKey) GetMasterSecretKey(k int) (msk []SecretKey) { + msk = make([]SecretKey, k) + msk[0] = *sec + for i := 1; i < k; i++ { + msk[i].Init() + } + return msk +} + +// GetMasterPublicKey -- +func GetMasterPublicKey(msk []SecretKey) (mpk []PublicKey) { + n := len(msk) + mpk = make([]PublicKey, n) + for i := 0; i < n; i++ { + mpk[i] = *msk[i].GetPublicKey() + } + return mpk +} + +// Set -- +func (sec *SecretKey) Set(msk []SecretKey, id *ID) { + C.blsSecretKeySet(sec.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer()) +} + +// Recover -- +func (sec *SecretKey) Recover(secVec []SecretKey, idVec []ID) { + C.blsSecretKeyRecover(sec.getPointer(), secVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(secVec))) +} + +// GetPop -- +func (sec *SecretKey) GetPop() (sign *Sign) { + sign = new(Sign) + C.blsSecretKeyGetPop(sec.getPointer(), sign.getPointer()) + return sign +} + +// PublicKey -- +type PublicKey struct { + v [4 * 2 * 3]C.uint64_t +} + +// getPointer -- +func (pub *PublicKey) getPointer() (p *C.blsPublicKey) { + // #nosec + return (*C.blsPublicKey)(unsafe.Pointer(&pub.v[0])) +} + +// String -- +func (pub *PublicKey) String() string { + buf := make([]byte, 1024) + // #nosec + n := C.blsPublicKeyGetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if n == 0 { + panic("implementation err. size of buf is small") + } + return string(buf[:n]) +} + +// SetStr -- +func (pub *PublicKey) SetStr(s string) error { + buf := []byte(s) + // #nosec + err := C.blsPublicKeySetStr(pub.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if err > 0 { + return fmt.Errorf("bad string:%s", s) + } + return nil +} + +// Add -- +func (pub *PublicKey) Add(rhs *PublicKey) { + C.blsPublicKeyAdd(pub.getPointer(), rhs.getPointer()) +} + +// Set -- +func (pub *PublicKey) Set(mpk []PublicKey, id *ID) { + C.blsPublicKeySet(pub.getPointer(), mpk[0].getPointer(), C.size_t(len(mpk)), id.getPointer()) +} + +// Recover -- +func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []ID) { + C.blsPublicKeyRecover(pub.getPointer(), pubVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(pubVec))) +} + +// Sign -- +type Sign struct { + v [4 * 3]C.uint64_t +} + +// getPointer -- +func (sign *Sign) getPointer() (p *C.blsSign) { + // #nosec + return (*C.blsSign)(unsafe.Pointer(&sign.v[0])) +} + +// String -- +func (sign *Sign) String() string { + buf := make([]byte, 1024) + // #nosec + n := C.blsSignGetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if n == 0 { + panic("implementation err. size of buf is small") + } + return string(buf[:n]) +} + +// SetStr -- +func (sign *Sign) SetStr(s string) error { + buf := []byte(s) + // #nosec + err := C.blsSignSetStr(sign.getPointer(), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) + if err > 0 { + return fmt.Errorf("bad string:%s", s) + } + return nil +} + +// GetPublicKey -- +func (sec *SecretKey) GetPublicKey() (pub *PublicKey) { + pub = new(PublicKey) + C.blsSecretKeyGetPublicKey(sec.getPointer(), pub.getPointer()) + return pub +} + +// Sign -- +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))) + return sign +} + +// Add -- +func (sign *Sign) Add(rhs *Sign) { + C.blsSignAdd(sign.getPointer(), rhs.getPointer()) +} + +// Recover -- +func (sign *Sign) Recover(signVec []Sign, idVec []ID) { + C.blsSignRecover(sign.getPointer(), signVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(signVec))) +} + +// Verify -- +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 +} + +// VerifyPop -- +func (sign *Sign) VerifyPop(pub *PublicKey) bool { + return C.blsSignVerifyPop(sign.getPointer(), pub.getPointer()) == 1 +} diff --git a/go/main.go b/go/main.go index 6cd5cb0..87934ad 100644 --- a/go/main.go +++ b/go/main.go @@ -1,7 +1,7 @@ package main import "fmt" -import "./bls" +import "./blscgo" import "runtime" import "time" @@ -13,7 +13,7 @@ func verifyTrue(b bool) { func testRecoverSecretKey() { fmt.Println("testRecoverSecretKey") k := 3000 - var sec bls.SecretKey + var sec blscgo.SecretKey sec.Init() fmt.Println("sec =", sec) @@ -21,14 +21,14 @@ func testRecoverSecretKey() { msk := sec.GetMasterSecretKey(k) n := k - secVec := make([]bls.SecretKey, n) - idVec := make([]bls.Id, n) + secVec := make([]blscgo.SecretKey, n) + idVec := make([]blscgo.ID, n) for i := 0; i < n; i++ { idVec[i].Set([]uint64{1, 2, 3, uint64(i)}) secVec[i].Set(msk, &idVec[i]) } // recover sec2 from secVec and idVec - var sec2 bls.SecretKey + var sec2 blscgo.SecretKey sec2.Recover(secVec, idVec) fmt.Println("sec2=", sec2) } @@ -37,7 +37,7 @@ func testSign() { m := "testSign" fmt.Println(m) - var sec0 bls.SecretKey + var sec0 blscgo.SecretKey sec0.Init() pub0 := sec0.GetPublicKey() s0 := sec0.Sign(m) @@ -45,15 +45,15 @@ func testSign() { k := 3 msk := sec0.GetMasterSecretKey(k) - mpk := bls.GetMasterPublicKey(msk) + mpk := blscgo.GetMasterPublicKey(msk) idTbl := []uint64{3, 5, 193, 22, 15} n := len(idTbl) - secVec := make([]bls.SecretKey, n) - pubVec := make([]bls.PublicKey, n) - signVec := make([]bls.Sign, n) - idVec := make([]bls.Id, n) + secVec := make([]blscgo.SecretKey, n) + pubVec := make([]blscgo.PublicKey, n) + signVec := make([]blscgo.Sign, n) + idVec := make([]blscgo.ID, n) for i := 0; i < n; i++ { idVec[i].Set([]uint64{idTbl[i], 0, 0, 0}) @@ -69,21 +69,21 @@ func testSign() { signVec[i] = *secVec[i].Sign(m) verifyTrue(signVec[i].Verify(&pubVec[i], m)) } - var sec1 bls.SecretKey + var sec1 blscgo.SecretKey sec1.Recover(secVec, idVec) verifyTrue(sec0.String() == sec1.String()) - var pub1 bls.PublicKey + var pub1 blscgo.PublicKey pub1.Recover(pubVec, idVec) verifyTrue(pub0.String() == pub1.String()) - var s1 bls.Sign + var s1 blscgo.Sign s1.Recover(signVec, idVec) verifyTrue(s0.String() == s1.String()) } func testAdd() { fmt.Println("testAdd") - var sec1 bls.SecretKey - var sec2 bls.SecretKey + var sec1 blscgo.SecretKey + var sec2 blscgo.SecretKey sec1.Init() sec2.Init() @@ -103,7 +103,7 @@ func testAdd() { func testPop() { fmt.Println("testPop") - var sec bls.SecretKey + var sec blscgo.SecretKey sec.Init() pop := sec.GetPop() verifyTrue(pop.VerifyPop(sec.GetPublicKey())) @@ -112,24 +112,24 @@ func testPop() { } func main() { fmt.Println("init") - bls.Init() + blscgo.Init() { - var id bls.Id + var id blscgo.ID id.Set([]uint64{4, 3, 2, 1}) fmt.Println("id :", id) - var id2 bls.Id + var id2 blscgo.ID id2.SetStr(id.String()) fmt.Println("id2:", id2) } { - var sec bls.SecretKey + var sec blscgo.SecretKey sec.SetArray([]uint64{1, 2, 3, 4}) fmt.Println("sec=", sec) } fmt.Println("create secret key") - m := "this is a bls sample for go" - var sec bls.SecretKey + m := "this is a blscgo sample for go" + var sec blscgo.SecretKey sec.Init() fmt.Println("sec:", sec) fmt.Println("create public key") @@ -141,7 +141,7 @@ func main() { // How to make array of SecretKey { - sec := make([]bls.SecretKey, 3) + sec := make([]blscgo.SecretKey, 3) for i := 0; i < len(sec); i++ { sec[i].Init() fmt.Println("sec=", sec[i].String()) -- cgit v1.2.3