diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2016-09-06 21:11:59 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2016-09-06 21:11:59 +0800 |
commit | 8d9d3dc516a0c286baea1555c0af20b8db06740d (patch) | |
tree | 17ca0b5e57ad6cf4a615a0a4eb66a64adf7a347e | |
parent | afc01080302fb3348536782e0af39cfb3943867f (diff) | |
download | dexon-bls-8d9d3dc516a0c286baea1555c0af20b8db06740d.tar.gz dexon-bls-8d9d3dc516a0c286baea1555c0af20b8db06740d.tar.zst dexon-bls-8d9d3dc516a0c286baea1555c0af20b8db06740d.zip |
add recover/set for C api
-rw-r--r-- | include/bls.hpp | 32 | ||||
-rw-r--r-- | src/bls.cpp | 52 | ||||
-rw-r--r-- | test/bls_test.cpp | 29 |
3 files changed, 95 insertions, 18 deletions
diff --git a/include/bls.hpp b/include/bls.hpp index cf251d5..7745f28 100644 --- a/include/bls.hpp +++ b/include/bls.hpp @@ -58,10 +58,15 @@ typedef std::vector<Id> IdVec; class Id { impl::Id *self_; - template<class G, class T> - friend void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec); friend class PublicKey; friend class SecretKey; + friend class Sign; + template<class G, class V1, class V2> + friend void LagrangeInterpolation(G& r, const V1& vec, const V2& S); + template<class T, class G> + friend struct Wrap; + template<class T, class G> + friend struct WrapPointer; public: Id(unsigned int id = 0); ~Id(); @@ -84,8 +89,8 @@ public: */ class SecretKey { impl::SecretKey *self_; - template<class G, class T> - friend void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec); + template<class G, class V1, class V2> + friend void LagrangeInterpolation(G& r, const V1& vec, const V2& S); template<class T, class G> friend struct Wrap; template<class T, class G> @@ -147,8 +152,8 @@ class PublicKey { impl::PublicKey *self_; friend class SecretKey; friend class Sign; - template<class G, class T> - friend void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec); + template<class G, class V1, class V2> + friend void LagrangeInterpolation(G& r, const V1& vec, const V2& S); template<class T, class G> friend struct Wrap; template<class T, class G> @@ -174,6 +179,10 @@ public: add public key */ 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); }; /* @@ -183,8 +192,12 @@ class Sign { impl::Sign *self_; friend class PublicKey; friend class SecretKey; - template<class G, class T> - friend void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec); + template<class G, class V1, class V2> + friend void LagrangeInterpolation(G& r, const V1& vec, const V2& S); + template<class T, class G> + friend struct Wrap; + template<class T, class G> + friend struct WrapPointer; public: Sign(); ~Sign(); @@ -207,6 +220,9 @@ public: add signature */ 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); }; /* diff --git a/src/bls.cpp b/src/bls.cpp index 3dfab45..a37834c 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -122,6 +122,7 @@ namespace impl { struct Id { Fr v; + const Fr& get() const { return v; } }; struct SecretKey { @@ -148,8 +149,8 @@ struct PublicKey { /* recover f(0) by { (x, y) | x = S[i], y = f(x) = vec[i] } */ -template<class G, class T> -void LagrangeInterpolation(G& r, const T& vec, const IdVec& S) +template<class G, class V1, class V2> +void LagrangeInterpolation(G& r, const V1& vec, const V2& S) { /* delta_{i,S}(0) = prod_{j != i} S[j] / (S[j] - S[i]) = a / b @@ -159,15 +160,15 @@ void LagrangeInterpolation(G& r, const T& vec, const IdVec& S) if (vec.size() != k) throw cybozu::Exception("bls:LagrangeInterpolation:bad size") << vec.size() << k; if (k < 2) throw cybozu::Exception("bls:LagrangeInterpolation:too small size") << k; FrVec delta(k); - Fr a = S[0].self_->v; + Fr a = S[0]; for (size_t i = 1; i < k; i++) { - a *= S[i].self_->v; + a *= S[i]; } for (size_t i = 0; i < k; i++) { - Fr b = S[i].self_->v; + Fr b = S[i]; for (size_t j = 0; j < k; j++) { if (j != i) { - Fr v = S[j].self_->v - S[i].self_->v; + Fr v = S[j] - S[i]; if (v.isZero()) throw cybozu::Exception("bls:LagrangeInterpolation:S has same id") << i << j; b *= v; } @@ -181,7 +182,7 @@ void LagrangeInterpolation(G& r, const T& vec, const IdVec& S) r.clear(); G t; for (size_t i = 0; i < delta.size(); i++) { - G::mul(t, vec[i].self_->get(), delta[i]); + G::mul(t, vec[i], delta[i]); r += t; } } @@ -296,7 +297,16 @@ bool Sign::verify(const PublicKey& pub) const void Sign::recover(const SignVec& signVec, const IdVec& idVec) { - LagrangeInterpolation(self_->sHm, signVec, idVec); + Wrap<Sign, G1> signW(signVec); + Wrap<Id, Fr> idW(idVec); + LagrangeInterpolation(self_->sHm, signW, idW); +} + +void Sign::recover(const Sign* const *signVec, const Id *const *idVec, size_t n) +{ + WrapPointer<Sign, G1> signW(signVec, n); + WrapPointer<Id, Fr> idW(idVec, n); + LagrangeInterpolation(self_->sHm, signW, idW); } void Sign::add(const Sign& rhs) @@ -346,9 +356,23 @@ void PublicKey::set(const PublicKeyVec& mpk, const Id& id) evalPoly(self_->sQ,id.self_->v, w); } +void PublicKey::set(const PublicKey *const *mpk, size_t k, const Id& id) +{ + WrapPointer<PublicKey, G2> w(mpk, k); + evalPoly(self_->sQ,id.self_->v, w); +} + void PublicKey::recover(const PublicKeyVec& pubVec, const IdVec& idVec) { - LagrangeInterpolation(self_->sQ, pubVec, idVec); + Wrap<PublicKey, G2> pubW(pubVec); + Wrap<Id, Fr> idW(idVec); + LagrangeInterpolation(self_->sQ, pubW, idW); +} +void PublicKey::recover(const PublicKey *const *pubVec, const Id *const *idVec, size_t n) +{ + WrapPointer<PublicKey, G2> pubW(pubVec, n); + WrapPointer<Id, Fr> idW(idVec, n); + LagrangeInterpolation(self_->sQ, pubW, idW); } void PublicKey::add(const PublicKey& rhs) @@ -446,7 +470,15 @@ void SecretKey::set(const SecretKey *const *msk, size_t k, const Id& id) void SecretKey::recover(const SecretKeyVec& secVec, const IdVec& idVec) { - LagrangeInterpolation(self_->s, secVec, idVec); + Wrap<SecretKey, Fr> secW(secVec); + Wrap<Id, Fr> idW(idVec); + LagrangeInterpolation(self_->s, secW, idW); +} +void SecretKey::recover(const SecretKey *const *secVec, const Id *const *idVec, size_t n) +{ + WrapPointer<SecretKey, Fr> secW(secVec, n); + WrapPointer<Id, Fr> idW(idVec, n); + LagrangeInterpolation(self_->s, secW, idW); } void SecretKey::add(const SecretKey& rhs) diff --git a/test/bls_test.cpp b/test/bls_test.cpp index 47ba330..9d3a7cb 100644 --- a/test/bls_test.cpp +++ b/test/bls_test.cpp @@ -105,18 +105,29 @@ CYBOZU_TEST_AUTO(k_of_n) */ bls::SecretKeyVec secVec(3); bls::IdVec idVec(3); + std::vector<const bls::SecretKey*> psec(secVec.size()); + std::vector<const bls::Id*> pid(idVec.size()); for (int a = 0; a < n; a++) { secVec[0] = allPrvVec[a]; idVec[0] = allIdVec[a]; + psec[0] = &secVec[0]; + pid[0] = &idVec[0]; for (int b = a + 1; b < n; b++) { secVec[1] = allPrvVec[b]; idVec[1] = allIdVec[b]; + psec[1] = &secVec[1]; + pid[1] = &idVec[1]; for (int c = b + 1; c < n; c++) { secVec[2] = allPrvVec[c]; idVec[2] = allIdVec[c]; + psec[2] = &secVec[2]; + pid[2] = &idVec[2]; bls::SecretKey sec; sec.recover(secVec, idVec); CYBOZU_TEST_EQUAL(sec, sec0); + bls::SecretKey sec2; + sec2.recover(&psec[0], &pid[0], psec.size()); + CYBOZU_TEST_EQUAL(sec, sec2); } } } @@ -207,14 +218,21 @@ CYBOZU_TEST_AUTO(k_of_n) { bls::PublicKeyVec pubVec(k); idVec.resize(k); + std::vector<const bls::PublicKey*> ppub(k); + std::vector<const bls::Id*> pid(k); // select [0, k) publicKey for (int i = 0; i < k; i++) { allPrvVec[i].getPublicKey(pubVec[i]); idVec[i] = allIdVec[i]; + ppub[i] = &pubVec[i]; + pid[i] = &idVec[i]; } bls::PublicKey pub; pub.recover(pubVec, idVec); CYBOZU_TEST_EQUAL(pub, pub0); + bls::PublicKey pub2; + pub2.recover(&ppub[0], &pid[0], ppub.size()); + CYBOZU_TEST_EQUAL(pub, pub2); } } @@ -276,6 +294,17 @@ CYBOZU_TEST_AUTO(pop) bls::Sign s; s.recover(sVec, idVec); CYBOZU_TEST_EQUAL(s, s0); + std::vector<const bls::Sign*> ps(sVec.size()); + for (size_t i = 0; i < ps.size(); i++) { + ps[i] = &sVec[i]; + } + std::vector<const bls::Id*> pid(idVec.size()); + for (size_t i = 0; i < pid.size(); i++) { + pid[i] = &idVec[i]; + } + bls::Sign s2; + s2.recover(&ps[0], &pid[0], ps.size()); + CYBOZU_TEST_EQUAL(s, s2); } CYBOZU_TEST_AUTO(add) |