Skip to content

Texnomic/Keccak

Repository files navigation

Texnomic.Keccak

NuGet Downloads License: MIT .NET 10

Managed C# / .NET 10 implementation of the Keccak family (224 / 256 / 384 / 512) using the original Keccak padding (0x01 .. 0x80) — the variant adopted by Ethereum, not FIPS-202 SHA-3. Allocation-free ref struct hasher, fully unrolled Keccak-f[1600] permutation, Span-based API throughout.

⚠️ This is not SHA-3 (FIPS-202). FIPS-202 SHA-3 uses the 0x06 .. 0x80 padding rule and produces different digests for the same input. Ethereum standardised on Keccak before FIPS-202 was finalised, hence the divergence. If you need FIPS-202 SHA-3, use System.Security.Cryptography.SHA3_256 / SHA3_512.

Installation

dotnet add package Texnomic.Keccak

Usage

using Texnomic.Keccak;

// One-shot
byte[] Digest = Keccak.Hash256("abc"u8);
// 4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45

// Hash into a caller-supplied buffer (no allocation)
Span<byte> Out = stackalloc byte[32];
Keccak.HashTo("abc"u8, KeccakSize.Bits256, Out);

Streaming

KeccakHasher Hasher = new(KeccakSize.Bits256);

Hasher.Update(BlockA);
Hasher.Update(BlockB);

Span<byte> Digest = stackalloc byte[32];
Hasher.Finish(Digest);

KeccakHasher is a ref struct — single-shot, stack-allocated, no GC traffic. Once Finish is called the hasher is consumed; reuse requires a fresh instance.

Other digest sizes

byte[] D224 = Keccak.Hash224(Data);   // 28 bytes
byte[] D256 = Keccak.Hash256(Data);   // 32 bytes — Ethereum's hash
byte[] D384 = Keccak.Hash384(Data);   // 48 bytes
byte[] D512 = Keccak.Hash512(Data);   // 64 bytes

Or pick a size dynamically:

byte[] Digest = Keccak.Hash(Data, KeccakSize.Bits512);

Why Keccak (and not SHA-3)?

Keccak (this library) FIPS-202 SHA-3
Padding 0x01 .. 0x80 (original) 0x06 .. 0x80 (domain separator)
Permutation Keccak-f[1600] Keccak-f[1600] (identical)
Hash256("") c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a
Used by Ethereum, Solidity keccak256, EVM precompiles NIST standard, System.Security.Cryptography.SHA3_*

If you're hashing Ethereum addresses, signing EIP-712 typed data, or implementing anything in the EVM ecosystem, you want Keccak — this library, not SHA-3.

Verification

Standard Keccak test vectors are exercised in Texnomic.Keccak.Tests:

  • Empty input ("").
  • "abc" (3 bytes).
  • 1-million-a long-message vector.
  • Random buffers hashed one-shot vs. byte-at-a-time streaming.

All four digest sizes (224 / 256 / 384 / 512) are covered.

Performance

The permutation is fully unrolled across 25 lanes with 24 inlined rounds. The hasher is a ref struct backed by inline arrays — no heap allocation on the hot path, no array-pool rentals, no Span<T>.CopyTo past the rate boundary.

For a single short message (e.g. 32-byte input → 32-byte digest) the entire hash runs in stack memory.

Supported platforms

Pure managed code — runs anywhere .NET 10 runs:

OS Architecture Status
Windows x64 / arm64
Linux x64 / arm64
macOS x64 / arm64

Building from source

git clone https://github.com/texnomic/Keccak
cd Keccak
dotnet build
dotnet test
dotnet pack -c Release

License

MIT © Texnomic.

About

Managed C# implementation of the Keccak family (224/256/384/512) using the original Keccak padding — the variant adopted by Ethereum, distinct from FIPS-202 SHA-3.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors