EIP-5000 MULDIV instruction

No call decision · EVM & execution · EIP status Stagnant · created 2022-03-14

Computes the operation of multiplying two numbers and then dividing by a third (x*y/z) in a single step with 512-bit precision. A new EVM instruction, MULDIV, is added for this purpose.

Why it is needed

Accurately computing fixed-point multiplication and division in smart contracts often requires intermediate values that exceed 256 bits, which can cause overflow. In particular, financial contracts frequently use operations of the form x*y/z, which currently have to be handled with complex workaround code built around mulmod (roughly 50 instructions, excluding stack manipulation). In addition, obtaining the full-precision multiplication (512-bit result) of a 256-bit by 256-bit multiply, as needed in cryptographic operations, requires separate assembly code. Given this situation, a single instruction was needed. That instruction had to be usable efficiently both in an unchecked mode that allows overflow to pass through and in a checked mode that halts on overflow.

What changes

Today

After EIP-5000

Who is affected

In depth

Instruction definition

MULDIV (0x1e) pops x, y, z from the stack in that order. If z==0, it computes r = (uint512(x)*y) / 2**256; otherwise it computes r = ((x*y)/z) % 2**256. Because the intermediate computation is kept at 512-bit precision, no overflow occurs at the multiplication step. The computed r is pushed onto the stack.

Gas cost

The gas cost of MULDIV is 8 gas, the same tier (mid) as the existing addmod and mulmod instructions.

The z=0 special case and the break from EVM convention

Other arithmetic instructions in the EVM follow the convention of returning 0 when dividing by zero or performing a modulo operation. MULDIV is designed to break this convention when z=0, instead returning (x*y)/2**256, i.e., the upper 256 bits of the multiplication result. The source explains that this is a deliberate choice intended to create a flexible opcode that can detect whether overflow (wrapping) occurred. It also states explicitly that callers must be aware that passing 0 triggers behavior entirely different from other instructions.

Alternatives considered

As alternatives considered instead of the z=0 handling approach, the source presents returning a flag indicating whether wrapping occurred, returning the upper and lower bits as two separate stack items, and computing the upper bits directly in EVM code using mulmod (including an example hob function). It explains that while these approaches are also feasible, the current proposal is more flexible.

Argument order

The order in which values are popped from the stack is x, y, z, which matches the argument order of the existing addmod and mulmod instructions.

Use cases

The source presents two use cases: simplifying operations commonly used in financial contracts, such as fixed-point multiplication and division, and supporting the full-precision 256-bit by 256-bit multiplication needed in cryptographic operations, among others. mul(x,y) or muldiv(x,y,1) computes the lower 256 bits of the result, while muldiv(x,y,0) computes the upper 256 bits.

Status and remaining work

This EIP's current status is Stagnant. The Security Considerations section of the source is marked 'TBA' and has not yet been written in detail. However, the source does present three test cases as numerical examples.

Original EIP · ← EIPs