diff --git a/storm/Big.cpp b/storm/Big.cpp index 141f3fc..93f8017 100644 --- a/storm/Big.cpp +++ b/storm/Big.cpp @@ -81,6 +81,10 @@ int32_t SBigIsOne(BigData* a) { return IsOne(a->Primary()); } +int32_t SBigIsZero(BigData* a) { + return IsZero(a->Primary()); +} + void SBigMod(BigData* a, BigData* b, BigData* c) { uint32_t allocCount = 0; auto& scratch = a->Stack().Alloc(&allocCount); diff --git a/storm/Big.hpp b/storm/Big.hpp index f7968e6..36e783d 100644 --- a/storm/Big.hpp +++ b/storm/Big.hpp @@ -34,6 +34,8 @@ int32_t SBigIsOdd(BigData* a); int32_t SBigIsOne(BigData* a); +int32_t SBigIsZero(BigData* a); + void SBigMod(BigData* a, BigData* b, BigData* c); void SBigMul(BigData* a, BigData* b, BigData* c); diff --git a/storm/big/Ops.cpp b/storm/big/Ops.cpp index c96064c..5b30274 100644 --- a/storm/big/Ops.cpp +++ b/storm/big/Ops.cpp @@ -225,6 +225,11 @@ int32_t IsOne(const BigBuffer &num) { return num.Count() == 1 && num[0] == 1; } +int32_t IsZero(const BigBuffer &num) { + num.Trim(); + return num.Count() == 0; +} + uint64_t MakeLarge(uint32_t low, uint32_t high) { return low + (static_cast(high) << 32); } diff --git a/storm/big/Ops.hpp b/storm/big/Ops.hpp index 84e54cb..5dd3b8d 100644 --- a/storm/big/Ops.hpp +++ b/storm/big/Ops.hpp @@ -39,6 +39,8 @@ int32_t IsOdd(const BigBuffer &num); int32_t IsOne(const BigBuffer &num); +int32_t IsZero(const BigBuffer &num); + uint64_t MakeLarge(uint32_t low, uint32_t high); void Mul(BigBuffer& a, const BigBuffer& b, uint64_t c); diff --git a/test/Big.cpp b/test/Big.cpp index 36ccac9..3620523 100644 --- a/test/Big.cpp +++ b/test/Big.cpp @@ -486,6 +486,26 @@ TEST_CASE("SBigIsOne", "[big]") { } } +TEST_CASE("SBigIsZero", "[big]") { + BigDataTest a; + + SECTION("unset is zero") { + CHECK(SBigIsZero(a)); + } + + SECTION("0 is 0") { + SBigFromUnsigned(a, 0); + CHECK(SBigIsZero(a)); + } + + SECTION("numbers are not 0") { + auto v = GENERATE(1ULL, 2ULL, 10ULL, 0xFFFFFFFFULL, 10000000000000ULL, 0xFF00000000000000ULL); + + SBigFromStr(a, std::to_string(v).c_str()); + CHECK_FALSE(SBigIsZero(a)); + } +} + TEST_CASE("SBigMod", "[big]") { SECTION("mods 7 by 4") { BigData* a;