Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/serialize stack #75

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/dashbls/elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class G1Element {
GTElement Pair(const G2Element &b) const;
uint32_t GetFingerprint(bool fLegacy = false) const;
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
std::array<uint8_t, SIZE> SerializeToArray(bool fLegacy = false) const;
G1Element Copy();

friend bool operator==(const G1Element &a, const G1Element &b);
Expand Down Expand Up @@ -102,6 +103,7 @@ class G2Element {
G2Element Negate() const;
GTElement Pair(const G1Element &a) const;
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
std::array<uint8_t, G2Element::SIZE> SerializeToArray(bool fLegacy = false) const;
G2Element Copy();

friend bool operator==(G2Element const &a, G2Element const &b);
Expand All @@ -127,6 +129,7 @@ class GTElement {

void Serialize(uint8_t *buffer) const;
std::vector<uint8_t> Serialize() const;
std::array<uint8_t, SIZE> SerializeToArray() const;

friend bool operator==(GTElement const &a, GTElement const &b);
friend bool operator!=(GTElement const &a, GTElement const &b);
Expand Down
1 change: 1 addition & 0 deletions include/dashbls/privatekey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class PrivateKey {
// Serialize the key into bytes
void Serialize(uint8_t *buffer) const;
std::vector<uint8_t> Serialize(bool fLegacy = false) const;
std::array<uint8_t, PrivateKey::PRIVATE_KEY_SIZE> SerializeToArray(bool fLegacy = false) const;

G2Element SignG2(
const uint8_t *msg,
Expand Down
68 changes: 68 additions & 0 deletions src/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ std::vector<uint8_t> G1Element::Serialize(const bool fLegacy) const {
return std::vector<uint8_t>(buffer + 1, buffer + 1 + G1Element::SIZE);
}

std::array<uint8_t, G1Element::SIZE> G1Element::SerializeToArray(const bool fLegacy) const {
uint8_t buffer[G1Element::SIZE + 1];
g1_write_bin(buffer, G1Element::SIZE + 1, p, 1);

if (buffer[0] == 0x00) { // infinity
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation of G1Element::Serialize and G1Element::SerializeToArray 90% same and basically code-paste...

Can you refactor it to use some util method or call functions from each of other?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one will work fine:

std::vector<uint8_t> G1Element::Serialize(const bool fLegacy) const {
  const auto arr = G1Element::SerializeToArray(fLegacy);
  return std::vector<uint8_t>{arr.begin(), arr.end()};
}

std::array<uint8_t, G1Element::SIZE> result{};
result[0] = 0xc0;
return result;
}

if (buffer[0] == 0x03) { // sign bit set
buffer[1] |= fLegacy ? 0x80 : 0x20;
}

if (!fLegacy) {
buffer[1] |= 0x80; // indicate compression
}

std::array<uint8_t, G1Element::SIZE> result{};
std::copy_n(buffer + 1, G1Element::SIZE, result.begin());
return result;
}

bool operator==(const G1Element & a, const G1Element &b)
{
return g1_cmp(a.p, b.p) == RLC_EQ;
Expand Down Expand Up @@ -422,6 +445,44 @@ std::vector<uint8_t> G2Element::Serialize(const bool fLegacy) const {
return result;
}

std::array<uint8_t, G2Element::SIZE> G2Element::SerializeToArray(const bool fLegacy) const {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code duplication with G2Element::Serialize()

uint8_t buffer[G2Element::SIZE + 1];
g2_write_bin(buffer, G2Element::SIZE + 1, (g2_st*)q, 1);

std::array<uint8_t, G2Element::SIZE> result{};

if (buffer[0] == 0x00) { // infinity
result.fill(0);
result[0] = 0xc0;
return result;
}

if (fLegacy) {
if (buffer[0] == 0x03) { // sign bit set
buffer[1] |= 0x80;
}
} else {
// remove leading 3 bits
buffer[1] &= 0x1f;
buffer[49] &= 0x1f;
if (buffer[0] == 0x03) {
buffer[49] |= 0xa0; // swapped later to 0
} else {
buffer[49] |= 0x80;
}
}

if (fLegacy) {
std::memcpy(result.data(), buffer + 1, G2Element::SIZE);
} else {
// Swap buffer, relic uses the opposite ordering for Fq2 elements
std::memcpy(result.data(), buffer + 1 + G2Element::SIZE / 2, G2Element::SIZE / 2);
std::memcpy(result.data() + G2Element::SIZE / 2, buffer + 1, G2Element::SIZE / 2);
}

return result;
}

bool operator==(G2Element const& a, G2Element const& b)
{
return g2_cmp((g2_st*)a.q, (g2_st*)b.q) == RLC_EQ;
Expand Down Expand Up @@ -551,4 +612,11 @@ std::vector<uint8_t> GTElement::Serialize() const
return data;
}

std::array<uint8_t, GTElement::SIZE> GTElement::SerializeToArray() const
{
std::array<uint8_t, GTElement::SIZE> data{};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required {} at the end

Serialize(data.data());
return data;
}

} // end namespace bls
7 changes: 7 additions & 0 deletions src/privatekey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ std::vector<uint8_t> PrivateKey::Serialize(const bool fLegacy) const
return data;
}

std::array<uint8_t, PrivateKey::PRIVATE_KEY_SIZE> PrivateKey::SerializeToArray(bool fLegacy) const
{
std::array<uint8_t, PRIVATE_KEY_SIZE> data{};
Serialize(data.data());
return data;
}

G2Element PrivateKey::SignG2(
const uint8_t *msg,
size_t len,
Expand Down
Loading