EIP-7543 EVM arbitrary-precision decimal arithmetic (EVM arbitrary precision decimal math)

No call decision · EVM & execution · EIP status Stagnant · created 2023-10-22

This EIP adds seven arbitrary-precision decimal opcodes to the EVM, covering everything from basic arithmetic to exponentials, logarithms, and sine with no error, and defines a way to precisely calculate their gas cost.

Why it is needed

The EVM currently supports only integer arithmetic. To compute something like a to the power of b (a^b), as in interest rate calculations, one must write long Solidity code to handle non-integer exponents. A common trading calculation, converting annual volatility to daily volatility, also requires taking a 16th root, which is not easy either. Furthermore, even a decimal that looks simple to humans, like 0.1, cannot be represented exactly in binary, risking accumulated error in fields like finance where precise calculation matters. If the EVM could directly and accurately provide exponential, logarithmic, and trigonometric functions at the level of a scientific calculator, applications requiring more complex computation, such as financial engineering, machine learning, science, digital art, and games, could be built on Ethereum.

What changes

Today

After EIP-7543

Who is affected

In depth

Seven opcodes added

This EIP adds seven new opcodes in the range 0xd0 to 0xd6. DECADD (0xd0, a+b→c), DECNEG (0xd1, -a→b), DECMUL (0xd2, a*b→c), and DECINV (0xd3, 1/a→b) perform basic arithmetic, while DECEXP (0xd4, exp(a)→b), DECLN (0xd5, ln(a)→b), and DECSIN (0xd6, sin(a)→b) compute transcendental functions. Each decimal value is pushed onto the stack as a coefficient (c) and exponent (q) pair. DECADD, DECMUL, DECINV, DECEXP, DECLN, and DECSIN take precision as an additional input, and DECEXP, DECLN, and DECSIN also take steps (number of iterations) as an additional input.

How decimals are represented exactly

Every decimal value is represented in the form c * 10^q, where both the coefficient c and the exponent q are int256. Unlike binary floating point, this representation can express human-used base-10 decimals like 0.1 exactly, with no rounding error. The limit on representable precision is the number of digits an int256 coefficient can hold.

Only minimal operations as opcodes, everything else composed from them

This EIP does not add subtraction or division as separate opcodes. Instead it adds only the more fundamental operations of negation (DECNEG) and inversion (DECINV), and implements DECSUB(a,b)=DECADD(a,DECNEG(b)) and DECDIV(a,b)=DECMUL(a,DECINV(b)) by composing the existing opcodes. This follows the design principle that opcodes should add only the most fundamental operations possible, and complex operations should be solved through composition.

Why +, -, *, /, exp, ln, sin are enough to build every elementary function

Combining the four basic arithmetic operations with exp, ln, and sin lets you build every elementary function in finite combinations, including polynomial, rational, trigonometric, hyperbolic, exponential functions and their inverses. A power is obtained as a^b = exp(b*ln(a)), cosine as cos(a) = sin(tau/4-a), and tangent as tan(a) = sin(a)/cos(a). The fact that adding just these three functions, exp, ln, and sin, to basic arithmetic covers every elementary function is the basis for limiting the opcode set to seven.

Computation via Taylor series and continued fractions

DECEXP and DECSIN are computed via Taylor series. The Taylor series for both functions converges for every input, and the error shrinks rapidly, at factorial speed, as steps (the number of expansion terms) increases. DECLN uses a continued fraction expansion, which converges quickly only on the interval ]0,2], so the implementation scales the input into this interval before computing and then rescales the result back to the original scale.

Metering gas accurately by executing twice

These opcodes are deterministic, but their cost varies with the input, making it hard to know the gas in advance of execution. Since the EVM interpreter requires gas up front before execution, the reference implementation runs the same operation twice. On the first run, the gas accumulated bottom-up by the underlying operations (uint256 wrappers) is tallied, and this amount is doubled to cover both the actual execution and the gas-calculation cost, with a fixed emulation cost added on top (gasEVMPlusEmulate). The source explains that precise gas estimation is needed to prevent attacks that force nodes into excessive computation through inaccurate gas. To reduce the cost of double execution, future EIPs are proposed to either find an upper bound on gas usage based on the input, or, for 'pure' contracts that do not read chain state, accumulate gas during execution and halt once a limit is exceeded. Fuzzing work to find the worst-case cost per input remains a TODO in the source itself.

Backwards compatibility and remaining work

The source states that this change introduces no backwards compatibility issues, but notes that adding new opcodes can make bytecode that was previously invalid become valid, an issue common to any new opcode addition. This EIP is submitted as a Standards Track proposal in the Core category, with test cases and a Go reference implementation, but its current status is Stagnant. In other words, core developer calls have never decided on fork inclusion, and the fuzzing work to find the worst case for gas estimation also remains incomplete (TODO), so the specification is not yet finalized.

Original EIP · ← EIPs