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 e65d0a1
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 313 deletions.
30 changes: 8 additions & 22 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 @@ -75,19 +75,12 @@ G1Element G1Element::FromNative(const g1_t element)
return ele;
}

G1Element G1Element::FromMessage(const std::vector<uint8_t>& message,
const uint8_t* dst,
int dst_len)
{
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 +196,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 +245,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 @@ -263,19 +256,12 @@ G2Element G2Element::FromNative(const g2_t element)
return ele;
}

G2Element G2Element::FromMessage(const std::vector<uint8_t>& message,
const uint8_t* dst,
int dst_len)
{
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
18 changes: 12 additions & 6 deletions src/elements.hpp
Expand Up @@ -39,13 +39,16 @@ 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,
int dst_len)
{
return G1Element::FromMessage(Bytes(message), dst, dst_len);
}
static G1Element FromMessage(Bytes message,
const uint8_t* dst,
int dst_len);
static G1Element Generator();
Expand Down Expand Up @@ -76,13 +79,16 @@ 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,
int dst_len)
{
return G2Element::FromMessage(Bytes(message), dst, dst_len);
}
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

0 comments on commit e65d0a1

Please sign in to comment.