feat(big): add SBigPowMod

This commit is contained in:
fallenoak 2023-02-05 16:36:38 -06:00
parent c2a6459c94
commit 09d2481be9
No known key found for this signature in database
GPG key ID: 7628F8E61AEA070D
5 changed files with 244 additions and 0 deletions

View file

@ -58,6 +58,10 @@ void SBigMul(BigData* a, BigData* b, BigData* c) {
Mul(a->Primary(), b->Primary(), c->Primary(), a->Stack());
}
void SBigPowMod(BigData* a, BigData* b, BigData* c, BigData* d) {
PowMod(a->Primary(), b->Primary(), c->Primary(), d->Primary(), a->Stack());
}
void SBigShl(BigData* a, BigData* b, uint32_t shift) {
Shl(a->Primary(), b->Primary(), shift);
}

View file

@ -22,6 +22,8 @@ void SBigMul(BigData* a, BigData* b, BigData* c);
void SBigNew(BigData** num);
void SBigPowMod(BigData* a, BigData* b, BigData* c, BigData* d);
void SBigShl(BigData* a, BigData* b, uint32_t shift);
void SBigShr(BigData* a, BigData* b, uint32_t shift);

View file

@ -241,6 +241,63 @@ void MulMod(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, const BigBuffe
stack.Free(allocCount);
}
void PowMod(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, const BigBuffer& d, BigStack& stack) {
c.Trim();
if (c.Count() == 0) {
SetOne(a);
return;
}
uint32_t allocCount = 0;
auto& temp = stack.Alloc(&allocCount);
auto& b2 = stack.Alloc(&allocCount);
auto& b3 = stack.Alloc(&allocCount);
auto& aa = stack.MakeDistinct(a, &a == &b || &a == &c || &a == &d);
MulMod(b2, b, b, d, stack);
MulMod(b3, b2, b, d, stack);
const BigBuffer* bPower[3];
bPower[0] = &b;
bPower[1] = &b2;
bPower[2] = &b3;
SetOne(aa);
for (uint32_t i = c.Count() - 1; i + 1 > 0; i--) {
auto v12 = c[i];
uint32_t v23 = 32;
if (i == c.Count() - 1 && !(v12 & 0xC0000000)) {
do {
v12 *= 4;
v23 -= 2;
} while (!(v12 & 0xC0000000));
}
if (v23) {
for (uint32_t j = ((v23 - 1) >> 1) + 1; j > 0; j--) {
Square(aa, aa, stack);
Div(temp, aa, aa, d, stack);
Square(aa, aa, stack);
Div(temp, aa, aa, d, stack);
if (v12 >> 30) {
auto& power = *bPower[(v12 >> 30) - 1];
MulMod(aa, aa, power, d, stack);
}
v12 *= 4;
}
}
}
stack.UnmakeDistinct(a, aa);
stack.Free(allocCount);
}
void SetOne(BigBuffer& buffer) {
buffer.SetCount(1);
buffer[0] = 1;

View file

@ -37,6 +37,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 PowMod(BigBuffer& a, const BigBuffer& b, const BigBuffer& c, const BigBuffer& d, BigStack& stack);
void SetOne(BigBuffer& buffer);
void SetZero(BigBuffer& buffer);