EIP-6690 EVM Modular Arithmetic Extensions (EVMMAX) (EVM Modular Arithmetic Extensions)

No call decision · EVM & execution · EIP status Stagnant · created 2023-03-15

Extends the EVM's modular arithmetic, previously limited to 256 bits, up to 2^768-1, and adds 6 new opcodes that can use dedicated virtual registers and Montgomery form optimization.

Why it is needed

The EVM's current ADDMOD and MULMOD opcodes only support modular arithmetic up to 256-bit width. However, cryptographic operations such as certain elliptic curve cryptography or multi-party computation often use moduli wider than 256 bits, forcing contracts to split the computation into multiple steps manually at high cost. Also, the existing opcodes only handle big-endian byte representation internally, leaving no room to optimize with faster internal representations such as Montgomery form. EIP-6690 adds a dedicated virtual register space (field context) to the EVM execution state, letting a modulus be set once and then used repeatedly for addition, subtraction, and multiplication within that context, so a wider range of cryptographic operations can run efficiently.

What changes

Today

After EIP-6690

Who is affected

In depth

6 new opcodes

A field context map keyed by id (0 to 256) is added to the EVM execution state. A field context is a modulus paired with its associated set of virtual registers. SETMODX (0xc0) sets or creates the active field context, LOADX (0xc1) and STOREX (0xc2) copy values between EVM memory and the virtual registers, and ADDMODX (0xc3), SUBMODX (0xc4), and MULMODX (0xc5) perform modular addition, subtraction, and multiplication between registers of the active context.

SETMODX: creating a field context

The stack inputs are id, modulus_offset, modulus_size, and alloc_count. id must be 0 to 256. If no context exists for that id, a new one is created; modulus_size must be at most 96 bytes, the modulus must be odd or a power of 2 with 3 <= modulus <= 2^768-1, and alloc_count must be 1 to 256. The size of a single register (element_size) is the modulus rounded up to a 64-bit unit, and within one call context the total register allocation across all field contexts cannot exceed 24576 bytes. The cost is COST_SETMODX_BASE (1) plus memory expansion cost, with an additional dynamic cost added from a size-based table (23 for 64 bits up to 58 for 768 bits) if the modulus is odd.

ADDMODX, SUBMODX, MULMODX: batch operations

These three opcodes take a 7-byte immediate encoding out_idx, out_stride, x_idx, x_stride, y_idx, y_stride, and count. out_idx+i*out_stride, x_idx+i*x_stride, and y_idx+i*y_stride (for i=0..count-1) must all fall within the register range of the currently active context, and out_stride and count cannot be zero. All count iterations are computed before the registers are updated at once, so overlapping input and output indices are safe. The cost is taken from a table based on modulus size, ranging 1 to 2 for addition/subtraction and 1 to 12 for multiplication, increasing as the modulus grows.

LOADX, STOREX: memory input/output

LOADX copies register values into EVM memory, and STOREX copies EVM memory values into registers. Values in memory are big-endian and padded to element_size bytes. If the modulus is a power of 2, the same memory copy cost structure as the MCOPY opcode applies (3 gas per 32 bytes of element size); if the modulus is odd, the multiplication cost table applies instead. This is because converting between normal and Montgomery representation for an odd modulus requires one modular multiplication per value. STOREX also checks that each value read from memory is smaller than the modulus.

Montgomery form and the rationale for the cost design

An optimized implementation is assumed to convert values internally into Montgomery form rather than storing them as the EVM's big-endian representation. Montgomery multiplication is defined as mulmont(A,B,M) = A*B*R^-1 % M, where R is a power of 2 larger than the modulus and coprime to it. SETMODX charges a dynamic cost for odd moduli to prepay for the one modular reduction needed for Montgomery conversion. The reason the total register allocation is capped at 24576 bytes is to keep this space within the L1 cache on most machines, so the cost model does not need to separately account for memory access latency.

Current status

This EIP's status is Stagnant. Tests exist only within the Go Ethereum (geth) implementation, and there are no cross-client tests yet. The Security Considerations section is empty, and the cost model for STOREX with power-of-2 moduli is still marked TBD in the source, so parts of the specification remain unfinalized.

Original EIP · ← EIPs