diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp index 987abce7bd..57c0bae64d 100644 --- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp +++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp @@ -39,7 +39,7 @@ UnsignedBigInteger UnsignedBigInteger::add(const UnsignedBigInteger& other) u32 word_addition_result = shorter->m_words[i] + longer->m_words[i]; u8 carry_out = 0; // if there was a carry, the result will be smaller than any of the operands - if (word_addition_result < shorter->m_words[i]) { + if (word_addition_result + carry < shorter->m_words[i]) { carry_out = 1; } if (carry) { diff --git a/Userland/test-crypto.cpp b/Userland/test-crypto.cpp index 127bf55989..5b38d8a692 100644 --- a/Userland/test-crypto.cpp +++ b/Userland/test-crypto.cpp @@ -843,6 +843,14 @@ void bigint_addition_edgecases() FAIL(Incorrect Result); } } + { + I_TEST((BigInteger | Borrow with zero)); + Crypto::UnsignedBigInteger num1({ UINT32_MAX - 3, UINT32_MAX }); + Crypto::UnsignedBigInteger num2({ UINT32_MAX - 2, 0 }); + if (num1.add(num2).words() == Vector { 4294967289, 0, 1 }) { + PASS; + } else { + FAIL(Incorrect Result); } void bigint_subtraction()