EIPs Explained

EIP-152 · Final · Core · 2016-10-04

EIP-152: Add BLAKE2 compression function `F` precompile

Added a precompile at address 0x09 that runs the BLAKE2b compression function F, so contracts can verify BLAKE2-based data such as Zcash headers cheaply.

What problem does it solve?

BLAKE2 is a fast, widely used hash function. Zcash's Equihash proof of work depends on BLAKE2b, and so do other projects such as Filecoin and Handshake. To verify a Zcash block header inside an Ethereum contract (for a BTC-Relay-style light client or a trustless cross-chain swap), you need many BLAKE2b runs per header.

BLAKE2b is built for 64-bit, little-endian CPUs. The EVM works with 256-bit, big-endian words, so a pure Solidity version is very expensive in gas. A precompile lets clients run native code instead.

How it works

Rather than a full "BLAKE2b hash" precompile, the authors chose to expose only the core compression function F from RFC 7693. Projects need different BLAKE2 features (personalization, salts, keyed hashing, tree hashing, varied output lengths). With F available, a contract can build any of these itself.

The precompile lives at address 0x09. Its input must be exactly 213 bytes:

Bytes Field Encoding
4 rounds big-endian uint32
64 h state vector 8 × little-endian uint64
128 m message block 16 × little-endian uint64
8 + 8 t_0, t_1 offset counters little-endian uint64
1 f final-block flag must be 0 or 1

Any other length, or an f value other than 0 or 1, is an error. The output is the updated 64-byte state h, still little-endian. Gas cost is simply 1 gas per round. Standard BLAKE2b uses 12 rounds, but callers may ask for more.

Sample code

A thin Solidity wrapper (adapted from the pattern in the spec):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

library Blake2F {
    function compress(
        uint32 rounds,
        bytes32[2] memory h,   // 64-byte state
        bytes32[4] memory m,   // 128-byte message block
        bytes8[2] memory t,    // offset counters
        bool f                 // final block?
    ) internal view returns (bytes32[2] memory out) {
        bytes memory input = abi.encodePacked(rounds, h[0], h[1], m[0], m[1], m[2], m[3], t[0], t[1], f);
        // input.length == 213
        assembly {
            if iszero(staticcall(gas(), 0x09, add(input, 32), 213, out, 64)) {
                revert(0, 0)
            }
        }
    }
}

abi.encodePacked encodes bool as a single byte, 0x00 or 0x01, which is exactly what the precompile expects.

Things to know

  • This proposal is Final. It went live in the Istanbul upgrade (EIP-1679) in 2019.
  • Watch the byte order. rounds is big-endian, but every other field is little-endian. Getting this wrong is the most common bug when you call F.
  • The precompile gives you the compression step only. Padding, counters and the final-block flag are your code's job, as RFC 7693 describes.
  • Like other precompiles, its address is fixed, so contracts that call it work only on chains that enabled Istanbul-era precompiles.

#precompile #cryptography #hashing #blake2 #istanbul

Details

Authors
Tjaden Hess <[email protected]>, Matt Luongo (@mhluongo), Piotr Dyraga (@pdyraga), James Hancock (@MadeOfTin)
Status
Final
Type
Standards Track / Core
Created
2016-10-04
Required by

Machine-readable: Markdown · JSON