From c9826d2336d2493dd922d1d7ba5fa9e7128ffc66 Mon Sep 17 00:00:00 2001 From: Adam Heinermann Date: Fri, 15 Nov 2024 08:35:42 -0800 Subject: [PATCH] feat(big): add SBigIsOne --- storm/Big.cpp | 4 ++++ storm/Big.hpp | 2 ++ storm/big/Ops.cpp | 5 +++++ storm/big/Ops.hpp | 2 ++ test/Big.cpp | 16 ++++++++++++++++ 5 files changed, 29 insertions(+) diff --git a/storm/Big.cpp b/storm/Big.cpp index c64beaf..141f3fc 100644 --- a/storm/Big.cpp +++ b/storm/Big.cpp @@ -77,6 +77,10 @@ int32_t SBigIsOdd(BigData* a) { return IsOdd(a->Primary()); } +int32_t SBigIsOne(BigData* a) { + return IsOne(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 5d19f38..f7968e6 100644 --- a/storm/Big.hpp +++ b/storm/Big.hpp @@ -32,6 +32,8 @@ int32_t SBigIsEven(BigData* a); int32_t SBigIsOdd(BigData* a); +int32_t SBigIsOne(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 7cc3a4c..c96064c 100644 --- a/storm/big/Ops.cpp +++ b/storm/big/Ops.cpp @@ -220,6 +220,11 @@ int32_t IsOdd(const BigBuffer &num) { return num.Count() != 0 && (num[0] & 1) != 0; } +int32_t IsOne(const BigBuffer &num) { + num.Trim(); + return num.Count() == 1 && num[0] == 1; +} + 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 28d3b38..84e54cb 100644 --- a/storm/big/Ops.hpp +++ b/storm/big/Ops.hpp @@ -37,6 +37,8 @@ int32_t IsEven(const BigBuffer &num); int32_t IsOdd(const BigBuffer &num); +int32_t IsOne(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 a29cbe8..36ccac9 100644 --- a/test/Big.cpp +++ b/test/Big.cpp @@ -470,6 +470,22 @@ TEST_CASE("SBigIsOdd", "[big]") { } } +TEST_CASE("SBigIsOne", "[big]") { + BigDataTest a; + + SECTION("1 is 1") { + SBigFromUnsigned(a, 1); + CHECK(SBigIsOne(a)); + } + + SECTION("numbers are not 1") { + auto v = GENERATE(0ULL, 2ULL, 10ULL, 11ULL, 10000000000001ULL, 0xFF00000000000001ULL); + + SBigFromStr(a, std::to_string(v).c_str()); + CHECK_FALSE(SBigIsOne(a)); + } +} + TEST_CASE("SBigMod", "[big]") { SECTION("mods 7 by 4") { BigData* a;