perf: use Math.unsignedMultiplyHigh on Java 18+ — up to 61% faster

On Java 18+, Math.unsignedMultiplyHigh compiles to a single UMULH
instruction, eliminating the 4-instruction signed→unsigned correction
(multiplyHigh + 2 AND + 1 SHR + 2 ADD) per 64×64 product. With 16
products per field multiply, this saves ~64 instructions per mul/sqr.

Detection uses MethodHandle resolved once at class init. On older JVMs
and Android, falls back to the signed+correction path automatically.

Results on Java 21 (vs previous):
  pubkeyCreate:  23,400 → 37,700 ops/s (+61%, 2.2× → 1.6× native)
  sign (cached): 16,700 → 27,400 ops/s (+64%, 1.5× → 1.1× native)
  verify:         5,600 →  7,300 ops/s (+31%, 4.4× → 3.6× native)
  ECDH:           7,100 → 10,000 ops/s (+41%, 3.9× → 3.5× native)
  ecdhXOnly:      5,600 → 10,500 ops/s (+88%, 4.4× → 2.6× native)

https://claude.ai/code/session_01BhU63WUe9AhikZxRdw3Lpg
This commit is contained in:
Claude
2026-04-06 17:58:02 +00:00
parent ef7790b775
commit 1b9706c23f
4 changed files with 70 additions and 2 deletions
@@ -33,3 +33,8 @@ internal actual fun multiplyHigh(
} else {
multiplyHighFallback(a, b)
}
internal actual fun unsignedMultiplyHigh(
a: Long,
b: Long,
): Long = unsignedMultiplyHighFallback(a, b)