EIPs Explained

EIP-197 · Final · Core · 2017-02-06

EIP-197: Precompiled contracts for optimal ate pairing check on the elliptic curve alt_bn128

Added a precompile at address 0x08 that checks elliptic-curve pairings on alt_bn128, making on-chain zkSNARK verification affordable.

What problem does it solve?

zkSNARKs let someone prove a statement is true without revealing why, using a proof that is small and quick to check. That helps with privacy, because data can stay hidden, and with scaling, because a large computation can be checked cheaply. Verifying a SNARK, however, needs pairing operations on an elliptic curve. Written in plain EVM code, these were far too expensive to fit in a block.

EIP-197 adds a native precompiled contract for this check. Together with EIP-196, which adds point addition and scalar multiplication on the same curve, it makes zkSNARK verification practical inside smart contracts.

How it works

  • Address: 0x08, active from the Byzantium fork.
  • Curve: alt_bn128 (also called BN254). G1 lies on y² = x³ + 3 over a prime field. G2 lies on a twist of that curve over a quadratic extension field. The spec chose this curve because it suits SNARKs and lets Ethereum reuse components and artifacts from Zcash.
  • Input: k pairs (a_i, b_i), with each a_i in G1 and each b_i in G2. Each pair is 192 bytes: G1 is two 32-byte coordinates, and G2 is two F_p² coordinates, each 2×32 bytes. The input length must be a multiple of 192. An empty input is valid and returns 1.
  • Output: 32 bytes. The value is 1 if e(a1, b1) · e(a2, b2) · … · e(ak, bk) = 1, and 0 otherwise. The call fails if the input is malformed or a point is not on the curve or not in the correct subgroup.
  • Encoding: big-endian 32-byte field elements. The point at infinity is (0, 0). An F_p² element a·i + b is encoded as (a, b), with the imaginary part first.

The precompile does not return a pairing value. It only checks whether a product of pairings equals 1. All known SNARK verifiers need only this check, and it avoids having to encode values from the complex target group.

Gas: originally 80,000 · k + 100,000.

Sample code

Calling the precompile from Solidity. Groth16 verifiers generated by common SNARK toolchains contain a routine like this:

function pairingCheck(uint256[] memory input) internal view returns (bool) {
    require(input.length % 6 == 0, "bad input"); // 6 words = 192 bytes per pair
    uint256[1] memory out;
    bool ok;
    assembly {
        ok := staticcall(gas(), 0x08, add(input, 0x20), mul(mload(input), 0x20), out, 0x20)
    }
    require(ok, "pairing failed");
    return out[0] == 1;
}

Layout of one pair (192 bytes):

[ a.x (32) | a.y (32) | b.x_imag (32) | b.x_real (32) | b.y_imag (32) | b.y_real (32) ]

Things to know

  • Gas was reduced in Istanbul by EIP-1108 to 34,000 · k + 45,000, which is the price today.
  • The ordering of F_p² coordinates (imaginary part first) is a common source of bugs when hand-encoding G2 points.
  • Later research lowered alt_bn128's estimated security below the 128 bits originally assumed. Newer designs often use BLS12-381, available through EIP-2537.
  • Shipped in Byzantium (EIP-609).

#precompile #cryptography #zksnark #pairing #byzantium

Details

Authors
Vitalik Buterin <[email protected]>, Christian Reitwiessner <[email protected]>
Status
Final
Type
Standards Track / Core
Created
2017-02-06
Required by

Machine-readable: Markdown · JSON