diff options
author | Itamar <itamar8910@gmail.com> | 2020-04-08 13:07:47 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-02 12:24:10 +0200 |
commit | e0cf40518c2bde05030058c487268d2001c79cda (patch) | |
tree | 2a958b86bd7cd080ab14cf5571a98d1faa277ede /Userland | |
parent | 6201f741d4fd3d5a71e0dc4e9ccb1ef0e34ba067 (diff) | |
download | serenity-e0cf40518c2bde05030058c487268d2001c79cda.zip |
LibCrypto: Add UnsignedBigInteger subtraction and comparison
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/test-crypto.cpp | 72 |
1 files changed, 64 insertions, 8 deletions
diff --git a/Userland/test-crypto.cpp b/Userland/test-crypto.cpp index b47988a643..127bf55989 100644 --- a/Userland/test-crypto.cpp +++ b/Userland/test-crypto.cpp @@ -304,6 +304,7 @@ void hmac_sha512_test_process(); void bigint_test_fibo500(); void bigint_addition_edgecases(); +void bigint_subtraction(); int aes_cbc_tests() { @@ -797,21 +798,26 @@ int bigint_tests() { bigint_test_fibo500(); bigint_addition_edgecases(); + bigint_subtraction(); return 0; } +Crypto::UnsignedBigInteger bigint_fibonacci(size_t n) +{ + Crypto::UnsignedBigInteger num1(0); + Crypto::UnsignedBigInteger num2(1); + for (size_t i = 0; i < n; ++i) { + Crypto::UnsignedBigInteger t = num1.add(num2); + num2 = num1; + num1 = t; + } + return num1; +} void bigint_test_fibo500() { { I_TEST((BigInteger | Fibonacci500)); - Crypto::UnsignedBigInteger num1(0); - Crypto::UnsignedBigInteger num2(1); - for (int i = 0; i < 500; ++i) { - Crypto::UnsignedBigInteger t = num1.add(num2); - num2 = num1; - num1 = t; - } - bool pass = (num1.words() == AK::Vector<u32> { 315178285, 505575602, 1883328078, 125027121, 3649625763, 347570207, 74535262, 3832543808, 2472133297, 1600064941, 65273441 }); + bool pass = (bigint_fibonacci(500).words() == AK::Vector<u32> { 315178285, 505575602, 1883328078, 125027121, 3649625763, 347570207, 74535262, 3832543808, 2472133297, 1600064941, 65273441 }); if (pass) PASS; @@ -838,3 +844,53 @@ void bigint_addition_edgecases() } } } + +void bigint_subtraction() +{ + { + I_TEST((BigInteger | Simple Subtraction 1)); + Crypto::UnsignedBigInteger num1(80); + Crypto::UnsignedBigInteger num2(70); + + if (num1.sub(num2) == Crypto::UnsignedBigInteger(10)) { + PASS; + } else { + FAIL(Incorrect Result); + } + } + { + I_TEST((BigInteger | Simple Subtraction 2)); + Crypto::UnsignedBigInteger num1(50); + Crypto::UnsignedBigInteger num2(70); + + if (num1.sub(num2).is_invalid()) { + PASS; + } else { + FAIL(Incorrect Result); + } + } + { + I_TEST((BigInteger | Subtraction with borrow)); + Crypto::UnsignedBigInteger num1(UINT32_MAX); + Crypto::UnsignedBigInteger num2(1); + Crypto::UnsignedBigInteger num3 = num1.add(num2); + Crypto::UnsignedBigInteger result = num3.sub(num2); + if (result == num1) { + PASS; + } else { + FAIL(Incorrect Result); + } + } + { + I_TEST((BigInteger | Subtraction with large numbers)); + Crypto::UnsignedBigInteger num1 = bigint_fibonacci(343); + Crypto::UnsignedBigInteger num2 = bigint_fibonacci(218); + Crypto::UnsignedBigInteger result = num1.sub(num2); + if ((result.add(num2) == num1) + && (result.words() == Vector<u32> { 811430588, 2958904896, 1130908877, 2830569969, 3243275482, 3047460725, 774025231, 7990 })) { + PASS; + } else { + FAIL(Incorrect Result); + } + } +} |