diff options
Diffstat (limited to 'go')
-rw-r--r-- | go/main.go | 55 |
1 files changed, 49 insertions, 6 deletions
@@ -7,17 +7,60 @@ package main */ import "C" import "fmt" +import "runtime" import "unsafe" -func main() { - fmt.Println("init") +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) + } +} - id := C.blsIdCreate() - defer C.blsIdDestroy(id) +func (id *BlsId) set(v []uint64) { + C.blsIdSet(id.self, (*C.uint64_t)(unsafe.Pointer(&v[0]))) +} - C.blsIdSet(id, (*C.uint64_t)(unsafe.Pointer(&[]uint64{1, 2, 3, 4}[0]))) - C.blsIdPut(id) +func main() { + fmt.Println("init") + BlsInit() + { + id := newBlsId() + id.set([]uint64{4, 3, 2, 1}) + fmt.Println("id :", id) + id2 := newBlsId() + id2.setStr(id.String()) + fmt.Println("id2:", id2) + } fmt.Println("create secret key") sec := C.blsSecretKeyCreate() |