diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2016-09-06 16:23:36 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2016-09-06 16:23:36 +0800 |
commit | a70abcd3dd2b80fe4a8ef49bbf151ad76ed01d29 (patch) | |
tree | 15474f1fb06f35dff2078d57b9817ed4a6bf84f5 /go | |
parent | 995a520b839e3405cf71d8cfa43ca471e125eb90 (diff) | |
download | dexon-bls-a70abcd3dd2b80fe4a8ef49bbf151ad76ed01d29.tar dexon-bls-a70abcd3dd2b80fe4a8ef49bbf151ad76ed01d29.tar.gz dexon-bls-a70abcd3dd2b80fe4a8ef49bbf151ad76ed01d29.tar.bz2 dexon-bls-a70abcd3dd2b80fe4a8ef49bbf151ad76ed01d29.tar.lz dexon-bls-a70abcd3dd2b80fe4a8ef49bbf151ad76ed01d29.tar.xz dexon-bls-a70abcd3dd2b80fe4a8ef49bbf151ad76ed01d29.tar.zst dexon-bls-a70abcd3dd2b80fe4a8ef49bbf151ad76ed01d29.zip |
remove Bls in class name
Diffstat (limited to 'go')
-rw-r--r-- | go/bls/bls.go | 74 | ||||
-rw-r--r-- | go/main.go | 191 |
2 files changed, 48 insertions, 217 deletions
diff --git a/go/bls/bls.go b/go/bls/bls.go index b3f509d..df03734 100644 --- a/go/bls/bls.go +++ b/go/bls/bls.go @@ -10,26 +10,26 @@ import "fmt" import "runtime" import "unsafe" -func BlsInit() { +func Init() { C.blsInit() } -type BlsId struct { +type Id struct { self *C.blsId } -func destroyBlsId(p *BlsId) { +func destroyBlsId(p *Id) { C.blsIdDestroy(p.self) } -func NewBlsId() *BlsId { - p := new(BlsId) +func NewId() *Id { + p := new(Id) p.self = C.blsIdCreate() runtime.SetFinalizer(p, destroyBlsId) return p } -func (id *BlsId) String() string { +func (id *Id) String() string { buf := make([]byte, 1024) n := C.blsIdGetStr(id.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) if n == 0 { @@ -38,38 +38,38 @@ func (id *BlsId) String() string { return string(buf[:n]) } -func (id *BlsId) SetStr(s string) { +func (id *Id) SetStr(s string) { buf := []byte(s) err := C.blsIdSetStr(id.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) if err > 0 { - fmt.Println("BlsId:SetStr", err) + fmt.Println("Id:SetStr", err) } } -func (id *BlsId) Set(v []uint64) { +func (id *Id) Set(v []uint64) { if len(v) != 4 { - fmt.Println("BlsId:set bad size", len(v)) + fmt.Println("Id:set bad size", len(v)) return } C.blsIdSet(id.self, (*C.uint64_t)(unsafe.Pointer(&v[0]))) } -type BlsSecretKey struct { +type SecretKey struct { self *C.blsSecretKey } -func destroyBlsSecretKey(p *BlsSecretKey) { +func destroyBlsSecretKey(p *SecretKey) { C.blsSecretKeyDestroy(p.self) } -func NewBlsSecretKey() *BlsSecretKey { - p := new(BlsSecretKey) +func NewSecretKey() *SecretKey { + p := new(SecretKey) p.self = C.blsSecretKeyCreate() runtime.SetFinalizer(p, destroyBlsSecretKey) return p } -func (sec *BlsSecretKey) String() string { +func (sec *SecretKey) String() string { buf := make([]byte, 1024) n := C.blsSecretKeyGetStr(sec.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) if n == 0 { @@ -78,34 +78,34 @@ func (sec *BlsSecretKey) String() string { return string(buf[:n]) } -func (sec *BlsSecretKey) SetStr(s string) { +func (sec *SecretKey) SetStr(s string) { buf := []byte(s) err := C.blsSecretKeySetStr(sec.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) if err > 0 { - fmt.Println("BlsSecretKey:SetStr", err) + fmt.Println("SecretKey:SetStr", err) } } -func (sec *BlsSecretKey) Init() { +func (sec *SecretKey) Init() { C.blsSecretKeyInit(sec.self) } -type BlsPublicKey struct { +type PublicKey struct { self *C.blsPublicKey } -func destroyBlsPublicKey(p *BlsPublicKey) { +func destroyBlsPublicKey(p *PublicKey) { C.blsPublicKeyDestroy(p.self) } -func NewBlsPublicKey() *BlsPublicKey { - p := new(BlsPublicKey) +func NewPublicKey() *PublicKey { + p := new(PublicKey) p.self = C.blsPublicKeyCreate() runtime.SetFinalizer(p, destroyBlsPublicKey) return p } -func (pub *BlsPublicKey) String() string { +func (pub *PublicKey) String() string { buf := make([]byte, 1024) n := C.blsPublicKeyGetStr(pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) if n == 0 { @@ -114,30 +114,30 @@ func (pub *BlsPublicKey) String() string { return string(buf[:n]) } -func (pub *BlsPublicKey) SetStr(s string) { +func (pub *PublicKey) SetStr(s string) { buf := []byte(s) err := C.blsPublicKeySetStr(pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) if err > 0 { - fmt.Println("BlsPublicKey:SetStr", err) + fmt.Println("PublicKey:SetStr", err) } } -type BlsSign struct { +type Sign struct { self *C.blsSign } -func destroyBlsSign(p *BlsSign) { +func destroyBlsSign(p *Sign) { C.blsSignDestroy(p.self) } -func NewBlsSign() *BlsSign { - p := new(BlsSign) +func NewSign() *Sign { + p := new(Sign) p.self = C.blsSignCreate() runtime.SetFinalizer(p, destroyBlsSign) return p } -func (sign *BlsSign) String() string { +func (sign *Sign) String() string { buf := make([]byte, 1024) n := C.blsSignGetStr(sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) if n == 0 { @@ -146,28 +146,28 @@ func (sign *BlsSign) String() string { return string(buf[:n]) } -func (sign *BlsSign) SetStr(s string) { +func (sign *Sign) SetStr(s string) { buf := []byte(s) err := C.blsSignSetStr(sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) if err > 0 { - fmt.Println("BlsSign:SetStr", err) + fmt.Println("Sign:SetStr", err) } } -func (sec *BlsSecretKey) GetPublicKey() (pub *BlsPublicKey) { - pub = NewBlsPublicKey() +func (sec *SecretKey) GetPublicKey() (pub *PublicKey) { + pub = NewPublicKey() C.blsSecretKeyGetPublicKey(sec.self, pub.self) return pub } -func (sec *BlsSecretKey) Sign(m string) (sign *BlsSign) { - sign = NewBlsSign() +func (sec *SecretKey) Sign(m string) (sign *Sign) { + sign = NewSign() buf := []byte(m) C.blsSecretKeySign(sec.self, sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) return sign } -func (sign *BlsSign) Verify(pub *BlsPublicKey, m string) bool { +func (sign *Sign) Verify(pub *PublicKey, m string) bool { buf := []byte(m) return C.blsSignVerify(sign.self, pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) == 1 } @@ -1,198 +1,29 @@ package main -/* -#cgo CFLAGS:-I../include -#cgo LDFLAGS:-lbls -lbls_if -lmcl -lgmp -lgmpxx -L../lib -L../../mcl/lib -lstdc++ -lcrypto -#include "bls_if.h" -*/ -import "C" import "fmt" -import "runtime" -import "unsafe" - -func BlsInit() { - C.blsInit() -} - -type BlsId struct { - self *C.blsId -} - -func destroyBlsId(p *BlsId) { - C.blsIdDestroy(p.self) -} - -func newBlsId() *BlsId { - p := new(BlsId) - p.self = C.blsIdCreate() - runtime.SetFinalizer(p, destroyBlsId) - return p -} - -func (id *BlsId) String() string { - buf := make([]byte, 1024) - n := C.blsIdGetStr(id.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if n == 0 { - return "err" - } - return string(buf[:n]) -} - -func (id *BlsId) setStr(s string) { - buf := []byte(s) - err := C.blsIdSetStr(id.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if err > 0 { - fmt.Println("BlsId:SetStr", err) - } -} - -func (id *BlsId) set(v []uint64) { - if len(v) != 4 { - fmt.Println("BlsId:set bad size", len(v)) - return - } - C.blsIdSet(id.self, (*C.uint64_t)(unsafe.Pointer(&v[0]))) -} - -type BlsSecretKey struct { - self *C.blsSecretKey -} - -func destroyBlsSecretKey(p *BlsSecretKey) { - C.blsSecretKeyDestroy(p.self) -} - -func newBlsSecretKey() *BlsSecretKey { - p := new(BlsSecretKey) - p.self = C.blsSecretKeyCreate() - runtime.SetFinalizer(p, destroyBlsSecretKey) - return p -} - -func (sec *BlsSecretKey) String() string { - buf := make([]byte, 1024) - n := C.blsSecretKeyGetStr(sec.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if n == 0 { - return "err" - } - return string(buf[:n]) -} - -func (sec *BlsSecretKey) setStr(s string) { - buf := []byte(s) - err := C.blsSecretKeySetStr(sec.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if err > 0 { - fmt.Println("BlsSecretKey:SetStr", err) - } -} - -func (sec *BlsSecretKey) init() { - C.blsSecretKeyInit(sec.self) -} - -type BlsPublicKey struct { - self *C.blsPublicKey -} - -func destroyBlsPublicKey(p *BlsPublicKey) { - C.blsPublicKeyDestroy(p.self) -} - -func newBlsPublicKey() *BlsPublicKey { - p := new(BlsPublicKey) - p.self = C.blsPublicKeyCreate() - runtime.SetFinalizer(p, destroyBlsPublicKey) - return p -} - -func (pub *BlsPublicKey) String() string { - buf := make([]byte, 1024) - n := C.blsPublicKeyGetStr(pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if n == 0 { - return "err" - } - return string(buf[:n]) -} - -func (pub *BlsPublicKey) setStr(s string) { - buf := []byte(s) - err := C.blsPublicKeySetStr(pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if err > 0 { - fmt.Println("BlsPublicKey:SetStr", err) - } -} - -type BlsSign struct { - self *C.blsSign -} - -func destroyBlsSign(p *BlsSign) { - C.blsSignDestroy(p.self) -} - -func newBlsSign() *BlsSign { - p := new(BlsSign) - p.self = C.blsSignCreate() - runtime.SetFinalizer(p, destroyBlsSign) - return p -} - -func (sign *BlsSign) String() string { - buf := make([]byte, 1024) - n := C.blsSignGetStr(sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if n == 0 { - return "err" - } - return string(buf[:n]) -} - -func (sign *BlsSign) setStr(s string) { - buf := []byte(s) - err := C.blsSignSetStr(sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - if err > 0 { - fmt.Println("BlsSign:SetStr", err) - } -} - -func (sec *BlsSecretKey) getPublicKey() (pub *BlsPublicKey) { - pub = newBlsPublicKey() - C.blsSecretKeyGetPublicKey(sec.self, pub.self) - return pub -} - -func (sec *BlsSecretKey) sign(m string) (sign *BlsSign) { - sign = newBlsSign() - buf := []byte(m) - C.blsSecretKeySign(sec.self, sign.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) - return sign -} - -func (sign *BlsSign) verify(pub *BlsPublicKey, m string) bool { - buf := []byte(m) - return C.blsSignVerify(sign.self, pub.self, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))) == 1 -} +import "./bls" func main() { fmt.Println("init") - BlsInit() + bls.Init() { - id := newBlsId() - id.set([]uint64{4, 3, 2, 1}) + id := bls.NewId() + id.Set([]uint64{4, 3, 2, 1}) fmt.Println("id :", id) - id2 := newBlsId() - id2.setStr(id.String()) + id2 := bls.NewId() + id2.SetStr(id.String()) fmt.Println("id2:", id2) } fmt.Println("create secret key") m := "this is a bls sample for go" - sec := newBlsSecretKey() - sec.init() + sec := bls.NewSecretKey() + sec.Init() fmt.Println("sec:", sec) fmt.Println("create public key") - pub := sec.getPublicKey() + pub := sec.GetPublicKey() fmt.Println("pub:", pub) - sign := sec.sign(m) + sign := sec.Sign(m) fmt.Println("sign:", sign) - fmt.Println("verify:", sign.verify(pub, m)) + fmt.Println("verify:", sign.Verify(pub, m)) } |