From 8bdbe3c6531b01219137576916ce50ec4018f2df Mon Sep 17 00:00:00 2001 From: fallenoak Date: Sat, 4 Feb 2023 14:46:03 -0600 Subject: [PATCH] feat(big): add MulMod --- storm/big/Ops.cpp | 10 ++++++++ storm/big/Ops.hpp | 2 ++ test/Big.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/storm/big/Ops.cpp b/storm/big/Ops.cpp index a2ff20a..3bf46cc 100644 --- a/storm/big/Ops.cpp +++ b/storm/big/Ops.cpp @@ -231,6 +231,16 @@ void Mul(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, BigStack& stack) stack.UnmakeDistinct(a, aa); } +void MulMod(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, const BigBuffer& d, BigStack& stack) { + uint32_t allocCount = 0; + auto& scratch = stack.Alloc(&allocCount); + + Mul(scratch, b, c, stack); + Div(scratch, a, scratch, d, stack); + + stack.Free(allocCount); +} + void SetOne(BigBuffer& buffer) { buffer.SetCount(1); buffer[0] = 1; diff --git a/storm/big/Ops.hpp b/storm/big/Ops.hpp index d4f22e3..3b01e87 100644 --- a/storm/big/Ops.hpp +++ b/storm/big/Ops.hpp @@ -35,6 +35,8 @@ void Mul(BigBuffer& a, const BigBuffer& b, uint64_t c); 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 SetOne(BigBuffer& buffer); void SetZero(BigBuffer& buffer); diff --git a/test/Big.cpp b/test/Big.cpp index e1f5807..66d509f 100644 --- a/test/Big.cpp +++ b/test/Big.cpp @@ -641,6 +641,68 @@ TEST_CASE("Mul", "[big]") { } } +TEST_CASE("MulMod", "[big]") { + SECTION("multiplies 0xFFFFFFFF by 0x100 and mods the result by 0xABC") { + BigData* a; + SBigNew(&a); + + BigData* b; + SBigNew(&b); + SBigFromUnsigned(b, 0xFFFFFFFF); + + BigData* c; + SBigNew(&c); + SBigFromUnsigned(c, 0x100); + + BigData* d; + SBigNew(&d); + SBigFromUnsigned(d, 0xABC); + + MulMod(a->Primary(), b->Primary(), c->Primary(), d->Primary(), a->Stack()); + + a->Primary().Trim(); + + CHECK(a->Primary().Count() == 1); + CHECK(a->Primary()[0] == 0x624); + + SBigDel(a); + SBigDel(b); + SBigDel(c); + SBigDel(d); + } + + SECTION("multiplies 0x123456789ABCDEF0 by 0xFEDCBA9876543210 and mods the result by 0x10000000") { + BigData* a; + SBigNew(&a); + + BigData* b; + SBigNew(&b); + uint64_t b_ = 0x123456789ABCDEF0; + SBigFromBinary(b, reinterpret_cast(&b_), sizeof(b_)); + + BigData* c; + SBigNew(&c); + uint64_t c_ = 0xFEDCBA9876543210; + SBigFromBinary(c, reinterpret_cast(&c_), sizeof(c_)); + + BigData* d; + SBigNew(&d); + SBigFromUnsigned(d, 0x10000000); + + MulMod(a->Primary(), b->Primary(), c->Primary(), d->Primary(), a->Stack()); + + a->Primary().Trim(); + + CHECK(a->Primary().Count() == 1); + CHECK(a->Primary()[0] == 0x618CF00); + + SBigDel(a); + SBigDel(b); + SBigDel(c); + SBigDel(d); + } +} + TEST_CASE("MakeLarge", "[big]") { SECTION("creates uint64_t out of 0xAABBCCDD and 0x11223344") { uint64_t value = MakeLarge(0xAABBCCDD, 0x11223344);