From aaa44dd400fe04e0d1b3ed165e9dc6630634c5de Mon Sep 17 00:00:00 2001 From: Adam Heinermann Date: Sun, 17 Nov 2024 04:02:44 -0800 Subject: [PATCH] feat(big): add SBigToUnsigned --- storm/Big.cpp | 4 ++++ storm/Big.hpp | 2 ++ storm/big/Ops.cpp | 5 +++++ storm/big/Ops.hpp | 2 ++ test/Big.cpp | 24 ++++++++++++++++++++++++ test/big/Ops.cpp | 1 - 6 files changed, 37 insertions(+), 1 deletion(-) diff --git a/storm/Big.cpp b/storm/Big.cpp index 298a924..1741df5 100644 --- a/storm/Big.cpp +++ b/storm/Big.cpp @@ -143,6 +143,10 @@ void SBigToBinaryBuffer(BigData* num, uint8_t* data, uint32_t maxBytes, uint32_t } } +void SBigToUnsigned(BigData* num, uint32_t* val) { + ToUnsigned(val, num->Primary()); +} + void SBigXor(BigData* a, BigData* b, BigData* c) { Xor(a->Primary(), b->Primary(), c->Primary()); } diff --git a/storm/Big.hpp b/storm/Big.hpp index f39a90e..dae161c 100644 --- a/storm/Big.hpp +++ b/storm/Big.hpp @@ -60,4 +60,6 @@ void SBigToBinaryBuffer(BigData* num, uint8_t* data, uint32_t maxBytes, uint32_t void SBigXor(BigData* a, BigData* b, BigData* c); +void SBigToUnsigned(BigData* num, uint32_t* val); + #endif diff --git a/storm/big/Ops.cpp b/storm/big/Ops.cpp index b40623f..a78cb1a 100644 --- a/storm/big/Ops.cpp +++ b/storm/big/Ops.cpp @@ -483,6 +483,11 @@ void ToStream(TSGrowableArray& output, const BigBuffer& buffer) { ToBinaryAppend(output, buffer); } +void ToUnsigned(uint32_t* a, const BigBuffer& b) { + *a = 0; + *a = b[0]; +} + void Xor(BigBuffer& a, const BigBuffer& b, const BigBuffer& c) { uint32_t i = 0; for (; b.IsUsed(i) || c.IsUsed(i); i++) { diff --git a/storm/big/Ops.hpp b/storm/big/Ops.hpp index 3f38596..c8b20dc 100644 --- a/storm/big/Ops.hpp +++ b/storm/big/Ops.hpp @@ -75,6 +75,8 @@ void ToBinary(TSGrowableArray& output, const BigBuffer& buffer); void ToStream(TSGrowableArray& output, const BigBuffer& buffer); +void ToUnsigned(uint32_t* a, const BigBuffer& b); + void Xor(BigBuffer& a, const BigBuffer& b, const BigBuffer& c); #endif diff --git a/test/Big.cpp b/test/Big.cpp index cd924e4..82b9e1d 100644 --- a/test/Big.cpp +++ b/test/Big.cpp @@ -746,6 +746,30 @@ TEST_CASE("SBigToBinaryBuffer", "[big]") { } } +TEST_CASE("SBigToUnsigned", "[big]") { + BigDataTest num; + + SECTION("converts bignum values to uint") { + auto v = GENERATE(0UL, 1UL, 1000UL, UINT32_MAX); + + SBigFromUnsigned(num, v); + + uint32_t result; + SBigToUnsigned(num, &result); + + CHECK(result == v); + } + + SECTION("truncates large values") { + SBigFromStr(num, std::to_string(0x123456789ABCDEFULL).c_str()); + + uint32_t result; + SBigToUnsigned(num, &result); + + CHECK(result == 0x89ABCDEF); + } +} + TEST_CASE("SBigXor", "[big]") { BigDataTest a, b, c; diff --git a/test/big/Ops.cpp b/test/big/Ops.cpp index 935c060..e2e7501 100644 --- a/test/big/Ops.cpp +++ b/test/big/Ops.cpp @@ -1070,7 +1070,6 @@ TEST_CASE("Sub", "[big]") { } } - TEST_CASE("ToStream", "[big]") { BigDataTest a;