From f3a6086363cccd41f940a30d0384580dcc65ba3b Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 31 Aug 2016 09:38:12 +0900 Subject: remove id from the other class --- include/bls.hpp | 23 ++++++++--------- src/bls.cpp | 77 ++++++++++++++++++++++--------------------------------- test/bls_test.cpp | 61 ++++++++++++++++++++++++++++--------------- 3 files changed, 82 insertions(+), 79 deletions(-) diff --git a/include/bls.hpp b/include/bls.hpp index 306dbfa..78c2c9c 100644 --- a/include/bls.hpp +++ b/include/bls.hpp @@ -57,7 +57,7 @@ typedef std::vector IdVec; class Id { impl::Id *self_; template - friend void LagrangeInterpolation(G& r, const T& vec); + friend void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec); friend class PublicKey; friend class SecretKey; public: @@ -83,11 +83,10 @@ public: class SecretKey { impl::SecretKey *self_; template - friend void LagrangeInterpolation(G& r, const T& vec); + friend void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec); template friend struct Wrap; public: - Id id; // master if id = 0, shared if id > 0 SecretKey(); ~SecretKey(); SecretKey(const SecretKey& rhs); @@ -123,9 +122,9 @@ public: /* recover secretKey from k secVec */ - void recover(const SecretKeyVec& secVec); + void recover(const SecretKeyVec& secVec, const IdVec& idVec); /* - add secret key only if id_ == 0 + add secret key */ void add(const SecretKey& rhs); }; @@ -138,11 +137,10 @@ class PublicKey { friend class SecretKey; friend class Sign; template - friend void LagrangeInterpolation(G& r, const T& vec); + friend void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec); template friend struct Wrap; public: - Id id; PublicKey(); ~PublicKey(); PublicKey(const PublicKey& rhs); @@ -159,9 +157,9 @@ public: /* recover publicKey from k pubVec */ - void recover(const PublicKeyVec& pubVec); + void recover(const PublicKeyVec& pubVec, const IdVec& idVec); /* - add public key only if id_ == 0 + add public key */ void add(const PublicKey& rhs); }; @@ -174,9 +172,8 @@ class Sign { friend class PublicKey; friend class SecretKey; template - friend void LagrangeInterpolation(G& r, const T& vec); + friend void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec); public: - Id id; Sign(); ~Sign(); Sign(const Sign& rhs); @@ -193,9 +190,9 @@ public: /* recover sign from k signVec */ - void recover(const SignVec& signVec); + void recover(const SignVec& signVec, const IdVec& idVec); /* - add signature key only if id_ == 0 + add signature */ void add(const Sign& rhs); }; diff --git a/src/bls.cpp b/src/bls.cpp index b6d12f9..c0add0b 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -132,24 +132,6 @@ static void calcDelta(FrVec& delta, const FrVec& S) } } -template -void LagrangeInterpolation(G& r, const T& vec) -{ - FrVec S(vec.size()); - for (size_t i = 0; i < vec.size(); i++) { - S[i] = vec[i].id.self_->v; - } - FrVec delta; - calcDelta(delta, S); - - r.clear(); - G t; - for (size_t i = 0; i < delta.size(); i++) { - G::mul(t, vec[i].self_->get(), delta[i]); - r += t; - } -} - namespace impl { struct Id { @@ -206,6 +188,25 @@ struct SecretKey { } // mcl::bls::impl +template +void LagrangeInterpolation(G& r, const T& vec, const IdVec& idVec) +{ + FrVec S(idVec.size()); + for (size_t i = 0; i < vec.size(); i++) { + S[i] = idVec[i].self_->v; + } + FrVec delta; + calcDelta(delta, S); + + r.clear(); + G t; + for (size_t i = 0; i < delta.size(); i++) { + G::mul(t, vec[i].self_->get(), delta[i]); + r += t; + } +} + + Id::Id(unsigned int id) : self_(new impl::Id()) { @@ -255,7 +256,6 @@ void Id::set(const uint64_t *p) Sign::Sign() : self_(new impl::Sign()) - , id(0) { } @@ -266,20 +266,18 @@ Sign::~Sign() Sign::Sign(const Sign& rhs) : self_(new impl::Sign(*rhs.self_)) - , id(rhs.id) { } Sign& Sign::operator=(const Sign& rhs) { *self_ = *rhs.self_; - id = rhs.id; return *this; } bool Sign::operator==(const Sign& rhs) const { - return id == rhs.id && self_->sHm == rhs.self_->sHm; + return self_->sHm == rhs.self_->sHm; } std::ostream& operator<<(std::ostream& os, const Sign& s) @@ -302,23 +300,21 @@ bool Sign::verify(const PublicKey& pub) const pub.getStr(str); return verify(pub, str); } -void Sign::recover(const SignVec& signVec) +void Sign::recover(const SignVec& signVec, const IdVec& idVec) { + if (signVec.size() != idVec.size()) throw cybozu::Exception("Sign:recover:bad size") << signVec.size() << idVec.size(); G1 sHm; - LagrangeInterpolation(sHm, signVec); + LagrangeInterpolation(sHm, signVec, idVec); self_->sHm = sHm; - id = 0; } void Sign::add(const Sign& rhs) { - if (!id.isZero() || !rhs.id.isZero()) throw cybozu::Exception("bls:Sign:add:bad id") << id << rhs.id; self_->sHm += rhs.self_->sHm; } PublicKey::PublicKey() : self_(new impl::PublicKey()) - , id(0) { } @@ -329,20 +325,18 @@ PublicKey::~PublicKey() PublicKey::PublicKey(const PublicKey& rhs) : self_(new impl::PublicKey(*rhs.self_)) - , id(rhs.id) { } PublicKey& PublicKey::operator=(const PublicKey& rhs) { *self_ = *rhs.self_; - id = rhs.id; return *this; } bool PublicKey::operator==(const PublicKey& rhs) const { - return id == rhs.id && self_->sQ == rhs.self_->sQ; + return self_->sQ == rhs.self_->sQ; } std::ostream& operator<<(std::ostream& os, const PublicKey& pub) @@ -366,26 +360,23 @@ void PublicKey::set(const PublicKeyVec& mpk, const Id& id) { Wrap w(mpk); evalPoly(self_->sQ,id.self_->v, w); - this->id = id; } -void PublicKey::recover(const PublicKeyVec& pubVec) +void PublicKey::recover(const PublicKeyVec& pubVec, const IdVec& idVec) { G2 sQ; - LagrangeInterpolation(sQ, pubVec); + if (pubVec.size() != idVec.size()) throw cybozu::Exception("PublicKey:recover:bad size") << pubVec.size() << idVec.size(); + LagrangeInterpolation(sQ, pubVec, idVec); self_->sQ = sQ; - id = 0; } void PublicKey::add(const PublicKey& rhs) { - if (!id.isZero() || !rhs.id.isZero()) throw cybozu::Exception("bls:PublicKey:add:bad id") << id << rhs.id; self_->sQ += rhs.self_->sQ; } SecretKey::SecretKey() : self_(new impl::SecretKey()) - , id(0) { } @@ -396,20 +387,18 @@ SecretKey::~SecretKey() SecretKey::SecretKey(const SecretKey& rhs) : self_(new impl::SecretKey(*rhs.self_)) - , id(rhs.id) { } SecretKey& SecretKey::operator=(const SecretKey& rhs) { *self_ = *rhs.self_; - id = rhs.id; return *this; } bool SecretKey::operator==(const SecretKey& rhs) const { - return id == rhs.id && self_->s == rhs.self_->s; + return self_->s == rhs.self_->s; } std::ostream& operator<<(std::ostream& os, const SecretKey& sec) @@ -435,13 +424,11 @@ void SecretKey::set(const uint64_t *p) void SecretKey::getPublicKey(PublicKey& pub) const { self_->getPublicKey(*pub.self_); - pub.id = id; } void SecretKey::sign(Sign& sign, const std::string& m) const { self_->sign(*sign.self_, m); - sign.id = id; } void SecretKey::getPop(Sign& pop) const @@ -467,20 +454,18 @@ void SecretKey::set(const SecretKeyVec& msk, const Id& id) { Wrap w(msk); evalPoly(self_->s, id.self_->v, w); - this->id = id; } -void SecretKey::recover(const SecretKeyVec& secVec) +void SecretKey::recover(const SecretKeyVec& secVec, const IdVec& idVec) { Fr s; - LagrangeInterpolation(s, secVec); + if (secVec.size() != idVec.size()) throw cybozu::Exception("SecretKey:recover:bad size") << secVec.size() << idVec.size(); + LagrangeInterpolation(s, secVec, idVec); self_->s = s; - id = 0; } void SecretKey::add(const SecretKey& rhs) { - if (!id.isZero() || !rhs.id.isZero()) throw cybozu::Exception("bls:SecretKey:add:bad id") << id << rhs.id; self_->s += rhs.self_->s; } diff --git a/test/bls_test.cpp b/test/bls_test.cpp index 186cb5c..59cad31 100644 --- a/test/bls_test.cpp +++ b/test/bls_test.cpp @@ -7,10 +7,10 @@ template void streamTest(const T& t) { std::ostringstream oss; - oss << t.id << ' ' << t; + oss << t; std::istringstream iss(oss.str()); T t2; - iss >> t2.id >> t2; + iss >> t2; CYBOZU_TEST_EQUAL(t, t2); } @@ -73,16 +73,14 @@ CYBOZU_TEST_AUTO(k_of_n) sec0.getMasterSecretKey(msk, k); bls::SecretKeyVec allPrvVec(n); + bls::IdVec allIdVec(n); for (int i = 0; i < n; i++) { int id = i + 1; allPrvVec[i].set(msk, id); - } - CYBOZU_TEST_EQUAL(allPrvVec.size(), n); - for (int i = 0; i < n; i++) { - CYBOZU_TEST_EQUAL(allPrvVec[i].id, i + 1); + allIdVec[i] = id; } - std::vector allSignVec(n); + bls::SignVec allSignVec(n); for (int i = 0; i < n; i++) { CYBOZU_TEST_ASSERT(allPrvVec[i] != sec0); allPrvVec[i].sign(allSignVec[i], m); @@ -96,15 +94,19 @@ CYBOZU_TEST_AUTO(k_of_n) 3-out-of-n can recover */ - std::vector secVec(3); + bls::SecretKeyVec secVec(3); + bls::IdVec idVec(3); for (int a = 0; a < n; a++) { secVec[0] = allPrvVec[a]; + idVec[0] = allIdVec[a]; for (int b = a + 1; b < n; b++) { secVec[1] = allPrvVec[b]; + idVec[1] = allIdVec[b]; for (int c = b + 1; c < n; c++) { secVec[2] = allPrvVec[c]; + idVec[2] = allIdVec[c]; bls::SecretKey sec; - sec.recover(secVec); + sec.recover(secVec, idVec); CYBOZU_TEST_EQUAL(sec, sec0); } } @@ -113,8 +115,11 @@ CYBOZU_TEST_AUTO(k_of_n) secVec[0] = allPrvVec[0]; secVec[1] = allPrvVec[1]; secVec[2] = allPrvVec[0]; // same of secVec[0] + idVec[0] = allIdVec[0]; + idVec[1] = allIdVec[1]; + idVec[2] = allIdVec[0]; bls::SecretKey sec; - CYBOZU_TEST_EXCEPTION_MESSAGE(sec.recover(secVec), std::exception, "same id"); + CYBOZU_TEST_EXCEPTION_MESSAGE(sec.recover(secVec, idVec), std::exception, "same id"); } { /* @@ -122,7 +127,7 @@ CYBOZU_TEST_AUTO(k_of_n) can recover */ bls::SecretKey sec; - sec.recover(allPrvVec); + sec.recover(allPrvVec, allIdVec); CYBOZU_TEST_EQUAL(sec, sec0); } /* @@ -130,12 +135,15 @@ CYBOZU_TEST_AUTO(k_of_n) can't recover */ secVec.resize(2); + idVec.resize(2); for (int a = 0; a < n; a++) { secVec[0] = allPrvVec[a]; + idVec[0] = allIdVec[a]; for (int b = a + 1; b < n; b++) { secVec[1] = allPrvVec[b]; + idVec[1] = allIdVec[b]; bls::SecretKey sec; - sec.recover(secVec); + sec.recover(secVec, idVec); CYBOZU_TEST_ASSERT(sec != sec0); } } @@ -143,15 +151,19 @@ CYBOZU_TEST_AUTO(k_of_n) 3-out-of-n can recover */ - std::vector signVec(3); + bls::SignVec signVec(3); + idVec.resize(3); for (int a = 0; a < n; a++) { signVec[0] = allSignVec[a]; + idVec[0] = allIdVec[a]; for (int b = a + 1; b < n; b++) { signVec[1] = allSignVec[b]; + idVec[1] = allIdVec[b]; for (int c = b + 1; c < n; c++) { signVec[2] = allSignVec[c]; + idVec[2] = allIdVec[c]; bls::Sign s; - s.recover(signVec); + s.recover(signVec, idVec); CYBOZU_TEST_EQUAL(s, s0); } } @@ -162,7 +174,7 @@ CYBOZU_TEST_AUTO(k_of_n) can recover */ bls::Sign s; - s.recover(allSignVec); + s.recover(allSignVec, allIdVec); CYBOZU_TEST_EQUAL(s, s0); } /* @@ -170,24 +182,29 @@ CYBOZU_TEST_AUTO(k_of_n) can't recover */ signVec.resize(2); + idVec.resize(2); for (int a = 0; a < n; a++) { signVec[0] = allSignVec[a]; + idVec[0] = allIdVec[a]; for (int b = a + 1; b < n; b++) { signVec[1] = allSignVec[b]; + idVec[1] = allIdVec[b]; bls::Sign s; - s.recover(signVec); + s.recover(signVec, idVec); CYBOZU_TEST_ASSERT(s != s0); } } // share and recover publicKey { - std::vector pubVec(k); + bls::PublicKeyVec pubVec(k); + idVec.resize(k); // select [0, k) publicKey for (int i = 0; i < k; i++) { allPrvVec[i].getPublicKey(pubVec[i]); + idVec[i] = allIdVec[i]; } bls::PublicKey pub; - pub.recover(pubVec); + pub.recover(pubVec, idVec); CYBOZU_TEST_EQUAL(pub, pub0); } } @@ -240,11 +257,15 @@ CYBOZU_TEST_AUTO(pop) } secVec.resize(k); sVec.resize(k); + bls::IdVec idVec(k); + for (int i = 0; i < k; i++) { + idVec[i] = idTbl[i]; + } bls::SecretKey sec; - sec.recover(secVec); + sec.recover(secVec, idVec); CYBOZU_TEST_EQUAL(sec, sec0); bls::Sign s; - s.recover(sVec); + s.recover(sVec, idVec); CYBOZU_TEST_EQUAL(s, s0); } -- cgit v1.2.3