Skip to content

Commit

Permalink
Use span<> and Bytes pervasively in the schemes APIs
Browse files Browse the repository at this point in the history
This results in a smaller interface, since the conversion vector -> Bytes is
done in the span constructor. Additionally it make the interface more flexible
in that it no longer requires the caller to allocate the array in a std::vector
  • Loading branch information
arvidn committed Mar 26, 2021
1 parent f11b54d commit f551c57
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 222 deletions.
16 changes: 8 additions & 8 deletions src/elements.cpp
Expand Up @@ -18,7 +18,7 @@

namespace bls {

G1Element G1Element::FromBytes(const Bytes& bytes)
G1Element G1Element::FromBytes(const Bytes bytes)
{
if (bytes.size() != SIZE) {
throw std::invalid_argument("G1Element::FromBytes: Invalid size");
Expand Down Expand Up @@ -64,7 +64,7 @@ G1Element G1Element::FromBytes(const Bytes& bytes)

G1Element G1Element::FromByteVector(const std::vector<uint8_t>& bytevec)
{
return G1Element::FromBytes(Bytes(bytevec));
return G1Element::FromBytes(bytevec);
}

G1Element G1Element::FromNative(const g1_t element)
Expand All @@ -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 @@ -203,7 +203,7 @@ G1Element operator*(const bn_t& k, const G1Element& a) { return a * k; }



G2Element G2Element::FromBytes(const Bytes& bytes)
G2Element G2Element::FromBytes(const Bytes bytes)
{
if (bytes.size() != SIZE) {
throw std::invalid_argument("G2Element::FromBytes: Invalid size");
Expand Down Expand Up @@ -252,7 +252,7 @@ G2Element G2Element::FromBytes(const Bytes& bytes)

G2Element G2Element::FromByteVector(const std::vector<uint8_t>& bytevec)
{
return G2Element::FromBytes(Bytes(bytevec));
return G2Element::FromBytes(bytevec);
}

G2Element G2Element::FromNative(const g2_t element)
Expand All @@ -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
8 changes: 4 additions & 4 deletions src/elements.hpp
Expand Up @@ -39,13 +39,13 @@ class G1Element {
g1_set_infty(p);
}

static G1Element FromBytes(const Bytes& bytes);
static G1Element FromBytes(Bytes bytes);
static G1Element FromByteVector(const std::vector<uint8_t> &bytevec);
static G1Element FromNative(const g1_t element);
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(Bytes message,
const uint8_t* dst,
int dst_len);
static G1Element Generator();
Expand Down Expand Up @@ -76,13 +76,13 @@ class G2Element {
g2_set_infty(q);
}

static G2Element FromBytes(const Bytes& bytes);
static G2Element FromBytes(Bytes bytes);
static G2Element FromByteVector(const std::vector<uint8_t> &bytevec);
static G2Element FromNative(const g2_t element);
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(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
143 changes: 44 additions & 99 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 @@ -150,18 +125,18 @@ G1Element CoreMPL::Aggregate(const vector<G1Element> &publicKeys)
return aggregated;
}

bool CoreMPL::AggregateVerify(const vector<vector<uint8_t>> &pubkeys,
const vector<vector<uint8_t>> &messages, // unhashed
const vector<uint8_t> &signature)
bool CoreMPL::AggregateVerify(const span<const vector<uint8_t>> pubkeys,
const span<const vector<uint8_t>> messages, // unhashed
const Bytes signature)
{
const std::vector<Bytes> vecPubKeyBytes(pubkeys.begin(), pubkeys.end());
const std::vector<Bytes> vecMessagesBytes(messages.begin(), messages.end());
return CoreMPL::AggregateVerify(vecPubKeyBytes, vecMessagesBytes, Bytes(signature));
}

bool CoreMPL::AggregateVerify(const vector<Bytes>& pubkeys,
const vector<Bytes>& messages, // unhashed
const Bytes& signature)
bool CoreMPL::AggregateVerify(const span<const Bytes> pubkeys,
const span<const Bytes> messages, // unhashed
const Bytes signature)
{
const size_t nPubKeys = pubkeys.size();
const G2Element signatureElement = G2Element::FromBytes(signature);
Expand All @@ -177,15 +152,15 @@ bool CoreMPL::AggregateVerify(const vector<Bytes>& pubkeys,
return CoreMPL::AggregateVerify(pubkeyElements, messages, signatureElement);
}

bool CoreMPL::AggregateVerify(const vector<G1Element> &pubkeys,
const vector<vector<uint8_t>> &messages,
bool CoreMPL::AggregateVerify(const span<const G1Element> pubkeys,
const span<const vector<uint8_t>> messages,
const G2Element &signature)
{
return CoreMPL::AggregateVerify(pubkeys, std::vector<Bytes>(messages.begin(), messages.end()), signature);
}

bool CoreMPL::AggregateVerify(const vector<G1Element>& pubkeys,
const vector<Bytes> &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 @@ -245,12 +220,12 @@ G1Element CoreMPL::DeriveChildPkUnhardened(const G1Element& pk, uint32_t index)
return HDKeys::DeriveChildG1Unhardened(pk, index);
}

bool BasicSchemeMPL::AggregateVerify(const vector<vector<uint8_t>> &pubkeys,
const vector<vector<uint8_t>> &messages,
const vector<uint8_t> &signature)
bool BasicSchemeMPL::AggregateVerify(const span<const vector<uint8_t>> pubkeys,
const span<const vector<uint8_t>> messages,
const Bytes signature)
{
const size_t nPubKeys = pubkeys.size();
auto arg_check = VerifyAggregateSignatureArguments(nPubKeys, messages.size(), G2Element::FromByteVector(signature));
auto arg_check = VerifyAggregateSignatureArguments(nPubKeys, messages.size(), G2Element::FromBytes(signature));
if (arg_check != CONTINUE) {
return arg_check;
}
Expand All @@ -262,9 +237,9 @@ bool BasicSchemeMPL::AggregateVerify(const vector<vector<uint8_t>> &pubkeys,
return CoreMPL::AggregateVerify(pubkeys, messages, signature);
}

bool BasicSchemeMPL::AggregateVerify(const vector<Bytes>& pubkeys,
const vector<Bytes>& messages,
const Bytes& signature)
bool BasicSchemeMPL::AggregateVerify(const span<const Bytes> pubkeys,
const span<const Bytes> messages,
const Bytes signature)
{
const size_t nPubKeys = pubkeys.size();
const auto arg_check = VerifyAggregateSignatureArguments(nPubKeys, messages.size(), G2Element::FromBytes(signature));
Expand All @@ -280,8 +255,8 @@ bool BasicSchemeMPL::AggregateVerify(const vector<Bytes>& pubkeys,
return CoreMPL::AggregateVerify(pubkeys, messages, signature);
}

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

bool BasicSchemeMPL::AggregateVerify(const vector<G1Element>& pubkeys,
const vector<Bytes> &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(const vector<G1Element>& pubkeys,
return CoreMPL::AggregateVerify(pubkeys, messages, signature);
}

G2Element AugSchemeMPL::Sign(const PrivateKey &seckey, const vector<uint8_t> &message)
G2Element AugSchemeMPL::Sign(const PrivateKey& seckey, const Bytes message)
{
return AugSchemeMPL::Sign(seckey, message, seckey.GetG1Element());
}

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 All @@ -381,18 +326,18 @@ bool AugSchemeMPL::Verify(const G1Element& pubkey,
return CoreMPL::Verify(pubkey, augMessage, signature);
}

bool AugSchemeMPL::AggregateVerify(const vector<vector<uint8_t>> &pubkeys,
const vector<vector<uint8_t>> &messages,
const vector<uint8_t> &signature)
bool AugSchemeMPL::AggregateVerify(const span<const vector<uint8_t>> pubkeys,
const span<const vector<uint8_t>> messages,
const Bytes signature)
{
std::vector<Bytes> vecPubKeyBytes(pubkeys.begin(), pubkeys.end());
std::vector<Bytes> vecMessagesBytes(messages.begin(), messages.end());
return AugSchemeMPL::AggregateVerify(vecPubKeyBytes, vecMessagesBytes, Bytes(signature));
}

bool AugSchemeMPL::AggregateVerify(const vector<Bytes>& pubkeys,
const vector<Bytes>& messages,
const Bytes& signature)
bool AugSchemeMPL::AggregateVerify(const span<const Bytes> pubkeys,
const span<const Bytes> messages,
const Bytes signature)
{
size_t nPubKeys = pubkeys.size();
auto arg_check = VerifyAggregateSignatureArguments(nPubKeys, messages.size(), G2Element::FromBytes(signature));
Expand All @@ -412,26 +357,26 @@ bool AugSchemeMPL::AggregateVerify(const vector<Bytes>& pubkeys,
return CoreMPL::AggregateVerify(pubkeys, vecAugMessageBytes, signature);
}

bool AugSchemeMPL::AggregateVerify(const vector<G1Element>& pubkeys,
const vector<vector<uint8_t>>& messages,
bool AugSchemeMPL::AggregateVerify(const span<const G1Element> pubkeys,
const span<const vector<uint8_t>> messages,
const G2Element& signature)
{
std::vector<Bytes> vecMessagesBytes(messages.begin(), messages.end());
return AugSchemeMPL::AggregateVerify(pubkeys, vecMessagesBytes, signature);
}

bool AugSchemeMPL::AggregateVerify(const vector<G1Element>& pubkeys,
const vector<Bytes>& messages,
bool AugSchemeMPL::AggregateVerify(const span<const G1Element> pubkeys,
const span<const Bytes> messages,
const G2Element& signature)
{
size_t nPubKeys = pubkeys.size();
auto arg_check = VerifyAggregateSignatureArguments(nPubKeys, messages.size(), signature);
size_t const nPubKeys = pubkeys.size();
auto const arg_check = VerifyAggregateSignatureArguments(nPubKeys, messages.size(), signature);
if (arg_check != CONTINUE) {
return arg_check;
}

vector<vector<uint8_t>> augMessages(nPubKeys);
for (int i = 0; i < nPubKeys; ++i) {
for (std::size_t i = 0; i < nPubKeys; ++i) {
vector<uint8_t>& aug = augMessages[i];
vector<uint8_t>&& pubkey = pubkeys[i].Serialize();
aug.reserve(pubkey.size() + messages[i].size());
Expand Down

0 comments on commit f551c57

Please sign in to comment.