diff options
-rw-r--r-- | go/bls/bls.go | 56 | ||||
-rw-r--r-- | go/main.go | 34 | ||||
-rw-r--r-- | include/bls_if.h | 4 | ||||
-rw-r--r-- | src/bls_if.cpp | 16 |
4 files changed, 77 insertions, 33 deletions
diff --git a/go/bls/bls.go b/go/bls/bls.go index 97f0fe4..b94fec2 100644 --- a/go/bls/bls.go +++ b/go/bls/bls.go @@ -29,6 +29,12 @@ func NewId() *Id { return p } +func (id *Id) Clone() (r *Id) { + r = NewId() + C.blsIdCopy(r.self, id.self) + return r +} + 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))) @@ -70,6 +76,12 @@ func NewSecretKey() *SecretKey { return p } +func (sec *SecretKey) Clone() (r *SecretKey) { + r = NewSecretKey() + C.blsSecretKeyCopy(r.self, sec.self) + return r +} + 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))) @@ -104,26 +116,26 @@ func (sec *SecretKey) Add(rhs *SecretKey) { C.blsSecretKeyAdd(sec.self, rhs.self); } -func (sec *SecretKey) GetMasterSecretKey(k int) (msk []SecretKey) { - msk = make([]SecretKey, k) - msk[0] = *sec +func (sec *SecretKey) GetMasterSecretKey(k int) (msk []*SecretKey) { + msk = make([]*SecretKey, k) + msk[0] = sec.Clone() for i := 1; i < k; i++ { - msk[i] = *NewSecretKey() + msk[i] = NewSecretKey() msk[i].Init() } return msk } -func GetMasterPublicKey(msk []SecretKey) (mpk []PublicKey) { +func GetMasterPublicKey(msk []*SecretKey) (mpk []*PublicKey) { n := len(msk) - mpk = make([]PublicKey, n) + mpk = make([]*PublicKey, n) for i := 0; i < n; i++ { - mpk[i] = *msk[i].GetPublicKey() + mpk[i] = msk[i].GetPublicKey() } return mpk } -func makeSecretKeyPointerArray(v []SecretKey) (pv []*C.blsSecretKey) { +func makeSecretKeyPointerArray(v []*SecretKey) (pv []*C.blsSecretKey) { n := len(v) pv = make([]*C.blsSecretKey, n) for i := 0; i < n; i++ { @@ -131,7 +143,7 @@ func makeSecretKeyPointerArray(v []SecretKey) (pv []*C.blsSecretKey) { } return pv } -func makePublicKeyPointerArray(v []PublicKey) (pv []*C.blsPublicKey) { +func makePublicKeyPointerArray(v []*PublicKey) (pv []*C.blsPublicKey) { n := len(v) pv = make([]*C.blsPublicKey, n) for i := 0; i < n; i++ { @@ -139,7 +151,7 @@ func makePublicKeyPointerArray(v []PublicKey) (pv []*C.blsPublicKey) { } return pv } -func makeSignPointerArray(v []Sign) (pv []*C.blsSign) { +func makeSignPointerArray(v []*Sign) (pv []*C.blsSign) { n := len(v) pv = make([]*C.blsSign, n) for i := 0; i < n; i++ { @@ -147,7 +159,7 @@ func makeSignPointerArray(v []Sign) (pv []*C.blsSign) { } return pv } -func makeIdPointerArray(v []Id) (pv []*C.blsId) { +func makeIdPointerArray(v []*Id) (pv []*C.blsId) { n := len(v) pv = make([]*C.blsId, n) for i := 0; i < n; i++ { @@ -155,12 +167,12 @@ func makeIdPointerArray(v []Id) (pv []*C.blsId) { } return pv } -func (sec *SecretKey) Set(msk []SecretKey, id *Id) { +func (sec *SecretKey) Set(msk []*SecretKey, id *Id) { v := makeSecretKeyPointerArray(msk) C.blsSecretKeySet(sec.self, (**C.blsSecretKey)(unsafe.Pointer(&v[0])), C.size_t(len(msk)), id.self) } -func (sec *SecretKey) Recover(secVec []SecretKey, idVec []Id) { +func (sec *SecretKey) Recover(secVec []*SecretKey, idVec []*Id) { sv := makeSecretKeyPointerArray(secVec) iv := makeIdPointerArray(idVec) C.blsSecretKeyRecover(sec.self, (**C.blsSecretKey)(unsafe.Pointer(&sv[0])), (**C.blsId)(unsafe.Pointer(&iv[0])), C.size_t(len(secVec))) @@ -187,6 +199,12 @@ func NewPublicKey() *PublicKey { return p } +func (pub *PublicKey) Clone() (r *PublicKey) { + r = NewPublicKey() + C.blsPublicKeyCopy(r.self, pub.self) + return r +} + 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))) @@ -208,12 +226,12 @@ func (pub *PublicKey) SetStr(s string) error { func (pub *PublicKey) Add(rhs *PublicKey) { C.blsPublicKeyAdd(pub.self, rhs.self); } -func (sec *PublicKey) Set(msk []PublicKey, id *Id) { +func (sec *PublicKey) Set(msk []*PublicKey, id *Id) { v := makePublicKeyPointerArray(msk) C.blsPublicKeySet(sec.self, (**C.blsPublicKey)(unsafe.Pointer(&v[0])), C.size_t(len(msk)), id.self) } -func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []Id) { +func (pub *PublicKey) Recover(pubVec []*PublicKey, idVec []*Id) { sv := makePublicKeyPointerArray(pubVec) iv := makeIdPointerArray(idVec) C.blsPublicKeyRecover(pub.self, (**C.blsPublicKey)(unsafe.Pointer(&sv[0])), (**C.blsId)(unsafe.Pointer(&iv[0])), C.size_t(len(pubVec))) @@ -234,6 +252,12 @@ func NewSign() *Sign { return p } +func (sign *Sign) Clone() (r *Sign) { + r = NewSign() + C.blsSignCopy(r.self, sign.self) + return r +} + 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))) @@ -268,7 +292,7 @@ func (sec *SecretKey) Sign(m string) (sign *Sign) { func (sign *Sign) Add(rhs *Sign) { C.blsSignAdd(sign.self, rhs.self); } -func (sign *Sign) Recover(signVec []Sign, idVec []Id) { +func (sign *Sign) Recover(signVec []*Sign, idVec []*Id) { sv := makeSignPointerArray(signVec) iv := makeIdPointerArray(idVec) C.blsSignRecover(sign.self, (**C.blsSign)(unsafe.Pointer(&sv[0])), (**C.blsId)(unsafe.Pointer(&iv[0])), C.size_t(len(signVec))) @@ -10,7 +10,7 @@ func verifyTrue(b bool) { } func testRecoverSecretKey() { fmt.Println("testRecoverSecretKey") - k := 5 + k := 1000 sec := bls.NewSecretKey() sec.Init() fmt.Println("sec =", sec) @@ -19,13 +19,13 @@ func testRecoverSecretKey() { msk := sec.GetMasterSecretKey(k) n := k - secVec := make([]bls.SecretKey, n) - idVec := make([]bls.Id, n) + secVec := make([]*bls.SecretKey, n) + idVec := make([]*bls.Id, n) for i := 0; i < n; i++ { - idVec[i] = *bls.NewId() + idVec[i] = bls.NewId() idVec[i].Set([]uint64{1, 2, 3, uint64(i)}) - secVec[i] = *bls.NewSecretKey() - secVec[i].Set(msk, &idVec[i]) + secVec[i] = bls.NewSecretKey() + secVec[i].Set(msk, idVec[i]) } // recover sec2 from secVec and idVec sec2 := bls.NewSecretKey() @@ -50,27 +50,27 @@ func testSign() { 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([]*bls.SecretKey, n) + pubVec := make([]*bls.PublicKey, n) + signVec := make([]*bls.Sign, n) + idVec := make([]*bls.Id, n) for i := 0; i < n; i++ { - idVec[i] = *bls.NewId() + idVec[i] = bls.NewId() idVec[i].Set([]uint64{idTbl[i], 0, 0, 0}) fmt.Printf("idVec[%d]=%s\n", i, idVec[i].String()) - secVec[i] = *bls.NewSecretKey() - secVec[i].Set(msk, &idVec[i]) + secVec[i] = bls.NewSecretKey() + secVec[i].Set(msk, idVec[i]) - pubVec[i] = *bls.NewPublicKey() - pubVec[i].Set(mpk, &idVec[i]) + pubVec[i] = bls.NewPublicKey() + pubVec[i].Set(mpk, idVec[i]) fmt.Printf("pubVec[%d]=%s\n", i, pubVec[i].String()) verifyTrue(pubVec[i].String() == secVec[i].GetPublicKey().String()) - signVec[i] = *secVec[i].Sign(m) - verifyTrue(signVec[i].Verify(&pubVec[i], m)) + signVec[i] = secVec[i].Sign(m) + verifyTrue(signVec[i].Verify(pubVec[i], m)) } sec1 := bls.NewSecretKey() sec1.Recover(secVec, idVec) diff --git a/include/bls_if.h b/include/bls_if.h index be4ba40..7872778 100644 --- a/include/bls_if.h +++ b/include/bls_if.h @@ -23,6 +23,7 @@ void blsInit(void); blsId *blsIdCreate(void); void blsIdDestroy(blsId *id); +void blsIdCopy(blsId *dst, const blsId *src); // return 0 if success int blsIdSetStr(blsId *id, const char *buf, size_t bufSize); @@ -40,6 +41,7 @@ void blsIdSet(blsId *id, const uint64_t *p); blsSecretKey* blsSecretKeyCreate(void); void blsSecretKeyDestroy(blsSecretKey *sec); 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); size_t blsSecretKeyGetStr(const blsSecretKey *sec, char *buf, size_t maxBufSize); @@ -55,6 +57,7 @@ void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign); blsPublicKey *blsPublicKeyCreate(void); void blsPublicKeyDestroy(blsPublicKey *pub); void blsPublicKeyPut(const blsPublicKey *pub); +void blsPublicKeyCopy(blsPublicKey *dst, const blsPublicKey *src); int blsPublicKeySetStr(blsPublicKey *pub, const char *buf, size_t bufSize); size_t blsPublicKeyGetStr(const blsPublicKey *pub, char *buf, size_t maxBufSize); void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs); @@ -64,6 +67,7 @@ void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *const *pubVec, c blsSign *blsSignCreate(void); void blsSignDestroy(blsSign *sign); void blsSignPut(const blsSign *sign); +void blsSignCopy(blsSign *dst, const blsSign *src); int blsSignSetStr(blsSign *sign, const char *buf, size_t bufSize); size_t blsSignGetStr(const blsSign *sign, char *buf, size_t maxBufSize); void blsSignAdd(blsSign *sign, const blsSign *rhs); diff --git a/src/bls_if.cpp b/src/bls_if.cpp index c8fb5d3..7576c93 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -57,6 +57,10 @@ void blsIdDestroy(blsId *id) { delete (bls::Id*)id; } +void blsIdCopy(blsId *dst, const blsId *src) +{ + *((bls::Id*)dst) = *((const bls::Id*)src); +} int blsIdSetStr(blsId *id, const char *buf, size_t bufSize) { @@ -82,6 +86,10 @@ void blsSecretKeyDestroy(blsSecretKey *sec) { delete (bls::SecretKey*)sec; } +void blsSecretKeyCopy(blsSecretKey *dst, const blsSecretKey *src) +{ + *((bls::SecretKey*)dst) = *((const bls::SecretKey*)src); +} void blsSecretKeyPut(const blsSecretKey *sec) { @@ -143,6 +151,10 @@ void blsPublicKeyDestroy(blsPublicKey *pub) { delete (bls::PublicKey*)pub; } +void blsPublicKeyCopy(blsPublicKey *dst, const blsPublicKey *src) +{ + *((bls::PublicKey*)dst) = *((const bls::PublicKey*)src); +} void blsPublicKeyPut(const blsPublicKey *pub) { std::cout << *(const bls::PublicKey*)pub << std::endl; @@ -178,6 +190,10 @@ void blsSignDestroy(blsSign *sign) { delete (bls::Sign*)sign; } +void blsSignCopy(blsSign *dst, const blsSign *src) +{ + *((bls::Sign*)dst) = *((const bls::Sign*)src); +} void blsSignPut(const blsSign *sign) { std::cout << *(const bls::Sign*)sign << std::endl; |