Skip to content

Add Math.multiplyHigh, unsignedMultiplyHigh and P-256 Montgomery multiply intrinsics - #28

Merged
Project516 merged 1 commit into
masterfrom
p516/ec-intrinsics
Nov 3, 2026
Merged

Project516 merged 1 commit into
masterfrom
p516/ec-intrinsics

Conversation

@Project516

Copy link
Copy Markdown
Owner

P-256 field arithmetic was half the CPU time of an EC-heavy run (one VM of java/security/cert/CertPathValidator/OCSP/OCSPTimeout.java):

Method Share
MontgomeryIntegerPolynomialP256.mult 25%
Math.multiplyHigh 8%
IntegerPolynomial$MutableElement.setProduct 6%
Math.unsignedMultiplyHigh 5%

Java longs are BigInts here, so every 64-bit step of the bytecode allocates. HotSpot replaces Math.multiplyHigh, Math.unsignedMultiplyHigh and the P-256 multiply with intrinsics, and so does this PR (src/natives/crypto.ts):

  • Math.multiplyHigh and unsignedMultiplyHigh each become one BigInt multiply and shift, instead of about 15 BigInt steps.
  • MontgomeryIntegerPolynomialP256.mult follows 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 before r is written, so mult(a, a, a) works.

That OCSPTimeout VM run went from 18.0 s to 10.1 s, with p256Mult now 9.5% of it. This speeds up EC key generation, ECDH, ECDSA and P-256 TLS handshakes.

Checks:

  • A fuzz run of 200,000 cases (random limbs below 2^52, limbs at the mask, values near p, mult(a, a, a), and random and edge longs for both multiplyHigh methods) printed the same checksums as HotSpot running the Java code.
  • test/java/CryptoMath.java now also covers multiplyHigh and unsignedMultiplyHigh on edge values, and seeded P-256 key generation, ECDH and ECDSA.
  • pnpm test passes (130 tests).

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Performance
    • Added optimized support for P-256 elliptic-curve operations, including key agreement and digital signatures.
    • Expanded intrinsic support for signed and unsigned high-half multiplication, improving coverage for arithmetic edge cases.
  • Documentation
    • Updated the documented cryptographic intrinsic coverage to include additional SHA algorithms and P-256 operations.

The crypto module adds a P-256 multiplication intrinsic and signed and unsigned Math.multiplyHigh intrinsics. The Java test adds arithmetic checks and exercises P-256 ECDH and SHA256withECDSA signing and verification.

Changes: Crypto intrinsic support

Layer / File(s) Summary
Native arithmetic intrinsics
src/natives/crypto.ts, AGENTS.md
The module adds P-256 limb multiplication and reduction, registers signed and unsigned multiply-high implementations, and updates the intrinsic description to include P-256 field methods.
Java arithmetic and cryptography checks
test/java/CryptoMath.java
The test checks signed and unsigned multiply-high results, derives ECDH secrets from seeded P-256 key pairs, and signs and verifies data with SHA256withECDSA.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Merge Risk: Minimal · up to 6070f

No identified issue currently prevents merging after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title names the main change: it adds Math.multiplyHigh, Math.unsignedMultiplyHigh, and P-256 Montgomery multiply intrinsics. The raw summary and PR objectives confirm these changes in src/natives/…
Description check Passed The description explains the motivation (P-256 field arithmetic CPU cost), the intrinsics added in src/natives/crypto.ts for Math.multiplyHigh, Math.unsignedMultiplyHigh, and MontgomeryIntegerPolynomi…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: it adds Math.multiplyHigh, Math.unsignedMultiplyHigh, and P-256 Montgomery multiply intrinsics. The raw summary and PR objectives confirm these changes in src/natives/crypto.ts and test/java/CryptoMath.java. The title is specific and concise, and it is 84 characters, which is long but acceptable.

Full details: Description check

Explanation

The description explains the motivation (P-256 field arithmetic CPU cost), the intrinsics added in src/natives/crypto.ts for Math.multiplyHigh, Math.unsignedMultiplyHigh, and MontgomeryIntegerPolynomialP256.mult, and the validation via CryptoMath.java and a fuzz run. It matches the changeset, so it passes.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Simplify code
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

I’m a rabbit with a math-sized grin,
I watch five limbs reduce and spin.
Signed and unsigned highs take flight,
Two P-256 keys agree just right.
I nibble leaves and check signatures tonight.

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 4e62f612-8552-4f76-9ccc-d47f40c2a034
Commits

Reviewing files that changed from the base of the PR and between 80b94d8 and 6070fda.

Files selected for processing: 3
  • AGENTS.md
  • src/natives/crypto.ts
  • test/java/CryptoMath.java

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Recent review details
Context from checks skipped due to timeout: 6
  • GitHub Check: CodeRabbit
  • GitHub Check: jtreg / run (4)
  • GitHub Check: jtreg / run (3)
  • GitHub Check: jtreg / run (1)
  • GitHub Check: jtreg / run (2)
  • GitHub Check: test
Additional comments: 3
src/natives/crypto.ts: 1

1-4: LGTM!

Also applies to: 16-16, 264-319, 322-326

AGENTS.md: 1

52-52: LGTM!

test/java/CryptoMath.java: 1

2-4: LGTM!

Also applies to: 11-11, 16-16, 72-104

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Project516
Project516 enabled auto-merge (squash) November 3, 2026 20:10
@Project516
Project516 merged commit 06cb8a3 into master Nov 3, 2026
12 checks passed
@Project516
Project516 deleted the p516/ec-intrinsics branch November 3, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ Proxy