diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2016-08-27 21:36:08 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2016-08-27 21:36:08 +0800 |
commit | 4d956aa85c0afcb6278b25a84b162349c8a00750 (patch) | |
tree | 8e929ac46583d01b357701c1e242a5f55e41ad14 | |
parent | fb67a31a6c7720e9499301127bfe57d5342c1d35 (diff) | |
download | tangerine-bls-4d956aa85c0afcb6278b25a84b162349c8a00750.tar.gz tangerine-bls-4d956aa85c0afcb6278b25a84b162349c8a00750.tar.zst tangerine-bls-4d956aa85c0afcb6278b25a84b162349c8a00750.zip |
change type of id to Id class
-rw-r--r-- | include/bls.hpp | 55 | ||||
-rw-r--r-- | sample/bls_smpl.cpp | 14 | ||||
-rw-r--r-- | src/bls.cpp | 98 | ||||
-rw-r--r-- | test/bls_test.cpp | 26 |
4 files changed, 149 insertions, 44 deletions
diff --git a/include/bls.hpp b/include/bls.hpp index d05449a..c905c06 100644 --- a/include/bls.hpp +++ b/include/bls.hpp @@ -17,6 +17,7 @@ namespace impl { struct SecretKey; struct PublicKey; struct Sign; +struct Id; } // bls::impl @@ -40,22 +41,46 @@ void init(); class SecretKey; class PublicKey; class Sign; +class Id; + /* value of secretKey and Id is less than r = 16798108731015832284940804142231733909759579603404752749028378864165570215949 */ -const size_t keySize = 32; +const size_t keySize = 4; // 256-bit size typedef std::vector<SecretKey> SecretKeyVec; typedef std::vector<PublicKey> PublicKeyVec; typedef std::vector<Sign> SignVec; +class Id { + impl::Id *self_; + template<class G, class T> + friend void LagrangeInterpolation(G& r, const T& vec); + friend class PublicKey; + friend class SecretKey; +public: + Id(unsigned int id = 0); + Id(const Id& rhs); + Id& operator=(const Id& rhs); + bool operator==(const Id& rhs) const; + bool operator!=(const Id& rhs) const { return !(*this == rhs); } + friend std::ostream& operator<<(std::ostream& os, const Id& id); + friend std::istream& operator>>(std::istream& is, Id& id); + bool isZero() const; + /* + set p[0, .., keySize) if p != 0 + @note the value should be less than r or truncated in [0, r) + */ + void set(const uint64_t *p = 0); +}; + /* s ; secret key */ class SecretKey { impl::SecretKey *self_; - int id_; // master if id_ = 0, shared if id_ > 0 + Id id_; // master if id_ = 0, shared if id_ > 0 template<class G, class T> friend void LagrangeInterpolation(G& r, const T& vec); template<class T, class G> @@ -67,15 +92,18 @@ public: SecretKey& operator=(const SecretKey& rhs); bool operator==(const SecretKey& rhs) const; bool operator!=(const SecretKey& rhs) const { return !(*this == rhs); } - int getId() const { return id_; } + const Id& getId() const { return id_; } friend std::ostream& operator<<(std::ostream& os, const SecretKey& sec); friend std::istream& operator>>(std::istream& is, SecretKey& sec); /* - make a secret key for id = 0 - set p[keySize] if p != 0 - @note the value should be less than r + initialize secretKey with random number and set id = 0 */ - void init(const uint64_t *p = 0); + void init(); + /* + set secretKey with p[0, .., keySize) and set id = 0 + @note the value should be less than r or truncated in [0, r) + */ + void set(const uint64_t *p); void getPublicKey(PublicKey& pub) const; void sign(Sign& sign, const std::string& m) const; /* @@ -90,7 +118,7 @@ public: /* set a secret key for id > 0 from msk */ - void set(const SecretKeyVec& msk, int id); + void set(const SecretKeyVec& msk, const Id& id); /* recover secretKey from k secVec */ @@ -106,7 +134,7 @@ public: */ class PublicKey { impl::PublicKey *self_; - int id_; + Id id_; friend class SecretKey; friend class Sign; template<class G, class T> @@ -120,14 +148,14 @@ public: PublicKey& operator=(const PublicKey& rhs); bool operator==(const PublicKey& rhs) const; bool operator!=(const PublicKey& rhs) const { return !(*this == rhs); } - int getId() const { return id_; } + const Id& getId() const { return id_; } friend std::ostream& operator<<(std::ostream& os, const PublicKey& pub); friend std::istream& operator>>(std::istream& is, PublicKey& pub); void getStr(std::string& str) const; /* set public for id from mpk */ - void set(const PublicKeyVec& mpk, int id); + void set(const PublicKeyVec& mpk, const Id& id); /* recover publicKey from k pubVec */ @@ -143,7 +171,7 @@ public: */ class Sign { impl::Sign *self_; - int id_; + Id id_; friend class PublicKey; friend class SecretKey; template<class G, class T> @@ -155,7 +183,7 @@ public: Sign& operator=(const Sign& rhs); bool operator==(const Sign& rhs) const; bool operator!=(const Sign& rhs) const { return !(*this == rhs); } - int getId() const { return id_; } + const Id& getId() const { return id_; } friend std::ostream& operator<<(std::ostream& os, const Sign& s); friend std::istream& operator>>(std::istream& is, Sign& s); bool verify(const PublicKey& pub, const std::string& m) const; @@ -173,7 +201,6 @@ public: void add(const Sign& rhs); }; - /* make master public key [s_0 Q, ..., s_{k-1} Q] from msk */ diff --git a/sample/bls_smpl.cpp b/sample/bls_smpl.cpp index 7c08a39..28d2c04 100644 --- a/sample/bls_smpl.cpp +++ b/sample/bls_smpl.cpp @@ -9,15 +9,17 @@ const std::string pubFile = "sample/publickey"; const std::string secFile = "sample/secretkey"; const std::string signFile = "sample/sign"; -std::string makeName(const std::string& name, int id) +std::string makeName(const std::string& name, const bls::Id& id) { const std::string suf = ".txt"; - if (id == 0) return name + suf; - return name + cybozu::itoa(id) + suf; + if (id.isZero()) return name + suf; + std::ostringstream os; + os << name << id << suf; + return os.str(); } template<class T> -void save(const std::string& file, const T& t, int id = 0) +void save(const std::string& file, const T& t, const bls::Id& id = 0) { const std::string name = makeName(file, id); std::ofstream ofs(name.c_str(), std::ios::binary); @@ -27,7 +29,7 @@ void save(const std::string& file, const T& t, int id = 0) } template<class T> -void load(T& t, const std::string& file, int id = 0) +void load(T& t, const std::string& file, const bls::Id& id = 0) { const std::string name = makeName(file, id); std::ifstream ifs(name.c_str(), std::ios::binary); @@ -87,7 +89,7 @@ int share(int n, int k) secVec[i].set(msk, i + 1); } for (int i = 0; i < n; i++) { - int id = secVec[i].getId(); + const bls::Id& id = secVec[i].getId(); save(secFile, secVec[i], id); bls::PublicKey pub; secVec[i].getPublicKey(pub); diff --git a/src/bls.cpp b/src/bls.cpp index 87a78f4..dc78c69 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -18,7 +18,6 @@ typedef BN::Fp6 Fp6; typedef BN::Fp12 Fp12; typedef BN::G1 G1; typedef BN::G2 G2; -typedef std::vector<int> IntVec; struct FrTag; typedef mcl::FpT<FrTag, 256> Fr; @@ -99,10 +98,10 @@ struct Polynomial { } } // y = f(id) - void eval(Fr& y, int id) const + void eval(Fr& y, const Fr& id) const { - if (id == 0) throw cybozu::Exception("bls:Polynomial:eval:id is zero"); - evalPoly(y, Fr(id), c); + if (id.isZero()) throw cybozu::Exception("bls:Polynomial:eval:id is zero"); + evalPoly(y, id, c); } }; @@ -110,7 +109,7 @@ struct Polynomial { delta_{i,S}(0) = prod_{j != i} S[j] / (S[j] - S[i]) = a / b where a = prod S[j], b = S[i] * prod_{j != i} (S[j] - S[i]) */ -static void calcDelta(FrVec& delta, const IntVec& S) +static void calcDelta(FrVec& delta, const FrVec& S) { const size_t k = S.size(); if (k < 2) throw cybozu::Exception("bls:calcDelta:bad size") << k; @@ -123,8 +122,8 @@ static void calcDelta(FrVec& delta, const IntVec& S) Fr b = S[i]; for (size_t j = 0; j < k; j++) { if (j != i) { - int v = S[j] - S[i]; - if (v == 0) throw cybozu::Exception("bls:calcDelta:S has same id") << i << j; + Fr v = S[j] - S[i]; + if (v.isZero()) throw cybozu::Exception("bls:calcDelta:S has same id") << i << j; b *= v; } } @@ -135,9 +134,9 @@ static void calcDelta(FrVec& delta, const IntVec& S) template<class G, class T> void LagrangeInterpolation(G& r, const T& vec) { - IntVec S(vec.size()); + FrVec S(vec.size()); for (size_t i = 0; i < vec.size(); i++) { - S[i] = vec[i].getId(); + S[i] = vec[i].getId().self_->v; } FrVec delta; calcDelta(delta, S); @@ -152,6 +151,10 @@ void LagrangeInterpolation(G& r, const T& vec) namespace impl { +struct Id { + Fr v; +}; + struct Sign { G1 sHm; // s Hash(m) const G1& get() const { return sHm; } @@ -180,13 +183,13 @@ inline bool Sign::verify(const PublicKey& pub, const std::string& m) const struct SecretKey { Fr s; const Fr& get() const { return s; } - void init(const uint64_t *p) + void set(const uint64_t *p) { - if (p) { - s.setArrayMask(p, keySize); - } else { - s.setRand(getRG()); - } + s.setArrayMask(p, keySize); + } + void init() + { + s.setRand(getRG()); } void getPublicKey(PublicKey& pub) const { @@ -202,6 +205,48 @@ struct SecretKey { } // mcl::bls::impl +Id::Id(unsigned int id) + : self_(new impl::Id()) +{ + self_->v = id; +} + +Id::Id(const Id& rhs) + : self_(new impl::Id(*rhs.self_)) +{ +} + +Id& Id::operator=(const Id& rhs) +{ + *self_ = *rhs.self_; + return *this; +} + +bool Id::operator==(const Id& rhs) const +{ + return self_->v == rhs.self_->v; +} + +std::ostream& operator<<(std::ostream& os, const Id& id) +{ + return os << id.self_->v; +} + +std::istream& operator>>(std::istream& is, Id& id) +{ + return is >> id.self_->v; +} + +bool Id::isZero() const +{ + return self_->v.isZero(); +} + +void Id::set(const uint64_t *p) +{ + self_->v.setArrayMask(p, keySize); +} + Sign::Sign() : self_(new impl::Sign()) , id_(0) @@ -261,7 +306,7 @@ void Sign::recover(const SignVec& signVec) void Sign::add(const Sign& rhs) { - if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:Sign:add:bad id") << id_ << rhs.id_; + if (!id_.isZero() || !rhs.id_.isZero()) throw cybozu::Exception("bls:Sign:add:bad id") << id_ << rhs.id_; self_->sHm += rhs.self_->sHm; } @@ -311,10 +356,10 @@ void PublicKey::getStr(std::string& str) const str = os.str(); } -void PublicKey::set(const PublicKeyVec& mpk, int id) +void PublicKey::set(const PublicKeyVec& mpk, const Id& id) { Wrap<PublicKey, G2> w(mpk); - evalPoly(self_->sQ, Fr(id), w); + evalPoly(self_->sQ,id.self_->v, w); id_ = id; } @@ -328,7 +373,7 @@ void PublicKey::recover(const PublicKeyVec& pubVec) void PublicKey::add(const PublicKey& rhs) { - if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:PublicKey:add:bad id") << id_ << rhs.id_; + if (!id_.isZero() || !rhs.id_.isZero()) throw cybozu::Exception("bls:PublicKey:add:bad id") << id_ << rhs.id_; self_->sQ += rhs.self_->sQ; } @@ -371,9 +416,14 @@ std::istream& operator>>(std::istream& is, SecretKey& sec) return is >> sec.id_ >> sec.self_->s; } -void SecretKey::init(const uint64_t *p) +void SecretKey::init() +{ + self_->init(); +} + +void SecretKey::set(const uint64_t *p) { - self_->init(p); + self_->set(p); } void SecretKey::getPublicKey(PublicKey& pub) const @@ -407,10 +457,10 @@ void SecretKey::getMasterSecretKey(SecretKeyVec& msk, int k) const } } -void SecretKey::set(const SecretKeyVec& msk, int id) +void SecretKey::set(const SecretKeyVec& msk, const Id& id) { Wrap<SecretKey, Fr> w(msk); - evalPoly(self_->s, id, w); + evalPoly(self_->s, id.self_->v, w); id_ = id; } @@ -424,7 +474,7 @@ void SecretKey::recover(const SecretKeyVec& secVec) void SecretKey::add(const SecretKey& rhs) { - if (id_ != 0 || rhs.id_ != 0) throw cybozu::Exception("bls:SecretKey:add:bad id") << id_ << rhs.id_; + 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 0c801c6..c7bdb68 100644 --- a/test/bls_test.cpp +++ b/test/bls_test.cpp @@ -13,6 +13,7 @@ void streamTest(const T& t) iss >> t2; CYBOZU_TEST_EQUAL(t, t2); } + CYBOZU_TEST_AUTO(bls) { bls::init(); @@ -33,6 +34,31 @@ CYBOZU_TEST_AUTO(bls) } } +CYBOZU_TEST_AUTO(id) +{ + bls::Id id; + CYBOZU_TEST_ASSERT(id.isZero()); + id = 5; + CYBOZU_TEST_EQUAL(id, 5); + { + const uint64_t id1[] = { 1, 2, 3, 4 }; + id.set(id1); + std::ostringstream os; + os << std::hex << id; + CYBOZU_TEST_EQUAL(os.str(), "4000000000000000300000000000000020000000000000001"); + } + { + /* + truncate the value in [0, r) + */ + const uint64_t id1[] = { uint64_t(-1), uint64_t(-1), uint64_t(-1), uint64_t(-1) }; + id.set(id1); + std::ostringstream os; + os << std::hex << id; + CYBOZU_TEST_ASSERT(os.str() != "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); + } +} + CYBOZU_TEST_AUTO(k_of_n) { const std::string m = "abc"; |