Skip to content

Commit

Permalink
restore const west. Use span<> and Bytes pervasively in the schemes A…
Browse files Browse the repository at this point in the history
…PIs, resulting in a smaller interface, since the conversion vector -> Bytes is done on the span constructor
  • Loading branch information
arvidn committed Mar 24, 2021
1 parent c6e2aa7 commit 7c17918
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 138 deletions.
8 changes: 4 additions & 4 deletions src/elements.cpp
Expand Up @@ -82,12 +82,12 @@ G1Element G1Element::FromMessage(const std::vector<uint8_t>& 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;
}
Expand Down Expand Up @@ -270,12 +270,12 @@ G2Element G2Element::FromMessage(const std::vector<uint8_t>& 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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/elements.hpp
Expand Up @@ -45,7 +45,7 @@ class G1Element {
static G1Element FromMessage(const std::vector<uint8_t> &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();
Expand Down Expand Up @@ -82,7 +82,7 @@ class G2Element {
static G2Element FromMessage(const std::vector<uint8_t>& 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();
Expand Down
8 changes: 1 addition & 7 deletions src/hdkeys.hpp
Expand Up @@ -34,13 +34,7 @@ class HDKeys {
**/
public:
static const uint8_t HASH_LEN = 32;

static PrivateKey KeyGen(const std::vector<uint8_t>& 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))
Expand Down
91 changes: 16 additions & 75 deletions src/schemes.cpp
Expand Up @@ -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<uint8_t>& seed) {
return HDKeys::KeyGen(seed);
}

PrivateKey CoreMPL::KeyGen(const Bytes& seed) {
PrivateKey CoreMPL::KeyGen(const Bytes seed) {
return HDKeys::KeyGen(seed);
}

Expand All @@ -68,38 +64,17 @@ G1Element CoreMPL::SkToG1(const PrivateKey &seckey)
return seckey.GetG1Element();
}

G2Element CoreMPL::Sign(const PrivateKey &seckey, const vector<uint8_t> &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<uint8_t> &pubkey,
const vector<uint8_t> &message, // unhashed
const vector<uint8_t> &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<uint8_t> &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());

Expand Down Expand Up @@ -184,8 +159,8 @@ bool CoreMPL::AggregateVerify(const vector<G1Element> &pubkeys,
return CoreMPL::AggregateVerify(pubkeys, std::vector<Bytes>(messages.begin(), messages.end()), signature);
}

bool CoreMPL::AggregateVerify(span<G1Element const> const pubkeys,
span<Bytes const> const messages,
bool CoreMPL::AggregateVerify(const span<const G1Element> pubkeys,
const span<const Bytes> messages,
const G2Element& signature)
{
const size_t nPubKeys = pubkeys.size();
Expand Down Expand Up @@ -297,8 +272,8 @@ bool BasicSchemeMPL::AggregateVerify(const vector<G1Element> &pubkeys,
return CoreMPL::AggregateVerify(pubkeys, messages, signature);
}

bool BasicSchemeMPL::AggregateVerify(span<G1Element const> const pubkeys,
span<Bytes const> const messages,
bool BasicSchemeMPL::AggregateVerify(const span<const G1Element> pubkeys,
const span<const Bytes> messages,
const G2Element& signature)
{
const size_t nPubKeys = pubkeys.size();
Expand All @@ -315,27 +290,14 @@ bool BasicSchemeMPL::AggregateVerify(span<G1Element const> const pubkeys,
return CoreMPL::AggregateVerify(pubkeys, messages, signature);
}

G2Element AugSchemeMPL::Sign(const PrivateKey &seckey, const vector<uint8_t> &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<uint8_t> &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<uint8_t> augMessage = prepend_pk.Serialize();
Expand All @@ -344,35 +306,18 @@ G2Element AugSchemeMPL::Sign(const PrivateKey& seckey,
return CoreMPL::Sign(seckey, augMessage);
}

bool AugSchemeMPL::Verify(const vector<uint8_t> &pubkey,
const vector<uint8_t> &message,
const vector<uint8_t> &signature)
{
vector<uint8_t> 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<uint8_t> augMessage(pubkey.begin(), pubkey.end());
augMessage.reserve(augMessage.size() + message.size());
augMessage.insert(augMessage.end(), message.begin(), message.end());
return CoreMPL::Verify(pubkey, Bytes(augMessage), Bytes(signature));
}

bool AugSchemeMPL::Verify(const G1Element &pubkey,
const vector<uint8_t> &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<uint8_t> augMessage = pubkey.Serialize();
Expand Down Expand Up @@ -420,14 +365,10 @@ bool AugSchemeMPL::AggregateVerify(const vector<G1Element>& pubkeys,
return AugSchemeMPL::AggregateVerify(pubkeys, vecMessagesBytes, signature);
}

bool AugSchemeMPL::AggregateVerify(span<G1Element const> const pubkeys,
span<Bytes const> const messages,
bool AugSchemeMPL::AggregateVerify(const span<const G1Element> pubkeys,
const span<const Bytes> 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) {
Expand Down
57 changes: 16 additions & 41 deletions src/schemes.hpp
Expand Up @@ -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<uint8_t>& seed);
virtual PrivateKey KeyGen(const Bytes& seed);
virtual PrivateKey KeyGen(Bytes seed);

// Generates a public key from a secret key
virtual vector<uint8_t> SkToPk(const PrivateKey &seckey);

virtual G1Element SkToG1(const PrivateKey &seckey);

virtual G2Element Sign(const PrivateKey &seckey, const vector<uint8_t> &message);
virtual G2Element Sign(const PrivateKey& seckey, const Bytes& message);
virtual G2Element Sign(const PrivateKey& seckey, Bytes message);

virtual bool Verify(const vector<uint8_t> &pubkey,
const vector<uint8_t> &message,
const vector<uint8_t> &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<uint8_t> &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<uint8_t> Aggregate(const vector<vector<uint8_t>> &signatures);
virtual vector<uint8_t> Aggregate(const vector<Bytes>& signatures);
Expand All @@ -82,8 +72,8 @@ class CoreMPL {
const vector<vector<uint8_t>> &messages,
const G2Element &signature);

virtual bool AggregateVerify(span<G1Element const> pubkeys,
span<Bytes const> messages,
virtual bool AggregateVerify(span<const G1Element> pubkeys,
span<const Bytes> messages,
const G2Element& signature);

PrivateKey DeriveChildSk(const PrivateKey& sk, uint32_t index);
Expand Down Expand Up @@ -111,8 +101,8 @@ class BasicSchemeMPL : public CoreMPL {
const vector<vector<uint8_t>> &messages,
const G2Element &signature) override;

bool AggregateVerify(span<G1Element const> pubkeys,
span<Bytes const> messages,
bool AggregateVerify(span<const G1Element> pubkeys,
span<const Bytes> messages,
const G2Element& signature) override;
};

Expand All @@ -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<uint8_t> &message) override;

G2Element Sign(const PrivateKey& seckey, const Bytes& message) override;

// Used for prepending different augMessage
G2Element Sign(const PrivateKey &seckey,
const vector<uint8_t> &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<uint8_t> &pubkey,
const vector<uint8_t> &message,
const vector<uint8_t> &signature) override;

bool Verify(const Bytes& pubkey,
const Bytes& message,
const Bytes& signature) override;

bool Verify(const G1Element &pubkey,
const vector<uint8_t> &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<vector<uint8_t>> &pubkeys,
Expand All @@ -164,8 +139,8 @@ class AugSchemeMPL : public CoreMPL {
const vector<vector<uint8_t>> &messages,
const G2Element &signature) override;

bool AggregateVerify(span<G1Element const> pubkeys,
span<Bytes const> messages,
bool AggregateVerify(span<const G1Element> pubkeys,
span<const Bytes> messages,
const G2Element& signature) override;
};

Expand Down
10 changes: 5 additions & 5 deletions src/test-bench.cpp
Expand Up @@ -79,15 +79,15 @@ void benchBatchVerification() {
uint8_t message[4];
Util::IntToFourBytes(message, i);
vector<uint8_t> 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();
Expand Down
6 changes: 3 additions & 3 deletions src/test.cpp
Expand Up @@ -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<uint8_t>{1, 2, 3}) == G2Element());
REQUIRE(AugSchemeMPL().Sign(sk, std::vector<uint8_t>{1, 2, 3}) == G2Element());
REQUIRE(PopSchemeMPL().Sign(sk, std::vector<uint8_t>{1, 2, 3}) == G2Element());
}

SECTION("Should use equality operators")
Expand Down
3 changes: 2 additions & 1 deletion src/util.hpp
Expand Up @@ -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<uint8_t const>;
using Bytes = span<const uint8_t>;

class Util {
public:
Expand Down

0 comments on commit 7c17918

Please sign in to comment.