-
Notifications
You must be signed in to change notification settings - Fork 295
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
fix: g2.Serialize sporadic failure #4626
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#include "barretenberg/ecc/curves/bn254/fq2.hpp" | ||
#include "barretenberg/numeric/uint256/uint256.hpp" | ||
#include "barretenberg/serialize/msgpack.hpp" | ||
#include <cstring> | ||
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. for memset |
||
#include <type_traits> | ||
#include <vector> | ||
|
||
|
@@ -21,7 +22,7 @@ template <typename Fq_, typename Fr_, typename Params> class alignas(64) affine_ | |
affine_element() noexcept = default; | ||
~affine_element() noexcept = default; | ||
|
||
constexpr affine_element(const Fq& a, const Fq& b) noexcept; | ||
constexpr affine_element(const Fq& x, const Fq& y) noexcept; | ||
|
||
constexpr affine_element(const affine_element& other) noexcept = default; | ||
|
||
|
@@ -103,13 +104,9 @@ template <typename Fq_, typename Fr_, typename Params> class alignas(64) affine_ | |
static void serialize_to_buffer(const affine_element& value, uint8_t* buffer) | ||
{ | ||
if (value.is_point_at_infinity()) { | ||
if constexpr (Fq::modulus.get_msb() == 255) { | ||
write(buffer, uint256_t(0)); | ||
write(buffer, Fq::modulus); | ||
} else { | ||
write(buffer, uint256_t(0)); | ||
write(buffer, uint256_t(1) << 255); | ||
} | ||
// if we are infinity, just set all buffer bits to 1 | ||
// we only need this case because the below gets mangled converting from montgomery for infinity points | ||
memset(buffer, 255, sizeof(Fq) * 2); | ||
} else { | ||
Fq::serialize_to_buffer(value.y, buffer); | ||
Fq::serialize_to_buffer(value.x, buffer + sizeof(Fq)); | ||
|
@@ -130,38 +127,17 @@ template <typename Fq_, typename Fr_, typename Params> class alignas(64) affine_ | |
*/ | ||
static affine_element serialize_from_buffer(uint8_t* buffer) | ||
{ | ||
affine_element result; | ||
|
||
// need to read a raw uint256_t to avoid reductions so we can check whether the point is the point at infinity | ||
auto raw_x = from_buffer<uint256_t>(buffer + sizeof(Fq)); | ||
|
||
if constexpr (Fq::modulus.get_msb() == 255) { | ||
if (raw_x == Fq::modulus) { | ||
result.y = Fq::zero(); | ||
result.x.data[0] = raw_x.data[0]; | ||
result.x.data[1] = raw_x.data[1]; | ||
result.x.data[2] = raw_x.data[2]; | ||
result.x.data[3] = raw_x.data[3]; | ||
} else { | ||
result.y = Fq::serialize_from_buffer(buffer); | ||
result.x = Fq(raw_x); | ||
} | ||
} else { | ||
if (raw_x.get_msb() == 255) { | ||
result.y = Fq::zero(); | ||
result.x = Fq::zero(); | ||
result.self_set_infinity(); | ||
} else { | ||
// conditional here to avoid reading the same data twice in case of a field of prime order | ||
if constexpr (std::is_same<Fq, fq2>::value) { | ||
result.y = Fq::serialize_from_buffer(buffer); | ||
result.x = Fq::serialize_from_buffer(buffer + sizeof(Fq)); | ||
} else { | ||
result.y = Fq::serialize_from_buffer(buffer); | ||
result.x = Fq(raw_x); | ||
} | ||
} | ||
// Does the buffer consist entirely of set bits? If so, we have a point at infinity | ||
// Note that if it isn't, this loop should end early. | ||
// We only need this case because the below gets mangled converting to montgomery for infinity points | ||
bool is_point_at_infinity = | ||
std::all_of(buffer, buffer + sizeof(Fq) * 2, [](uint8_t val) { return val == 255; }); | ||
if (is_point_at_infinity) { | ||
return affine_element::infinity(); | ||
} | ||
affine_element result; | ||
result.y = Fq::serialize_from_buffer(buffer); | ||
result.x = Fq::serialize_from_buffer(buffer + sizeof(Fq)); | ||
return result; | ||
} | ||
|
||
|
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.
It worked before, and this is 2x slower, but boy is it useful seeing exactly where the problem memory was allocated. Worth it.