diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2016-08-21 14:34:52 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2016-08-21 14:34:52 +0800 |
commit | 76d28310b055dd596f4cd67fc7de49624948a85f (patch) | |
tree | 03ada7508d6e3301795437a37f23387951505222 /src | |
parent | ade6ac7396e497b1509bec9234440029ad4323cf (diff) | |
download | dexon-bls-76d28310b055dd596f4cd67fc7de49624948a85f.tar.gz dexon-bls-76d28310b055dd596f4cd67fc7de49624948a85f.tar.zst dexon-bls-76d28310b055dd596f4cd67fc7de49624948a85f.zip |
design IF again
Diffstat (limited to 'src')
-rw-r--r-- | src/bls.cpp | 165 |
1 files changed, 81 insertions, 84 deletions
diff --git a/src/bls.cpp b/src/bls.cpp index 949cdf6..b887469 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -65,8 +65,8 @@ static void HashAndMapToG1(G1& P, const std::string& m) mapToG1(P, t); } -template<class T, class G> -void evalPoly(G& y, const T& x, const std::vector<G>& c) +template<class T, class G, class Vec> +void evalPoly(G& y, const T& x, const Vec& c) { if (c.size() < 2) throw cybozu::Exception("bls:evalPoly:bad size") << c.size(); y = c[c.size() - 1]; @@ -76,6 +76,17 @@ void evalPoly(G& y, const T& x, const std::vector<G>& c) } } +template<class T, class G> +struct Wrap { + const std::vector<T> *pv; + Wrap(const std::vector<T>& v) : pv(&v) {} + const G& operator[](size_t i) const + { + return (*pv)[i].self_->get(); + } + size_t size() const { return pv->size(); } +}; + struct Polynomial { FrVec c; // f[x] = sum_{i=0}^{k-1} c[i] x^i void init(const Fr& s, int k) @@ -166,10 +177,6 @@ inline bool Sign::verify(const PublicKey& pub, const std::string& m) const return e1 == e2; } -struct MasterPublicKey { - std::vector<G2> vecR; -}; - struct PrivateKey { Fr s; const Fr& get() const { return s; } @@ -234,6 +241,12 @@ bool Sign::verify(const PublicKey& pub, const std::string& m) const { return self_->verify(*pub.self_, m); } +bool Sign::verify(const PublicKey& pub) const +{ + std::string str; + pub.getStr(str); + return verify(pub, str); +} void Sign::recover(const std::vector<Sign>& signVec) { G1 sHm; @@ -248,53 +261,6 @@ void Sign::add(const Sign& rhs) self_->sHm += rhs.self_->sHm; } -MasterPublicKey::MasterPublicKey() - : self_(new impl::MasterPublicKey()) -{ -} - -MasterPublicKey::~MasterPublicKey() -{ - delete self_; -} - -MasterPublicKey::MasterPublicKey(const MasterPublicKey& rhs) - : self_(new impl::MasterPublicKey(*rhs.self_)) -{ -} - -MasterPublicKey& MasterPublicKey::operator=(const MasterPublicKey& rhs) -{ - *self_ = *rhs.self_; - return *this; -} - -bool MasterPublicKey::operator==(const MasterPublicKey& rhs) const -{ - return self_->vecR == rhs.self_->vecR; -} - -std::ostream& operator<<(std::ostream& os, const MasterPublicKey& mpk) -{ - const size_t n = mpk.self_->vecR.size(); - os << n; - for (size_t i = 0; i < n; i++) { - os << '\n' << mpk.self_->vecR[i]; - } - return os; -} - -std::istream& operator>>(std::istream& is, MasterPublicKey& mpk) -{ - size_t n; - is >> n; - mpk.self_->vecR.resize(n); - for (size_t i = 0; i < n; i++) { - is >> mpk.self_->vecR[i]; - } - return is; -} - PublicKey::PublicKey() : self_(new impl::PublicKey()) , id_(0) @@ -334,6 +300,20 @@ std::istream& operator>>(std::istream& is, PublicKey& pub) return is >> pub.id_ >> pub.self_->sQ; } +void PublicKey::getStr(std::string& str) const +{ + std::ostringstream os; + os << *this; + str = os.str(); +} + +void PublicKey::set(const MasterPublicKey& mpk, int id) +{ + Wrap<PublicKey, G2> w(mpk); + evalPoly(self_->sQ, Fr(id), w); + id_ = id; +} + void PublicKey::recover(const std::vector<PublicKey>& pubVec) { G2 sQ; @@ -342,13 +322,6 @@ void PublicKey::recover(const std::vector<PublicKey>& pubVec) id_ = 0; } -bool PublicKey::isValid(const MasterPublicKey& mpk) const -{ - G2 v; - evalPoly(v, Fr(id_), mpk.self_->vecR); - 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_; @@ -384,6 +357,16 @@ bool PrivateKey::operator==(const PrivateKey& rhs) const return id_ == rhs.id_ && self_->s == rhs.self_->s; } +std::ostream& operator<<(std::ostream& os, const PrivateKey& prv) +{ + return os << prv.id_ << ' ' << prv.self_->s; +} + +std::istream& operator>>(std::istream& is, PrivateKey& prv) +{ + return is >> prv.id_ >> prv.self_->s; +} + void PrivateKey::init() { self_->init(); @@ -395,40 +378,34 @@ void PrivateKey::getPublicKey(PublicKey& pub) const pub.id_ = id_; } -std::ostream& operator<<(std::ostream& os, const PrivateKey& prv) +void PrivateKey::sign(Sign& sign, const std::string& m) const { - return os << prv.id_ << ' ' << prv.self_->s; + self_->sign(*sign.self_, m); + sign.id_ = id_; } -std::istream& operator>>(std::istream& is, PrivateKey& prv) +void PrivateKey::getPop(Sign& pop, const PublicKey& pub) const { - return is >> prv.id_ >> prv.self_->s; + std::string m; + pub.getStr(m); + sign(pop, m); } -void PrivateKey::sign(Sign& sign, const std::string& m) const +void PrivateKey::getMasterPrivateKey(MasterPrivateKey& msk, int k) const { - self_->sign(*sign.self_, m); - sign.id_ = id_; + if (k <= 1) throw cybozu::Exception("bls:PrivateKey:getMasterPrivateKey:bad k") << k; + msk.resize(k); + msk[0] = *this; + for (int i = 1; i < k; i++) { + msk[i].init(); + } } -void PrivateKey::share(std::vector<PrivateKey>& prvVec, int n, int k, MasterPublicKey *mpk) +void PrivateKey::set(const MasterPrivateKey& msk, int id) { - if (id_ != 0) throw cybozu::Exception("bls:PrivateKey:share:already shared") << id_; - if (n <= 0 || k <= 0 || k > n) throw cybozu::Exception("bls:PrivateKey:share:bad n, k") << n << k; - Polynomial poly; - poly.init(self_->s, k); - prvVec.resize(n); - for (int i = 0; i < n; i++) { - int id = i + 1; - poly.eval(prvVec[i].self_->s, id); - prvVec[i].id_ = id; - } - if (mpk == 0) return; - std::vector<G2>& vecR = mpk->self_->vecR; - vecR.resize(k); - for (size_t i = 0; i < vecR.size(); i++) { - G2::mul(vecR[i], getQ(), poly.c[i]); - } + Wrap<PrivateKey, Fr> w(msk); + evalPoly(self_->s, id, w); + id_ = id; } void PrivateKey::recover(const std::vector<PrivateKey>& prvVec) @@ -445,4 +422,24 @@ void PrivateKey::add(const PrivateKey& rhs) self_->s += rhs.self_->s; } +void getMasterPublicKey(MasterPublicKey& mpk, const MasterPrivateKey& msk) +{ + mpk.resize(msk.size()); + for (size_t i = 0; i < msk.size(); i++) { + msk[i].getPublicKey(mpk[i]); + } +} + +void getPopVec(std::vector<Sign>& popVec, const MasterPrivateKey& msk, const MasterPublicKey& mpk) +{ + if (msk.size() != mpk.size()) throw cybozu::Exception("bls:getPopVec:bad size") << msk.size() << mpk.size(); + const size_t n = msk.size(); + popVec.resize(n); + std::string m; + for (size_t i = 0; i < n; i++) { + mpk[i].getStr(m); + msk[i].sign(popVec[i], m); + } +} + } // bls |