Skip to content

Refactor EC key padding length lookup into shared cryptoECC helper; add BN-254 support#27

Merged
ljoy913 merged 7 commits into
masterfrom
ljoy/update-build-dependencies
Jun 29, 2026
Merged

Refactor EC key padding length lookup into shared cryptoECC helper; add BN-254 support#27
ljoy913 merged 7 commits into
masterfrom
ljoy/update-build-dependencies

Conversation

@ljoy913

@ljoy913 ljoy913 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

PR #25’s leading-zero padding logic introduced four duplicated curve-length tables in ECDH/ECDSA, and all four omitted BN-254. This change consolidates curve element-length resolution into a single shared source and makes lookups case-insensitive to avoid drift and mismatches.

  • Shared curve element-length source in cryptoECC

    • Added curveElementLengths in src/cryptoECC.js with the full supported set, including "BN-254": 32.
    • Added/exported cryptoECC.curveElementLength(curveName) to normalize via toUpperCase() and return the fixed element byte length.
  • Replaced duplicated partLen tables (4 call sites)

    • src/ecdh.js
      • generateKey
      • importKey (p.format === "jwk")
    • src/ecdsa.js
      • generateKey
      • importKey (p.format === "jwk")
    • Each now reads from cryptoECC.curveElementLength(...) instead of inline object literals.
  • Targeted BN-254 coverage updates in existing ECC tests

    • Added BN-254 key-generation test cases in test/Test.Ecdh.js and test/Test.Ecdsa.js.
    • Updated test key-length checks to use cryptoECC.curveElementLength(...) directly instead of duplicated ecdhKeyLengths / ecdsaKeyLengths tables.
  • Rebuilt distribution bundle

    • Regenerated dist/msrcrypto.js and dist/msrcrypto.min.js so the published bundle includes the fix.

Example of the lookup change:

// before
var partLen = {
    "P-256": 32, "P-384": 48, "P-521": 66,
    "NUMSP256D1": 32, "NUMSP256T1": 32,
    "NUMSP384D1": 48, "NUMSP384T1": 48,
    "NUMSP512D1": 64, "NUMSP512T1": 64
}[p.algorithm.namedCurve];

// after
var partLen = cryptoECC.curveElementLength(p.algorithm.namedCurve);

@ljoy913 ljoy913 changed the title Merge BN-254 padding fix (PR #26) into master Refactor EC key padding length lookup into shared cryptoECC helper; add BN-254 support Jun 28, 2026
@ljoy913 ljoy913 requested a review from Copilot June 28, 2026 14:09

Copilot AI 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.

Pull request overview

This PR centralizes ECC curve element byte-length resolution into cryptoECC to eliminate duplicated per-curve tables in ECDH/ECDSA padding logic, and extends that shared lookup to include BN-254 (32-byte elements). It also updates the QUnit ECDH/ECDSA tests to cover BN-254 and to reference the shared helper, and regenerates the distribution bundle accordingly.

Changes:

  • Added cryptoECC.curveElementLength(curveName) (case-insensitive) and a shared curveElementLengths table including "BN-254": 32.
  • Updated ECDH/ECDSA generateKey and JWK importKey padding logic to use cryptoECC.curveElementLength(...) instead of inline tables.
  • Added BN-254 generateKey coverage in ECDH/ECDSA tests and removed duplicated test-side curve-length tables.

Reviewed changes

Copilot reviewed 5 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/cryptoECC.js Introduces shared curve element-length lookup (including BN-254) and exports it from cryptoECC.
src/ecdh.js Replaces inline curve-length table with cryptoECC.curveElementLength(...) for key padding in generate/import (JWK).
src/ecdsa.js Replaces inline curve-length table with cryptoECC.curveElementLength(...) for key padding in generate/import (JWK).
test/Test.Ecdh.js Adds BN-254 generateKey test and switches length assertions to the shared helper.
test/Test.Ecdsa.js Adds BN-254 generateKey test and switches length assertions to the shared helper.
dist/msrcrypto.js Regenerated bundle reflecting the shared helper + call-site updates.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/Test.Ecdsa.js Outdated
Comment thread test/Test.Ecdh.js Outdated
@ljoy913 ljoy913 requested a review from zaverucha June 28, 2026 14:23
The #26 change replaced the tests' own curve-length tables with calls to
cryptoECC.curveElementLength(). cryptoECC is an internal module inside the
UMD bundle and is not exposed as a global, so SubtleTests.html threw
ReferenceError: cryptoECC is not defined. Restore independent test-local
tables (keeping the new BN-254 entry); the production de-duplication in
ecdh.js/ecdsa.js/cryptoECC.js is unaffected.
@ljoy913 ljoy913 merged commit 48b662f into master Jun 29, 2026
3 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.

4 participants