feat(big): add MulMod

This commit is contained in:
fallenoak 2023-02-04 14:46:03 -06:00
parent a597e8f495
commit 8bdbe3c653
No known key found for this signature in database
GPG key ID: 7628F8E61AEA070D
3 changed files with 74 additions and 0 deletions

View file

@ -231,6 +231,16 @@ void Mul(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, BigStack& stack)
stack.UnmakeDistinct(a, aa); 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) { void SetOne(BigBuffer& buffer) {
buffer.SetCount(1); buffer.SetCount(1);
buffer[0] = 1; buffer[0] = 1;

View file

@ -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 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 SetOne(BigBuffer& buffer);
void SetZero(BigBuffer& buffer); void SetZero(BigBuffer& buffer);

View file

@ -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<uint8_t*>(&b_), sizeof(b_));
BigData* c;
SBigNew(&c);
uint64_t c_ = 0xFEDCBA9876543210;
SBigFromBinary(c, reinterpret_cast<uint8_t*>(&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]") { TEST_CASE("MakeLarge", "[big]") {
SECTION("creates uint64_t out of 0xAABBCCDD and 0x11223344") { SECTION("creates uint64_t out of 0xAABBCCDD and 0x11223344") {
uint64_t value = MakeLarge(0xAABBCCDD, 0x11223344); uint64_t value = MakeLarge(0xAABBCCDD, 0x11223344);