Skip to content

Commit

Permalink
Allowing inifinity point in deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Feb 24, 2022
1 parent ed95c59 commit bb58a10
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/secp256k1/include/GroupElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class GroupElement final {
static constexpr size_t memoryRequired() { return serialize_size; }
unsigned char* serialize() const;
unsigned char* serialize(unsigned char* buffer) const;
// The function deserializes the GroupElement and checks the validity,
// it accepts infinity point, handle it based on your use case
unsigned const char* deserialize(unsigned const char* buffer);

// These functions are for READWRITE() in serialize.h
Expand Down
2 changes: 1 addition & 1 deletion src/secp256k1/src/cpp/GroupElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ const unsigned char* GroupElement::deserialize(const unsigned char* buffer) {

secp256k1_gej_set_ge(reinterpret_cast<secp256k1_gej *>(g_), &result);

if (!secp256k1_ge_is_valid_var(&result)) {
if (!secp256k1_ge_is_valid_var(&result) && !result.infinity) {
throw std::runtime_error("GroupElement: deserialize failed");
}
return buffer + memoryRequired();
Expand Down
22 changes: 12 additions & 10 deletions src/sigma/test/serialize_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ BOOST_AUTO_TEST_CASE(group_element_invalid)
buffer[0] = std::rand();
buffer[1] = std::rand();

// Making the point not infinity as it is allowed in deserialization
buffer[33] = 0;

secp_primitives::GroupElement resulted;
BOOST_CHECK_THROW(resulted.deserialize(buffer), std::exception);
}

//This test is not valid already, as infinity point is invalid in check secp256k1_ge_is_valid_var, GroupElement.cpp:533
//BOOST_AUTO_TEST_CASE(group_element_serialize_infinity)
//{
// secp_primitives::GroupElement initial;
// unsigned char buffer [initial.memoryRequired()];
// initial.serialize(buffer);
// secp_primitives::GroupElement resulted;
// resulted.deserialize(buffer);
// BOOST_CHECK(initial == resulted);
//}
BOOST_AUTO_TEST_CASE(group_element_serialize_infinity)
{
secp_primitives::GroupElement initial;
unsigned char buffer [initial.memoryRequired()];
initial.serialize(buffer);
secp_primitives::GroupElement resulted;
resulted.deserialize(buffer);
BOOST_CHECK(initial == resulted);
}

BOOST_AUTO_TEST_CASE(scalar_serialize)
{
Expand Down

0 comments on commit bb58a10

Please sign in to comment.