From c333790e33e76914100171a808bd7d868aeee127 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Fri, 15 Dec 2023 18:16:31 -0600 Subject: [PATCH] Add missing operator== --- include/bls12-381/fp.hpp | 3 ++- test/unittests.cpp | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/bls12-381/fp.hpp b/include/bls12-381/fp.hpp index 6519647..4b77374 100644 --- a/include/bls12-381/fp.hpp +++ b/include/bls12-381/fp.hpp @@ -76,10 +76,11 @@ class fp template static fp modPrime(const std::array& k); // Those operators are defined to support set and map. - // They are mathematically correctly in certain cases. + // They are mathematically correct in certain cases. // However, there are still ambiguity there as the fp can be in Montgomery form or not. // Please avoid using those operators. constexpr std::strong_ordering operator<=>(const fp& e) const { return cmp(e); } + constexpr bool operator==(const fp& e) const { return cmp(e) == std::strong_ordering::equal; } static const fp MODULUS; // base field modulus: p = 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787 or 0x1A0111EA397FE69A4B1BA7B6434BACD764774B84F38512BF6730D2A0F6B0F6241EABFFFEB153FFFFB9FEFFFFFFFFAAAB static const uint64_t INP; // INP = -(p^{-1} mod 2^64) mod 2^64 diff --git a/test/unittests.cpp b/test/unittests.cpp index 1760fcf..0c903e9 100644 --- a/test/unittests.cpp +++ b/test/unittests.cpp @@ -634,6 +634,14 @@ void TestFieldElementSerialization() { throw invalid_argument("BE: bad serialization"); } + if( a != b ) + { + throw invalid_argument("BE: bad serialization"); + } + if(!(a == b)) + { + throw invalid_argument("BE: bad serialization"); + } } for(size_t i = 0; i < fuz; i++) { @@ -683,6 +691,14 @@ void TestG1Serialization() g1 a = random_g1(); array buf = a.toJacobianBytesBE(); g1 b = g1::fromJacobianBytesBE(buf).value(); + if(a != b) + { + throw invalid_argument("g1, jacobian: bad serialization from/to"); + } + if(!(a == b)) + { + throw invalid_argument("g1, jacobian: bad serialization from/to"); + } if(!a.equal(b)) { throw invalid_argument("g1, jacobian: bad serialization from/to");