JDK-4891331 : BigInteger a.multiply(a) should use squaring code
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2003-07-16
  • Updated: 2014-07-29
  • Resolved: 2014-02-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u20 b01Fixed 9Fixed
Description
Some algorithms need to perform a lot of squaring operations (e.g. elliptic curve crypto). Squaring can be implemented in approximately half the number of primitive multiplications as general multiplication code. 

BigInteger has that code internally but it is not accessible by applications. To change that, the multiply(BigInteger val) code should include a this.equals(val) check and use the squaring code if so.

Comments
Release team: Too late to take performance enhancements. Rejecting critical request and deferring to 8u20 for reevaluation for 8u20
03-12-2013

SQE is ok to take this performance enhancement in jdk8. Existing regression and SQE tests for java.math.BigInteger will be run to make sure there's no (functional) regression.
03-12-2013

EVALUATION Offhand, I think calling .equals would be too expensive in general since it would be adding a potential O(n) operation to multiply. Using == instead would be a small constant cost in the cases where it was not used. For this to go into Tiger, some measured speed advantage would need to be demonstrated. ###@###.### 2003-11-05
05-11-2003

SUGGESTED FIX ------- BigInteger.java ------- --- /tmp/sccs.WHaqmc Wed Jul 16 15:37:16 2003 +++ BigInteger.java Wed Jul 16 15:36:38 2003 @@ -1142,6 +1142,9 @@ public BigInteger multiply(BigInteger val) { if (signum == 0 || val.signum==0) return ZERO; + if (this.equals(val)) { + return square(); + } int[] result = multiplyToLen(mag, mag.length, val.mag, val.mag.length, null); If the call to equals() is considered to be too expensive, just use (this == val), which would be sufficient for most applications. ###@###.### 2003-07-16
16-07-2003