Skip to content

Comments

fix(crypto): correct compareTo checks in BigInteger RNG helpers#647

Merged
kjur merged 1 commit intokjur:masterfrom
Kr0emer:fix/bug-003-dsa-nonce-compareto
Feb 20, 2026
Merged

fix(crypto): correct compareTo checks in BigInteger RNG helpers#647
kjur merged 1 commit intokjur:masterfrom
Kr0emer:fix/bug-003-dsa-nonce-compareto

Conversation

@Kr0emer
Copy link
Contributor

@Kr0emer Kr0emer commented Feb 20, 2026

Summary

This PR fixes incorrect compareTo handling in BigInteger rejection sampling utilities used by DSA nonce generation.

Root Cause

BigInteger.compareTo may return any positive/negative integer, but code assumed sentinel values (-1/1), causing:

  • possible acceptance of out-of-range random candidates in getRandomBigIntegerZeroToMax
  • fragile min/max guard in getRandomBigIntegerMinToMax

Fix

  • src/crypto-1.1.js
    • biMax.compareTo(biRand) != -1 -> biMax.compareTo(biRand) >= 0
    • flagCompare == 1 -> flagCompare > 0

Tests

Added regressions in test/qunit-do-crypto.html:

  • reject out-of-range candidate and retry in getRandomBigIntegerZeroToMax
  • throw for any positive compareTo result in getRandomBigIntegerMinToMax

Validation

  • Targeted fix checks passed (rejection-sampling and min/max guard behavior).
  • DSA-path telemetry check (1000 signatures) showed overflow = 0.
  • Full npm test may depend on local network/installed dependencies.

@Kr0emer Kr0emer force-pushed the fix/bug-003-dsa-nonce-compareto branch from bc11065 to de2b309 Compare February 20, 2026 12:40
@Kr0emer Kr0emer force-pushed the fix/bug-003-dsa-nonce-compareto branch from b022050 to d89f0ec Compare February 20, 2026 13:11
@kjur kjur merged commit ee4b013 into kjur:master Feb 20, 2026
2 checks passed
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