diff --git a/src/elements.cpp b/src/elements.cpp index 1e2b7fb0e..7927895b7 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -82,12 +82,12 @@ G1Element G1Element::FromMessage(const std::vector& message, return FromMessage(Bytes(message), dst, dst_len); } -G1Element G1Element::FromMessage(const Bytes& message, +G1Element G1Element::FromMessage(const Bytes message, const uint8_t* dst, int dst_len) { G1Element ans; - ep_map_dst(ans.p, message.begin(), (int)message.size(), dst, dst_len); + ep_map_dst(ans.p, message.data(), (int)message.size(), dst, dst_len); ans.CheckValid(); return ans; } @@ -270,12 +270,12 @@ G2Element G2Element::FromMessage(const std::vector& message, return FromMessage(Bytes(message), dst, dst_len); } -G2Element G2Element::FromMessage(const Bytes& message, +G2Element G2Element::FromMessage(const Bytes message, const uint8_t* dst, int dst_len) { G2Element ans; - ep2_map_dst(ans.q, message.begin(), (int)message.size(), dst, dst_len); + ep2_map_dst(ans.q, message.data(), (int)message.size(), dst, dst_len); ans.CheckValid(); return ans; } diff --git a/src/elements.hpp b/src/elements.hpp index 997d5f862..efffd9b33 100644 --- a/src/elements.hpp +++ b/src/elements.hpp @@ -45,7 +45,7 @@ class G1Element { static G1Element FromMessage(const std::vector &message, const uint8_t *dst, int dst_len); - static G1Element FromMessage(const Bytes& message, + static G1Element FromMessage(const Bytes message, const uint8_t* dst, int dst_len); static G1Element Generator(); @@ -82,7 +82,7 @@ class G2Element { static G2Element FromMessage(const std::vector& message, const uint8_t* dst, int dst_len); - static G2Element FromMessage(const Bytes& message, + static G2Element FromMessage(const Bytes message, const uint8_t* dst, int dst_len); static G2Element Generator(); diff --git a/src/hdkeys.hpp b/src/hdkeys.hpp index 629ec5e3d..25fd5cef9 100644 --- a/src/hdkeys.hpp +++ b/src/hdkeys.hpp @@ -34,13 +34,7 @@ class HDKeys { **/ public: static const uint8_t HASH_LEN = 32; - - static PrivateKey KeyGen(const std::vector& seed) - { - return KeyGen(Bytes(seed)); - } - - static PrivateKey KeyGen(const Bytes& seed) + static PrivateKey KeyGen(const Bytes seed) { // KeyGen // 1. PRK = HKDF-Extract("BLS-SIG-KEYGEN-SALT-", IKM || I2OSP(0, 1)) diff --git a/src/schemes.cpp b/src/schemes.cpp index 199654429..d3cc4ba95 100644 --- a/src/schemes.cpp +++ b/src/schemes.cpp @@ -50,11 +50,7 @@ const std::string AugSchemeMPL::CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256 const std::string PopSchemeMPL::CIPHERSUITE_ID = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; const std::string PopSchemeMPL::POP_CIPHERSUITE_ID = "BLS_POP_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; -PrivateKey CoreMPL::KeyGen(const vector& seed) { - return HDKeys::KeyGen(seed); -} - -PrivateKey CoreMPL::KeyGen(const Bytes& seed) { +PrivateKey CoreMPL::KeyGen(const Bytes seed) { return HDKeys::KeyGen(seed); } @@ -68,38 +64,17 @@ G1Element CoreMPL::SkToG1(const PrivateKey &seckey) return seckey.GetG1Element(); } -G2Element CoreMPL::Sign(const PrivateKey &seckey, const vector &message) -{ - return CoreMPL::Sign(seckey, Bytes(message)); -} - -G2Element CoreMPL::Sign(const PrivateKey& seckey, const Bytes& message) +G2Element CoreMPL::Sign(const PrivateKey& seckey, const Bytes message) { return seckey.SignG2(message.begin(), message.size(), (const uint8_t*)strCiphersuiteId.c_str(), strCiphersuiteId.length()); } -bool CoreMPL::Verify(const vector &pubkey, - const vector &message, // unhashed - const vector &signature) -{ - return CoreMPL::Verify(G1Element::FromBytes(Bytes(pubkey)), - Bytes(message), - G2Element::FromBytes(Bytes(signature))); -} - -bool CoreMPL::Verify(const Bytes& pubkey, const Bytes& message, const Bytes& signature) +bool CoreMPL::Verify(const Bytes pubkey, const Bytes message, const Bytes signature) { return CoreMPL::Verify(G1Element::FromBytes(pubkey), message, G2Element::FromBytes(signature)); } -bool CoreMPL::Verify(const G1Element &pubkey, - const vector &message, // unhashed - const G2Element &signature) -{ - return CoreMPL::Verify(pubkey, Bytes(message), signature); -} - -bool CoreMPL::Verify(const G1Element& pubkey, const Bytes& message, const G2Element& signature) +bool CoreMPL::Verify(const G1Element& pubkey, const Bytes message, const G2Element& signature) { const G2Element hashedPoint = G2Element::FromMessage(message, (const uint8_t*)strCiphersuiteId.c_str(), strCiphersuiteId.length()); @@ -184,8 +159,8 @@ bool CoreMPL::AggregateVerify(const vector &pubkeys, return CoreMPL::AggregateVerify(pubkeys, std::vector(messages.begin(), messages.end()), signature); } -bool CoreMPL::AggregateVerify(span const pubkeys, - span const messages, +bool CoreMPL::AggregateVerify(const span pubkeys, + const span messages, const G2Element& signature) { const size_t nPubKeys = pubkeys.size(); @@ -297,8 +272,8 @@ bool BasicSchemeMPL::AggregateVerify(const vector &pubkeys, return CoreMPL::AggregateVerify(pubkeys, messages, signature); } -bool BasicSchemeMPL::AggregateVerify(span const pubkeys, - span const messages, +bool BasicSchemeMPL::AggregateVerify(const span pubkeys, + const span messages, const G2Element& signature) { const size_t nPubKeys = pubkeys.size(); @@ -315,27 +290,14 @@ bool BasicSchemeMPL::AggregateVerify(span const pubkeys, return CoreMPL::AggregateVerify(pubkeys, messages, signature); } -G2Element AugSchemeMPL::Sign(const PrivateKey &seckey, const vector &message) -{ - return AugSchemeMPL::Sign(seckey, message, seckey.GetG1Element()); -} - -G2Element AugSchemeMPL::Sign(const PrivateKey& seckey, const Bytes& message) +G2Element AugSchemeMPL::Sign(const PrivateKey& seckey, const Bytes message) { return AugSchemeMPL::Sign(seckey, message, seckey.GetG1Element()); } -// Used for prepending different augMessage -G2Element AugSchemeMPL::Sign(const PrivateKey &seckey, - const vector &message, - const G1Element &prepend_pk) -{ - return AugSchemeMPL::Sign(seckey, Bytes(message), prepend_pk); -} - // Used for prepending different augMessage G2Element AugSchemeMPL::Sign(const PrivateKey& seckey, - const Bytes& message, + const Bytes message, const G1Element& prepend_pk) { vector augMessage = prepend_pk.Serialize(); @@ -344,19 +306,9 @@ G2Element AugSchemeMPL::Sign(const PrivateKey& seckey, return CoreMPL::Sign(seckey, augMessage); } -bool AugSchemeMPL::Verify(const vector &pubkey, - const vector &message, - const vector &signature) -{ - vector augMessage(pubkey); - augMessage.reserve(augMessage.size() + message.size()); - augMessage.insert(augMessage.end(), message.begin(), message.end()); - return CoreMPL::Verify(pubkey, augMessage, signature); -} - -bool AugSchemeMPL::Verify(const Bytes& pubkey, - const Bytes& message, - const Bytes& signature) +bool AugSchemeMPL::Verify(const Bytes pubkey, + const Bytes message, + const Bytes signature) { vector augMessage(pubkey.begin(), pubkey.end()); augMessage.reserve(augMessage.size() + message.size()); @@ -364,15 +316,8 @@ bool AugSchemeMPL::Verify(const Bytes& pubkey, return CoreMPL::Verify(pubkey, Bytes(augMessage), Bytes(signature)); } -bool AugSchemeMPL::Verify(const G1Element &pubkey, - const vector &message, - const G2Element &signature) -{ - return AugSchemeMPL::Verify(pubkey, Bytes(message), signature); -} - bool AugSchemeMPL::Verify(const G1Element& pubkey, - const Bytes& message, + const Bytes message, const G2Element& signature) { vector augMessage = pubkey.Serialize(); @@ -420,14 +365,10 @@ bool AugSchemeMPL::AggregateVerify(const vector& pubkeys, return AugSchemeMPL::AggregateVerify(pubkeys, vecMessagesBytes, signature); } -bool AugSchemeMPL::AggregateVerify(span const pubkeys, - span const messages, +bool AugSchemeMPL::AggregateVerify(const span pubkeys, + const span messages, const G2Element& signature) { - if (pubkeys.size() != messages.size()) { - return false; - } - size_t const nPubKeys = pubkeys.size(); auto const arg_check = VerifyAggregateSignatureArguments(nPubKeys, messages.size(), signature); if (arg_check != CONTINUE) { diff --git a/src/schemes.hpp b/src/schemes.hpp index 2ac823d4d..0881f8599 100644 --- a/src/schemes.hpp +++ b/src/schemes.hpp @@ -40,28 +40,18 @@ class CoreMPL { CoreMPL(const std::string& strId) : strCiphersuiteId(strId) {} // Generates a private key from a seed, similar to HD key generation // (hashes the seed), and reduces it mod the group order - virtual PrivateKey KeyGen(const vector& seed); - virtual PrivateKey KeyGen(const Bytes& seed); + virtual PrivateKey KeyGen(Bytes seed); // Generates a public key from a secret key virtual vector SkToPk(const PrivateKey &seckey); virtual G1Element SkToG1(const PrivateKey &seckey); - virtual G2Element Sign(const PrivateKey &seckey, const vector &message); - virtual G2Element Sign(const PrivateKey& seckey, const Bytes& message); + virtual G2Element Sign(const PrivateKey& seckey, Bytes message); - virtual bool Verify(const vector &pubkey, - const vector &message, - const vector &signature); + virtual bool Verify(Bytes pubkey, Bytes message, Bytes signature); - virtual bool Verify(const Bytes& pubkey, const Bytes& message, const Bytes& signature); - - virtual bool Verify(const G1Element &pubkey, - const vector &message, - const G2Element &signature); - - virtual bool Verify(const G1Element& pubkey, const Bytes& message, const G2Element& signature); + virtual bool Verify(const G1Element& pubkey, Bytes message, const G2Element& signature); virtual vector Aggregate(const vector> &signatures); virtual vector Aggregate(const vector& signatures); @@ -82,8 +72,8 @@ class CoreMPL { const vector> &messages, const G2Element &signature); - virtual bool AggregateVerify(span pubkeys, - span messages, + virtual bool AggregateVerify(span pubkeys, + span messages, const G2Element& signature); PrivateKey DeriveChildSk(const PrivateKey& sk, uint32_t index); @@ -111,8 +101,8 @@ class BasicSchemeMPL : public CoreMPL { const vector> &messages, const G2Element &signature) override; - bool AggregateVerify(span pubkeys, - span messages, + bool AggregateVerify(span pubkeys, + span messages, const G2Element& signature) override; }; @@ -122,34 +112,19 @@ class AugSchemeMPL : public CoreMPL { static const std::string CIPHERSUITE_ID; AugSchemeMPL() : CoreMPL(AugSchemeMPL::CIPHERSUITE_ID) {} - G2Element Sign(const PrivateKey &seckey, const vector &message) override; - - G2Element Sign(const PrivateKey& seckey, const Bytes& message) override; - - // Used for prepending different augMessage - G2Element Sign(const PrivateKey &seckey, - const vector &message, - const G1Element &prepend_pk); + G2Element Sign(const PrivateKey& seckey, Bytes message) override; // Used for prepending different augMessage G2Element Sign(const PrivateKey& seckey, - const Bytes& message, + Bytes message, const G1Element& prepend_pk); - bool Verify(const vector &pubkey, - const vector &message, - const vector &signature) override; - - bool Verify(const Bytes& pubkey, - const Bytes& message, - const Bytes& signature) override; - - bool Verify(const G1Element &pubkey, - const vector &message, - const G2Element &signature) override; + bool Verify(Bytes pubkey, + Bytes message, + Bytes signature) override; bool Verify(const G1Element& pubkey, - const Bytes& message, + Bytes message, const G2Element& signature) override; bool AggregateVerify(const vector> &pubkeys, @@ -164,8 +139,8 @@ class AugSchemeMPL : public CoreMPL { const vector> &messages, const G2Element &signature) override; - bool AggregateVerify(span pubkeys, - span messages, + bool AggregateVerify(span pubkeys, + span messages, const G2Element& signature) override; }; diff --git a/src/test-bench.cpp b/src/test-bench.cpp index 17cb0f865..0a5adf4bb 100644 --- a/src/test-bench.cpp +++ b/src/test-bench.cpp @@ -79,15 +79,15 @@ void benchBatchVerification() { uint8_t message[4]; Util::IntToFourBytes(message, i); vector messageBytes(message, message + 4); - PrivateKey sk = AugSchemeMPL().KeyGen(getRandomSeed()); + const PrivateKey sk = AugSchemeMPL().KeyGen(getRandomSeed()); G1Element pk = sk.GetG1Element(); - sigs.push_back(AugSchemeMPL().Sign(sk, messageBytes)); - pks.push_back(pk); - ms.push_back(messageBytes); + sigs.emplace_back(AugSchemeMPL().Sign(sk, messageBytes)); + pks.emplace_back(std::move(pk)); + ms.emplace_back(std::move(messageBytes)); } auto start = startStopwatch(); - G2Element aggSig = AugSchemeMPL().Aggregate(sigs); + const G2Element aggSig = AugSchemeMPL().Aggregate(sigs); endStopwatch("Aggregation", start, numIters); start = startStopwatch(); diff --git a/src/test.cpp b/src/test.cpp index 2f1a863e1..391d96567 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -560,9 +560,9 @@ TEST_CASE("Signature tests") PrivateKey sk = PrivateKey::FromByteVector(sk0); REQUIRE(sk.GetG1Element() == G1Element()); // Infinity REQUIRE(sk.GetG2Element() == G2Element()); // Infinity - REQUIRE(BasicSchemeMPL().Sign(sk, {1, 2, 3}) == G2Element()); - REQUIRE(AugSchemeMPL().Sign(sk, {1, 2, 3}) == G2Element()); - REQUIRE(PopSchemeMPL().Sign(sk, {1, 2, 3}) == G2Element()); + REQUIRE(BasicSchemeMPL().Sign(sk, std::vector{1, 2, 3}) == G2Element()); + REQUIRE(AugSchemeMPL().Sign(sk, std::vector{1, 2, 3}) == G2Element()); + REQUIRE(PopSchemeMPL().Sign(sk, std::vector{1, 2, 3}) == G2Element()); } SECTION("Should use equality operators") diff --git a/src/util.hpp b/src/util.hpp index 94d9b6bc6..3639f52a2 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -44,12 +44,13 @@ class span { inline const T* begin() const { return pData; } inline const T* end() const { return pData + nSize; } + inline const T* data() const { return pData; } inline size_t size() const { return nSize; } const T& operator[](const int nIndex) const { return pData[nIndex]; } }; -using Bytes = span; +using Bytes = span; class Util { public: