mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:18:11 +00:00
LibCrypto: Add a simple SignedBigInteger
This patchset adds a simple SignedBigInteger that is entirely defined in terms of UnsignedBigInteger. It also adds a NumberTheory::Power function, which is terribly inefficient, but since the use of exponentiation is very much discouraged for large inputs, no particular attempts were made to make it more performant.
This commit is contained in:
parent
b4591f0037
commit
d8208fd37c
5 changed files with 661 additions and 0 deletions
|
@ -161,6 +161,30 @@ static auto ModularPower(const UnsignedBigInteger& b, const UnsignedBigInteger&
|
|||
return exp;
|
||||
}
|
||||
|
||||
// Note: This function _will_ generate extremely huge numbers, and in doing so,
|
||||
// it will allocate and free a lot of memory!
|
||||
// Please use |ModularPower| if your use-case is modexp.
|
||||
template<typename IntegerType>
|
||||
static auto Power(const IntegerType& b, const IntegerType& e) -> IntegerType
|
||||
{
|
||||
IntegerType ep { e };
|
||||
IntegerType base { b };
|
||||
IntegerType exp { 1 };
|
||||
|
||||
while (!(ep < 1)) {
|
||||
if (ep.words()[0] % 2 == 1)
|
||||
exp.set_to(exp.multiplied_by(base));
|
||||
|
||||
// ep = ep / 2;
|
||||
ep.set_to(ep.divided_by(2).quotient);
|
||||
|
||||
// base = base * base
|
||||
base.set_to(base.multiplied_by(base));
|
||||
}
|
||||
|
||||
return exp;
|
||||
}
|
||||
|
||||
static void GCD_without_allocation(
|
||||
const UnsignedBigInteger& a,
|
||||
const UnsignedBigInteger& b,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue