Skip to content

Commit

Permalink
fix: Should not check validity for legacy G1 and G2 in FromBytes (das…
Browse files Browse the repository at this point in the history
…hpay#58)

* fix: Should not check validity for legacy G1 and G2 in FromBytes

* tests: add CheckValid tests for Legacy scheme
  • Loading branch information
UdjinM6 committed Dec 14, 2022
1 parent 7457939 commit 85b7e61
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const size_t G1Element::SIZE;

G1Element G1Element::FromBytes(Bytes const bytes, bool fLegacy) {
G1Element ele = G1Element::FromBytesUnchecked(bytes, fLegacy);
ele.CheckValid();
if (!fLegacy) {
ele.CheckValid();
}
return ele;
}

Expand Down Expand Up @@ -229,7 +231,9 @@ const size_t G2Element::SIZE;

G2Element G2Element::FromBytes(Bytes const bytes, const bool fLegacy) {
G2Element ele = G2Element::FromBytesUnchecked(bytes, fLegacy);
ele.CheckValid();
if (!fLegacy) {
ele.CheckValid();
}
return ele;
}

Expand Down
49 changes: 49 additions & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,55 @@ TEST_CASE("CheckValid")
}
}

TEST_CASE("CheckValid for Legacy")
{
SECTION("Invalid G1 points should throw in CheckValid but not in FromBytes")
{
std::map<std::string, bool> vecPointsHex{
{"11df3a748b713460f9b21083315c0dca1742b7962ca98685be4094d302e84b0884a04c1a55beb0ed921dae1dd66c0111", true},
{"11df3a748b713460f9b21083315c0dca1742b7962ca98685be4094d302e84b0884a04c1a55beb0ed921dae1dd66c0a11", false},
};

for (const auto& pair : vecPointsHex) {
const auto& strPointHex = pair.first;
const auto& valid = pair.second;

// FromBytes does not throw
G1Element::FromBytes(Bytes(Util::HexToBytes(strPointHex)), true);
// FromBytesUnchecked does not throw
G1Element pk = G1Element::FromBytesUnchecked(Bytes(Util::HexToBytes(strPointHex)), true);

REQUIRE(pk.IsValid() == valid);
if (!valid) {
REQUIRE_THROWS(pk.CheckValid());
}
}
}

SECTION("Invalid G2 points should throw in CheckValid but not in FromBytes")
{
std::map<std::string, bool> vecPointsHex{
{"0888879c99852460912fd28c7a9138926c1e87fd6609fd2d3d307764e49feb85702fd8f9b3b836bc11f7ce151b769dc70b760879d26f8c33a29e24f69297f45ef028f0794e63ddb0610db7de1a608b6d6a2129ada62b845004a408f651fd44a5", true},
{"0888879c99852460912fd28c7a9138926c1e87fd6609fd2d3d307764e49feb85702fd8f9b3b836bc11f7ce151b769dc70b760879d26f8c33a29e24f69297f45ef028f0794e63ddb0610db7de1a608b6d6a2129ada62b845004a408f651fd44a6", false},
};

for (const auto& pair : vecPointsHex) {
const auto& strPointHex = pair.first;
const auto& valid = pair.second;

// FromBytes does not throw
G2Element::FromBytes(Bytes(Util::HexToBytes(strPointHex)), true);
// FromBytesUnchecked does not throw
G2Element sig = G2Element::FromBytesUnchecked(Bytes(Util::HexToBytes(strPointHex)), true);

REQUIRE(sig.IsValid() == valid);
if (!valid) {
REQUIRE_THROWS(sig.CheckValid());
}
}
}
}

int main(int argc, char* argv[])
{
int result = Catch::Session().run(argc, argv);
Expand Down

0 comments on commit 85b7e61

Please sign in to comment.