From 5688b7ec3e87613ae5923a4c67182356f54d39b7 Mon Sep 17 00:00:00 2001 From: Adam Heinermann Date: Fri, 15 Nov 2024 09:01:26 -0800 Subject: [PATCH] feat(big): add SBigNot --- storm/Big.cpp | 4 ++++ storm/Big.hpp | 2 ++ storm/big/Ops.cpp | 9 +++++++++ storm/big/Ops.hpp | 2 ++ test/Big.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/storm/Big.cpp b/storm/Big.cpp index 93f8017..291f6de 100644 --- a/storm/Big.cpp +++ b/storm/Big.cpp @@ -103,6 +103,10 @@ void SBigNew(BigData** num) { *num = new (m) BigData(); } +void SBigNot(BigData* a, BigData* b) { + Not(a->Primary(), b->Primary()); +} + void SBigPowMod(BigData* a, BigData* b, BigData* c, BigData* d) { PowMod(a->Primary(), b->Primary(), c->Primary(), d->Primary(), a->Stack()); } diff --git a/storm/Big.hpp b/storm/Big.hpp index 36e783d..595b872 100644 --- a/storm/Big.hpp +++ b/storm/Big.hpp @@ -42,6 +42,8 @@ void SBigMul(BigData* a, BigData* b, BigData* c); void SBigNew(BigData** num); +void SBigNot(BigData* a, BigData* b); + void SBigPowMod(BigData* a, BigData* b, BigData* c, BigData* d); void SBigShl(BigData* a, BigData* b, uint32_t shift); diff --git a/storm/big/Ops.cpp b/storm/big/Ops.cpp index 5b30274..3f49e12 100644 --- a/storm/big/Ops.cpp +++ b/storm/big/Ops.cpp @@ -277,6 +277,15 @@ void MulMod(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, const BigBuffe stack.Free(allocCount); } +void Not(BigBuffer& a, const BigBuffer& b) { + uint32_t i = 0; + for (; b.IsUsed(i); i++) { + a[i] = ~b[i]; + } + + a.SetCount(i); +} + void PowMod(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, const BigBuffer& d, BigStack& stack) { c.Trim(); diff --git a/storm/big/Ops.hpp b/storm/big/Ops.hpp index 5dd3b8d..ce75b49 100644 --- a/storm/big/Ops.hpp +++ b/storm/big/Ops.hpp @@ -49,6 +49,8 @@ void Mul(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, BigStack& stack); void MulMod(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, const BigBuffer& d, BigStack& stack); +void Not(BigBuffer& a, const BigBuffer& b); + void PowMod(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, const BigBuffer& d, BigStack& stack); void SetOne(BigBuffer& buffer); diff --git a/test/Big.cpp b/test/Big.cpp index 3620523..b7d1218 100644 --- a/test/Big.cpp +++ b/test/Big.cpp @@ -728,6 +728,47 @@ TEST_CASE("SBigPowMod", "[big]") { } } +TEST_CASE("SBigNot", "[big]") { + BigDataTest a, b; + + SECTION("bitwise negates small values") { + auto v = GENERATE(uint32_t(1), 2, 10, 0xFFFFFFFF); + + SBigFromUnsigned(b, v); + SBigNot(a, b); + + CHECK(a->Primary().Count() == 1); + CHECK(a->Primary()[0] == ~v); + } + + SECTION("bitwise negates large values") { + auto v = GENERATE(10000000000000ULL, 0xFF00000000000000ULL); + + SBigFromStr(b, std::to_string(v).c_str()); + SBigNot(a, b); + + CHECK(a->Primary().Count() == 2); + CHECK(a->Primary()[0] == ~uint32_t(v)); + CHECK(a->Primary()[1] == ~uint32_t(v >> 32)); + } + + SECTION("bitwise negates huge value") { + uint32_t data[] = { 0xF00DFEED, 0xBA1D, 0xBEEBBEEB, 0x12345678, 0x9ABCDEF, 0xDEADCAD, 0xD011A }; + + SBigFromBinary(b, data, sizeof(data)); + SBigNot(a, b); + + CHECK(a->Primary().Count() == 7); + CHECK(a->Primary()[0] == ~data[0]); + CHECK(a->Primary()[1] == ~data[1]); + CHECK(a->Primary()[2] == ~data[2]); + CHECK(a->Primary()[3] == ~data[3]); + CHECK(a->Primary()[4] == ~data[4]); + CHECK(a->Primary()[5] == ~data[5]); + CHECK(a->Primary()[6] == ~data[6]); + } +} + TEST_CASE("SBigShl", "[big]") { SECTION("shifts 256 left 7 bits") { BigData* a;