Skip to content

perf: compute Keccak-256 without BigInt - #12

Merged
Samarth208P merged 4 commits into
Samarth208P:mainfrom
Dhananjay-alt:perf/keccak-no-bigint
Nov 21, 2026
Merged

Samarth208P merged 4 commits into
Samarth208P:mainfrom
Dhananjay-alt:perf/keccak-no-bigint

Conversation

@Dhananjay-alt

Copy link
Copy Markdown
Contributor

Closes #1.

Keccak-256 now runs on two 32-bit halves per 64-bit lane (a Uint32Array) instead of BigInt, and the rho/pi lane destinations are precomputed. The EIP-55 string is built with a loop instead of spread/map/parseInt.

Results are unchanged. That includes valid and details.checksum, so 0xde709f2102306220921060314715629080e2fb77 still reports checksum: true with strict mode off. The PR includes the #11 repeated-prefix tests from main.

Benchmark

npm run benchmark was run unchanged on both revisions, on the same host, alternating main (2f6facb) and this branch 7 times each. Values are medians, in validations/s.

Node 22.23.5 main this PR
ethereum 10,059 120,746 12.0×
EVM detection 8,914 39,867 4.5×
bitcoin 212,380 209,117 0.98×
solana 488,645 487,940 1.00×
tron 222,743 218,405 0.98×
Node 24.16.0 main this PR
ethereum 11,704 124,314 10.6×
EVM detection 9,345 38,065 4.1×
bitcoin 218,500 214,766 0.98×
solana 509,574 510,256 1.00×
tron 228,983 219,582 0.96×

This PR doesn't touch the non-EVM chains, so their differences are run-to-run noise.

Size

Measured with gzip -9 on dist/:

  • ethereum.mjs chunk: 1,235 → 1,374 B (+139 B)
  • ethereum.cjs chunk: 1,264 → 1,394 B (+130 B)
  • index.umd.js: 6,815 → 6,956 B (+141 B)
  • root index.mjs and index.cjs entries: unchanged

The packed-consumer size budgets still pass.

Tests

  • tests/utils.test.ts:
    • the published digests of "" and "abc"
    • output equal to @noble/hashes for 1, 40, 135, 136, 137, 271, 272, 273 and 500-byte input, which straddles the 136-byte rate and the second block
  • tests/ethereum.test.ts:
    • the EIP-55 vectors whose checksummed form is all lowercase (0xde709f…) or all uppercase (0x52908400…) return { valid: true, details: { checksum: true } }, with and without strict mode
    • the opposite case of each is valid with checksum: false, and is rejected in strict mode
  • scripts/test-differential.mjs:
    • compares valid and details.checksum with a direct EIP-55 implementation on @noble/hashes, in both default and strict mode
    • runs over seeded addresses and length-preserving mutations: embedded or repeated 0x/0X, case flips, and non-hex pairs
    • passes on main and on this branch, and fails on the code reviewed in perf + fixes: faster hashing/decoding/detection, network option, Ripple details, missing tests #10 (0x0x + 38 digits)
    • the existing Keccak checks for lengths 0–300 against @noble/hashes still pass
    • differential comparisons go from 9,795 to 67,287

Results

  • 259 tests; 67,287 differential comparisons.
  • Coverage: statements 96.2%, branches 91.9%, functions 100%, lines 99.1%.
  • Locally on Linux, I ran a clean npm ci, npm run check and npm audit (0 vulnerabilities) on Node 22.23.5, 24.16.0 and 26.13.1, and npm run test:coverage on Node 24.16.0. All passed.
  • This repository's CI workflow ran on my fork for this exact commit (254ac40), and all 7 jobs passed: package on Ubuntu and Windows with Node 22, 24 and 26 (Chrome stable), plus coverage. Run: https://github.com/Dhananjay-alt/validator0x/actions/runs/44905428052
  • Locally, the packed worker test ran in Playwright's Chromium because Chrome stable isn't installed on that machine. The fork CI run above used Chrome stable.

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 Samarth208P#10.

Closes Samarth208P#1
Copilot AI balanced review requested due to automatic review settings November 21, 2026 12:47

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.

@Samarth208P
Samarth208P merged commit 23a0ddc into Samarth208P:main Nov 21, 2026
7 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.

perf: Ethereum/EVM validation is ~20× slower than other chains (BigInt Keccak)

3 participants