EIPs Explained

EIP-5988 · Stagnant · Core · 2022-11-15

EIP-5988: Add Poseidon hash function precompile

Proposes a precompile for the Poseidon hash, a ZK-friendly hash used by many rollups, with parameters supplied by the caller so different projects' variants can share it.

What problem does it solve?

Zero-knowledge proof systems are expensive when they have to prove Keccak or SHA-256, because those hashes are built from bit operations. Poseidon is an "arithmetic" hash designed to work natively over a prime field, which makes it far cheaper inside SNARK and STARK circuits. Many ZK rollups and ZK applications use it for Merkle trees and commitments.

The EVM has no native Poseidon, so contracts that need to recompute a Poseidon hash (to check a Merkle root built off-chain, for example) must implement it in Solidity, which costs a lot of gas. EIP-5988 proposes a precompile to make this cheap.

How it works

Different projects use different Poseidon parameters (field, rounds, state width), so instead of fixing one variant the precompile takes the parameters as input:

Field Bytes Meaning
p 32 Prime field modulus
security_level 2 Security level in bits (M)
alpha 1 S-box power
input_rate 2 Number of input elements
t 1 State size
full_round 1 Full rounds (R_F)
partial_round 1 Partial rounds (R_P)
input input_rate × 32 Field elements to hash

The fields are concatenated in that order and passed to the precompile, which computes the hash as defined in the Poseidon paper and returns the output. The draft lists the address as 0xA and leaves the activation block and gas cost as TBD.

The security section stresses that parameter choice matters, especially the MDS mixing matrix, and points to published cryptanalysis of Poseidon.

Sample code

Illustrative only: how a contract would build the input and call the precompile if it existed at the draft address. No such precompile is live.

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

library PoseidonDraft {
    address constant POSEIDON = address(0x0A); // draft value, not deployed

    function hash(
        uint256 p, uint16 securityLevel, uint8 alpha, uint8 t,
        uint8 fullRounds, uint8 partialRounds, uint256[] memory inputs
    ) internal view returns (bytes memory out) {
        bytes memory data = abi.encodePacked(
            p, securityLevel, alpha, uint16(inputs.length), t, fullRounds, partialRounds
        );
        for (uint256 i; i < inputs.length; ++i) data = abi.encodePacked(data, inputs[i]);
        (bool ok, bytes memory ret) = POSEIDON.staticcall(data);
        require(ok, "poseidon failed");
        return ret;
    }
}

Things to know

  • Status: Stagnant. The proposal is incomplete (gas cost, fork block and rationale are TBD) and has not been scheduled for any upgrade. Today, contracts that need Poseidon use Solidity or Yul implementations.
  • Address 0xA was later used by the KZG point-evaluation precompile from EIP-4844, so the address in this draft could not be used as written.
  • Poseidon is much younger than Keccak or SHA-256; its security rests on more recent and less battle-tested analysis, which the spec itself acknowledges.
  • A configurable precompile shifts responsibility for safe parameters onto callers.

#precompile #cryptography #zk #rollups #hash

Details

Authors
Abdelhamid Bakhta (@abdelhamidbakhta), Eli Ben Sasson (@Elistark), Avihu Levy (@avihu28), David Levit Gurevich (@DavidLevitGurevich)
Status
Stagnant
Type
Standards Track / Core
Created
2022-11-15

Machine-readable: Markdown · JSON