Repository navigation
Add keccak_224, keccak_256, keccak_384 and keccak_512 - #48
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,
0x01instead of FIPS 202's0x06.Hash2 already implements both through
detail::keccak_base, sodetail::keccak_base<0x01, 512, 256>is exactly Keccak-256, but no public name exposes it. Users who need Keccak-256 today have to reach intodetailor link another library. This PR makes the existing code public under the names other libraries use (Keccak-256in OpenSSL's default provider,keccak_256in noble-hashes and in Ethereum's tooling).The change
In
boost/hash2/sha3.hpp, aftershake_256:detail::keccak_seeded<D>, a class template overkeccak_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 asdetail::write64lewithout a temporary array.keccak_224,keccak_256,keccak_384andkeccak_512, which inherit those constructors.hmac_keccak_224,hmac_keccak_256,hmac_keccak_384andhmac_keccak_512.No existing code changes; the permutation and sponge are the ones
sha3_*already use.Tests
New
test/keccak.cpp, registered intest/Jamfile(and thus in the CMake tests):0xCCare theLen = 0andLen = 8entries of the Keccak team's known-answer files from the third round of the SHA-3 competition (ShortMsgKAT_224.txttoShortMsgKAT_512.txt)."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'shashlibSHA-3 digests.update, for all four sizes.keccak_basefed 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/testsuite 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 withstatic_assertunder 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 withsha3_224tosha3_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