Skip to main content
QUICK REVIEW

[Paper Review] Space-Efficient Karatsuba Multiplication for Multi-Precision Integers

Yiping Cheng|arXiv (Cornell University)|May 22, 2016
Cryptography and Residue ArithmeticComputer Science2 references3 citations
TL;DR

This paper presents a space-efficient implementation of the Karatsuba algorithm for multi-precision integer multiplication, adapting Roche's polynomial-based approach to handle integer-specific challenges like carries. It introduces a subtractive variant with four carry variables and case-based handling for even-length multiplication, achieving $O(\log n)$ space complexity while maintaining $O(n^{1.59})$ time complexity, with benchmarks showing a ~20% performance overhead compared to standard Karatsuba.

ABSTRACT

The traditional Karatsuba algorithm for the multiplication of polynomials and multi-precision integers has a time complexity of $O(n^{1.59})$ and a space complexity of $O(n)$. Roche proposed an improved algorithm with the same $O(n^{1.59})$ time complexity but with a much reduced $O(\log n)$ space complexity. In Roche's paper details were provided for multiplication of polynomials, but not for multi-precision integers. Multi-precision integers differ from polynomials by the presence of carries, which poses difficulties in implementing Roche's scheme in multi-precision integers. This paper provides a detailed solution to these difficulties. Finally, numerical comparisons between the schoolbook, traditional Karatsuba, and space-efficient Karatsuba algorithms are provided.

Motivation & Objective

  • To adapt Roche's space-efficient Karatsuba algorithm—originally designed for polynomials—to work correctly with multi-precision integers, which involve carries.
  • To resolve the challenges introduced by integer-specific arithmetic, particularly the propagation and management of carries during subtraction and addition in recursive Karatsuba steps.
  • To provide a complete, correct, and efficient C implementation of space-efficient Karatsuba multiplication for long integers, including handling of even and odd-length cases.
  • To evaluate the performance trade-off between space efficiency and time efficiency, comparing the proposed method with schoolbook and standard Karatsuba algorithms.

Proposed method

  • Adopts a subtractive version of Karatsuba multiplication instead of the additive version to better manage sign and carry propagation in integer arithmetic.
  • Introduces four separate carry variables to store intermediate results that may fall outside the standard limb range, ensuring correctness during additions and subtractions.
  • Splits even-length multiplication into three distinct cases based on the value of the intermediate difference $E = (A^{(0)}_0 - A^{(1)}_0) - (A^{(0)}_1 - A^{(1)}_1)$, handling edge cases where $E = -\rho^k$ separately.
  • Uses a recursive divide-and-conquer structure with base cases handled by schoolbook multiplication, switching to standard Karatsuba below a threshold length of 128 limbs.
  • Employs a modified recursive top-level function that computes $D = (A^{(0)} - A^{(1)})B + C\rho^n$ in-place, reusing the input buffer and minimizing temporary storage.
  • Implements careful carry propagation and limb alignment across recursive levels, ensuring correct final result even with negative or large intermediate values.

Experimental results

Research questions

  • RQ1How can Roche's space-efficient Karatsuba algorithm for polynomials be adapted to work correctly with multi-precision integers, given the presence of carries?
  • RQ2What modifications are required to handle the sign and range issues introduced by integer arithmetic, especially during subtraction and addition in recursive steps?
  • RQ3How does the performance of the space-efficient Karatsuba algorithm compare to the standard Karatsuba and schoolbook methods in terms of time and memory usage?
  • RQ4What are the key implementation challenges in supporting both even and odd-length integer multiplication in a space-efficient recursive framework?

Key findings

  • The proposed space-efficient Karatsuba algorithm achieves $O(\log n)$ space complexity, eliminating the need for $O(n)$ heap memory used in the traditional Karatsuba method.
  • The algorithm correctly handles integer-specific issues such as carries and sign extensions, particularly through case-based treatment of the intermediate value $E$ and four dedicated carry variables.
  • Numerical experiments show that the space-efficient Karatsuba algorithm runs approximately 20% slower than the standard Karatsuba algorithm, with average execution time growing by a factor of ~1622 for $n=10000$, close to the theoretical $n^{\log_2 3} \approx 1479$.
  • The performance overhead is attributed to additional carry management and conditional logic, but the algorithm maintains bit-exact correctness, matching results from both schoolbook and standard Karatsuba implementations.
  • The implementation is robust and portable, with a modular C design that supports recursive decomposition and threshold-based switching to schoolbook multiplication for small inputs.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.