diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-01-20 16:43:04 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-01-20 16:43:04 +0800 |
commit | b680d8e089cd81f95c458f2c24f003cc36783c5a (patch) | |
tree | 0bb5fde1b19840e47b9c532aad67c4fe4a55ba70 | |
parent | ca93275c32037b83577f7e52bc63714006228d21 (diff) | |
download | dexon-bls-b680d8e089cd81f95c458f2c24f003cc36783c5a.tar dexon-bls-b680d8e089cd81f95c458f2c24f003cc36783c5a.tar.gz dexon-bls-b680d8e089cd81f95c458f2c24f003cc36783c5a.tar.bz2 dexon-bls-b680d8e089cd81f95c458f2c24f003cc36783c5a.tar.lz dexon-bls-b680d8e089cd81f95c458f2c24f003cc36783c5a.tar.xz dexon-bls-b680d8e089cd81f95c458f2c24f003cc36783c5a.tar.zst dexon-bls-b680d8e089cd81f95c458f2c24f003cc36783c5a.zip |
cgo accesses to T[] of Go
-rw-r--r-- | go/bls/bls.go | 18 | ||||
-rw-r--r-- | include/bls.hpp | 14 | ||||
-rw-r--r-- | include/bls_if.h | 10 | ||||
-rw-r--r-- | src/bls.cpp | 38 | ||||
-rw-r--r-- | src/bls_if.cpp | 20 |
5 files changed, 54 insertions, 46 deletions
diff --git a/go/bls/bls.go b/go/bls/bls.go index 68c5c7a..d9fbdd6 100644 --- a/go/bls/bls.go +++ b/go/bls/bls.go @@ -140,14 +140,11 @@ func makeIdPointerArray(v []Id) (pv []*C.blsId) { return pv } func (sec *SecretKey) Set(msk []SecretKey, id *Id) { - v := makeSecretKeyPointerArray(msk) - C.blsSecretKeySet(sec.getPointer(), (**C.blsSecretKey)(unsafe.Pointer(&v[0])), C.size_t(len(msk)), id.getPointer()) + C.blsSecretKeySet(sec.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer()) } func (sec *SecretKey) Recover(secVec []SecretKey, idVec []Id) { - sv := makeSecretKeyPointerArray(secVec) - iv := makeIdPointerArray(idVec) - C.blsSecretKeyRecover(sec.getPointer(), (**C.blsSecretKey)(unsafe.Pointer(&sv[0])), (**C.blsId)(unsafe.Pointer(&iv[0])), C.size_t(len(secVec))) + C.blsSecretKeyRecover(sec.getPointer(), secVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(secVec))) } func (sec *SecretKey) GetPop() (sign *Sign) { @@ -186,14 +183,11 @@ func (pub *PublicKey) Add(rhs *PublicKey) { C.blsPublicKeyAdd(pub.getPointer(), rhs.getPointer()) } func (pub *PublicKey) Set(msk []PublicKey, id *Id) { - v := makePublicKeyPointerArray(msk) - C.blsPublicKeySet(pub.getPointer(), (**C.blsPublicKey)(unsafe.Pointer(&v[0])), C.size_t(len(msk)), id.getPointer()) + C.blsPublicKeySet(pub.getPointer(), msk[0].getPointer(), C.size_t(len(msk)), id.getPointer()) } func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []Id) { - sv := makePublicKeyPointerArray(pubVec) - iv := makeIdPointerArray(idVec) - C.blsPublicKeyRecover(pub.getPointer(), (**C.blsPublicKey)(unsafe.Pointer(&sv[0])), (**C.blsId)(unsafe.Pointer(&iv[0])), C.size_t(len(pubVec))) + C.blsPublicKeyRecover(pub.getPointer(), pubVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(pubVec))) } type Sign struct { @@ -239,9 +233,7 @@ func (sign *Sign) Add(rhs *Sign) { C.blsSignAdd(sign.getPointer(), rhs.getPointer()) } func (sign *Sign) Recover(signVec []Sign, idVec []Id) { - sv := makeSignPointerArray(signVec) - iv := makeIdPointerArray(idVec) - C.blsSignRecover(sign.getPointer(), (**C.blsSign)(unsafe.Pointer(&sv[0])), (**C.blsId)(unsafe.Pointer(&iv[0])), C.size_t(len(signVec))) + C.blsSignRecover(sign.getPointer(), signVec[0].getPointer(), idVec[0].getPointer(), C.size_t(len(signVec))) } func (sign *Sign) Verify(pub *PublicKey, m string) bool { diff --git a/include/bls.hpp b/include/bls.hpp index cf2580b..d9ad086 100644 --- a/include/bls.hpp +++ b/include/bls.hpp @@ -62,6 +62,7 @@ class Id { friend class SecretKey; template<class T, class G> friend struct Wrap; template<class T, class G> friend struct WrapPointer; + template<class T, class G> friend struct WrapArray; impl::Id& getInner() { return *reinterpret_cast<impl::Id*>(self_); } const impl::Id& getInner() const { return *reinterpret_cast<const impl::Id*>(self_); } public: @@ -86,6 +87,7 @@ class SecretKey { uint64_t self_[4]; // 256-bit template<class T, class G> friend struct Wrap; template<class T, class G> friend struct WrapPointer; + template<class T, class G> friend struct WrapArray; impl::SecretKey& getInner() { return *reinterpret_cast<impl::SecretKey*>(self_); } const impl::SecretKey& getInner() const { return *reinterpret_cast<const impl::SecretKey*>(self_); } public: @@ -131,8 +133,8 @@ public: /* the size of msk must be k */ - void set(const SecretKey *const *msk, size_t k, const Id& id); - void recover(const SecretKey *const *secVec, const Id *const *idVec, size_t n); + void set(const SecretKey *msk, size_t k, const Id& id); + void recover(const SecretKey *secVec, const Id *idVec, size_t n); }; /* @@ -144,6 +146,7 @@ class PublicKey { friend class Sign; template<class T, class G> friend struct Wrap; template<class T, class G> friend struct WrapPointer; + template<class T, class G> friend struct WrapArray; impl::PublicKey& getInner() { return *reinterpret_cast<impl::PublicKey*>(self_); } const impl::PublicKey& getInner() const { return *reinterpret_cast<const impl::PublicKey*>(self_); } public: @@ -166,8 +169,8 @@ public: void add(const PublicKey& rhs); // the following methods are for C api - void set(const PublicKey *const *mpk, size_t k, const Id& id); - void recover(const PublicKey *const *pubVec, const Id *const *idVec, size_t n); + void set(const PublicKey *mpk, size_t k, const Id& id); + void recover(const PublicKey *pubVec, const Id *idVec, size_t n); }; /* @@ -178,6 +181,7 @@ class Sign { friend class SecretKey; template<class T, class G> friend struct Wrap; template<class T, class G> friend struct WrapPointer; + template<class T, class G> friend struct WrapArray; impl::Sign& getInner() { return *reinterpret_cast<impl::Sign*>(self_); } const impl::Sign& getInner() const { return *reinterpret_cast<const impl::Sign*>(self_); } public: @@ -201,7 +205,7 @@ public: void add(const Sign& rhs); // the following methods are for C api - void recover(const Sign* const *signVec, const Id *const *idVec, size_t n); + void recover(const Sign* signVec, const Id *idVec, size_t n); }; /* diff --git a/include/bls_if.h b/include/bls_if.h index 79885a0..b45f733 100644 --- a/include/bls_if.h +++ b/include/bls_if.h @@ -62,8 +62,8 @@ void blsSecretKeyAdd(blsSecretKey *sec, const blsSecretKey *rhs); void blsSecretKeyInit(blsSecretKey *sec); void blsSecretKeyGetPublicKey(const blsSecretKey *sec, blsPublicKey *pub); void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, size_t size); -void blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* const *msk, size_t k, const blsId *id); -void blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey* const *secVec, const blsId *const *idVec, size_t n); +void blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id); +void blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n); void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign); blsPublicKey *blsPublicKeyCreate(void); @@ -73,8 +73,8 @@ 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); -void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *const *mpk, size_t k, const blsId *id); -void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *const *pubVec, const blsId *const *idVec, size_t n); +void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id); +void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n); blsSign *blsSignCreate(void); void blsSignDestroy(blsSign *sign); @@ -83,7 +83,7 @@ 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); -void blsSignRecover(blsSign *sign, const blsSign *const *signVec, const blsId *const *idVec, size_t n); +void blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n); int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size); diff --git a/src/bls.cpp b/src/bls.cpp index 98f0888..8358c0f 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -90,6 +90,18 @@ struct WrapPointer { size_t size() const { return k; } }; +template<class T, class G> +struct WrapArray { + const T *v; + size_t k; + WrapArray(const T *v, size_t k) : v(v), k(k) {} + const G& operator[](size_t i) const + { + return v[i].getInner().get(); + } + size_t size() const { return k; } +}; + struct Polynomial { FrVec c; // f[x] = sum_{i=0}^{k-1} c[i] x^i void init(const Fr& s, int k) @@ -268,10 +280,10 @@ void Sign::recover(const SignVec& signVec, const IdVec& idVec) LagrangeInterpolation(getInner().sHm, signW, idW); } -void Sign::recover(const Sign* const *signVec, const Id *const *idVec, size_t n) +void Sign::recover(const Sign* signVec, const Id *idVec, size_t n) { - WrapPointer<Sign, G1> signW(signVec, n); - WrapPointer<Id, Fr> idW(idVec, n); + WrapArray<Sign, G1> signW(signVec, n); + WrapArray<Id, Fr> idW(idVec, n); LagrangeInterpolation(getInner().sHm, signW, idW); } @@ -301,9 +313,9 @@ void PublicKey::set(const PublicKeyVec& mpk, const Id& id) evalPoly(getInner().sQ, id.getInner().v, w); } -void PublicKey::set(const PublicKey *const *mpk, size_t k, const Id& id) +void PublicKey::set(const PublicKey *mpk, size_t k, const Id& id) { - WrapPointer<PublicKey, G2> w(mpk, k); + WrapArray<PublicKey, G2> w(mpk, k); evalPoly(getInner().sQ, id.getInner().v, w); } @@ -313,10 +325,10 @@ void PublicKey::recover(const PublicKeyVec& pubVec, const IdVec& idVec) Wrap<Id, Fr> idW(idVec); LagrangeInterpolation(getInner().sQ, pubW, idW); } -void PublicKey::recover(const PublicKey *const *pubVec, const Id *const *idVec, size_t n) +void PublicKey::recover(const PublicKey *pubVec, const Id *idVec, size_t n) { - WrapPointer<PublicKey, G2> pubW(pubVec, n); - WrapPointer<Id, Fr> idW(idVec, n); + WrapArray<PublicKey, G2> pubW(pubVec, n); + WrapArray<Id, Fr> idW(idVec, n); LagrangeInterpolation(getInner().sQ, pubW, idW); } @@ -386,9 +398,9 @@ void SecretKey::set(const SecretKeyVec& msk, const Id& id) Wrap<SecretKey, Fr> w(msk); evalPoly(getInner().s, id.getInner().v, w); } -void SecretKey::set(const SecretKey *const *msk, size_t k, const Id& id) +void SecretKey::set(const SecretKey *msk, size_t k, const Id& id) { - WrapPointer<SecretKey, Fr> w(msk, k); + WrapArray<SecretKey, Fr> w(msk, k); evalPoly(getInner().s, id.getInner().v, w); } @@ -398,10 +410,10 @@ void SecretKey::recover(const SecretKeyVec& secVec, const IdVec& idVec) Wrap<Id, Fr> idW(idVec); LagrangeInterpolation(getInner().s, secW, idW); } -void SecretKey::recover(const SecretKey *const *secVec, const Id *const *idVec, size_t n) +void SecretKey::recover(const SecretKey *secVec, const Id *idVec, size_t n) { - WrapPointer<SecretKey, Fr> secW(secVec, n); - WrapPointer<Id, Fr> idW(idVec, n); + WrapArray<SecretKey, Fr> secW(secVec, n); + WrapArray<Id, Fr> idW(idVec, n); LagrangeInterpolation(getInner().s, secW, idW); } diff --git a/src/bls_if.cpp b/src/bls_if.cpp index c64e868..1611e74 100644 --- a/src/bls_if.cpp +++ b/src/bls_if.cpp @@ -131,14 +131,14 @@ void blsSecretKeySign(const blsSecretKey *sec, blsSign *sign, const char *m, siz ((const bls::SecretKey*)sec)->sign(*(bls::Sign*)sign, std::string(m, size)); } -void blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* const *msk, size_t k, const blsId *id) +void blsSecretKeySet(blsSecretKey *sec, const blsSecretKey* msk, size_t k, const blsId *id) { - ((bls::SecretKey*)sec)->set((const bls::SecretKey *const *)msk, k, *(const bls::Id*)id); + ((bls::SecretKey*)sec)->set((const bls::SecretKey *)msk, k, *(const bls::Id*)id); } -void blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey* const *secVec, const blsId *const *idVec, size_t n) +void blsSecretKeyRecover(blsSecretKey *sec, const blsSecretKey *secVec, const blsId *idVec, size_t n) { - ((bls::SecretKey*)sec)->recover((const bls::SecretKey *const *)secVec, (const bls::Id *const *)idVec, n); + ((bls::SecretKey*)sec)->recover((const bls::SecretKey *)secVec, (const bls::Id *)idVec, n); } void blsSecretKeyGetPop(const blsSecretKey *sec, blsSign *sign) @@ -176,13 +176,13 @@ void blsPublicKeyAdd(blsPublicKey *pub, const blsPublicKey *rhs) { ((bls::PublicKey*)pub)->add(*(const bls::PublicKey*)rhs); } -void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *const *mpk, size_t k, const blsId *id) +void blsPublicKeySet(blsPublicKey *pub, const blsPublicKey *mpk, size_t k, const blsId *id) { - ((bls::PublicKey*)pub)->set((const bls::PublicKey* const *)mpk, k, *(const bls::Id*)id); + ((bls::PublicKey*)pub)->set((const bls::PublicKey*)mpk, k, *(const bls::Id*)id); } -void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *const *pubVec, const blsId *const *idVec, size_t n) +void blsPublicKeyRecover(blsPublicKey *pub, const blsPublicKey *pubVec, const blsId *idVec, size_t n) { - ((bls::PublicKey*)pub)->recover((const bls::PublicKey* const *)pubVec, (const bls::Id* const *)idVec, n); + ((bls::PublicKey*)pub)->recover((const bls::PublicKey*)pubVec, (const bls::Id*)idVec, n); } blsSign *blsSignCreate() @@ -215,9 +215,9 @@ void blsSignAdd(blsSign *sign, const blsSign *rhs) { ((bls::Sign*)sign)->add(*(const bls::Sign*)rhs); } -void blsSignRecover(blsSign *sign, const blsSign *const *signVec, const blsId *const *idVec, size_t n) +void blsSignRecover(blsSign *sign, const blsSign *signVec, const blsId *idVec, size_t n) { - ((bls::Sign*)sign)->recover((const bls::Sign* const *)signVec, (const bls::Id* const *)idVec, n); + ((bls::Sign*)sign)->recover((const bls::Sign*)signVec, (const bls::Id*)idVec, n); } int blsSignVerify(const blsSign *sign, const blsPublicKey *pub, const char *m, size_t size) |