diff options
-rw-r--r-- | include/bls.hpp | 20 | ||||
-rw-r--r-- | src/bls.cpp | 18 | ||||
-rw-r--r-- | test/bls_test.cpp | 20 |
3 files changed, 56 insertions, 2 deletions
diff --git a/include/bls.hpp b/include/bls.hpp index 23bff1c..0b8279a 100644 --- a/include/bls.hpp +++ b/include/bls.hpp @@ -49,11 +49,14 @@ public: int getId() const { return id_; } friend std::ostream& operator<<(std::ostream& os, const Sign& s); friend std::istream& operator>>(std::istream& is, Sign& s); - /* recover sign from k signVec */ void recover(const std::vector<Sign>& signVec); + /* + add signature key only if id_ == 0 + */ + void add(const Sign& rhs); }; /* @@ -106,8 +109,15 @@ public: validate self by MasterPublicKey */ bool isValid(const MasterPublicKey& mpk) const; + /* + add public key only if id_ == 0 + */ + void add(const PublicKey& rhs); }; +/* + s ; private key +*/ class PrivateKey { impl::PrivateKey *self_; int id_; // master if id_ = 0, shared if id_ > 0 @@ -135,6 +145,14 @@ public: recover privateKey from k prvVec */ void recover(const std::vector<PrivateKey>& prvVec); + /* + add private key only if id_ == 0 + */ + void add(const PrivateKey& rhs); }; +inline Sign operator+(const Sign& a, const Sign& b) { Sign r(a); r.add(b); return r; } +inline PublicKey operator+(const PublicKey& a, const PublicKey& b) { PublicKey r(a); r.add(b); return r; } +inline PrivateKey operator+(const PrivateKey& a, const PrivateKey& b) { PrivateKey r(a); r.add(b); return r; } + } //bls diff --git a/src/bls.cpp b/src/bls.cpp index ecbb59e..6bc5fc6 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -236,6 +236,12 @@ void Sign::recover(const std::vector<Sign>& signVec) id_ = 0; } +void Sign::add(const Sign& rhs) +{ + if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:Sign:add:bad id") << id_ << rhs.id_; + self_->sHm += rhs.self_->sHm; +} + MasterPublicKey::MasterPublicKey() : self_(new impl::MasterPublicKey()) { @@ -342,6 +348,12 @@ bool PublicKey::isValid(const MasterPublicKey& mpk) const return v == self_->sQ; } +void PublicKey::add(const PublicKey& rhs) +{ + if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:PublicKey:add:bad id") << id_ << rhs.id_; + self_->sQ += rhs.self_->sQ; +} + PrivateKey::PrivateKey() : self_(new impl::PrivateKey()) , id_(0) @@ -426,4 +438,10 @@ void PrivateKey::recover(const std::vector<PrivateKey>& prvVec) id_ = 0; } +void PrivateKey::add(const PrivateKey& rhs) +{ + if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:PrivateKey:add:bad id") << id_ << rhs.id_; + self_->s += rhs.self_->s; +} + } // bls diff --git a/test/bls_test.cpp b/test/bls_test.cpp index 64c04c2..dd948b4 100644 --- a/test/bls_test.cpp +++ b/test/bls_test.cpp @@ -163,7 +163,7 @@ CYBOZU_TEST_AUTO(k_of_n) } } -CYBOZU_TEST_AUTO(verifier) +CYBOZU_TEST_AUTO(MasterPublicKey) { const int n = 6; const int k = 3; @@ -182,3 +182,21 @@ CYBOZU_TEST_AUTO(verifier) } streamTest(mpk); } + +CYBOZU_TEST_AUTO(add) +{ + bls::PrivateKey prv1, prv2; + prv1.init(); + prv2.init(); + CYBOZU_TEST_ASSERT(prv1 != prv2); + + bls::PublicKey pub1, pub2; + prv1.getPublicKey(pub1); + prv2.getPublicKey(pub2); + + const std::string m = "doremi"; + bls::Sign s1, s2; + prv1.sign(s1, m); + prv2.sign(s2, m); + CYBOZU_TEST_ASSERT((pub1 + pub2).verify(s1 + s2, m)); +} |