EIP-7543 · Stagnant · Core · 2023-10-22
EIP-7543: EVM arbitrary precision decimal math
A stagnant proposal for EVM opcodes that do arbitrary-precision decimal arithmetic plus exp, ln and sin, so contracts could compute powers, roots and other elementary functions natively.
What problem does it solve?
The EVM only knows 256-bit integers. Anything involving fractions, such as compounding interest, converting yearly volatility to daily (a 16th root), option pricing or a neural-network activation, must be emulated with fixed-point libraries in Solidity. Those libraries are long, gas-hungry and limited in precision.
Binary floating point is not a great fit either: a simple value like 0.1 has no exact binary representation. EIP-7543 proposes decimal floating point opcodes, where the caller chooses the precision, so contracts get the abilities of a scientific calculator.
How it works
A decimal number is a pair of signed 256-bit integers (c, q) meaning c × 10^q. Each opcode takes these pairs from the stack plus a precision (number of digits kept) and returns a new pair:
| Opcode | Name | Computes | Inputs |
|---|---|---|---|
0xd0 |
DECADD |
a + b | ac, aq, bc, bq, precision |
0xd1 |
DECNEG |
−a | ac, aq |
0xd2 |
DECMUL |
a × b | ac, aq, bc, bq, precision |
0xd3 |
DECINV |
1 / a | ac, aq, precision |
0xd4 |
DECEXP |
exp(a) | ac, aq, precision, steps |
0xd5 |
DECLN |
ln(a) | ac, aq, precision, steps |
0xd6 |
DECSIN |
sin(a) | ac, aq, precision, steps |
Subtraction and division come from combining these (a − b = a + (−b), a / b = a × (1/b)). Powers use a^b = exp(b · ln a), and cosine and tangent follow from sine. exp and sin use Taylor series with steps terms; ln uses a continued-fraction expansion with steps depth.
Because the cost depends on the inputs, the reference implementation meters gas "bottom-up": it runs the computation once to count the underlying integer operations, charges double that plus a fixed overhead, then runs it for real.
Sample code
Pseudocode for the 16th root of 1.5, i.e. exp(ln(1.5) / 16), using the proposed opcodes:
P, S = 30, 40 # precision digits, series steps
a = (15, -1) # 1.5 = 15 × 10^-1
ln_a = DECLN(*a, P, S) # ln(1.5)
inv16 = DECINV(16, 0, P) # 1/16
y = DECMUL(*ln_a, *inv16, P) # ln(1.5) / 16
r = DECEXP(*y, P, S) # ≈ 1.02566...
# r is returned as (coefficient, exponent)
Things to know
- Status is Stagnant: it is not scheduled for any network upgrade, and the opcodes do not exist on mainnet.
- The gas model is acknowledged as provisional; the spec lists worst-case fuzzing as a TODO and floats a future "pure contract" metering idea.
- Results depend on the caller's
precisionandsteps; too few steps give inaccurate answers while still costing gas. - Today, on-chain math is done with fixed-point libraries (for example 18-decimal "wad" math) rather than native opcodes.