Repository navigation
Add Math.multiplyHigh, unsignedMultiplyHigh and P-256 Montgomery multiply intrinsics - #28
Conversation
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
There was a problem hiding this comment.
This PR adds HotSpot-style intrinsics for Math.multiplyHigh, Math.unsignedMultiplyHigh, and MontgomeryIntegerPolynomialP256.mult in src/natives/crypto.ts. The multiplyHigh intrinsics replace ~15 BigInt steps with a single multiply-and-shift. The P-256 Montgomery multiply follows the Java algorithm row-by-row using exact BigInt arithmetic, validated by 200k fuzz cases matching HotSpot. Tests in CryptoMath.java now cover edge cases for the new intrinsics and seeded P-256 key generation, ECDH, and ECDSA.
review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve
P-256 field arithmetic was half the CPU time of an EC-heavy run (one VM of
java/security/cert/CertPathValidator/OCSP/OCSPTimeout.java):MontgomeryIntegerPolynomialP256.multMath.multiplyHighIntegerPolynomial$MutableElement.setProductMath.unsignedMultiplyHighJava longs are BigInts here, so every 64-bit step of the bytecode allocates. HotSpot replaces
Math.multiplyHigh,Math.unsignedMultiplyHighand the P-256 multiply with intrinsics, and so does this PR (src/natives/crypto.ts):Math.multiplyHighandunsignedMultiplyHigheach become one BigInt multiply and shift, instead of about 15 BigInt steps.MontgomeryIntegerPolynomialP256.multfollows the Java code row by row with exact BigInt steps. For limbs below 2^52, which the Java code expects and HotSpot's x64 intrinsic assumes (it reads only those bits), every intermediate value stays non-negative and below 2^63 until the final signed subtraction. So exact BigInt arithmetic gives the same 64-bit results without wrapping. The inputs are read beforeris written, somult(a, a, a)works.That OCSPTimeout VM run went from 18.0 s to 10.1 s, with
p256Multnow 9.5% of it. This speeds up EC key generation, ECDH, ECDSA and P-256 TLS handshakes.Checks:
mult(a, a, a), and random and edge longs for bothmultiplyHighmethods) printed the same checksums as HotSpot running the Java code.test/java/CryptoMath.javanow also coversmultiplyHighandunsignedMultiplyHighon edge values, and seeded P-256 key generation, ECDH and ECDSA.pnpm testpasses (130 tests).