2020-08-15 21:20:33 +00:00
|
|
|
|
/*
|
2021-04-22 20:13:01 +00:00
|
|
|
|
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
2020-08-15 21:20:33 +00:00
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-15 21:20:33 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2021-01-15 20:46:23 +00:00
|
|
|
|
#include <AK/Debug.h>
|
2024-11-20 22:10:48 +00:00
|
|
|
|
#include <AK/Random.h>
|
2021-05-10 18:55:25 +00:00
|
|
|
|
#include <LibCrypto/BigInt/Algorithms/UnsignedBigIntegerAlgorithms.h>
|
2020-08-15 21:20:33 +00:00
|
|
|
|
#include <LibCrypto/NumberTheory/ModularFunctions.h>
|
|
|
|
|
|
2023-07-11 17:49:08 +00:00
|
|
|
|
namespace Crypto::NumberTheory {
|
2020-08-15 21:20:33 +00:00
|
|
|
|
|
2024-03-13 11:30:59 +00:00
|
|
|
|
UnsignedBigInteger Mod(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
|
|
|
|
|
{
|
|
|
|
|
UnsignedBigInteger result;
|
|
|
|
|
result.set_to(a);
|
|
|
|
|
result.set_to(result.divided_by(b).remainder);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
|
UnsignedBigInteger ModularInverse(UnsignedBigInteger const& a_, UnsignedBigInteger const& b)
|
2020-08-15 21:20:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (b == 1)
|
|
|
|
|
return { 1 };
|
|
|
|
|
|
|
|
|
|
UnsignedBigInteger temp_1;
|
|
|
|
|
UnsignedBigInteger temp_minus;
|
|
|
|
|
UnsignedBigInteger temp_quotient;
|
2021-05-10 18:55:25 +00:00
|
|
|
|
UnsignedBigInteger temp_d;
|
|
|
|
|
UnsignedBigInteger temp_u;
|
|
|
|
|
UnsignedBigInteger temp_v;
|
|
|
|
|
UnsignedBigInteger temp_x;
|
|
|
|
|
UnsignedBigInteger result;
|
|
|
|
|
|
2024-03-18 19:33:44 +00:00
|
|
|
|
UnsignedBigIntegerAlgorithms::modular_inverse_without_allocation(a_, b, temp_1, temp_minus, temp_quotient, temp_d, temp_u, temp_v, temp_x, result);
|
2021-05-10 18:55:25 +00:00
|
|
|
|
return result;
|
2020-08-15 21:20:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
|
UnsignedBigInteger ModularPower(UnsignedBigInteger const& b, UnsignedBigInteger const& e, UnsignedBigInteger const& m)
|
2020-08-15 21:20:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (m == 1)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2021-05-12 20:47:07 +00:00
|
|
|
|
if (m.is_odd()) {
|
|
|
|
|
UnsignedBigInteger temp_z0 { 0 };
|
|
|
|
|
UnsignedBigInteger temp_rr { 0 };
|
|
|
|
|
UnsignedBigInteger temp_one { 0 };
|
|
|
|
|
UnsignedBigInteger temp_z { 0 };
|
|
|
|
|
UnsignedBigInteger temp_zz { 0 };
|
|
|
|
|
UnsignedBigInteger temp_x { 0 };
|
|
|
|
|
UnsignedBigInteger temp_extra { 0 };
|
|
|
|
|
|
|
|
|
|
UnsignedBigInteger result;
|
|
|
|
|
UnsignedBigIntegerAlgorithms::montgomery_modular_power_with_minimal_allocations(b, e, m, temp_z0, temp_rr, temp_one, temp_z, temp_zz, temp_x, temp_extra, result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-15 21:20:33 +00:00
|
|
|
|
UnsignedBigInteger ep { e };
|
|
|
|
|
UnsignedBigInteger base { b };
|
|
|
|
|
|
2021-05-10 18:55:25 +00:00
|
|
|
|
UnsignedBigInteger result;
|
2020-08-15 21:20:33 +00:00
|
|
|
|
UnsignedBigInteger temp_1;
|
|
|
|
|
UnsignedBigInteger temp_2;
|
|
|
|
|
UnsignedBigInteger temp_3;
|
|
|
|
|
UnsignedBigInteger temp_multiply;
|
|
|
|
|
UnsignedBigInteger temp_quotient;
|
|
|
|
|
UnsignedBigInteger temp_remainder;
|
|
|
|
|
|
2024-03-18 19:33:44 +00:00
|
|
|
|
UnsignedBigIntegerAlgorithms::destructive_modular_power_without_allocation(ep, base, m, temp_1, temp_2, temp_3, temp_multiply, temp_quotient, temp_remainder, result);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
|
2021-05-10 18:55:25 +00:00
|
|
|
|
return result;
|
2020-08-15 21:20:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
|
UnsignedBigInteger GCD(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
|
2020-08-15 21:20:33 +00:00
|
|
|
|
{
|
2021-05-10 18:55:25 +00:00
|
|
|
|
UnsignedBigInteger temp_a { a };
|
|
|
|
|
UnsignedBigInteger temp_b { b };
|
2020-08-15 21:20:33 +00:00
|
|
|
|
UnsignedBigInteger temp_quotient;
|
|
|
|
|
UnsignedBigInteger temp_remainder;
|
|
|
|
|
UnsignedBigInteger output;
|
|
|
|
|
|
2024-03-18 19:33:44 +00:00
|
|
|
|
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder, output);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
|
UnsignedBigInteger LCM(UnsignedBigInteger const& a, UnsignedBigInteger const& b)
|
2020-08-15 21:20:33 +00:00
|
|
|
|
{
|
2021-05-10 18:55:25 +00:00
|
|
|
|
UnsignedBigInteger temp_a { a };
|
|
|
|
|
UnsignedBigInteger temp_b { b };
|
2020-08-15 21:20:33 +00:00
|
|
|
|
UnsignedBigInteger temp_1;
|
|
|
|
|
UnsignedBigInteger temp_2;
|
|
|
|
|
UnsignedBigInteger temp_3;
|
|
|
|
|
UnsignedBigInteger temp_quotient;
|
|
|
|
|
UnsignedBigInteger temp_remainder;
|
|
|
|
|
UnsignedBigInteger gcd_output;
|
|
|
|
|
UnsignedBigInteger output { 0 };
|
|
|
|
|
|
2024-03-18 19:33:44 +00:00
|
|
|
|
UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(temp_a, temp_b, temp_quotient, temp_remainder, gcd_output);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
if (gcd_output == 0) {
|
2021-05-01 19:10:08 +00:00
|
|
|
|
dbgln_if(NT_DEBUG, "GCD is zero");
|
2020-08-15 21:20:33 +00:00
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// output = (a / gcd_output) * b
|
2024-03-18 19:33:44 +00:00
|
|
|
|
UnsignedBigIntegerAlgorithms::divide_without_allocation(a, gcd_output, temp_quotient, temp_remainder);
|
2021-05-12 08:47:21 +00:00
|
|
|
|
UnsignedBigIntegerAlgorithms::multiply_without_allocation(temp_quotient, b, temp_1, temp_2, temp_3, output);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
|
2021-02-07 12:03:24 +00:00
|
|
|
|
dbgln_if(NT_DEBUG, "quot: {} rem: {} out: {}", temp_quotient, temp_remainder, output);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
|
static bool MR_primality_test(UnsignedBigInteger n, Vector<UnsignedBigInteger, 256> const& tests)
|
2020-08-15 21:20:33 +00:00
|
|
|
|
{
|
|
|
|
|
// Written using Wikipedia:
|
|
|
|
|
// https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Miller%E2%80%93Rabin_test
|
2021-02-23 19:42:32 +00:00
|
|
|
|
VERIFY(!(n < 4));
|
2020-08-15 21:20:33 +00:00
|
|
|
|
auto predecessor = n.minus({ 1 });
|
|
|
|
|
auto d = predecessor;
|
|
|
|
|
size_t r = 0;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto div_result = d.divided_by(2);
|
|
|
|
|
while (div_result.remainder == 0) {
|
|
|
|
|
d = div_result.quotient;
|
|
|
|
|
div_result = d.divided_by(2);
|
|
|
|
|
++r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (r == 0) {
|
|
|
|
|
// n - 1 is odd, so n was even. But there is only one even prime:
|
|
|
|
|
return n == 2;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 11:22:18 +00:00
|
|
|
|
for (auto& a : tests) {
|
2021-02-23 19:42:32 +00:00
|
|
|
|
// Technically: VERIFY(2 <= a && a <= n - 2)
|
|
|
|
|
VERIFY(a < n);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
auto x = ModularPower(a, d, n);
|
|
|
|
|
if (x == 1 || x == predecessor)
|
|
|
|
|
continue;
|
|
|
|
|
bool skip_this_witness = false;
|
|
|
|
|
// r − 1 iterations.
|
|
|
|
|
for (size_t i = 0; i < r - 1; ++i) {
|
|
|
|
|
x = ModularPower(x, 2, n);
|
|
|
|
|
if (x == predecessor) {
|
|
|
|
|
skip_this_witness = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (skip_this_witness)
|
|
|
|
|
continue;
|
|
|
|
|
return false; // "composite"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true; // "probably prime"
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
|
UnsignedBigInteger random_number(UnsignedBigInteger const& min, UnsignedBigInteger const& max_excluded)
|
2020-08-15 21:20:33 +00:00
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
|
VERIFY(min < max_excluded);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
auto range = max_excluded.minus(min);
|
|
|
|
|
UnsignedBigInteger base;
|
|
|
|
|
auto size = range.trimmed_length() * sizeof(u32) + 2;
|
|
|
|
|
// "+2" is intentional (see below).
|
2022-01-20 17:47:39 +00:00
|
|
|
|
auto buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
|
2021-05-13 07:43:11 +00:00
|
|
|
|
auto* buf = buffer.data();
|
|
|
|
|
|
2023-04-02 17:08:43 +00:00
|
|
|
|
fill_with_random(buffer);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
UnsignedBigInteger random { buf, size };
|
|
|
|
|
// At this point, `random` is a large number, in the range [0, 256^size).
|
|
|
|
|
// To get down to the actual range, we could just compute random % range.
|
|
|
|
|
// This introduces "modulo bias". However, since we added 2 to `size`,
|
|
|
|
|
// we know that the generated range is at least 65536 times as large as the
|
|
|
|
|
// required range! This means that the modulo bias is only 0.0015%, if all
|
|
|
|
|
// inputs are chosen adversarially. Let's hope this is good enough.
|
|
|
|
|
auto divmod = random.divided_by(range);
|
|
|
|
|
// The proper way to fix this is to restart if `divmod.quotient` is maximal.
|
|
|
|
|
return divmod.remainder.plus(min);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
|
bool is_probably_prime(UnsignedBigInteger const& p)
|
2020-08-15 21:20:33 +00:00
|
|
|
|
{
|
|
|
|
|
// Is it a small number?
|
|
|
|
|
if (p < 49) {
|
|
|
|
|
u32 p_value = p.words()[0];
|
|
|
|
|
// Is it a very small prime?
|
|
|
|
|
if (p_value == 2 || p_value == 3 || p_value == 5 || p_value == 7)
|
|
|
|
|
return true;
|
|
|
|
|
// Is it the multiple of a very small prime?
|
|
|
|
|
if (p_value % 2 == 0 || p_value % 3 == 0 || p_value % 5 == 0 || p_value % 7 == 0)
|
|
|
|
|
return false;
|
|
|
|
|
// Then it must be a prime, but not a very small prime, like 37.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<UnsignedBigInteger, 256> tests;
|
|
|
|
|
// Make some good initial guesses that are guaranteed to find all primes < 2^64.
|
|
|
|
|
tests.append(UnsignedBigInteger(2));
|
|
|
|
|
tests.append(UnsignedBigInteger(3));
|
|
|
|
|
tests.append(UnsignedBigInteger(5));
|
|
|
|
|
tests.append(UnsignedBigInteger(7));
|
|
|
|
|
tests.append(UnsignedBigInteger(11));
|
|
|
|
|
tests.append(UnsignedBigInteger(13));
|
|
|
|
|
UnsignedBigInteger seventeen { 17 };
|
|
|
|
|
for (size_t i = tests.size(); i < 256; ++i) {
|
|
|
|
|
tests.append(random_number(seventeen, p.minus(2)));
|
|
|
|
|
}
|
|
|
|
|
// Miller-Rabin's "error" is 8^-k. In adversarial cases, it's 4^-k.
|
|
|
|
|
// With 200 random numbers, this would mean an error of about 2^-400.
|
|
|
|
|
// So we don't need to worry too much about the quality of the random numbers.
|
|
|
|
|
|
|
|
|
|
return MR_primality_test(p, tests);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnsignedBigInteger random_big_prime(size_t bits)
|
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
|
VERIFY(bits >= 33);
|
2024-01-12 21:34:22 +00:00
|
|
|
|
UnsignedBigInteger min = "6074001000"_bigint.shift_left(bits - 33);
|
2020-08-15 21:20:33 +00:00
|
|
|
|
UnsignedBigInteger max = UnsignedBigInteger { 1 }.shift_left(bits).minus(1);
|
|
|
|
|
for (;;) {
|
|
|
|
|
auto p = random_number(min, max);
|
|
|
|
|
if ((p.words()[0] & 1) == 0) {
|
|
|
|
|
// An even number is definitely not a large prime.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (is_probably_prime(p))
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|