Skip to content

fix(ext/node): implement safe, add, rem options for crypto.generatePrime#32618

Open
bartlomieju wants to merge 5 commits intomainfrom
fix/crypto-generate-prime-options
Open

fix(ext/node): implement safe, add, rem options for crypto.generatePrime#32618
bartlomieju wants to merge 5 commits intomainfrom
fix/crypto-generate-prime-options

Conversation

@bartlomieju
Copy link
Member

@bartlomieju bartlomieju commented Mar 10, 2026

Summary

  • Implements the previously stubbed safe, add, and rem options for crypto.generatePrime() / crypto.generatePrimeSync(), matching Node.js/OpenSSL behavior
  • Fixes checkPrime / checkPrimeSync bigint truncation bug where candidates > 2^63 were silently truncated to i64, causing incorrect primality results
  • Fixes unsignedBigIntToBuffer using StringPrototypeToString (a String.prototype method) on BigInt values
  • Adds ERR_OSSL_BN_BIGNUM_TOO_LONG validation for oversized buffer candidates
  • Fixes async generatePrime promise error handling (.then().catch() → two-arg .then(onResolve, onReject)) to prevent double callback invocation

Implements the previously stubbed `safe`, `add`, and `rem` options for
`crypto.generatePrime()` and `crypto.generatePrimeSync()`, and fixes
`checkPrime`/`checkPrimeSync` for large bigint candidates.

- Add `Prime::generate_with_options()` in Rust supporting safe primes
  and add/rem constraints matching OpenSSL/Node.js behavior
- Update `op_node_gen_prime` and `op_node_gen_prime_async` ops to accept
  safe, add, rem parameters
- Fix bigint truncation bug in `checkPrime`/`checkPrimeSync` where
  bigints > 2^63 were silently truncated to i64
- Add `ERR_OSSL_BN_BIGNUM_TOO_LONG` validation for oversized candidates
- Fix `unsignedBigIntToBuffer` using wrong primordial method on BigInt
- Fix async `generatePrime` promise error handling to prevent double
  callback invocation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

@kajukitli kajukitli left a comment

Choose a reason for hiding this comment

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

lgtm, solid implementation of safe/add/rem options

key fixes:

  • bigint truncation bug fixed by converting to bytes in JS before passing to op
  • safe prime generation correctly checks (p-1)/2 is also prime
  • add/rem constraints properly adjust candidates to satisfy p % add == rem
  • default rem values match Node.js: 1 for normal, 3 for safe primes

the edge case handling for large add values (where only a few candidates exist) is good — directly iterating instead of random sampling.

minor observations:

  1. the is_biguint_probably_prime with 20 rounds is solid (matches OpenSSL's default)
  2. the gen_biguint_below rejection sampling loop could theoretically run many times for unlucky values, but in practice it converges quickly

the promise fix (.then().catch() → two-arg .then()) prevents double callback — good catch.

Copy link
Contributor

@kajukitli kajukitli left a comment

Choose a reason for hiding this comment

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

found one correctness bug: options.add = 0 is accepted, but the rust side assumes a non-zero modulus and can hit divide-by-zero / invalid arithmetic in prime generation

Copy link
Contributor

@kajukitli kajukitli left a comment

Choose a reason for hiding this comment

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

found one correctness bug: options.add = 0 is accepted, but the rust side assumes a non-zero modulus and can hit divide-by-zero / invalid arithmetic in prime generation

bartlomieju and others added 2 commits March 11, 2026 22:09
A zero-length or all-zero add buffer would pass validation and reach
the Rust generator where modulus operations assume non-zero add,
causing a division-by-zero panic. Throw ERR_OUT_OF_RANGE early.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

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

Any tests we can enable?

@bartlomieju bartlomieju requested a review from littledivy March 12, 2026 16:16
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.

3 participants