Skip to content

Add keccak_224, keccak_256, keccak_384 and keccak_512 - #48

Merged
pdimov merged 1 commit into
boostorg:developfrom
pinhopro:feature/keccak
Nov 18, 2026
Merged

pdimov merged 1 commit into
boostorg:developfrom
pinhopro:feature/keccak

Conversation

@pinhopro

Copy link
Copy Markdown

Motivation

Keccak-256, the Keccak team's submission to the SHA-3 competition with its original padding, is the hash function of Ethereum and of every chain and tool compatible with it: addresses, transaction ids, Solidity function selectors, EIP-55 checksums. It differs from SHA3-256 only in its padding byte, 0x01 instead of FIPS 202's 0x06.

Hash2 already implements both through detail::keccak_base, so detail::keccak_base<0x01, 512, 256> is exactly Keccak-256, but no public name exposes it. Users who need Keccak-256 today have to reach into detail or link another library. This PR makes the existing code public under the names other libraries use (Keccak-256 in OpenSSL's default provider, keccak_256 in noble-hashes and in Ethereum's tooling).

The change

In boost/hash2/sha3.hpp, after shake_256:

  • detail::keccak_seeded<D>, a class template over keccak_base<0x01, 2 * D, D> with the three constructors every SHA-3 class has (default, 64-bit seed, byte seed), each with the same meaning as for the SHA-3 classes. The seed's eight bytes are fed little-endian, one at a time, which gives the same bytes as detail::write64le without a temporary array.
  • keccak_224, keccak_256, keccak_384 and keccak_512, which inherit those constructors.
  • hmac_keccak_224, hmac_keccak_256, hmac_keccak_384 and hmac_keccak_512.

No existing code changes; the permutation and sponge are the ones sha3_* already use.

Tests

New test/keccak.cpp, registered in test/Jamfile (and thus in the CMake tests):

  • The digests of the empty message and of the byte 0xCC are the Len = 0 and Len = 8 entries of the Keccak team's known-answer files from the third round of the SHA-3 competition (ShortMsgKAT_224.txt to ShortMsgKAT_512.txt).
  • The digests of "abc" and an HMAC-Keccak-256 vector (RFC 4231 test case 2's key and message) were computed with an independent Keccak implementation that reproduces those KAT entries and Python's hashlib SHA-3 digests.
  • Incremental input (one byte at a time) against a single update, for all four sizes.
  • The integer and byte seeds against keccak_base fed the same bytes, and a zero seed against default construction.

The four classes and their HMAC aliases are also added to the general requirement tests (concept.cpp, plaintext_leak.cpp, integral_result.cpp, multiple_result.cpp), next to the SHA-3 ones.

Locally, the whole libs/hash2/test suite passes with b2 for Apple clang 21 and GCC 15.3, cxxstd=11,17,20, debug and release, and the CMake tests pass with Apple clang. The constructors are also usable in constant expressions (checked with static_assert under C++14).

Documentation

  • doc/hash2/reference/sha3.adoc: the classes and HMAC aliases in the synopsis, a note that they implement Keccak as submitted to the SHA-3 competition, before FIPS 202 changed its padding (Keccak-256 being Ethereum's hash), and are not interchangeable with sha3_224 to sha3_512, whose digests differ; and a reference section for each class.
  • doc/hash2/changes.adoc: an entry under 1.93.0.

🤖 Generated with Claude Code

Keccak as submitted to the SHA-3 competition, with the padding byte 0x01
instead of FIPS 202's 0x06. Keccak-256 is the hash function used by
Ethereum. The classes expose the existing detail::keccak_base under public
names, seeded the same way as sha3_224 to sha3_512, with HMAC aliases.
@pdimov
pdimov merged commit ab738a6 into boostorg:develop Nov 18, 2026
86 of 90 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