Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EIP: EVM arbitrary precision decimal math #7904

Merged
merged 46 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
879e7d2
Abstract
Oct 22, 2023
a01a6a5
progress
Oct 22, 2023
a5cb785
definitions
Oct 22, 2023
b6bae33
functional
Oct 22, 2023
7bdf0af
main file
Oct 22, 2023
e8ae5e2
reference implementation
1m1-github Oct 22, 2023
a712b74
main code. could be used as reference.
1m1-github Oct 23, 2023
314afe1
Rename decimal_fixed.go.txt to decimal_fixed.go
1m1-github Oct 23, 2023
d1449a0
bottom-up gas
1m1-github Oct 23, 2023
79cf7f3
Rename uint256_wrapped.go.txt to uint256_wrapped.go
1m1-github Oct 23, 2023
d05c846
gasEVMPlusEmulate.go
1m1-github Oct 23, 2023
c148e97
no links out
1m1-github Oct 23, 2023
62ce641
EIP # 7543 assigned
1m1-github Oct 23, 2023
1ad93eb
better title and description
1m1-github Oct 23, 2023
3b36ba5
example smart contracts
1m1-github Oct 23, 2023
bde90d6
Merge pull request #1 from ethereum/master
1m1-github Oct 23, 2023
a83fddb
Update and rename eip-EVM+.md to eip-7543.md
1m1-github Oct 23, 2023
95458c5
remove debug code
1m1-github Oct 23, 2023
b0ce024
rename assets/folder
Oct 23, 2023
a9752fc
BlackScholes,Neuron
Oct 23, 2023
ea7c9f3
Error: EIPS/eip-7543.md:27 MD022/blanks-around-headings/blanks-around…
Oct 23, 2023
1a421fb
like this?
Oct 23, 2023
97edf3e
title, description more precise
Oct 24, 2023
fcbba1f
it is actually a floating type
Oct 24, 2023
413bfa9
Backwards Compatibility
Oct 24, 2023
2aead6e
this would be better
Oct 24, 2023
4fee997
local -> pure
1m1-github Oct 24, 2023
ea133ab
title should be easy to remember ... description contains details
Oct 24, 2023
faac185
fixed->float
Oct 24, 2023
5b8dfb4
Merge branch 'master' into main
1m1-github Nov 2, 2023
b2b48e9
update...small improvements
1m1-github Nov 3, 2023
83d94b5
small changes
1m1-github Nov 3, 2023
02883b0
Merge branch 'master' into main
1m1-github Nov 4, 2023
cee3ed2
remove comments
1m1-github Nov 14, 2023
8c4d172
capital letters beginning sentences
1m1-github Nov 14, 2023
5084adc
no math/big
1m1-github Nov 14, 2023
3188e5f
grammer
1m1-github Nov 14, 2023
4927194
Merge branch 'master' into main
1m1-github Nov 14, 2023
6e42bea
Merge branch 'master' into main
1m1-github Nov 15, 2023
c12347d
Merge pull request #2 from ethereum/master
1m1-github Jan 15, 2024
b41e882
0.1 infinite binary rep
Jan 15, 2024
0a8565e
remove commented test code
Jan 15, 2024
5fdfccb
Merge pull request #3 from ethereum/master
1m1-github Feb 22, 2024
a18c4cf
upper bound for gas
1m1-github Feb 26, 2024
3401423
typo, clarification
1m1-github Mar 2, 2024
36c27fe
better comment
1m1-github Mar 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions EIPS/eip-7543.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
eip: 7543
title: EVM decimal math
description: This EIP adds OPCODEs to allow high precision decimal float calculation of all elementary functions with precise gas enumeration.
author: 1m1 (@1m1-github)
discussions-to: https://ethereum-magicians.org/t/decimal-math-on-evm/16194
status: Draft
type: Standards Track
category: Core
created: 2023-10-22
---


## Abstract

This EIP adds *decimal float* OPCODEs for arithmetic via DECADD, DECNEG, DECMUL, DECINV and expression of all elementary functions via DECEXP, DECLN, DECSIN. All decimal values upto the maximal precision allowed by a int256 coefficient and exponent are represented exactly, as c*10^q. All implemented algorithms converge for all inputs given enough precision, as chosen by the user. All calculations are deterministic and gas is precisely embedded bottom-up. Allowing high precision decimal elementary functions invites the worlds of mathematical finance, machine learning, science, digital art, games and others to Ethereum. The implementation is functional.

## Motivation

Currently, to take a power, a^b, of non integer values, requires vast amounts of Solidity code.
The simplest task in trading e.g. is to convert volatilities from yearly to daily, which involves taking the 16th root.

Giving users/devs the same ability that scientific calculators have allows for the creation of apps with higher complexity.

The files [BlackScholes.yul](../assets/eip-7543/BlackScholes.yul) and [Neuron.yul](../assets/eip-7543/Neuron.yul) demonstrate how these OPCODEs simplify numerical smart contract code.

### Why decimal?

To represent a simple value like 0.1 in binary requires infinite many digits and is therefore not exactly represently in a finite binary machine. Decimal types are much closer to the vast majority of numerical calculations run by humans.

### eVm
g11tech marked this conversation as resolved.
Show resolved Hide resolved

The EVM is a virtual machine and thereby not restricted by hardware. Usually, assembly languages provide OPCODES that are mimic the ability of hardware. In a virtual machine, we have no such limitations and nothing stops us from adding more complex OPCODEs, as long as fair gas is provided. At the same time, we do not want to clutter the OPCODEs library. EXP, LN and SIN are universal functions that open the path to: powers, trigonometry, integrals, differential equations, machine learning, digital art, etc.

## Specification

### Decimal

A decimal is defined as

c * 10^q

where c and q are int256.

Notationwise:
a = ac * 10^aq
b = bc * 10^bq
etc.

### OPCODE defs

0xd0 DECADD a+b -> c : (ac, aq, bc, bq, precision) -> (cc, cq)
0xd1 DECNEG -a -> b : (ac, aq) -> (bc, bq)
0xd2 DECMUL a*b -> c : (ac, aq, bc, bq, precision) -> (cc, cq)
0xd3 DECINV 1/a -> b : (ac, aq, precision) -> (bc, bq)
0xd4 DECEXP exp(a) -> b : (ac, aq, precision, steps) -> (bc, bq)
0xd5 DECLN ln(a) -> b : (ac, aq, precision, steps) -> (bc, bq)
0xd6 DECSIN sin(a) -> b : (ac, aq, precision, steps) -> (bc, bq)

`precision` is the # of digits kept during all calculations. `steps` for DECEXP and DECSIN are the # of Taylor expansion steps. `steps` for DECLN is the depth of the continued fractions expansion.

### Why these functions?

The proposed functions (+,-,*,/,exp,ln,sin) form a small set that combined enable all calculation of all elementary functions, which includes the sets of sums, products, roots and compositions of finitely many polynomial, rational, trigonometric, hyperbolic, and exponential functions, including their inverse functions.

a^b = exp(b * ln(a)) gives us powers and polynomials.
cos(a) = sin(tau/4-a), tan(a)=sin(a)/cos(a), etc., gives us all of trigonometry.

Together with arithmetic, we get all elementary functions.

### DECNEG instead of DECSUB

Negation is a more general operation vs subtraction. OPCODEs should be as fundamental as possible and as complex as desirable.
For the same reason, we have DECINV instead of DECDIV.

DECSUB(a,b) = DECADD(a,DECNEG(b))
DECDIV(a,b) = DECMUL(a,DECINV(b))

### DECEXP, DECSIN via Taylor series

The Taylor series of exp and sin converge everywhere and fast. The error falls as fast as the factorial of steps.

### DECLN via continued fractions

Ln converges fast using continued fractions within the interval ]0,2]. The implementation scales the input into this interval and scales the result back correctly.


The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 and RFC 8174.

## Rationale

### gas

All the above OPCODEs are deterministic, hence the gas cost can be determined. At the same time, the calculations are complex and depend on the input.

It is crucial to have accurate gas costs to avoid energy attacks on nodes.

To this end, the underlying uint256 lib can be wrapped with gas accumulation, as found in the reference implementation in ../assets/eip-EVM+/uint256_wrapped.go. This gives a bottom-up approach to calculating gas, by running the OPCODE.

Because the EVM interprator expects the gas cost before actually running the OPCODE, we are running the OPCODE twice. the first run, identical to the second, is to get the bottom-up gas cost, which is then doubled to account for the actual run plus the gas calculation. On top, we add a fixed emulation cost.

This gives an embedded gas calcuation, which works well for complex OPCODEs (see gasEVMPlusEmulate in ../assets/eip-EVM+/gasEVMPlusEmulate.go).

To remove the double gas, we could find an upper bound on gas usage dependent on the function inputs.
Alternatively, a future EIP would suggest the following: allow contract code to run whilst accumulating gas (at runtime) and panicking in case of limit breach, without requiring the cost in advance. This only works for contract code that is pure, defined as code that only depends on the user input and the inner bytecode of the contract. Pure contracts cannot use state from the chain, nor make calls to other contracts. pure mathematical functions would e.g. be pure contracts. pure contracts are fully deterministic given the input, allowing a user to estimate gas costs offline (cheaper) and the EVM to panic at runtime, without knowing gas in advance.

Since the costs depend on the input, a fuzzing would give us close to the worst cases (TODO).

## Backwards Compatibility

No backward compatibility issues found.

Since the EVM allows contracts to deploy with invalid code, there could be previously invalid code that becomes valid when adding a new OPCODE. The EVM could be designed to expect a version tag at the beginning of all bytecode or (not xor) to only deploy valid code. This is an issue for any new OPCODE.

## Test Cases

../assets/eip-EVM+/decimal_float_test.go

## Reference Implementation

The reference implementation is found in ../assets/eip-EVM+/decimal_float.go

## Security Considerations

There are no security considerations, as long as numerical correctness is guaranteed and gas is collected fairly.

## Copyright

Copyright and related rights waived via [CC0](../LICENSE.md).
Loading
Loading