Skip to content

keccak: Use shared int_bytes.hpp helpers and constexpr dispatch - #1775

Merged
chfast merged 2 commits into
masterfrom
crypto/keccak-modernize
Oct 22, 2026
Merged

chfast merged 2 commits into
masterfrom
crypto/keccak-modernize

Conversation

@chfast

@chfast chfast commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Move byteswap(), to_le(), load_le() and store_le() out of the RIPEMD-160 implementation into a new int_bytes.hpp header (std::byte interface). load_le() now requires std::integral like to_le(), which it already depended on.

Use them in Keccak instead of the local load_le() and to_le64() helpers and the preprocessor fallbacks (__has_builtin, __builtin_memcpy, BYTE_ORDER). keccak() now takes std::byte input; ethash_keccak256() keeps its uint8_t API.

Make keccakf1600_best constexpr on targets without the x86 runtime dispatch.

The generated code is identical at -O3 and -O1 for clang-23, g++-15 and g++-16 on x86-64, and clang-23 on aarch64 (also with the SHA3 path). RIPEMD-160 code is identical too.

@chfast
chfast force-pushed the crypto/keccak-modernize branch from 0b09a81 to a44ca60 Compare October 22, 2026 08:56
@codspeed

codspeed Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 1.45%

1 warning

⚠️ Base and head ran on different runner types

Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.

Learn more about Macro Runners

Open the report in CodSpeed to investigate

⚡ 2 improved benchmarks
✅ 897 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_calldatasize[fork_Osaka-blockchain_test-zero_data_True-calldata_size_1024-benchmark-gas-value_1M] 14.5 ms 14.2 ms +1.68%
⚡ test_callvalue_from_call[fork_Osaka-blockchain_test-non_zero_value_False-benchmark-gas-value_1M] 17.4 ms 17.2 ms +1.22%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing crypto/keccak-modernize (f86cbcd) with master (763bb89)

Open in CodSpeed

Move byteswap(), to_le(), load_le() and store_le() out of the RIPEMD-160
implementation to share them with other hash functions.
Replace the local load_le() and to_le64() helpers and the preprocessor
fallbacks with the shared int_bytes.hpp helpers. Make keccakf1600_best
constexpr on targets without runtime dispatch. The generated code is
identical.
@chfast
chfast force-pushed the crypto/keccak-modernize branch from a44ca60 to f86cbcd Compare October 22, 2026 09:16
@chfast chfast changed the title keccak: Use intx endian helpers and constexpr dispatch keccak: Use shared int_bytes.hpp helpers and constexpr dispatch Oct 22, 2026
@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.23%. Comparing base (763bb89) to head (f86cbcd).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1775      +/-   ##
==========================================
- Coverage   98.23%   98.23%   -0.01%     
==========================================
  Files         184      185       +1     
  Lines       17135    17131       -4     
  Branches     3912     3912              
==========================================
- Hits        16833    16829       -4     
  Misses        233      233              
  Partials       69       69              
Flag Coverage Δ
eest-develop-gmp 26.92% <100.00%> (-0.02%) ⬇️
eest-legacy 17.16% <90.00%> (+<0.01%) ⬆️
eest-libsecp256k1 29.08% <100.00%> (-0.02%) ⬇️
eest-stable 84.26% <100.00%> (-0.01%) ⬇️
evmone-unittests 94.10% <100.00%> (-0.01%) ⬇️
riscv64 94.81% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
core 96.61% <100.00%> (-0.01%) ⬇️
tooling 94.87% <ø> (ø)
tests 99.82% <ø> (ø)
Files with missing lines Coverage Δ
lib/evmone_precompiles/int_bytes.hpp 100.00% <100.00%> (ø)
lib/evmone_precompiles/keccak.cpp 99.59% <100.00%> (-0.01%) ⬇️
lib/evmone_precompiles/ripemd160.cpp 100.00% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

🟢 Approval recommended

The focused refactor preserves byte-order handling, input processing, and dispatch behavior, with no blocking issues identified.

No open issues 🎉

What changed in this PR

This PR consolidates integer byte-order helpers used by RIPEMD-160 and Keccak while preserving the public Keccak API and hash behavior.

Changes:

  • Extract shared byte-swap, little-endian load, and store helpers into int_bytes.hpp.
  • Use std::byte internally in Keccak, replacing local helpers and compiler-specific fallbacks.
  • Make the Keccak function pointer constexpr where runtime dispatch is unnecessary.
File Description
lib/​evmone_precompiles/​ripemd160.cpp Replace local byte-order helpers with the shared header.
lib/​evmone_precompiles/​keccak.cpp Adopt shared helpers, byte-based input, and constant dispatch where applicable.
lib/​evmone_precompiles/​int_bytes.hpp Define shared integral byte-conversion helpers.
lib/​evmone_precompiles/​CMakeLists.txt Register the new header in the precompiles target.

Review effort: Balanced

@chfast
chfast merged commit 0412867 into master Oct 22, 2026
33 checks passed
@chfast
chfast deleted the crypto/keccak-modernize branch October 22, 2026 09:58
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