forked from Chia-Network/bls-signatures
-
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 stack #75
Open
PastaPastaPasta
wants to merge
3
commits into
dashpay:develop
Choose a base branch
from
PastaPastaPasta:feat/serialize-stack
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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; | ||
|
@@ -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 { | ||
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 | ||
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; | ||
|
@@ -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{}; | ||
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 |
||
Serialize(data.data()); | ||
return data; | ||
} | ||
|
||
} // end namespace bls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: