Skip to content

SHA and BigInteger intrinsics - #20

Merged
Project516 merged 4 commits into
masterfrom
p516/crypto-intrinsics
Nov 2, 2026
Merged

Project516 merged 4 commits into
masterfrom
p516/crypto-intrinsics

Conversation

@Project516

@Project516 Project516 commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Part of #15.

HotSpot replaces these methods with intrinsics; java-web ran them as bytecode, where every Java long operation allocates a JS BigInt. src/natives/crypto.ts implements them in JS through the existing intrinsics table:

  • SHA.implCompress0, SHA2.implCompress0, SHA5.implCompress0: SHA-1, SHA-224/256 and SHA-384/512 block compression on the digest's state (SHA-512 on 32-bit halves, so no BigInt).
  • BigInteger.implMontgomeryMultiply, implMontgomerySquare (one BigInt REDC; the result fully reduced in product[0..len), the rest of a 2 len product zeroed, as the Java version leaves it), implMultiplyToLen, implSquareToLen and implMulAdd.

Measured on a small ARM board, two cores (HotSpot in brackets):

before after HotSpot
SHA-256, 8 MB 2,014 ms 111 ms 9 ms
SHA-1, 8 MB 2,359 ms 116 ms 36 ms
SHA-512, 8 MB 5,422 ms 304 ms 71 ms
HMAC-SHA256, 100k x 64 B 11,936 ms 3,434 ms 109 ms
modPow, 2048 bit x 20 29,903 ms 1,418 ms 558 ms
RSA-2048 key generation x 3 87,147 ms 11,184 ms 1,444 ms

java/security/KeyPairGenerator/Failover.java takes 13 s there now and returns to passing.txt. java/security/SecureRandom/NoSync.java went from about 235 s to 114 s, still too close to CI's timeout, so it stays out: what remains is general cost (long arithmetic in MutableBigInteger, many small calls around each HMAC and AES block), not the intrinsified methods. #15 stays open for it.

test/java/CryptoMath.java compares with HotSpot: every SHA variant at lengths around the block boundaries and from an offset, HMAC-SHA512, BigInteger multiply, square, pow and modPow (odd and even moduli) from 31 to 4,100 bits, deterministic RSA-2048 key generation and probablePrime.

Verified: CryptoMath, NoSync and Failover through the jtreg runner, pnpm typecheck. CI runs every OpenJDK test, since the natives changed.

…turn KeyPairGenerator/Failover to the passing list.
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 8 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

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: 33071cac-db20-4463-a716-697fb725d277
Commits

Reviewing files that changed from the base of the PR and between 083ffa2 and 43ef332.

Files selected for processing: 5
  • AGENTS.md
  • src/natives/crypto.ts
  • src/natives/index.ts
  • test/java/CryptoMath.java
  • test/jtreg/passing.txt
  • 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

Comment thread test/java/CryptoMath.java Fixed

@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 native JavaScript intrinsics for SHA-1/256/512 compression and BigInteger arithmetic (Montgomery multiply/square, multiply, square, mulAdd) to replace bytecode that allocated a JS BigInt per 64-bit operation. Performance improvements are significant. However, the SHA and SHA-512 implementations use module-level shared mutable arrays (w32, wh, wl, sh, sl) which will cause data corruption when multiple threads execute these intrinsics concurrently. The JVM supports threading, so this is a correctness bug.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict request_changes

Comment thread src/natives/crypto.ts Resolved
Comment thread src/natives/crypto.ts Resolved
Comment thread src/natives/crypto.ts Resolved

@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.

All points from the last review are settled in the threads.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve

@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 high-performance intrinsics for SHA-1/256/512 compression and BigInteger arithmetic (Montgomery multiply/square, multiply, square, mulAdd). The algorithms are correct and deliver large speedups. However, the SHA implementations use module-level shared arrays (w32 for SHA-1/256; wh, wl, sh, sl for SHA-512) for the message schedule and working variables. Concurrent calls from different threads will overwrite each other's state, producing wrong digests. This is a correctness bug that must be fixed before merge.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict request_changes

Comment thread src/natives/crypto.ts Resolved
Comment thread src/natives/crypto.ts Resolved
Comment thread src/natives/crypto.ts Resolved

@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 native JavaScript intrinsics for SHA-1, SHA-256, SHA-512 block compression and BigInteger arithmetic (Montgomery multiply/square, multiplyToLen, squareToLen, mulAdd), replacing bytecode that allocated a JS BigInt per 64-bit operation. Performance improves by 10-50x on benchmarks. The CryptoMath test verifies correctness against HotSpot across SHA variants, HMAC, and BigInteger operations up to 4100 bits.

Not approved: 3 earlier points are still open in the threads.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict comment

@project516-review-bot
project516-review-bot Bot dismissed stale reviews from themself November 2, 2026 17:49

Superseded by the review of 43ef332, which has no blocking findings, and its threads are settled.

@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.

All points from the last review are settled in the threads.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve

@Project516
Project516 merged commit 3173a4e into master Nov 2, 2026
12 checks passed
@Project516
Project516 deleted the p516/crypto-intrinsics branch November 2, 2026 18:49
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.

2 participants

⌂ Proxy