EIPs Explained

EIP-196 · Final · Core · 2017-02-02

EIP-196: Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128

Added precompiled contracts at 0x06 and 0x07 for point addition and scalar multiplication on the alt_bn128 curve, making zkSNARK verification affordable on Ethereum.

What problem does it solve?

zkSNARKs let someone prove a statement is true without revealing why. On Ethereum that means privacy (hidden inputs) and scaling (a small proof that stands for a lot of work). Verifying a proof needs elliptic-curve arithmetic. Written in EVM bytecode, that arithmetic was far too expensive to fit in a block.

EIP-196 builds the two most basic curve operations into the protocol as precompiled contracts. Together with the pairing check in EIP-197, they make on-chain SNARK verification practical. Both shipped in the Byzantium upgrade (EIP-609).

How it works

The curve is alt_bn128 (also called BN254), y² = x³ + 3 over a prime field whose modulus p is given in the spec. It was chosen because it is pairing-friendly and shares tooling with Zcash.

Address Operation Input (bytes) Output (bytes)
0x06 ECADD: P + Q x1, y1, x2, y2 (128) x, y (64)
0x07 ECMUL: s · P x, y, s (96) x, y (64)

Encoding rules:

  • Every field element and scalar is a 32-byte big-endian integer. A point is (x, y), and the point at infinity is (0, 0).
  • Short input is padded with zeros on the right. Extra input is ignored.
  • If a point is not on the curve, or a coordinate is >= p, the call fails and consumes all gas it was given. The scalar may be any 256-bit value.

The original gas costs were 500 for ECADD and 40,000 for ECMUL.

Sample code

Calling the precompiles from Solidity:

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

library BN254 {
    struct G1 { uint256 x; uint256 y; }

    function add(G1 memory p, G1 memory q) internal view returns (G1 memory r) {
        (bool ok, bytes memory out) = address(0x06).staticcall(abi.encode(p.x, p.y, q.x, q.y));
        require(ok, "ecAdd failed");
        (r.x, r.y) = abi.decode(out, (uint256, uint256));
    }

    function mul(G1 memory p, uint256 s) internal view returns (G1 memory r) {
        (bool ok, bytes memory out) = address(0x07).staticcall(abi.encode(p.x, p.y, s));
        require(ok, "ecMul failed");
        (r.x, r.y) = abi.decode(out, (uint256, uint256));
    }
}

// Generator of G1 is (1, 2); mul(G1(1, 2), 2) returns 2·G

Things to know

  • Gas prices were cut by EIP-1108 in the Istanbul upgrade to 150 (ECADD) and 6,000 (ECMUL). Do not hard-code the original numbers.
  • Invalid input burns all forwarded gas. Forward only a bounded amount if your contract handles untrusted points.
  • The precompiles only cover the G1 group. Pairing checks, which use G2 points, are in EIP-197.
  • BN254 is estimated to give somewhat less than its originally assumed 128-bit security. Newer designs often prefer BLS12-381, but BN254 remains widely used by deployed SNARK verifiers on Ethereum.

#precompile #cryptography #zk #core #byzantium

Details

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

Machine-readable: Markdown · JSON