LibCrypto: Update ModularInverse implementation to use extended GCD

The previous implementation of `ModularInverse` was flaky and did not
compute the correct value in many occasions, especially with big numbers
like in RSA.

Also added a bunch of tests with big numbers.
This commit is contained in:
devgianlu 2024-12-15 16:06:14 +01:00 committed by Ali Mohammad Pur
commit f49a55d089
Notes: github-actions[bot] 2024-12-15 22:32:53 +00:00
4 changed files with 65 additions and 80 deletions

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
* Copyright (c) 2020-2021, Dex <dexes.ttp@gmail.com>
* Copyright (c) 2024, Altomani Gianluca <altomanigianluca@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -12,79 +13,24 @@ namespace Crypto {
void UnsignedBigIntegerAlgorithms::modular_inverse_without_allocation(
UnsignedBigInteger const& a,
UnsignedBigInteger const& b,
UnsignedBigInteger& temp_1,
UnsignedBigInteger& temp_minus,
UnsignedBigInteger& result,
UnsignedBigInteger& temp_y,
UnsignedBigInteger& temp_gcd,
UnsignedBigInteger& temp_quotient,
UnsignedBigInteger& temp_d,
UnsignedBigInteger& temp_u,
UnsignedBigInteger& temp_v,
UnsignedBigInteger& temp_x,
UnsignedBigInteger& result)
UnsignedBigInteger& temp_1,
UnsignedBigInteger& temp_2,
UnsignedBigInteger& temp_shift_result,
UnsignedBigInteger& temp_shift_plus,
UnsignedBigInteger& temp_shift,
UnsignedBigInteger& temp_r,
UnsignedBigInteger& temp_s,
UnsignedBigInteger& temp_t)
{
UnsignedBigInteger one { 1 };
extended_GCD_without_allocation(a, b, result, temp_y, temp_gcd, temp_quotient, temp_1, temp_2, temp_shift_result, temp_shift_plus, temp_shift, temp_r, temp_s, temp_t);
temp_u.set_to(a);
if (!a.is_odd()) {
// u += b
add_into_accumulator_without_allocation(temp_u, b);
}
temp_v.set_to(b);
temp_x.set_to(0);
// d = b - 1
subtract_without_allocation(b, one, temp_d);
while (!(temp_v == 1)) {
while (temp_v < temp_u) {
// u -= v
subtract_without_allocation(temp_u, temp_v, temp_minus);
temp_u.set_to(temp_minus);
// d += x
add_into_accumulator_without_allocation(temp_d, temp_x);
while (!temp_u.is_odd()) {
if (temp_d.is_odd()) {
// d += b
add_into_accumulator_without_allocation(temp_d, b);
}
// u /= 2
divide_u16_without_allocation(temp_u, 2, temp_quotient, temp_1);
temp_u.set_to(temp_quotient);
// d /= 2
divide_u16_without_allocation(temp_d, 2, temp_quotient, temp_1);
temp_d.set_to(temp_quotient);
}
}
// v -= u
subtract_without_allocation(temp_v, temp_u, temp_minus);
temp_v.set_to(temp_minus);
// x += d
add_into_accumulator_without_allocation(temp_x, temp_d);
while (!temp_v.is_odd()) {
if (temp_x.is_odd()) {
// x += b
add_into_accumulator_without_allocation(temp_x, b);
}
// v /= 2
divide_u16_without_allocation(temp_v, 2, temp_quotient, temp_1);
temp_v.set_to(temp_quotient);
// x /= 2
divide_u16_without_allocation(temp_x, 2, temp_quotient, temp_1);
temp_x.set_to(temp_quotient);
}
}
// return x % b
divide_without_allocation(temp_x, b, temp_quotient, result);
divide_without_allocation(result, b, temp_quotient, temp_1);
add_into_accumulator_without_allocation(temp_1, b);
divide_without_allocation(temp_1, b, temp_quotient, result);
}
}