mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:08:12 +00:00
LibCrypto: Split BigInteger operations into an Algorithms class
Since the operations are already complicated and will become even more so soon, let's split them into their own files. We can also integrate the NumberTheory operations that would better fit there into this class as well. This commit doesn't change behaviors, but moves the allocation of some variables into caller classes.
This commit is contained in:
parent
0853d98420
commit
5963f6f9ff
13 changed files with 736 additions and 582 deletions
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
* Copyright (c) 2020-2021, Dex♪ <dexes.ttp@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "UnsignedBigIntegerAlgorithms.h"
|
||||
|
||||
namespace Crypto {
|
||||
|
||||
void UnsignedBigIntegerAlgorithms::destructive_modular_power_without_allocation(
|
||||
UnsignedBigInteger& ep,
|
||||
UnsignedBigInteger& base,
|
||||
UnsignedBigInteger const& m,
|
||||
UnsignedBigInteger& temp_1,
|
||||
UnsignedBigInteger& temp_2,
|
||||
UnsignedBigInteger& temp_3,
|
||||
UnsignedBigInteger& temp_4,
|
||||
UnsignedBigInteger& temp_multiply,
|
||||
UnsignedBigInteger& temp_quotient,
|
||||
UnsignedBigInteger& temp_remainder,
|
||||
UnsignedBigInteger& exp)
|
||||
{
|
||||
exp.set_to(1);
|
||||
while (!(ep < 1)) {
|
||||
if (ep.words()[0] % 2 == 1) {
|
||||
// exp = (exp * base) % m;
|
||||
multiply_without_allocation(exp, base, temp_1, temp_2, temp_3, temp_4, temp_multiply);
|
||||
divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
|
||||
exp.set_to(temp_remainder);
|
||||
}
|
||||
|
||||
// ep = ep / 2;
|
||||
divide_u16_without_allocation(ep, 2, temp_quotient, temp_remainder);
|
||||
ep.set_to(temp_quotient);
|
||||
|
||||
// base = (base * base) % m;
|
||||
multiply_without_allocation(base, base, temp_1, temp_2, temp_3, temp_4, temp_multiply);
|
||||
divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
|
||||
base.set_to(temp_remainder);
|
||||
|
||||
// Note that not clamping here would cause future calculations (multiply, specifically) to allocate even more unused space
|
||||
// which would then persist through the temp bigints, and significantly slow down later loops.
|
||||
// To avoid that, we can clamp to a specific max size, or just clamp to the min needed amount of space.
|
||||
ep.clamp_to_trimmed_length();
|
||||
exp.clamp_to_trimmed_length();
|
||||
base.clamp_to_trimmed_length();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue