Skip to content

perf + fixes: faster hashing/decoding/detection, network option, Ripple details, missing tests - #10

Closed
Dhananjay-alt wants to merge 6 commits into
Samarth208P:mainfrom
Dhananjay-alt:perf/keccak-and-test-coverage
Closed

Dhananjay-alt wants to merge 6 commits into
Samarth208P:mainfrom
Dhananjay-alt:perf/keccak-and-test-coverage

Conversation

@Dhananjay-alt

@Dhananjay-alt Dhananjay-alt commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1, closes #2, closes #3, closes #4, closes #5, closes #6, closes #7, closes #8, closes #9.

Each commit is self-contained, so the PR can be reviewed one commit at a time.

Performance

npm run benchmark (extended in this PR) on Node 24, 0.4.0 vs this branch, run back to back:

0.4.0 this PR
ethereum (checksummed) 4,306 48,873 11×
ethereum (lowercase) 4,381 1,041,451 238×
bitcoin (legacy) 77,677 181,999 2.3×
bitcoin (bech32) 219,009 337,944 1.5×
solana 182,641 312,129 1.7×
tron 83,678 192,398 2.3×
cosmos 294,321 452,560 1.5×
EVM detection 3,619 36,998 10×
bitcoin detection 17,568 147,069 8.4×
solana detection 28,580 68,768 2.4×
invalid-input detection 49,255 454,102 9.2×

All numbers are ops/s.

  • Keccak-256 (perf: Ethereum/EVM validation is ~20× slower than other chains (BigInt Keccak) #1): works on 32-bit lane halves instead of BigInt, with a precomputed rho/pi table.
  • EVM (perf (optional): skip Keccak for uniform-case EVM input when not in strict mode #9): outside strict mode, uniform-case input is no longer hashed. details.checksum is true only for a verified mixed-case checksum, or for an address with no hex letters. Strict mode still checks uniform-case input against EIP-55, including the all-caps EIP-55 vector.
  • Detection (perf: detectBlockchain runs every non-EVM validator on every input #8): each non-EVM chain has a cheap necessary precondition (Bech32 prefix, Base58 alphabet, 0x prefix) that is checked before its validator runs. Results and their order are unchanged. A new test compares detection against exhaustive validation over about 1,000 seeded and mutated inputs; I confirmed it fails when a gate is deliberately broken.
  • Bech32: the checksum is computed once while decoding, and Bech32 vs Bech32m is read from the residue instead of hashing twice. It no longer builds intermediate arrays.
  • SHA-256: uses a single message buffer, keeps state in locals, and inlines rotations.
  • Base58: a shared typed-array decoder via createBase58Decoder(alphabet). Ripple reuses it instead of keeping its own copy.
  • Cardano Byron: returns early unless the first byte is a CBOR array, so non-Byron input no longer throws inside the CBOR reader.
  • Bundle size: root ESM goes from 7,205 to 7,502 B gzip and Solana-only from 1,224 to 1,285 B. Both are within the package size budgets.

Behaviour changes

All three are noted under "Unreleased" in CHANGELOG.md:

Tests

Tests go from 220 to 290, and branch coverage from 91.8% to 95.4%.

Tooling and cleanup

Verification

npm run check, npm run test:coverage and npm audit (0 vulnerabilities) pass locally on Node 24. The 9,795 differential comparisons against Node crypto, @noble/hashes and @scure/base still pass.

Performance (npm run benchmark, Node 24):
- ethereum validation  ~3.9k/s  -> ~42k/s  (Keccak-f on 32-bit lane halves)
- EVM detection        ~3.2k/s  -> ~13.8k/s
- solana validation    ~187k/s  -> ~329k/s (typed-array Base58 decoder)
- bitcoin / tron       ~80k/s   -> ~100k/s (hoisted SHA-256 constants)

Ripple now reuses the shared Base58 decoder via createBase58Decoder
instead of a duplicated copy (-230 B gzip on the ripple chunk).

Tests (220 -> 275, branch coverage 91.8% -> 95.4%):
- Cosmos family had no unit tests; add per-chain prefix, 20/32-byte
  length policy, Bech32m, padding, and operator-prefix cases
- Cardano Shelley: CIP-19 pointer/enterprise vectors, pointer varint
  edge cases, header/prefix mismatches; fold cardano_debug test in
- Network policy (network/allowTestnet/NETWORK_MISMATCH details)
- Aptos formatting, Keccak block boundaries vs @noble/hashes,
  Base58/Base58Check error paths

Tooling: test:package falls back to Playwright's bundled Chromium when
system Chrome is absent. Remove stale tracked coverage/ output and old
lint/command logs; fix the `.txt` gitignore entry.
Copilot AI balanced review requested due to automatic review settings November 21, 2026 10:07

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Values other than 'mainnet', 'testnet' or 'auto' previously made every
network-bearing address fail with NETWORK_MISMATCH while EVM and other
network-less chains still passed. Reject them up front like an
unsupported chain.

Closes Samarth208P#6
Valid XRP results now include details { format: 'classic', checksum: true },
matching the other Base58Check chains. Document that X-addresses (XLS-5d)
are not supported.

Closes Samarth208P#7
All-lowercase or all-uppercase hex carries no checksum, so hashing it only
filled details.checksum. Outside strict mode such input is now accepted
without hashing, and details.checksum is true only for a verified
mixed-case checksum or an address with no hex letters. Strict mode still
verifies uniform-case input against EIP-55.

Closes Samarth208P#9
Each non-EVM chain now has a necessary condition (Bech32 prefix, Base58
alphabet, 0x prefix, ...) checked before its validator runs, so detection
no longer runs every Bech32 polymod and Base58 decode on every input.
Results and their order are unchanged; a new test compares detection
against exhaustive validation over ~1,000 seeded and mutated inputs.

The benchmark now covers lowercase EVM, Bech32, Cosmos and detection of
several formats.

Benchmark (ops/s): bitcoin detection 17k -> 224k, invalid input
42k -> 1.3M, EVM detection 15k -> 48k, solana detection 30k -> 48k.

Closes Samarth208P#8
- Bech32: compute the polymod once while decoding (no hrpExpand/concat
  arrays, lookup table instead of indexOf) and classify Bech32 vs
  Bech32m from the residue instead of hashing twice.
- SHA-256: one buffer for message and padding, state in locals,
  rotations inlined.
- Byron: return early unless the first byte is a CBOR array, so non-Byron
  Base58 input no longer throws and catches inside the CBOR reader.

Benchmark (ops/s): bitcoin legacy 100k -> 250k, tron 90k -> 245k,
bech32 190k -> 460k, cosmos 230k -> 520k, solana detection 50k -> 98k.
Add an Unreleased changelog entry for Samarth208P#6-Samarth208P#9 and these changes.

@Samarth208P Samarth208P left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@Dhananjay-alt, thank you for the performance work and the additional protocol tests. All 290 tests and 9,795 differential checks passed locally. An additional regression check found that the uniform-case fast path accepts an EVM address with a repeated prefix and reports a valid checksum. This changes which destinations callers can accept. I’ve posted the details on #9.

I’m closing this PR. Please open a new PR from the latest main with the format check and regression coverage. If the replacement includes #6, please also cover an empty batch and empty detection input with an invalid network option, or document when the RangeError applies. We’ll review the replacement after the CI matrix passes.

Samarth208P pushed a commit that referenced this pull request Nov 21, 2026
Store each 64-bit lane as two 32-bit halves in a Uint32Array and
precompute the rho/pi destinations. Build the EIP-55 string with a loop
instead of spread/map/parseInt. Validation results, including
details.checksum, are unchanged.

Node 24.16, scripts/benchmark.mjs, median of 5 alternating runs on the
same host (validations/s):
  ethereum       11,556 -> 121,126  (10.5x)
  EVM detection   9,149 ->  36,244  (4.0x)
  bitcoin/solana/tron unchanged within noise (0.95-0.99x)
gzip: ethereum.mjs chunk +139 B, index.umd.js +141 B.

Tests: published digests for "" and "abc", and @noble/hashes at 1, 40,
135-137, 271-273 and 500 bytes. The EIP-55 vectors whose checksum form
is all lowercase or all uppercase keep checksum: true with and without
strict mode. The differential suite now compares valid and
details.checksum with a direct EIP-55 implementation over
length-preserving mutations (repeated or embedded 0x, case flips,
non-hex pairs); it fails on the code reviewed in #10.

Closes #1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants