-
Notifications
You must be signed in to change notification settings - Fork 34
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 on the stack #75
base: develop
Are you sure you want to change the base?
Changes from all commits
09ac2e9
d1b3d24
677db58
0f3705b
a3afed8
30aa085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,11 +171,16 @@ uint32_t G1Element::GetFingerprint(const bool fLegacy) const | |
} | ||
|
||
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> G1Element::SerializeToArray(const bool fLegacy) const { | ||
uint8_t buffer[G1Element::SIZE + 1]; | ||
g1_write_bin(buffer, G1Element::SIZE + 1, p, 1); | ||
|
||
std::array<uint8_t, G1Element::SIZE> result{}; | ||
if (buffer[0] == 0x00) { // infinity | ||
std::vector<uint8_t> result(G1Element::SIZE, 0); | ||
result[0] = 0xc0; | ||
return result; | ||
} | ||
|
@@ -187,7 +192,9 @@ std::vector<uint8_t> G1Element::Serialize(const bool fLegacy) const { | |
if (!fLegacy) { | ||
buffer[1] |= 0x80; // indicate compression | ||
} | ||
return std::vector<uint8_t>(buffer + 1, buffer + 1 + G1Element::SIZE); | ||
|
||
std::copy_n(buffer + 1, G1Element::SIZE, result.begin()); | ||
return result; | ||
} | ||
|
||
bool operator==(const G1Element & a, const G1Element &b) | ||
|
@@ -386,11 +393,18 @@ G2Element G2Element::Negate() const | |
GTElement G2Element::Pair(const G1Element& a) const { return a & (*this); } | ||
|
||
std::vector<uint8_t> G2Element::Serialize(const bool fLegacy) const { | ||
const auto arr = G2Element::SerializeToArray(fLegacy); | ||
return std::vector<uint8_t>{arr.begin(), arr.end()}; | ||
} | ||
|
||
std::array<uint8_t, G2Element::SIZE> G2Element::SerializeToArray(const bool fLegacy) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code duplication with |
||
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 | ||
std::vector<uint8_t> result(G2Element::SIZE, 0); | ||
result.fill(0); | ||
result[0] = 0xc0; | ||
return result; | ||
} | ||
|
@@ -410,7 +424,6 @@ std::vector<uint8_t> G2Element::Serialize(const bool fLegacy) const { | |
} | ||
} | ||
|
||
std::vector<uint8_t> result(G2Element::SIZE, 0); | ||
if (fLegacy) { | ||
std::memcpy(result.data(), buffer + 1, G2Element::SIZE); | ||
} else { | ||
|
@@ -551,4 +564,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{}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not required {} at the end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better to initialize data, then let it potentially be uninitialized |
||
Serialize(data.data()); | ||
return data; | ||
} | ||
|
||
} // end namespace bls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation of
G1Element::Serialize
andG1Element::SerializeToArray
90% same and basically code-paste...Can you refactor it to use some util method or call functions from each of other?
There was a problem hiding this comment.
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: