1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:37:35 +00:00

LibCrypto: Add UnsignedBigInteger division

The division operation returns both the quotient and the remainder.
This commit is contained in:
Itamar 2020-04-09 12:52:25 +03:00 committed by Andreas Kling
parent 2959c4a5e9
commit 0d2777752e
3 changed files with 113 additions and 6 deletions

View file

@ -306,6 +306,7 @@ void bigint_test_fibo500();
void bigint_addition_edgecases();
void bigint_subtraction();
void bigint_multiplication();
void bigint_division();
int aes_cbc_tests()
{
@ -801,6 +802,7 @@ int bigint_tests()
bigint_addition_edgecases();
bigint_subtraction();
bigint_multiplication();
bigint_division();
return 0;
}
@ -905,6 +907,14 @@ void bigint_subtraction()
FAIL(Incorrect Result);
}
}
{
I_TEST((BigInteger | Subtraction with large numbers 2));
Crypto::UnsignedBigInteger num1(Vector<u32> { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 });
Crypto::UnsignedBigInteger num2(Vector<u32> { 4196414175, 1117247942, 1123294122, 191895498, 3347106536, 16 });
Crypto::UnsignedBigInteger result = num1.sub(num2);
// this test only verifies that we don't crash on an assertion
PASS;
}
}
void bigint_multiplication()
@ -944,3 +954,44 @@ void bigint_multiplication()
}
}
}
void bigint_division()
{
{
I_TEST((BigInteger | Simple Division));
Crypto::UnsignedBigInteger num1(27194);
Crypto::UnsignedBigInteger num2(251);
auto result = num1.divide(num2);
Crypto::UnsignedDivisionResult expected = { Crypto::UnsignedBigInteger(108), Crypto::UnsignedBigInteger(86) };
if (result.quotient == expected.quotient && result.remainder == expected.remainder) {
PASS;
} else {
FAIL(Incorrect Result);
}
}
{
I_TEST((BigInteger | Division with big numbers));
Crypto::UnsignedBigInteger num1 = bigint_fibonacci(386);
Crypto::UnsignedBigInteger num2 = bigint_fibonacci(238);
auto result = num1.divide(num2);
Crypto::UnsignedDivisionResult expected = {
Crypto::UnsignedBigInteger(Vector<u32> { 2300984486, 2637503534, 2022805584, 107 }),
Crypto::UnsignedBigInteger(Vector<u32> { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 })
};
if (result.quotient == expected.quotient && result.remainder == expected.remainder) {
PASS;
} else {
FAIL(Incorrect Result);
}
}
{
I_TEST((BigInteger | Combined test));
auto num1 = bigint_fibonacci(497);
auto num2 = bigint_fibonacci(238);
auto div_result = num1.divide(num2);
if (div_result.quotient.multiply(num2).add(div_result.remainder) == num1) {
PASS;
} else {
FAIL(Incorrect Result);
}
}
}