EIPs Explained

EIP-6690 · Stagnant · Core · 2023-03-15

EIP-6690: EVM Modular Arithmetic Extensions

EVMMAX: proposed opcodes for fast modular add, subtract and multiply on numbers up to 768 bits, so contracts can implement elliptic-curve and other cryptography without new precompiles.

What problem does it solve?

Most cryptography (elliptic curves, pairings, many zero-knowledge verifiers) spends its time on modular arithmetic over large prime fields. The EVM's existing ADDMOD / MULMOD only work on 256-bit values, accept any input, and re-reduce on every call. Curves like BLS12-381 need 381-bit fields, so contracts end up either very slow or dependent on a dedicated precompile for each new curve.

EIP-6690 ("EVM Modular Arithmetic Extensions", often called EVMMAX) proposes general, efficient opcodes for modular arithmetic with moduli up to 768 bits, so new primitives can be written as ordinary contracts.

How it works

A call frame gets a set of field contexts, each identified by an id (0–256). A context holds a modulus plus a block of virtual registers kept outside normal EVM memory.

Opcode Role
SETMODX (0xc0) Select context id, creating it if needed from a modulus in memory and a register count
LOADX (0xc1) Copy register values out to EVM memory
STOREX (0xc2) Copy values from EVM memory into registers (each must be < modulus)
ADDMODX (0xc3), SUBMODX (0xc4), MULMODX (0xc5) Vectorised modular add / sub / mul on registers

Key rules:

  • The modulus must be odd or a power of two, between 3 and 2^768 − 1 (at most 96 bytes).
  • Values are stored padded to a multiple of 64 bits. Total register space per call frame is capped at 24,576 bytes, sized to fit in a CPU's L1 cache.
  • Arithmetic opcodes take a 7-byte immediate: out_idx, out_stride, x_idx, x_stride, y_idx, y_stride, count, so one instruction can process a strided vector of count elements.
  • For odd moduli, implementations are expected to keep values in Montgomery form internally; conversion happens in LOADX/STOREX, which is why those cost one multiplication per value.
  • Gas is cheap and scales with modulus size; for example, multiplication costs 1 gas per element up to 192-bit moduli and 12 gas at 768 bits.

Sample code

Illustrative pseudo-assembly computing r = a * b mod p for a 381-bit prime (layout and immediates simplified):

// memory[0x00..0x30) holds p (48 bytes); a and b are at 0x40 and 0x70
PUSH1 3          // alloc_count: registers r0, r1, r2
PUSH1 48         // modulus_size
PUSH1 0x00       // modulus_offset
PUSH1 0          // id
SETMODX          // create + activate field context 0

PUSH1 2 PUSH1 0x40 PUSH1 0  STOREX   // regs[0..2) <- a, b   (dest=0, source=0x40, count=2)
MULMODX 0x02 0x01 0x00 0x01 0x01 0x01 0x01  // r2 = r0 * r1 mod p
PUSH1 1 PUSH1 2 PUSH1 0xa0  LOADX    // memory[0xa0] <- r2   (dest=0xa0, source=2, count=1)

Things to know

  • Status is Stagnant; it is not scheduled for any network upgrade.
  • The spec notes only Geth-side tests and an open Geth PR as reference; there are no cross-client tests.
  • The Security Considerations section is empty, and power-of-two multiplication costs are explicitly provisional.
  • Failed checks use assert semantics: the call frame consumes all its gas and halts exceptionally (for example, STOREX of a value not below the modulus).

#evm #opcodes #cryptography #evmmax #core

Details

Authors
Jared Wasinger (@jwasinger), Alex Beregszaszi (@axic), Vitalik Buterin (@vbuterin), Radosław Zagórowicz (@rodiazet), Paweł Bylica (@chfast)
Status
Stagnant
Type
Standards Track / Core
Created
2023-03-15

Machine-readable: Markdown · JSON