Fixed bug about bigint product, updated unit tests and sample usage.

This commit is contained in:
2025-12-17 17:04:16 +01:00
parent 65358fc766
commit 7cc2615f8b
5 changed files with 103 additions and 12 deletions

View File

@@ -1413,9 +1413,11 @@ bigint_result_t bigint_karatsuba(const bigint_t *x, const bigint_t *y) {
const size_t x_size = vector_size(x->digits);
const size_t y_size = vector_size(y->digits);
const size_t min_size = x_size < y_size ? x_size : y_size;
const size_t max_size = x_size > y_size ? x_size : y_size;
// Base case using "grade school" quadratic algorithm
if (x_size <= 32 || y_size <= 32) {
if (min_size <= 32 || max_size / min_size > 2) {
return bigint_karatsuba_base(x, y);
}
@@ -1498,6 +1500,8 @@ bigint_result_t bigint_karatsuba(const bigint_t *x, const bigint_t *y) {
result.status = BIGINT_OK;
SET_MSG(result, "Product between big integers was successful");
return result;
cleanup: // Destroy intermediate allocations on error
if (x1) { bigint_destroy(x1); }
if (x0) { bigint_destroy(x0); }